Skip to content

Commit

Permalink
fix: memstats new stat should reuse measurement (#516)
Browse files Browse the repository at this point in the history
  • Loading branch information
lvrach committed Jun 5, 2024
1 parent 1441aae commit ab7c8f6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions stats/memstats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions stats/memstats/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
}

0 comments on commit ab7c8f6

Please sign in to comment.