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

Function Test_counter

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

Source from the content-addressed store, hash-verified

13)
14
15func Test_counter(t *testing.T) {
16 var seriesAdded int
17 lifecycler := &mockLimiter{
18 onAddFunc: func(hash uint64, _ uint32, lbls labels.Labels) (labels.Labels, uint64) {
19 seriesAdded++
20 return lbls, hash
21 },
22 }
23
24 c := newCounter("my_counter", lifecycler, map[string]string{}, 15*time.Minute)
25
26 c.Inc(buildTestLabels([]string{"label"}, []string{"value-1"}), 1.0)
27 c.Inc(buildTestLabels([]string{"label"}, []string{"value-2"}), 2.0)
28
29 assert.Equal(t, 2, seriesAdded)
30
31 collectionTimeMs := time.Now().UnixMilli()
32 endOfLastMinuteMs := getEndOfLastMinuteMs(collectionTimeMs)
33 expectedSamples := []sample{
34 newSample(map[string]string{"__name__": "my_counter", "label": "value-1"}, endOfLastMinuteMs, 0),
35 newSample(map[string]string{"__name__": "my_counter", "label": "value-1"}, collectionTimeMs, 1),
36 newSample(map[string]string{"__name__": "my_counter", "label": "value-2"}, endOfLastMinuteMs, 0),
37 newSample(map[string]string{"__name__": "my_counter", "label": "value-2"}, collectionTimeMs, 2),
38 }
39 collectMetricAndAssert(t, c, collectionTimeMs, 2, expectedSamples, nil)
40
41 c.Inc(buildTestLabels([]string{"label"}, []string{"value-2"}), 2.0)
42 c.Inc(buildTestLabels([]string{"label"}, []string{"value-3"}), 3.0)
43
44 assert.Equal(t, 3, seriesAdded)
45
46 collectionTimeMs = time.Now().UnixMilli()
47 endOfLastMinuteMs = getEndOfLastMinuteMs(collectionTimeMs)
48 expectedSamples = []sample{
49 newSample(map[string]string{"__name__": "my_counter", "label": "value-1"}, collectionTimeMs, 1),
50 newSample(map[string]string{"__name__": "my_counter", "label": "value-2"}, collectionTimeMs, 4),
51 newSample(map[string]string{"__name__": "my_counter", "label": "value-3"}, endOfLastMinuteMs, 0),
52 newSample(map[string]string{"__name__": "my_counter", "label": "value-3"}, collectionTimeMs, 3),
53 }
54
55 collectMetricAndAssert(t, c, collectionTimeMs, 3, expectedSamples, nil)
56}
57
58func TestCounterDifferentLabels(t *testing.T) {
59 var seriesAdded int

Callers

nothing calls this directly

Calls 8

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

Tested by

no test coverage detected