github.com/msales/pkg/v3@v3.24.0/stats/prometheus_test.go (about) 1 package stats_test 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "testing" 7 "time" 8 9 "github.com/msales/pkg/v3/stats" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestPrometheus_Handler(t *testing.T) { 14 s := stats.NewPrometheus("test.test") 15 16 h := s.Handler() 17 18 assert.Implements(t, (*http.Handler)(nil), h) 19 } 20 21 func TestPrometheus_Inc(t *testing.T) { 22 s := stats.NewPrometheus("test.test") 23 24 err := s.Inc("test", 2, 1.0, "test", "test") 25 26 rr := httptest.NewRecorder() 27 req := httptest.NewRequest("GET", "/metrics", nil) 28 s.Handler().ServeHTTP(rr, req) 29 30 assert.NoError(t, err) 31 assert.Contains(t, rr.Body.String(), "test_test_test{test=\"test\"} 2") 32 } 33 34 func TestPrometheus_Dec(t *testing.T) { 35 s := stats.NewPrometheus("test.test") 36 37 err := s.Dec("test", 2, 1.0, "test", "test") 38 39 assert.Error(t, err) 40 } 41 42 func TestPrometheus_Gauge(t *testing.T) { 43 s := stats.NewPrometheus("test.test") 44 45 err := s.Gauge("test", 2.1, 1.0, "test", "test") 46 47 rr := httptest.NewRecorder() 48 req := httptest.NewRequest("GET", "/metrics", nil) 49 s.Handler().ServeHTTP(rr, req) 50 51 assert.NoError(t, err) 52 assert.Contains(t, rr.Body.String(), "test_test_test{test=\"test\"} 2.1") 53 } 54 55 func TestPrometheus_Timing(t *testing.T) { 56 s := stats.NewPrometheus("test.test") 57 58 err := s.Timing("test", 1234500*time.Nanosecond, 1.0, "test", "test") 59 60 rr := httptest.NewRecorder() 61 req := httptest.NewRequest("GET", "/metrics", nil) 62 s.Handler().ServeHTTP(rr, req) 63 64 assert.NoError(t, err) 65 assert.Contains(t, rr.Body.String(), "test_test_test{test=\"test\",quantile=\"0.5\"} 1.234") 66 assert.Contains(t, rr.Body.String(), "test_test_test{test=\"test\",quantile=\"0.9\"} 1.234") 67 assert.Contains(t, rr.Body.String(), "test_test_test{test=\"test\",quantile=\"0.99\"} 1.234") 68 assert.Contains(t, rr.Body.String(), "test_test_test_sum{test=\"test\"} 1.234") 69 assert.Contains(t, rr.Body.String(), "test_test_test_count{test=\"test\"} 1") 70 } 71 72 func TestPrometheus_Close(t *testing.T) { 73 s := stats.NewPrometheus("test.test") 74 75 err := s.Close() 76 77 assert.NoError(t, err) 78 }