From 0295aaab73bc91ea7892f95a3d05cc16a7a4bae9 Mon Sep 17 00:00:00 2001 From: Steve Briskin Date: Fri, 17 May 2024 11:35:56 -0400 Subject: [PATCH] Add counter5 --- perf/statz/counters.go | 15 +++++++++++++++ perf/statz/counters_test.go | 26 ++++++++++++++++++++++++++ perf/statz/statz.go | 11 +++++++++-- 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/perf/statz/counters.go b/perf/statz/counters.go index 5c2c4575..87a83642 100644 --- a/perf/statz/counters.go +++ b/perf/statz/counters.go @@ -83,6 +83,21 @@ func (c *Counter4[T1, T2, T3, T4]) IncBy(v1 T1, v2 T2, v3 T3, v4 T4, by int64) { c.wrapper.incBy(context.Background(), labelsToStringSlice(v1, v2, v3, v4), by) } +// Counter5 is a incremental int64 counter type with 5 metric label. +type Counter5[T1 labelContraint, T2 labelContraint, T3 labelContraint, T4 labelContraint, T5 labelContraint] struct { + wrapper *ocCounterWrapper +} + +// Inc increments counter by 1. +func (c *Counter5[T1, T2, T3, T4, T5]) Inc(v1 T1, v2 T2, v3 T3, v4 T4, v5 T5) { + c.IncBy(v1, v2, v3, v4, v5, 1) +} + +// IncBy increments counter by X. +func (c *Counter5[T1, T2, T3, T4, T5]) IncBy(v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, by int64) { + c.wrapper.incBy(context.Background(), labelsToStringSlice(v1, v2, v3, v4, v5), by) +} + ///// internal type ocCounterWrapper struct { diff --git a/perf/statz/counters_test.go b/perf/statz/counters_test.go index 1635a1f5..2ed7b6fe 100644 --- a/perf/statz/counters_test.go +++ b/perf/statz/counters_test.go @@ -27,6 +27,19 @@ func TestCounter(t *testing.T) { }, }) + counter5 := NewCounter5[string, string, string, string, string]("statz/test/counter5", + MetricConfig{ + Description: "The number of requests", + Unit: units.Dimensionless, + Labels: []Label{ + {Name: "label1", Description: "Label1"}, + {Name: "label2", Description: "Other label2"}, + {Name: "label3", Description: "Other label3"}, + {Name: "label4", Description: "Other label4"}, + {Name: "label5", Description: "Other label5"}, + }, + }) + t.Run("counter1", func(t *testing.T) { recorder := statztest.NewCounterRecorder("statz/test/counter1") @@ -62,4 +75,17 @@ func TestCounter(t *testing.T) { test.That(t, recorder.Value("label", "v1", "bool", "true"), test.ShouldEqual, 10) test.That(t, recorder.Value("label", "v1", "bool", "false"), test.ShouldEqual, 1) }) + + t.Run("counter5", func(t *testing.T) { + recorder := statztest.NewCounterRecorder("statz/test/counter5") + + test.That(t, recorder.Value("label1", "a", "label2", "a", "label3", "a", "label4", "a", "label5", "a"), test.ShouldEqual, 0) + test.That(t, recorder.Value("label1", "a", "label2", "a", "label3", "a", "label4", "a", "label5", "z"), test.ShouldEqual, 0) + + counter5.IncBy("a", "a", "a", "a", "a", 10) + counter5.Inc("a", "a", "a", "a", "z") + + test.That(t, recorder.Value("label1", "a", "label2", "a", "label3", "a", "label4", "a", "label5", "a"), test.ShouldEqual, 10) + test.That(t, recorder.Value("label1", "a", "label2", "a", "label3", "a", "label4", "a", "label5", "z"), test.ShouldEqual, 1) + }) } diff --git a/perf/statz/statz.go b/perf/statz/statz.go index d547df0e..87a6bbbb 100644 --- a/perf/statz/statz.go +++ b/perf/statz/statz.go @@ -73,14 +73,21 @@ func NewCounter3[T1, T2, T3 labelContraint](name string, cfg MetricConfig) Count } // NewCounter4 creates a new counter metric with 4 labels. -func NewCounter4[T1, T2 labelContraint, - T3, T4 labelContraint](name string, cfg MetricConfig, +func NewCounter4[T1, T2, T3, T4 labelContraint](name string, cfg MetricConfig, ) Counter4[T1, T2, T3, T4] { return Counter4[T1, T2, T3, T4]{ wrapper: createCounterWrapper(name, cfg), } } +// NewCounter5 creates a new counter metric with 5 labels. +func NewCounter5[T1, T2, T3, T4, T5 labelContraint](name string, cfg MetricConfig, +) Counter5[T1, T2, T3, T4, T5] { + return Counter5[T1, T2, T3, T4, T5]{ + wrapper: createCounterWrapper(name, cfg), + } +} + //// Int64 Summations - Create a summation at the package level. // // var uploadsInFlightCounter = statz.NewSummation1[string]("datasync/uploads_in_flight", statz.MetricConfig{