Update metrics to convert dots
This commit is contained in:
parent
c219bb58be
commit
3e391079f3
@ -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
|
||||
}
|
||||
|
||||
19
app/ocache/metrics_test.go
Normal file
19
app/ocache/metrics_test.go
Normal file
@ -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"))
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user