From d3802ac48ddc84f2369b94a1006baa3192cfb829 Mon Sep 17 00:00:00 2001 From: Alpal Date: Tue, 23 May 2023 16:20:47 -0400 Subject: [PATCH] [Tally Snapshot] -- add some functionality to histogram snapshot so that it is easier to test. Add the number of values and durations as well as which buckets have values to enable easier histogram testing --- scope.go | 22 ++++++++++++++++++++++ scope_test.go | 2 ++ 2 files changed, 24 insertions(+) diff --git a/scope.go b/scope.go index 1418348d..4e0e21d0 100644 --- a/scope.go +++ b/scope.go @@ -660,6 +660,12 @@ type HistogramSnapshot interface { // Durations returns the sample values by upper bound for a durationHistogram Durations() map[time.Duration]int64 + + // NumValues returns the number of values which were emitted by this metric + NumValues() int64 + + // NumDurations returns the number of values which were emitted by this metric + NumDurations() int64 } // mergeRightTags merges 2 sets of tags with the tags from tagsRight overriding values from tagsLeft @@ -792,3 +798,19 @@ func (s *histogramSnapshot) Values() map[float64]int64 { func (s *histogramSnapshot) Durations() map[time.Duration]int64 { return s.durations } + +func (s *histogramSnapshot) NumValues() int64 { + valCounter := 0 + for _, val := range s.values { + valCounter += int(val) + } + return int64(valCounter) +} + +func (s *histogramSnapshot) NumDurations() int64 { + durationCounter := 0 + for _, val := range s.durations { + durationCounter += int(val) + } + return int64(durationCounter) +} diff --git a/scope_test.go b/scope_test.go index 70d91aaa..17873db5 100644 --- a/scope_test.go +++ b/scope_test.go @@ -1147,6 +1147,8 @@ func TestSnapshot(t *testing.T) { ) assert.EqualValues(t, map[time.Duration]int64(nil), histograms["foo.fizz+env=test"].Durations()) assert.EqualValues(t, commonTags, histograms["foo.fizz+env=test"].Tags()) + assert.Equal(t, histograms["foo.fizz+env=test"].NumValues(), int64(2)) + fmt.Println(histograms["foo.fizz+env=test"].NumDurations()) assert.EqualValues(t, map[float64]int64(nil), histograms["foo.buzz+env=test"].Values()) assert.EqualValues(