Path: blob/main/components/supervisor/pkg/metrics/reporter_test.go
2500 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package metrics56import (7"testing"89"github.com/google/go-cmp/cmp"10"github.com/prometheus/client_golang/prometheus"11dto "github.com/prometheus/client_model/go"12)1314func TestHistogram(t *testing.T) {15reporter := NewGrpcMetricsReporter("test.io")16type testReq struct {17Name string18Labels map[string]string19Count uint6420Sum float6421Buckets []uint6422}23type expectation struct {24Requests []*testReq25Unexpected []string26}27var actual *expectation28reporter.addHistogram = func(name string, labels map[string]string, count uint64, sum float64, buckets []uint64) {29if actual == nil {30actual = &expectation{}31}32actual.Requests = append(actual.Requests, &testReq{name, labels, count, sum, buckets})33}34reporter.onUnexpected = func(family *dto.MetricFamily) {35if actual == nil {36actual = &expectation{}37}38actual.Unexpected = append(actual.Unexpected, family.GetName())39}40assertDiff := func(expected *expectation) string {41actual = nil42reporter.gather()43return cmp.Diff(expected, actual)44}4546if diff := assertDiff(nil); diff != "" {47t.Errorf("unexpected output (-want +got):\n%s", diff)48}4950histogram := prometheus.NewHistogramVec(prometheus.HistogramOpts{51Name: "grpc_server_handling_seconds",52Buckets: []float64{.005, .01, .025},53}, []string{"grpc_method"})54err := reporter.Registry.Register(histogram)55if err != nil {56t.Fatal(err)57}58if diff := assertDiff(nil); diff != "" {59t.Errorf("unexpected output (-want +got):\n%s", diff)60}6162histogram.WithLabelValues("foo").Observe(.004)63if diff := assertDiff(&expectation{64Requests: []*testReq{{65Name: "grpc_server_handling_seconds",66Labels: map[string]string{"grpc_method": "foo"},67Count: 1,68Sum: 0.004,69Buckets: []uint64{1, 1, 1},70}},71}); diff != "" {72t.Errorf("unexpected output (-want +got):\n%s", diff)73}7475histogram.WithLabelValues("foo").Observe(.001)76histogram.WithLabelValues("foo").Observe(.002)77histogram.WithLabelValues("foo").Observe(.003)78histogram.WithLabelValues("foo").Observe(.004)79histogram.WithLabelValues("foo").Observe(.005)80if diff := assertDiff(&expectation{81Requests: []*testReq{{82Name: "grpc_server_handling_seconds",83Labels: map[string]string{"grpc_method": "foo"},84Count: 5,85Sum: 0.015,86Buckets: []uint64{5, 5, 5},87}},88}); diff != "" {89t.Errorf("unexpected output (-want +got):\n%s", diff)90}9192histogram.WithLabelValues("foo").Observe(.009)93if diff := assertDiff(&expectation{94Requests: []*testReq{{95Name: "grpc_server_handling_seconds",96Labels: map[string]string{"grpc_method": "foo"},97Count: 1,98Sum: 0.008999999999999998,99Buckets: []uint64{0, 1, 1},100}},101}); diff != "" {102t.Errorf("unexpected output (-want +got):\n%s", diff)103}104105histogram.WithLabelValues("foo").Observe(.006)106histogram.WithLabelValues("foo").Observe(.007)107histogram.WithLabelValues("foo").Observe(.008)108histogram.WithLabelValues("foo").Observe(.009)109histogram.WithLabelValues("foo").Observe(.01)110if diff := assertDiff(&expectation{111Requests: []*testReq{{112Name: "grpc_server_handling_seconds",113Labels: map[string]string{"grpc_method": "foo"},114Count: 5,115Sum: 0.039999999999999994,116Buckets: []uint64{0, 5, 5},117}},118}); diff != "" {119t.Errorf("unexpected output (-want +got):\n%s", diff)120}121122histogram.WithLabelValues("foo").Observe(.001)123histogram.WithLabelValues("foo").Observe(.002)124histogram.WithLabelValues("foo").Observe(.003)125histogram.WithLabelValues("foo").Observe(.004)126histogram.WithLabelValues("foo").Observe(.005)127histogram.WithLabelValues("foo").Observe(.006)128histogram.WithLabelValues("foo").Observe(.007)129histogram.WithLabelValues("foo").Observe(.008)130histogram.WithLabelValues("foo").Observe(.009)131histogram.WithLabelValues("foo").Observe(.01)132if diff := assertDiff(&expectation{133Requests: []*testReq{{134Name: "grpc_server_handling_seconds",135Labels: map[string]string{"grpc_method": "foo"},136Count: 10,137Sum: 0.05500000000000001,138Buckets: []uint64{5, 10, 10},139}},140}); diff != "" {141t.Errorf("unexpected output (-want +got):\n%s", diff)142}143144histogram.WithLabelValues("foo").Observe(.02)145if diff := assertDiff(&expectation{146Requests: []*testReq{{147Name: "grpc_server_handling_seconds",148Labels: map[string]string{"grpc_method": "foo"},149Count: 1,150Sum: 0.01999999999999999,151Buckets: []uint64{0, 0, 1},152}},153}); diff != "" {154t.Errorf("unexpected output (-want +got):\n%s", diff)155}156157histogram.WithLabelValues("foo").Observe(.021)158histogram.WithLabelValues("foo").Observe(.022)159histogram.WithLabelValues("foo").Observe(.023)160histogram.WithLabelValues("foo").Observe(.024)161histogram.WithLabelValues("foo").Observe(.025)162if diff := assertDiff(&expectation{163Requests: []*testReq{{164Name: "grpc_server_handling_seconds",165Labels: map[string]string{"grpc_method": "foo"},166Count: 5,167Sum: 0.11499999999999996,168Buckets: []uint64{0, 0, 5},169}},170}); diff != "" {171t.Errorf("unexpected output (-want +got):\n%s", diff)172}173174histogram.WithLabelValues("foo").Observe(.001)175histogram.WithLabelValues("foo").Observe(.002)176histogram.WithLabelValues("foo").Observe(.003)177histogram.WithLabelValues("foo").Observe(.004)178histogram.WithLabelValues("foo").Observe(.005)179histogram.WithLabelValues("foo").Observe(.006)180histogram.WithLabelValues("foo").Observe(.007)181histogram.WithLabelValues("foo").Observe(.008)182histogram.WithLabelValues("foo").Observe(.009)183histogram.WithLabelValues("foo").Observe(.01)184histogram.WithLabelValues("foo").Observe(.021)185histogram.WithLabelValues("foo").Observe(.022)186histogram.WithLabelValues("foo").Observe(.023)187histogram.WithLabelValues("foo").Observe(.024)188histogram.WithLabelValues("foo").Observe(.025)189if diff := assertDiff(&expectation{190Requests: []*testReq{{191Name: "grpc_server_handling_seconds",192Labels: map[string]string{"grpc_method": "foo"},193Count: 15,194Sum: 0.17000000000000015,195Buckets: []uint64{5, 10, 15},196}},197}); diff != "" {198t.Errorf("unexpected output (-want +got):\n%s", diff)199}200201histogram.WithLabelValues("foo").Observe(.03)202if diff := assertDiff(&expectation{203Requests: []*testReq{{204Name: "grpc_server_handling_seconds",205Labels: map[string]string{"grpc_method": "foo"},206Count: 1,207Sum: 0.02999999999999997,208Buckets: []uint64{0, 0, 0},209}},210}); diff != "" {211t.Errorf("unexpected output (-want +got):\n%s", diff)212}213214histogram.WithLabelValues("foo").Observe(.04)215histogram.WithLabelValues("foo").Observe(.05)216histogram.WithLabelValues("foo").Observe(.06)217histogram.WithLabelValues("foo").Observe(.07)218histogram.WithLabelValues("foo").Observe(.08)219if diff := assertDiff(&expectation{220Requests: []*testReq{{221Name: "grpc_server_handling_seconds",222Labels: map[string]string{"grpc_method": "foo"},223Count: 5,224Sum: 0.30000000000000004,225Buckets: []uint64{0, 0, 0},226}},227}); diff != "" {228t.Errorf("unexpected output (-want +got):\n%s", diff)229}230231histogram.WithLabelValues("foo").Observe(.001)232histogram.WithLabelValues("foo").Observe(.002)233histogram.WithLabelValues("foo").Observe(.003)234histogram.WithLabelValues("foo").Observe(.004)235histogram.WithLabelValues("foo").Observe(.005)236histogram.WithLabelValues("foo").Observe(.006)237histogram.WithLabelValues("foo").Observe(.007)238histogram.WithLabelValues("foo").Observe(.008)239histogram.WithLabelValues("foo").Observe(.009)240histogram.WithLabelValues("foo").Observe(.01)241histogram.WithLabelValues("foo").Observe(.021)242histogram.WithLabelValues("foo").Observe(.022)243histogram.WithLabelValues("foo").Observe(.023)244histogram.WithLabelValues("foo").Observe(.024)245histogram.WithLabelValues("foo").Observe(.025)246histogram.WithLabelValues("foo").Observe(.04)247histogram.WithLabelValues("foo").Observe(.05)248histogram.WithLabelValues("foo").Observe(.06)249histogram.WithLabelValues("foo").Observe(.07)250histogram.WithLabelValues("foo").Observe(.08)251if diff := assertDiff(&expectation{252Requests: []*testReq{{253Name: "grpc_server_handling_seconds",254Labels: map[string]string{"grpc_method": "foo"},255Count: 20,256Sum: 0.4700000000000003,257Buckets: []uint64{5, 10, 15},258}},259}); diff != "" {260t.Errorf("unexpected output (-want +got):\n%s", diff)261}262}263264func TestCounters(t *testing.T) {265reporter := NewGrpcMetricsReporter("test.io")266type testReq struct {267Name string268Labels map[string]string269Value uint64270}271type expectation struct {272Requests []*testReq273Unexpected []string274}275var actual *expectation276reporter.addCounter = func(name string, labels map[string]string, value uint64) {277if actual == nil {278actual = &expectation{}279}280actual.Requests = append(actual.Requests, &testReq{name, labels, value})281}282reporter.onUnexpected = func(family *dto.MetricFamily) {283if actual == nil {284actual = &expectation{}285}286actual.Unexpected = append(actual.Unexpected, family.GetName())287}288assertDiff := func(expected *expectation) string {289actual = nil290reporter.gather()291return cmp.Diff(expected, actual)292}293294if diff := assertDiff(nil); diff != "" {295t.Errorf("unexpected output (-want +got):\n%s", diff)296}297298expectedCounter := prometheus.NewCounterVec(prometheus.CounterOpts{299Name: "grpc_server_handled_total",300}, []string{"grpc_method"})301err := reporter.Registry.Register(expectedCounter)302if err != nil {303t.Fatal(err)304}305if diff := assertDiff(nil); diff != "" {306t.Errorf("unexpected output (-want +got):\n%s", diff)307}308309expectedCounter.WithLabelValues("foo").Add(10)310if diff := assertDiff(&expectation{311Requests: []*testReq{{312Name: "grpc_server_handled_total",313Labels: map[string]string{"grpc_method": "foo"},314Value: 10,315}},316}); diff != "" {317t.Errorf("unexpected output (-want +got):\n%s", diff)318}319320expectedCounter.WithLabelValues("foo").Add(30)321if diff := assertDiff(&expectation{322Requests: []*testReq{{323Name: "grpc_server_handled_total",324Labels: map[string]string{"grpc_method": "foo"},325Value: 30,326}},327}); diff != "" {328t.Errorf("unexpected output (-want +got):\n%s", diff)329}330331expectedCounter.WithLabelValues("foo").Add(10)332expectedCounter.WithLabelValues("bar").Add(20)333if diff := assertDiff(&expectation{334Requests: []*testReq{{335Name: "grpc_server_handled_total",336Labels: map[string]string{"grpc_method": "bar"},337Value: 20,338}, {339Name: "grpc_server_handled_total",340Labels: map[string]string{"grpc_method": "foo"},341Value: 10,342}},343}); diff != "" {344t.Errorf("unexpected output (-want +got):\n%s", diff)345}346347unexpectedCounter := prometheus.NewCounterVec(prometheus.CounterOpts{348Name: "some_unexpected_counter_total",349}, []string{"grpc_method"})350err = reporter.Registry.Register(unexpectedCounter)351if err != nil {352t.Fatal(err)353}354if diff := assertDiff(nil); diff != "" {355t.Errorf("unexpected output (-want +got):\n%s", diff)356}357358unexpectedCounter.WithLabelValues("bazz").Add(20)359if diff := assertDiff(&expectation{360Unexpected: []string{"some_unexpected_counter_total"},361}); diff != "" {362t.Errorf("unexpected output (-want +got):\n%s", diff)363}364365unexpectedCounter.WithLabelValues("bazz").Add(30)366if diff := assertDiff(nil); diff != "" {367t.Errorf("unexpected output (-want +got):\n%s", diff)368}369}370371372