Skip to content

Commit abee10e

Browse files
committed
fix(outputs.loki): sanitize colons in label names
1 parent 50cf281 commit abee10e

File tree

4 files changed

+10
-5
lines changed

4 files changed

+10
-5
lines changed

plugins/outputs/loki/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ to use them.
6161

6262
## Sanitize Tag Names
6363
## If true, all tag names will have invalid characters replaced with
64-
## underscores that do not match the regex: ^[a-zA-Z_:][a-zA-Z0-9_:]*.
64+
## underscores that do not match the regex: ^[a-zA-Z_][a-zA-Z0-9_]*.
6565
# sanitize_label_names = false
6666

6767
## Metric Name Label

plugins/outputs/loki/loki.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,12 @@ func (l *Loki) writeMetrics(s Streams) error {
208208
return nil
209209
}
210210

211-
// Verify the label name matches the regex [a-zA-Z_:][a-zA-Z0-9_:]*
211+
// Verify the label name matches the regex [a-zA-Z_][a-zA-Z0-9_]*
212212
func sanitizeLabelName(name string) string {
213-
re := regexp.MustCompile(`^[^a-zA-Z_:]`)
213+
re := regexp.MustCompile(`^[^a-zA-Z_]`)
214214
result := re.ReplaceAllString(name, "_")
215215

216-
re = regexp.MustCompile(`[^a-zA-Z0-9_:]`)
216+
re = regexp.MustCompile(`[^a-zA-Z0-9_]`)
217217
return re.ReplaceAllString(result, "_")
218218
}
219219

plugins/outputs/loki/loki_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,11 @@ func TestSanitizeLabelName(t *testing.T) {
620620
input: "foobar.foobar.2002",
621621
expected: "foobar_foobar_2002",
622622
},
623+
{
624+
name: "replace colon",
625+
input: "foo:bar",
626+
expected: "foo_bar",
627+
},
623628
}
624629

625630
for _, tt := range tests {

plugins/outputs/loki/sample.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
## Sanitize Tag Names
2828
## If true, all tag names will have invalid characters replaced with
29-
## underscores that do not match the regex: ^[a-zA-Z_:][a-zA-Z0-9_:]*.
29+
## underscores that do not match the regex: ^[a-zA-Z_][a-zA-Z0-9_]*.
3030
# sanitize_label_names = false
3131

3232
## Metric Name Label

0 commit comments

Comments
 (0)