github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/clients/pkg/logentry/metric/histograms_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 TestHistogramExpiration(t *testing.T) {
    12  	t.Parallel()
    13  	cfg := HistogramConfig{}
    14  
    15  	hist, err := NewHistograms("test1", "HELP ME!!!!!", cfg, 1)
    16  	assert.Nil(t, err)
    17  
    18  	// Create a label and increment the histogram
    19  	lbl1 := model.LabelSet{}
    20  	lbl1["test"] = "app"
    21  	hist.With(lbl1).Observe(23)
    22  
    23  	// Collect the metrics, should still find the metric in the map
    24  	collect(hist)
    25  	assert.Contains(t, hist.metrics, lbl1.Fingerprint())
    26  
    27  	time.Sleep(1100 * time.Millisecond) // Wait just past our max idle of 1 sec
    28  
    29  	//Add another histogram with new label val
    30  	lbl2 := model.LabelSet{}
    31  	lbl2["test"] = "app2"
    32  	hist.With(lbl2).Observe(2)
    33  
    34  	// Collect the metrics, first histogram should have expired and removed, second should still be present
    35  	collect(hist)
    36  	assert.NotContains(t, hist.metrics, lbl1.Fingerprint())
    37  	assert.Contains(t, hist.metrics, lbl2.Fingerprint())
    38  }