From 9f812e98fa5a651860a2d289a0f6b29790099b67 Mon Sep 17 00:00:00 2001 From: Ketan Umare <16888709+kumare3@users.noreply.github.com> Date: Wed, 9 Feb 2022 10:55:32 -0800 Subject: [PATCH] santize all scope names and metric names (#115) Signed-off-by: Ketan Umare --- promutils/scope.go | 8 ++++---- promutils/scope_test.go | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/promutils/scope.go b/promutils/scope.go index 4d29902f..326094f3 100644 --- a/promutils/scope.go +++ b/promutils/scope.go @@ -23,7 +23,7 @@ func panicIfError(err error) { } } -// A Simple StopWatch that works with prometheus summary +// StopWatch implements a stopwatch style interface that works with prometheus summary // It will scale the output to match the expected time scale (milliseconds, seconds etc) // NOTE: Do not create a StopWatch object by hand, use a Scope to get a new instance of the StopWatch object type StopWatch struct { @@ -46,7 +46,7 @@ func (s StopWatch) Start() Timer { } } -// Observes specified duration between the start and end time +// Observe records a specified duration between the start and end time func (s StopWatch) Observe(start, end time.Time) { observed := end.Sub(start).Nanoseconds() outputScaleDuration := s.outputScale.Nanoseconds() @@ -58,7 +58,7 @@ func (s StopWatch) Observe(start, end time.Time) { s.Observer.Observe(scaled) } -// Observes/records the time to execute the given function synchronously +// Time Observes/records the time to execute the given function synchronously func (s StopWatch) Time(f func()) { t := s.Start() f() @@ -435,7 +435,7 @@ func NewScope(name string) Scope { } return metricsScope{ - scope: name, + scope: SanitizeMetricName(name), } } diff --git a/promutils/scope_test.go b/promutils/scope_test.go index 610f29f2..b27c3b8d 100644 --- a/promutils/scope_test.go +++ b/promutils/scope_test.go @@ -37,6 +37,7 @@ func TestNewScope(t *testing.T) { assert.Equal(t, "test:timer_x", s.NewScopedMetricName("timer_x")) assert.Equal(t, "test:hello:timer:", s.NewSubScope("hello").NewSubScope("timer").CurrentScope()) assert.Equal(t, "test:hello:timer:", s.NewSubScope("hello").NewSubScope("timer:").CurrentScope()) + assert.Equal(t, "test:k8s_array:test_1:", s.NewSubScope("k8s-array").NewSubScope("test-1:").CurrentScope()) } func TestMetricsScope(t *testing.T) {