From b93fb4536748571d544ba8523801342fcc3ba086 Mon Sep 17 00:00:00 2001 From: Karthik Kondapally Date: Wed, 22 Oct 2025 15:25:46 +0530 Subject: [PATCH] minor refactor of replaceInvalidRune() in bridge.go Signed-off-by: Karthik Kondapally --- prometheus/graphite/bridge.go | 9 +++++++-- prometheus/graphite/bridge_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) 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) + } + } +}