forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collectd_test.go
142 lines (127 loc) · 4.05 KB
/
collectd_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
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package collectdreceiver
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
)
func TestDecodeEvent(t *testing.T) {
metrics := pmetric.NewMetrics()
scopeMetrics := metrics.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty()
jsonData, err := os.ReadFile(filepath.Join("testdata", "event.json"))
require.NoError(t, err)
var records []collectDRecord
err = json.Unmarshal(jsonData, &records)
require.NoError(t, err)
for _, cdr := range records {
err := cdr.appendToMetrics(zap.NewNop(), scopeMetrics, map[string]string{})
assert.NoError(t, err)
assert.Equal(t, 0, metrics.MetricCount())
}
}
func TestDecodeMetrics(t *testing.T) {
metrics := pmetric.NewMetrics()
scopeMemtrics := metrics.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty()
jsonData, err := os.ReadFile(filepath.Join("testdata", "collectd.json"))
require.NoError(t, err)
var records []collectDRecord
err = json.Unmarshal(jsonData, &records)
require.NoError(t, err)
for _, cdr := range records {
err = cdr.appendToMetrics(zap.NewNop(), scopeMemtrics, map[string]string{})
assert.NoError(t, err)
}
assert.Equal(t, 10, metrics.MetricCount())
assertMetricsEqual(t, metrics)
}
func createPtrFloat64(v float64) *float64 {
return &v
}
func TestStartTimestamp(t *testing.T) {
tests := []struct {
name string
record collectDRecord
metricDescriptorType string
wantStartTimestamp pcommon.Timestamp
}{
{
name: "metric type cumulative distribution",
record: collectDRecord{
Time: createPtrFloat64(10),
Interval: createPtrFloat64(5),
},
metricDescriptorType: "cumulative",
wantStartTimestamp: pcommon.NewTimestampFromTime(time.Unix(5, 0)),
},
{
name: "metric type cumulative double",
record: collectDRecord{
Time: createPtrFloat64(10),
Interval: createPtrFloat64(5),
},
metricDescriptorType: "cumulative",
wantStartTimestamp: pcommon.NewTimestampFromTime(time.Unix(5, 0)),
},
{
name: "metric type cumulative int64",
record: collectDRecord{
Time: createPtrFloat64(10),
Interval: createPtrFloat64(5),
},
metricDescriptorType: "cumulative",
wantStartTimestamp: pcommon.NewTimestampFromTime(time.Unix(5, 0)),
},
{
name: "metric type non-cumulative gauge distribution",
record: collectDRecord{
Time: createPtrFloat64(0),
Interval: createPtrFloat64(0),
},
metricDescriptorType: "gauge",
wantStartTimestamp: pcommon.NewTimestampFromTime(time.Unix(0, 0)),
},
{
name: "metric type non-cumulative gauge int64",
record: collectDRecord{
Time: createPtrFloat64(0),
Interval: createPtrFloat64(0),
},
metricDescriptorType: "gauge",
wantStartTimestamp: pcommon.NewTimestampFromTime(time.Unix(0, 0)),
},
{
name: "metric type non-cumulativegauge double",
record: collectDRecord{
Time: createPtrFloat64(0),
Interval: createPtrFloat64(0),
},
metricDescriptorType: "gauge",
wantStartTimestamp: pcommon.NewTimestampFromTime(time.Unix(0, 0)),
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
gotStartTimestamp := tc.record.startTimestamp(tc.metricDescriptorType)
assert.Equal(t, tc.wantStartTimestamp.AsTime(), gotStartTimestamp.AsTime())
})
}
}
func assertMetricsEqual(t *testing.T, actual pmetric.Metrics) {
goldenPath := filepath.Join("testdata", "expected.yaml")
expectedMetrics, err := golden.ReadMetrics(goldenPath)
require.NoError(t, err)
err = pmetrictest.CompareMetrics(expectedMetrics, actual, pmetrictest.IgnoreStartTimestamp(),
pmetrictest.IgnoreTimestamp(),
pmetrictest.IgnoreMetricsOrder())
require.NoError(t, err)
}