Skip to content

Commit

Permalink
Escape characters in prom label values (#6985)
Browse files Browse the repository at this point in the history
  • Loading branch information
ptodev authored Jul 18, 2024
1 parent 3eb7f7e commit bb5889f
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions static/logs/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,17 @@ configs:
// We expect the config reload log line to not be printed.
checkConfigReloadLog(t, logBuffer.String(), 0)

// Windows file paths contain `\` characters.
// Those are not allowed in Prometheus label values:
// https://prometheus.io/docs/instrumenting/exposition_formats/#text-format-details
// `label_value` can be any sequence of UTF-8 characters, but the backslash (\), double-quote ("),
// and line feed (\n) characters have to be escaped as \\, \", and \n, respectively.
tmpFileLabelVal := strings.ReplaceAll(tmpFile.Name(), `\`, `\\`)

require.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(`
# HELP my_promtail_custom_log_lines_total total number of log lines
# TYPE my_promtail_custom_log_lines_total counter
my_promtail_custom_log_lines_total{filename="`+tmpFile.Name()+`",job="test",logs_config="default"} 1
my_promtail_custom_log_lines_total{filename="`+tmpFileLabelVal+`",job="test",logs_config="default"} 1
`), "my_promtail_custom_log_lines_total"))

//
Expand All @@ -150,7 +157,7 @@ my_promtail_custom_log_lines_total{filename="`+tmpFile.Name()+`",job="test",logs
require.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(`
# HELP my_promtail_custom_log_lines_total total number of log lines
# TYPE my_promtail_custom_log_lines_total counter
my_promtail_custom_log_lines_total{filename="`+tmpFile.Name()+`",job="test",logs_config="default"} 1
my_promtail_custom_log_lines_total{filename="`+tmpFileLabelVal+`",job="test",logs_config="default"} 1
`), "my_promtail_custom_log_lines_total"))

//
Expand Down Expand Up @@ -209,7 +216,7 @@ configs:
require.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(`
# HELP my_promtail_custom2_log_lines_total2 total number of log lines
# TYPE my_promtail_custom2_log_lines_total2 counter
my_promtail_custom2_log_lines_total2{filename="`+tmpFile.Name()+`",job="test-2",logs_config="default"} 1
my_promtail_custom2_log_lines_total2{filename="`+tmpFileLabelVal+`",job="test-2",logs_config="default"} 1
`), "my_promtail_custom_log_lines_total", "my_promtail_custom2_log_lines_total2"))

t.Run("update to nil", func(t *testing.T) {
Expand Down

0 comments on commit bb5889f

Please sign in to comment.