github.com/grafana/synthetic-monitoring-agent@v0.64.2/internal/pusher/metrics_test.go (about) 1 package pusher 2 3 import ( 4 "reflect" 5 "sort" 6 "testing" 7 8 "github.com/prometheus/client_golang/prometheus" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestNewMetrics(t *testing.T) { 13 t.Run("non-nil fields", func(t *testing.T) { 14 reg := prometheus.NewRegistry() 15 m := NewMetrics(reg) 16 17 rVal := reflect.ValueOf(m) 18 for i := 0; i < rVal.NumField(); i++ { 19 fType := rVal.Type().Field(i) 20 21 fVal := rVal.Field(i) 22 if fVal.Kind() == reflect.Pointer { 23 require.NotZero(t, fVal.Pointer(), fType.Name) 24 } 25 } 26 }) 27 t.Run("registered fields", func(t *testing.T) { 28 reg := prometheus.NewRegistry() 29 m := NewMetrics(reg). 30 WithTenant(1234, 50). 31 WithType(LabelValueMetrics) 32 33 m.PushCounter.WithLabelValues().Inc() 34 m.DroppedCounter.WithLabelValues().Inc() 35 m.FailedCounter.WithLabelValues(LabelValueRetryExhausted).Inc() 36 m.BytesOut.WithLabelValues().Add(1200) 37 m.ErrorCounter.WithLabelValues("500").Inc() 38 m.ResponseCounter.WithLabelValues("200").Inc() 39 m.InstalledHandlers.Inc() 40 41 fam, err := reg.Gather() 42 require.NoError(t, err) 43 44 var ( 45 expected = []string{ 46 "sm_agent_publisher_drop_total", 47 "sm_agent_publisher_handlers_total", 48 "sm_agent_publisher_push_bytes", 49 "sm_agent_publisher_push_errors_total", 50 "sm_agent_publisher_push_failed_total", 51 "sm_agent_publisher_push_total", 52 "sm_agent_publisher_responses_total", 53 } 54 actual []string 55 ) 56 for _, metric := range fam { 57 actual = append(actual, metric.GetName()) 58 } 59 60 sort.Strings(expected) 61 sort.Strings(actual) 62 63 require.Equal(t, expected, actual) 64 }) 65 }