MCPcopy
hub / github.com/grafana/tempo / Test_counter_cantAdd

Function Test_counter_cantAdd

modules/generator/registry/counter_test.go:85–132  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

83}
84
85func Test_counter_cantAdd(t *testing.T) {
86 canAdd := false
87 overflowLabels := labels.FromStrings("metric_overflow", "true")
88 overflowHash := overflowLabels.Hash()
89 lifecycler := &mockLimiter{
90 onAddFunc: func(_ uint64, count uint32, lbls labels.Labels) (labels.Labels, uint64) {
91 assert.Equal(t, uint32(1), count)
92 if canAdd {
93 return lbls, lbls.Hash()
94 }
95 return overflowLabels, overflowHash
96 },
97 }
98
99 c := newCounter("my_counter", lifecycler, map[string]string{}, 15*time.Minute)
100
101 // allow adding new series
102 canAdd = true
103
104 c.Inc(buildTestLabels([]string{"label"}, []string{"value-1"}), 1.0)
105 c.Inc(buildTestLabels([]string{"label"}, []string{"value-2"}), 2.0)
106
107 collectionTimeMs := time.Now().UnixMilli()
108 endOfLastMinuteMs := getEndOfLastMinuteMs(collectionTimeMs)
109 expectedSamples := []sample{
110 newSample(map[string]string{"__name__": "my_counter", "label": "value-1"}, endOfLastMinuteMs, 0),
111 newSample(map[string]string{"__name__": "my_counter", "label": "value-1"}, collectionTimeMs, 1),
112 newSample(map[string]string{"__name__": "my_counter", "label": "value-2"}, endOfLastMinuteMs, 0),
113 newSample(map[string]string{"__name__": "my_counter", "label": "value-2"}, collectionTimeMs, 2),
114 }
115 collectMetricAndAssert(t, c, collectionTimeMs, 2, expectedSamples, nil)
116
117 // block new series - existing series can still be updated, new series map to overflow
118 canAdd = false
119
120 c.Inc(buildTestLabels([]string{"label"}, []string{"value-2"}), 2.0)
121 c.Inc(buildTestLabels([]string{"label"}, []string{"value-3"}), 3.0)
122
123 collectionTimeMs = time.Now().UnixMilli()
124 endOfLastMinuteMs = getEndOfLastMinuteMs(collectionTimeMs)
125 expectedSamples = []sample{
126 newSample(map[string]string{"__name__": "my_counter", "label": "value-1"}, collectionTimeMs, 1),
127 newSample(map[string]string{"__name__": "my_counter", "label": "value-2"}, collectionTimeMs, 4),
128 newSample(map[string]string{"__name__": "my_counter", "metric_overflow": "true"}, endOfLastMinuteMs, 0),
129 newSample(map[string]string{"__name__": "my_counter", "metric_overflow": "true"}, collectionTimeMs, 3),
130 }
131 collectMetricAndAssert(t, c, collectionTimeMs, 3, expectedSamples, nil)
132}
133
134func Test_counter_removeStaleSeries(t *testing.T) {
135 var removedSeries int

Callers

nothing calls this directly

Calls 9

newCounterFunction · 0.85
buildTestLabelsFunction · 0.85
getEndOfLastMinuteMsFunction · 0.85
newSampleFunction · 0.85
collectMetricAndAssertFunction · 0.85
HashMethod · 0.80
IncMethod · 0.65
NowMethod · 0.65
EqualMethod · 0.45

Tested by

no test coverage detected