summaryrefslogtreecommitdiff
path: root/util/metrics/counter_test.go
blob: 1e1fa2e16b5c706685245abb4912d7c651446c87 (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// 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 CounterTest struct {
	MetricTest
}

func TestMetricCounter(t *testing.T) {
	partitiontest.PartitionTest(t)

	test := &CounterTest{
		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())

	counter := MakeCounter(MetricName{Name: "metric_test_name1", Description: "this is the metric test for counter object"})

	for i := 0; i < 20; i++ {
		counter.Inc(map[string]string{"pid": "123", "data_host": fmt.Sprintf("host%d", i%5)})
		// 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()

	counter.Deregister(nil)
	// test the metrics values.

	test.Lock()
	defer test.Unlock()
	// the the loop above we've created a single metric name with five different labels set ( host0, host1 .. host 4)
	// let's see if we received all the 5 different labels.
	require.Equal(t, 5, len(test.metrics), "Missing metric counts were reported: %+v", test.metrics)

	for k, v := range test.metrics {
		// we have increased each one of the labels exactly 4 times. See that the counter was counting correctly.
		// ( counters starts at zero )
		require.Equal(t, "4", v, fmt.Sprintf("The metric '%s' reached value '%s'", k, v))
	}
}

func TestMetricCounterFastInts(t *testing.T) {
	partitiontest.PartitionTest(t)

	test := &CounterTest{
		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())

	counter := MakeCounter(MetricName{Name: "metric_test_name1", Description: "this is the metric test for counter object"})

	for i := 0; i < 20; i++ {
		counter.Inc(nil)
		// wait half-a cycle
		time.Sleep(test.sampleRate / 2)
	}
	counter.AddUint64(2, nil)
	// wait two reporting cycles to ensure we received all the messages.
	time.Sleep(test.sampleRate * 2)

	metricService.Shutdown()

	counter.Deregister(nil)
	// test the metrics values.

	test.Lock()
	defer test.Unlock()
	// the the loop above we've created a single metric name with five different labels set ( host0, host1 .. host 4)
	// let's see if we received all the 5 different labels.
	require.Equal(t, 1, len(test.metrics), "Missing metric counts were reported: %+v", test.metrics)

	for k, v := range test.metrics {
		// we have increased each one of the labels exactly 4 times. See that the counter was counting correctly.
		// ( counters starts at zero )
		require.Equal(t, "22", v, fmt.Sprintf("The metric '%s' reached value '%s'", k, v))
	}
}

func TestMetricCounterMixed(t *testing.T) {
	partitiontest.PartitionTest(t)

	test := &CounterTest{
		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())

	counter := MakeCounter(MetricName{Name: "metric_test_name1", Description: "this is the metric test for counter object"})

	counter.AddUint64(5, nil)
	counter.AddUint64(8, map[string]string{})
	for i := 0; i < 20; i++ {
		counter.Inc(nil)
		// wait half-a cycle
		time.Sleep(test.sampleRate / 2)
	}
	counter.AddUint64(2, nil)
	// wait two reporting cycles to ensure we received all the messages.
	time.Sleep(test.sampleRate * 2)

	metricService.Shutdown()

	counter.Deregister(nil)
	// test the metrics values.

	test.Lock()
	defer test.Unlock()
	// the the loop above we've created a single metric name with five different labels set ( host0, host1 .. host 4)
	// let's see if we received all the 5 different labels.
	require.Equal(t, 1, len(test.metrics), "Missing metric counts were reported: %+v", test.metrics)

	for k, v := range test.metrics {
		// we have increased each one of the labels exactly 4 times. See that the counter was counting correctly.
		// ( counters starts at zero )
		require.Equal(t, "35", v, fmt.Sprintf("The metric '%s' reached value '%s'", k, v))
	}
}

func TestCounterWriteMetric(t *testing.T) {
	partitiontest.PartitionTest(t)

	c := MakeCounter(MetricName{Name: "testname", Description: "testhelp"})
	c.Deregister(nil)

	// ensure 0 counters are still logged
	sbOut := strings.Builder{}
	c.WriteMetric(&sbOut, `host="myhost"`)
	expected := `# HELP testname testhelp
# TYPE testname counter
testname{host="myhost"} 0
`
	require.Equal(t, expected, sbOut.String())

	c.AddUint64(2, nil)
	// ensure non-zero counters are logged
	sbOut = strings.Builder{}
	c.WriteMetric(&sbOut, `host="myhost"`)
	expected = `# HELP testname testhelp
# TYPE testname counter
testname{host="myhost"} 2
`
	require.Equal(t, expected, sbOut.String())
}

func TestGetValue(t *testing.T) {
	partitiontest.PartitionTest(t)

	c := MakeCounter(MetricName{Name: "testname", Description: "testhelp"})
	c.Deregister(nil)

	require.Equal(t, uint64(0), c.GetUint64Value())
	c.Inc(nil)
	require.Equal(t, uint64(1), c.GetUint64Value())
	c.Inc(nil)
	require.Equal(t, uint64(2), c.GetUint64Value())
}

func TestGetValueForLabels(t *testing.T) {
	partitiontest.PartitionTest(t)

	c := MakeCounter(MetricName{Name: "testname", Description: "testhelp"})
	c.Deregister(nil)

	labels := map[string]string{"a": "b"}
	require.Equal(t, uint64(0), c.GetUint64ValueForLabels(labels))
	c.Inc(labels)
	require.Equal(t, uint64(1), c.GetUint64ValueForLabels(labels))
	c.Inc(labels)
	require.Equal(t, uint64(2), c.GetUint64ValueForLabels(labels))
	// confirm that the value is not shared between labels
	c.Inc(nil)
	require.Equal(t, uint64(2), c.GetUint64ValueForLabels(labels))
	labels2 := map[string]string{"a": "c"}
	c.Inc(labels2)
	require.Equal(t, uint64(1), c.GetUint64ValueForLabels(labels2))
}