diff --git a/app/ocache/metrics.go b/app/ocache/metrics.go index b5fe9aa4..b520dff2 100644 --- a/app/ocache/metrics.go +++ b/app/ocache/metrics.go @@ -1,11 +1,18 @@ package ocache -import "github.com/prometheus/client_golang/prometheus" +import ( + "github.com/prometheus/client_golang/prometheus" + "strings" +) func WithPrometheus(reg *prometheus.Registry, namespace, subsystem string) Option { if subsystem == "" { subsystem = "cache" } + nameSplit := strings.Split(namespace, ".") + subSplit := strings.Split(subsystem, ".") + namespace = strings.Join(nameSplit, "_") + subsystem = strings.Join(subSplit, "_") if reg == nil { return nil } diff --git a/app/ocache/metrics_test.go b/app/ocache/metrics_test.go new file mode 100644 index 00000000..e14d670d --- /dev/null +++ b/app/ocache/metrics_test.go @@ -0,0 +1,19 @@ +package ocache + +import ( + "context" + "github.com/prometheus/client_golang/prometheus" + "github.com/stretchr/testify/require" + "strings" + "testing" +) + +func TestWithPrometheus_MetricsConvertsDots(t *testing.T) { + opt := WithPrometheus(prometheus.NewRegistry(), "some.name", "some.system") + cache := New(func(ctx context.Context, id string) (value Object, err error) { + return &testObject{}, nil + }, opt).(*oCache) + _, err := cache.Get(context.Background(), "id") + require.NoError(t, err) + require.True(t, strings.Contains(cache.metrics.hit.Desc().String(), "some_name_some_system_hit")) +}