Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/loki/process/internal/metric/gauges_test.go
4096 views
1
package metric
2
3
import (
4
"testing"
5
"time"
6
7
"github.com/prometheus/common/model"
8
"github.com/stretchr/testify/assert"
9
)
10
11
func TestGaugeExpiration(t *testing.T) {
12
t.Parallel()
13
cfg := &GaugeConfig{
14
Description: "HELP ME!!!!!",
15
Action: "inc",
16
MaxIdle: 1 * time.Second,
17
}
18
19
gag, err := NewGauges("test1", cfg)
20
assert.Nil(t, err)
21
22
// Create a label and increment the gauge
23
lbl1 := model.LabelSet{}
24
lbl1["test"] = "app"
25
gag.With(lbl1).Inc()
26
27
// Collect the metrics, should still find the metric in the map
28
collect(gag)
29
assert.Contains(t, gag.metrics, lbl1.Fingerprint())
30
31
time.Sleep(1100 * time.Millisecond) // Wait just past our max idle of 1 sec
32
33
//Add another gauge with new label val
34
lbl2 := model.LabelSet{}
35
lbl2["test"] = "app2"
36
gag.With(lbl2).Inc()
37
38
// Collect the metrics, first gauge should have expired and removed, second should still be present
39
collect(gag)
40
assert.NotContains(t, gag.metrics, lbl1.Fingerprint())
41
assert.Contains(t, gag.metrics, lbl2.Fingerprint())
42
}
43
44