From ff64e0fddd178ed3a5772761342abda352ae63be Mon Sep 17 00:00:00 2001 From: "a.petraki" <a.petraki@perx.ru> Date: Wed, 20 Dec 2023 14:28:05 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D0=BC=D0=B5=D1=82=D1=80=D0=B8=D0=BA,=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=BA=D0=B0=D0=B7=D1=8B=D0=B2=D0=B0=D1=8E=D1=89=D0=B8=D0=B5=20?= =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=83=20=D1=81=20=D1=81=D0=B5?= =?UTF-8?q?=D0=BC=D0=B5=D0=B9=D1=81=D1=82=D0=B2=D0=B0=D0=BC=D0=B8=20=D0=BC?= =?UTF-8?q?=D0=B5=D1=82=D1=80=D0=B8=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/metrics/metrics_test.go | 42 +++++++++++++++++++++++++++++++++++++ pkg/optional/optional.go | 4 ++++ 2 files changed, 46 insertions(+) diff --git a/pkg/metrics/metrics_test.go b/pkg/metrics/metrics_test.go index 0e171735..369c9a70 100644 --- a/pkg/metrics/metrics_test.go +++ b/pkg/metrics/metrics_test.go @@ -7,8 +7,10 @@ import ( "testing" "git.perx.ru/perxis/perxis-go/pkg/id" + "git.perx.ru/perxis/perxis-go/pkg/optional" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" + dto "github.com/prometheus/client_model/go" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -72,4 +74,44 @@ func TestMetrics_Example(t *testing.T) { // fmt.Println(string(b)) assert.Contains(t, string(b), name+"{a=\"v_a1\",b=\"v_b1\",const_1=\"val_1\"} 1") }) + + t.Run("Register multiple metrics with the same name and labels combine into metric family", func(t *testing.T) { + name := "test_counter_" + id.GenerateNewID() + m1 := prometheus.NewCounterVec(prometheus.CounterOpts{Name: name, ConstLabels: prometheus.Labels{"const_1": "val_1"}}, []string{"a1", "b1"}) + m2 := prometheus.NewCounterVec(prometheus.CounterOpts{Name: name, ConstLabels: prometheus.Labels{"const_1": "val_2"}}, []string{"a1", "b1"}) + + registry := prometheus.NewRegistry() + registry.MustRegister(m1) + registry.MustRegister(m2) + + m1.With(prometheus.Labels{"a1": "v_a1", "b1": "v_b1"}).Inc() + m2.With(prometheus.Labels{"a1": "v_a2", "b1": "v_b2"}).Inc() + + metricsFamily, err := registry.Gather() + require.NoError(t, err) + require.Len(t, metricsFamily, 1) + + assert.Equal(t, name, *metricsFamily[0].Name) + require.Len(t, metricsFamily[0].Metric, 2) + + registeredMetric1 := metricsFamily[0].Metric[0] + assert.Equal(t, + []*dto.LabelPair{ + {Name: optional.Ptr("a1"), Value: optional.Ptr("v_a1")}, + {Name: optional.Ptr("b1"), Value: optional.Ptr("v_b1")}, + {Name: optional.Ptr("const_1"), Value: optional.Ptr("val_1")}, + }, + registeredMetric1.Label, + ) + + registeredMetric2 := metricsFamily[0].Metric[1] + assert.Equal(t, + []*dto.LabelPair{ + {Name: optional.Ptr("a1"), Value: optional.Ptr("v_a2")}, + {Name: optional.Ptr("b1"), Value: optional.Ptr("v_b2")}, + {Name: optional.Ptr("const_1"), Value: optional.Ptr("val_2")}, + }, + registeredMetric2.Label, + ) + }) } diff --git a/pkg/optional/optional.go b/pkg/optional/optional.go index 94e89bf6..b33d76b9 100644 --- a/pkg/optional/optional.go +++ b/pkg/optional/optional.go @@ -8,3 +8,7 @@ var ( func Bool(v bool) *bool { return &v } + +func Ptr[T any](v T) *T { + return &v +} -- GitLab