forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnector_test.go
110 lines (95 loc) · 3.04 KB
/
connector_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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package grafanacloudconnector
import (
"context"
"testing"
"time"
"go.opentelemetry.io/collector/connector/connectortest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
"gotest.tools/assert"
)
func TestNewConnector(t *testing.T) {
for _, tc := range []struct {
name string
hostIdentifiers []string
metricsFlushInterval *time.Duration
expectedConfig *Config
}{
{
name: "default config",
expectedConfig: createDefaultConfig().(*Config),
},
{
name: "other config",
hostIdentifiers: []string{"host.id", "host.name", "k8s.node.uid"},
metricsFlushInterval: durationPtr(15 * time.Second),
expectedConfig: &Config{
HostIdentifiers: []string{"host.id", "host.name", "k8s.node.uid"},
MetricsFlushInterval: 15 * time.Second,
},
},
} {
t.Run(tc.name, func(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig().(*Config)
if tc.hostIdentifiers != nil {
cfg.HostIdentifiers = tc.hostIdentifiers
}
if tc.metricsFlushInterval != nil {
cfg.MetricsFlushInterval = *tc.metricsFlushInterval
}
c, err := factory.CreateTracesToMetrics(context.Background(), connectortest.NewNopCreateSettings(), cfg, consumertest.NewNop())
imp := c.(*connectorImp)
assert.NilError(t, err)
assert.Assert(t, imp != nil)
assert.DeepEqual(t, tc.expectedConfig.HostIdentifiers, imp.config.HostIdentifiers)
assert.DeepEqual(t, tc.expectedConfig.MetricsFlushInterval, imp.config.MetricsFlushInterval)
})
}
}
func TestConsumeTraces(t *testing.T) {
testCases := []struct {
name string
input ptrace.Traces
output []pmetric.Metrics
}{
{
name: "sample",
input: testTraces(),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig().(*Config)
cfg.MetricsFlushInterval = 50 * time.Millisecond
sink := &consumertest.MetricsSink{}
c, err := factory.CreateTracesToMetrics(context.Background(), connectortest.NewNopCreateSettings(), cfg, sink)
assert.NilError(t, err)
ctx := context.Background()
assert.NilError(t, c.Start(ctx, nil))
err = c.ConsumeTraces(ctx, tc.input)
assert.NilError(t, err)
assert.NilError(t, c.Shutdown(ctx))
metrics := sink.AllMetrics()
assert.Assert(t, len(metrics) > 0)
sm := metrics[0].ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0)
assert.Equal(t, hostInfoMetric, sm.Name())
val, ok := sm.Gauge().DataPoints().At(0).Attributes().Get(hostIdentifierAttr)
assert.Assert(t, ok)
assert.Equal(t, "foo", val.AsString())
})
}
}
func testTraces() ptrace.Traces {
traces := ptrace.NewTraces()
resourceSpans := traces.ResourceSpans().AppendEmpty()
resourceSpans.Resource().Attributes().PutStr("host.id", "foo")
return traces
}
func durationPtr(t time.Duration) *time.Duration {
return &t
}