From ab7c8f63ea647e470999d7129a2aee021f221e4e Mon Sep 17 00:00:00 2001 From: Leonidas Vrachnis Date: Wed, 5 Jun 2024 08:38:51 +0200 Subject: [PATCH] fix: memstats new stat should reuse measurement (#516) --- stats/memstats/stats.go | 4 ++++ stats/memstats/stats_test.go | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/stats/memstats/stats.go b/stats/memstats/stats.go index 4f12f31b..157371a9 100644 --- a/stats/memstats/stats.go +++ b/stats/memstats/stats.go @@ -258,6 +258,10 @@ func (ms *Store) NewSampledTaggedStat(name, statType string, tags stats.Tags) st ms.mu.Lock() defer ms.mu.Unlock() + if m, found := ms.byKey[ms.getKey(name, tags)]; found { + return m + } + m := &Measurement{ name: name, tags: tags, diff --git a/stats/memstats/stats_test.go b/stats/memstats/stats_test.go index 0372e755..97ef52d8 100644 --- a/stats/memstats/stats_test.go +++ b/stats/memstats/stats_test.go @@ -391,4 +391,22 @@ func TestStats(t *testing.T) { // checking hierarchy require.Equal(t, spans[1].SpanContext.SpanID, spans[0].Parent.SpanID) }) + + t.Run("multiple stats with same name and tags", func(t *testing.T) { + store, err := memstats.New() + require.NoError(t, err) + + name := "testHistogram" + store.NewTaggedStat(name, stats.HistogramType, commonTags).Observe(1.0) + store.NewTaggedStat(name, stats.HistogramType, commonTags).Observe(2.0) + + require.Equal(t, 2.0, store.Get(name, commonTags).LastValue()) + require.Equal(t, []float64{1.0, 2.0}, store.Get(name, commonTags).Values()) + + require.Equal(t, []memstats.Metric{{ + Name: name, + Tags: commonTags, + Values: []float64{1.0, 2.0}, + }}, store.GetByName(name)) + }) }