Path: blob/main/components/ide-metrics/pkg/metrics/aggregated-histograms_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/google/go-cmp/cmp/cmpopts"1112dto "github.com/prometheus/client_model/go"13)1415func TestAggregatedHistograms(t *testing.T) {16labelName := "grpc_method"17lowBound := .00518midBound := .0119highBound := .02520agg := NewAggregatedHistograms(21"grpc_server_handling_seconds",22"help",23[]string{labelName},24[]float64{lowBound, midBound, highBound},25)26labelValue := "foo"27var count uint64 = 128sum := 0.00429var lowCount uint64 = 130var midCount uint64 = 131var highCount uint64 = 132assertDiff := func() string {33var actual []*dto.Metric34for _, m := range agg.collect() {35dto := &dto.Metric{}36err := m.Write(dto)37if err != nil {38t.Fatal(err)39}40actual = append(actual, dto)41}4243return cmp.Diff([]*dto.Metric{{44Label: []*dto.LabelPair{{45Name: &labelName,46Value: &labelValue,47}},48Histogram: &dto.Histogram{49SampleCount: &count,50SampleSum: &sum,51Bucket: []*dto.Bucket{52{CumulativeCount: &lowCount, UpperBound: &lowBound},53{CumulativeCount: &midCount, UpperBound: &midBound},54{CumulativeCount: &highCount, UpperBound: &highBound},55},56},57}}, actual, cmpopts.IgnoreUnexported(dto.Metric{}, dto.LabelPair{}, dto.Histogram{}, dto.Bucket{}))58}5960_ = agg.Add([]string{"foo"}, 1, 0.004, []uint64{1, 1, 1})61if diff := assertDiff(); diff != "" {62t.Errorf("unexpected output (-want +got):\n%s", diff)63}6465// invalid buckets66_ = agg.Add([]string{"foo"}, 1, 0.004, []uint64{})67if diff := assertDiff(); diff != "" {68t.Errorf("unexpected output (-want +got):\n%s", diff)69}7071// invalid labels72_ = agg.Add([]string{"foo", "bar"}, 1, 0.004, []uint64{1, 1, 1})73if diff := assertDiff(); diff != "" {74t.Errorf("unexpected output (-want +got):\n%s", diff)75}7677count = count + 578sum = sum + 0.01579lowCount = lowCount + 580midCount = midCount + 581highCount = highCount + 582_ = agg.Add([]string{"foo"}, 5, 0.015, []uint64{5, 5, 5})83if diff := assertDiff(); diff != "" {84t.Errorf("unexpected output (-want +got):\n%s", diff)85}8687count = count + 2088sum = sum + 0.470000000000000389lowCount = lowCount + 590midCount = midCount + 1091highCount = highCount + 1592_ = agg.Add([]string{"foo"}, 20, 0.4700000000000003, []uint64{5, 10, 15})93if diff := assertDiff(); diff != "" {94t.Errorf("unexpected output (-want +got):\n%s", diff)95}96}979899