diff --git a/prometheus/graphite/bridge.go b/prometheus/graphite/bridge.go index c763797ca..389749e39 100644 --- a/prometheus/graphite/bridge.go +++ b/prometheus/graphite/bridge.go @@ -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 diff --git a/prometheus/graphite/bridge_test.go b/prometheus/graphite/bridge_test.go index 8c596d5ac..a00fff528 100644 --- a/prometheus/graphite/bridge_test.go +++ b/prometheus/graphite/bridge_test.go @@ -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) + } + } +}