Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions prometheus/graphite/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,13 @@ func replaceInvalidRune(c rune) rune {
if c == ' ' {
return '.'
}
// TODO: Apply De Morgan's law to the condition. Make sure to test the condition first.
if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || c == ':' || c == '-' || (c >= '0' && c <= '9')) { //nolint:staticcheck
// !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || c == ':' || c == '-' || (c >= '0' && c <= '9'))
if (c < 'a' || c > 'z') &&
(c < 'A' || c > 'Z') &&
c != '_' &&
c != ':' &&
c != '-' &&
(c < '0' || c > '9') {
return '_'
}
return c
Expand Down
30 changes: 30 additions & 0 deletions prometheus/graphite/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,3 +468,33 @@ func ExampleBridge() {
// Start pushing metrics to Graphite in the Run() loop.
b.Run(ctx)
}

func TestReplaceInvalidRune(t *testing.T) {
tests := []struct {
in rune
want rune
}{
{' ', '.'},

{'a', 'a'},
{'B', 'B'},
{'0', '0'},
{'9', '9'},
{'_', '_'},
{':', ':'},
{'-', '-'},

{'#', '_'},
{'$', '_'},
{'@', '_'},
{'!', '_'},
{'~', '_'},
}

for _, tt := range tests {
got := replaceInvalidRune(tt.in)
if got != tt.want {
t.Fatalf("replaceInvalidRune(%q) = %q; want %q", tt.in, got, tt.want)
}
}
}
Loading