github.com/lyft/flytestdlib@v0.3.12-0.20210213045714-8cdd111ecda1/promutils/scope_test.go (about)

     1  package promutils
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"k8s.io/apimachinery/pkg/util/rand"
     8  
     9  	"github.com/prometheus/client_golang/prometheus"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestDurationToString(t *testing.T) {
    14  	assert.Equal(t, "m", DurationToString(time.Minute))
    15  	assert.Equal(t, "m", DurationToString(time.Minute*10))
    16  	assert.Equal(t, "h", DurationToString(time.Hour))
    17  	assert.Equal(t, "h", DurationToString(time.Hour*10))
    18  	assert.Equal(t, "s", DurationToString(time.Second))
    19  	assert.Equal(t, "s", DurationToString(time.Second*10))
    20  	assert.Equal(t, "us", DurationToString(time.Microsecond*10))
    21  	assert.Equal(t, "us", DurationToString(time.Microsecond))
    22  	assert.Equal(t, "ms", DurationToString(time.Millisecond*10))
    23  	assert.Equal(t, "ms", DurationToString(time.Millisecond))
    24  	assert.Equal(t, "ns", DurationToString(1))
    25  }
    26  
    27  func TestNewScope(t *testing.T) {
    28  	assert.Panics(t, func() {
    29  		NewScope("")
    30  	})
    31  	s := NewScope("test")
    32  	assert.Equal(t, "test:", s.CurrentScope())
    33  	assert.Equal(t, "test:hello:", s.NewSubScope("hello").CurrentScope())
    34  	assert.Panics(t, func() {
    35  		s.NewSubScope("")
    36  	})
    37  	assert.Equal(t, "test:timer_x", s.NewScopedMetricName("timer_x"))
    38  	assert.Equal(t, "test:hello:timer:", s.NewSubScope("hello").NewSubScope("timer").CurrentScope())
    39  	assert.Equal(t, "test:hello:timer:", s.NewSubScope("hello").NewSubScope("timer:").CurrentScope())
    40  }
    41  
    42  func TestMetricsScope(t *testing.T) {
    43  	s := NewScope("test")
    44  	const description = "some x"
    45  	if !assert.NotNil(t, prometheus.DefaultRegisterer) {
    46  		assert.Fail(t, "Prometheus registrar failed")
    47  	}
    48  	t.Run("Counter", func(t *testing.T) {
    49  		m := s.MustNewCounter("xc", description)
    50  		assert.Equal(t, `Desc{fqName: "test:xc", help: "some x", constLabels: {}, variableLabels: []}`, m.Desc().String())
    51  		mv := s.MustNewCounterVec("xcv", description)
    52  		assert.NotNil(t, mv)
    53  		assert.Panics(t, func() {
    54  			_ = s.MustNewCounter("xc", description)
    55  		})
    56  		assert.Panics(t, func() {
    57  			_ = s.MustNewCounterVec("xcv", description)
    58  		})
    59  	})
    60  
    61  	t.Run("Histogram", func(t *testing.T) {
    62  		m := s.MustNewHistogram("xh", description)
    63  		assert.Equal(t, `Desc{fqName: "test:xh", help: "some x", constLabels: {}, variableLabels: []}`, m.Desc().String())
    64  		mv := s.MustNewHistogramVec("xhv", description)
    65  		assert.NotNil(t, mv)
    66  		assert.Panics(t, func() {
    67  			_ = s.MustNewHistogram("xh", description)
    68  		})
    69  		assert.Panics(t, func() {
    70  			_ = s.MustNewHistogramVec("xhv", description)
    71  		})
    72  	})
    73  
    74  	t.Run("Summary", func(t *testing.T) {
    75  		m := s.MustNewSummary("xs", description)
    76  		assert.Equal(t, `Desc{fqName: "test:xs", help: "some x", constLabels: {}, variableLabels: []}`, m.Desc().String())
    77  		mco, err := s.NewSummaryWithOptions("xsco", description, SummaryOptions{Objectives: map[float64]float64{0.5: 0.05, 1.0: 0.0}})
    78  		assert.Nil(t, err)
    79  		assert.Equal(t, `Desc{fqName: "test:xsco", help: "some x", constLabels: {}, variableLabels: []}`, mco.Desc().String())
    80  		mv := s.MustNewSummaryVec("xsv", description)
    81  		assert.NotNil(t, mv)
    82  		assert.Panics(t, func() {
    83  			_ = s.MustNewSummary("xs", description)
    84  		})
    85  		assert.Panics(t, func() {
    86  			_ = s.MustNewSummaryVec("xsv", description)
    87  		})
    88  	})
    89  
    90  	t.Run("Gauge", func(t *testing.T) {
    91  		m := s.MustNewGauge("xg", description)
    92  		assert.Equal(t, `Desc{fqName: "test:xg", help: "some x", constLabels: {}, variableLabels: []}`, m.Desc().String())
    93  		mv := s.MustNewGaugeVec("xgv", description)
    94  		assert.NotNil(t, mv)
    95  		assert.Panics(t, func() {
    96  			m = s.MustNewGauge("xg", description)
    97  		})
    98  		assert.Panics(t, func() {
    99  			_ = s.MustNewGaugeVec("xgv", description)
   100  		})
   101  	})
   102  
   103  	t.Run("Timer", func(t *testing.T) {
   104  		m := s.MustNewStopWatch("xt", description, time.Second)
   105  		asDesc, ok := m.Observer.(prometheus.Metric)
   106  		assert.True(t, ok)
   107  		assert.Equal(t, `Desc{fqName: "test:xt_s", help: "some x", constLabels: {}, variableLabels: []}`, asDesc.Desc().String())
   108  		assert.Panics(t, func() {
   109  			_ = s.MustNewStopWatch("xt", description, time.Second)
   110  		})
   111  	})
   112  
   113  }
   114  
   115  func TestStopWatch_Start(t *testing.T) {
   116  	scope := NewTestScope()
   117  	s, e := scope.NewStopWatch("yt"+rand.String(3), "timer", time.Millisecond)
   118  	assert.NoError(t, e)
   119  	assert.Equal(t, time.Millisecond, s.outputScale)
   120  	i := s.Start()
   121  	assert.Equal(t, time.Millisecond, i.outputScale)
   122  	assert.NotNil(t, i.start)
   123  	i.Stop()
   124  }
   125  
   126  func TestStopWatch_Observe(t *testing.T) {
   127  	scope := NewTestScope()
   128  	s, e := scope.NewStopWatch("yt"+rand.String(3), "timer", time.Millisecond)
   129  	assert.NoError(t, e)
   130  	assert.Equal(t, time.Millisecond, s.outputScale)
   131  	s.Observe(time.Now(), time.Now().Add(time.Second))
   132  }
   133  
   134  func TestStopWatch_Time(t *testing.T) {
   135  	scope := NewTestScope()
   136  	s, e := scope.NewStopWatch("yt"+rand.String(3), "timer", time.Millisecond)
   137  	assert.NoError(t, e)
   138  	assert.Equal(t, time.Millisecond, s.outputScale)
   139  	s.Time(func() {
   140  	})
   141  }
   142  
   143  func TestStopWatchVec_WithLabelValues(t *testing.T) {
   144  	scope := NewTestScope()
   145  	v, e := scope.NewStopWatchVec("yt"+rand.String(3), "timer", time.Millisecond, "workflow", "label")
   146  	assert.NoError(t, e)
   147  	assert.Equal(t, time.Millisecond, v.outputScale)
   148  	s := v.WithLabelValues("my_wf", "something")
   149  	assert.NotNil(t, s)
   150  	i := s.Start()
   151  	assert.Equal(t, time.Millisecond, i.outputScale)
   152  	assert.NotNil(t, i.start)
   153  	i.Stop()
   154  }