Skip to content

Commit

Permalink
go/oasis-node/cmd/common/metrics: Fix label escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
kostko committed Feb 4, 2021
1 parent 60545db commit 65dd18a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions .changelog/3678.internal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go/oasis-node/cmd/common/metrics: Fix label escaping
9 changes: 8 additions & 1 deletion go/oasis-node/cmd/common/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,14 @@ func Enabled() bool {

// EscapeLabelCharacters replaces invalid prometheus label name characters with "_".
func EscapeLabelCharacters(l string) string {
return strings.Replace(l, ".", "_", -1)
return strings.Map(func(r rune) rune {
switch r {
case '.', '-', ' ':
return '_'
default:
return r
}
}, l)
}

// GetDefaultPushLabels generates standard Prometheus push labels based on test current test instance info.
Expand Down
23 changes: 23 additions & 0 deletions go/oasis-node/cmd/common/metrics/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package metrics

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestEscapeLabelCharacters(t *testing.T) {
require := require.New(t)

for _, tc := range []struct {
input string
expected string
}{
{"hello", "hello"},
{"hello world", "hello_world"},
{"one-two_three", "one_two_three"},
{"a-b c.d.e.f--g", "a_b_c_d_e_f__g"},
} {
require.EqualValues(tc.expected, EscapeLabelCharacters(tc.input))
}
}

0 comments on commit 65dd18a

Please sign in to comment.