Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/ide-metrics/pkg/metrics/aggregated-histograms_test.go
2500 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package metrics
6
7
import (
8
"testing"
9
10
"github.com/google/go-cmp/cmp"
11
"github.com/google/go-cmp/cmp/cmpopts"
12
13
dto "github.com/prometheus/client_model/go"
14
)
15
16
func TestAggregatedHistograms(t *testing.T) {
17
labelName := "grpc_method"
18
lowBound := .005
19
midBound := .01
20
highBound := .025
21
agg := NewAggregatedHistograms(
22
"grpc_server_handling_seconds",
23
"help",
24
[]string{labelName},
25
[]float64{lowBound, midBound, highBound},
26
)
27
labelValue := "foo"
28
var count uint64 = 1
29
sum := 0.004
30
var lowCount uint64 = 1
31
var midCount uint64 = 1
32
var highCount uint64 = 1
33
assertDiff := func() string {
34
var actual []*dto.Metric
35
for _, m := range agg.collect() {
36
dto := &dto.Metric{}
37
err := m.Write(dto)
38
if err != nil {
39
t.Fatal(err)
40
}
41
actual = append(actual, dto)
42
}
43
44
return cmp.Diff([]*dto.Metric{{
45
Label: []*dto.LabelPair{{
46
Name: &labelName,
47
Value: &labelValue,
48
}},
49
Histogram: &dto.Histogram{
50
SampleCount: &count,
51
SampleSum: &sum,
52
Bucket: []*dto.Bucket{
53
{CumulativeCount: &lowCount, UpperBound: &lowBound},
54
{CumulativeCount: &midCount, UpperBound: &midBound},
55
{CumulativeCount: &highCount, UpperBound: &highBound},
56
},
57
},
58
}}, actual, cmpopts.IgnoreUnexported(dto.Metric{}, dto.LabelPair{}, dto.Histogram{}, dto.Bucket{}))
59
}
60
61
_ = agg.Add([]string{"foo"}, 1, 0.004, []uint64{1, 1, 1})
62
if diff := assertDiff(); diff != "" {
63
t.Errorf("unexpected output (-want +got):\n%s", diff)
64
}
65
66
// invalid buckets
67
_ = agg.Add([]string{"foo"}, 1, 0.004, []uint64{})
68
if diff := assertDiff(); diff != "" {
69
t.Errorf("unexpected output (-want +got):\n%s", diff)
70
}
71
72
// invalid labels
73
_ = agg.Add([]string{"foo", "bar"}, 1, 0.004, []uint64{1, 1, 1})
74
if diff := assertDiff(); diff != "" {
75
t.Errorf("unexpected output (-want +got):\n%s", diff)
76
}
77
78
count = count + 5
79
sum = sum + 0.015
80
lowCount = lowCount + 5
81
midCount = midCount + 5
82
highCount = highCount + 5
83
_ = agg.Add([]string{"foo"}, 5, 0.015, []uint64{5, 5, 5})
84
if diff := assertDiff(); diff != "" {
85
t.Errorf("unexpected output (-want +got):\n%s", diff)
86
}
87
88
count = count + 20
89
sum = sum + 0.4700000000000003
90
lowCount = lowCount + 5
91
midCount = midCount + 10
92
highCount = highCount + 15
93
_ = agg.Add([]string{"foo"}, 20, 0.4700000000000003, []uint64{5, 10, 15})
94
if diff := assertDiff(); diff != "" {
95
t.Errorf("unexpected output (-want +got):\n%s", diff)
96
}
97
}
98
99