Path: blob/main/component/loki/process/internal/metric/gauges_test.go
4096 views
package metric12import (3"testing"4"time"56"github.com/prometheus/common/model"7"github.com/stretchr/testify/assert"8)910func TestGaugeExpiration(t *testing.T) {11t.Parallel()12cfg := &GaugeConfig{13Description: "HELP ME!!!!!",14Action: "inc",15MaxIdle: 1 * time.Second,16}1718gag, err := NewGauges("test1", cfg)19assert.Nil(t, err)2021// Create a label and increment the gauge22lbl1 := model.LabelSet{}23lbl1["test"] = "app"24gag.With(lbl1).Inc()2526// Collect the metrics, should still find the metric in the map27collect(gag)28assert.Contains(t, gag.metrics, lbl1.Fingerprint())2930time.Sleep(1100 * time.Millisecond) // Wait just past our max idle of 1 sec3132//Add another gauge with new label val33lbl2 := model.LabelSet{}34lbl2["test"] = "app2"35gag.With(lbl2).Inc()3637// Collect the metrics, first gauge should have expired and removed, second should still be present38collect(gag)39assert.NotContains(t, gag.metrics, lbl1.Fingerprint())40assert.Contains(t, gag.metrics, lbl2.Fingerprint())41}424344