Skip to content

Commit

Permalink
Add counter5
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebriskin committed May 17, 2024
1 parent 5929be5 commit 0295aaa
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
15 changes: 15 additions & 0 deletions perf/statz/counters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 26 additions & 0 deletions perf/statz/counters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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)
})
}
11 changes: 9 additions & 2 deletions perf/statz/statz.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 0295aaa

Please sign in to comment.