forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
host_metrics_test.go
65 lines (60 loc) · 1.63 KB
/
host_metrics_test.go
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package grafanacloudconnector
import (
"strings"
"testing"
"gotest.tools/assert"
)
func TestHostMetrics(t *testing.T) {
for _, tc := range []struct {
name string
hosts []string
expectedHostCount int
expectedMetricCount int
expectedDatapointCount int
}{
{
name: "single host",
hosts: []string{"hostA"},
expectedHostCount: 1,
expectedMetricCount: 1,
expectedDatapointCount: 1,
},
{
name: "multiple hosts",
hosts: []string{"hostA", "hostB", "hostC", "hostA", "hostB"},
expectedHostCount: 3,
expectedMetricCount: 1,
expectedDatapointCount: 3,
},
{
name: "none",
hosts: []string{},
},
} {
t.Run(tc.name, func(t *testing.T) {
hm := newHostMetrics()
for _, h := range tc.hosts {
hm.add(h)
}
metrics, count := hm.metrics()
hm.reset()
assert.Equal(t, tc.expectedHostCount, count)
if metrics != nil {
assert.Equal(t, tc.expectedMetricCount, metrics.MetricCount())
assert.Equal(t, tc.expectedDatapointCount, metrics.DataPointCount())
rm := metrics.ResourceMetrics()
metric := rm.At(0).ScopeMetrics().At(0).Metrics().At(0)
assert.Equal(t, hostInfoMetric, metric.Name())
for i := 0; i < count; i++ {
dp := metric.Gauge().DataPoints().At(i)
val, ok := dp.Attributes().Get(hostIdentifierAttr)
assert.Assert(t, ok)
assert.Assert(t, strings.HasPrefix(val.AsString(), "host"))
assert.Equal(t, int64(1), dp.IntValue())
}
}
})
}
}