1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.
package metrics
import (
"context"
"fmt"
"strings"
"testing"
"time"
"github.com/algorand/go-algorand/test/partitiontest"
"github.com/stretchr/testify/require"
)
type GaugeTest struct {
MetricTest
}
func TestMetricGauge(t *testing.T) {
partitiontest.PartitionTest(t)
test := &GaugeTest{
MetricTest: NewMetricTest(),
}
// create a http listener.
port := test.createListener("127.0.0.1:0")
metricService := MakeMetricService(&ServiceConfig{
NodeExporterListenAddress: fmt.Sprintf("localhost:%d", port),
Labels: map[string]string{
"host_name": "host_one",
"session_id": "AFX-229"},
})
metricService.Start(context.Background())
gauges := make([]*Gauge, 3)
for i := 0; i < 3; i++ {
gauges[i] = MakeGauge(MetricName{Name: fmt.Sprintf("gauge_%d", i), Description: "this is the metric test for gauge object"})
}
for i := 0; i < 9; i++ {
gauges[i%3].Set(uint64(i * 100))
gauges[i%3].Add(uint64(i))
// wait half-a cycle
time.Sleep(test.sampleRate / 2)
}
// wait two reporting cycles to ensure we received all the messages.
time.Sleep(test.sampleRate * 2)
metricService.Shutdown()
for _, gauge := range gauges {
gauge.Deregister(nil)
}
// test the metrics values.
test.Lock()
defer test.Unlock()
// the the loop above we've created 3 separate gauges
// let's see if we received all 3 metrics
require.Equal(t, 3, len(test.metrics), "Missing metric counts were reported: %+v", test.metrics)
// iterate through the metrics and check the each of the metrics reached it's correct count.
for k, v := range test.metrics {
if strings.Contains(k, "gauge_0") {
require.Equal(t, "606", v, fmt.Sprintf("The metric '%s' reached value '%s'", k, v))
}
if strings.Contains(k, "gauge_1") {
require.Equal(t, "707", v, fmt.Sprintf("The metric '%s' reached value '%s'", k, v))
}
if strings.Contains(k, "gauge_2") {
require.Equal(t, "808", v, fmt.Sprintf("The metric '%s' reached value '%s'", k, v))
}
}
}
|