github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/clients/pkg/logentry/metric/gauges_test.go (about)

     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  		Action: "inc",
    15  	}
    16  
    17  	gag, err := NewGauges("test1", "HELP ME!!!!!", cfg, 1)
    18  	assert.Nil(t, err)
    19  
    20  	// Create a label and increment the gauge
    21  	lbl1 := model.LabelSet{}
    22  	lbl1["test"] = "app"
    23  	gag.With(lbl1).Inc()
    24  
    25  	// Collect the metrics, should still find the metric in the map
    26  	collect(gag)
    27  	assert.Contains(t, gag.metrics, lbl1.Fingerprint())
    28  
    29  	time.Sleep(1100 * time.Millisecond) // Wait just past our max idle of 1 sec
    30  
    31  	//Add another gauge with new label val
    32  	lbl2 := model.LabelSet{}
    33  	lbl2["test"] = "app2"
    34  	gag.With(lbl2).Inc()
    35  
    36  	// Collect the metrics, first gauge should have expired and removed, second should still be present
    37  	collect(gag)
    38  	assert.NotContains(t, gag.metrics, lbl1.Fingerprint())
    39  	assert.Contains(t, gag.metrics, lbl2.Fingerprint())
    40  }