Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/loki/process/internal/metric/histograms_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 TestHistogramExpiration(t *testing.T) {
12
t.Parallel()
13
cfg := &HistogramConfig{
14
Description: "HELP ME!!!!!",
15
MaxIdle: 1 * time.Second,
16
}
17
18
hist, err := NewHistograms("test1", cfg)
19
assert.Nil(t, err)
20
21
// Create a label and increment the histogram
22
lbl1 := model.LabelSet{}
23
lbl1["test"] = "app"
24
hist.With(lbl1).Observe(23)
25
26
// Collect the metrics, should still find the metric in the map
27
collect(hist)
28
assert.Contains(t, hist.metrics, lbl1.Fingerprint())
29
30
time.Sleep(1100 * time.Millisecond) // Wait just past our max idle of 1 sec
31
32
//Add another histogram with new label val
33
lbl2 := model.LabelSet{}
34
lbl2["test"] = "app2"
35
hist.With(lbl2).Observe(2)
36
37
// Collect the metrics, first histogram should have expired and removed, second should still be present
38
collect(hist)
39
assert.NotContains(t, hist.metrics, lbl1.Fingerprint())
40
assert.Contains(t, hist.metrics, lbl2.Fingerprint())
41
}
42
43