-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
generated_component_test.go
149 lines (134 loc) · 4.69 KB
/
generated_component_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
143
144
145
146
147
148
149
// Code generated by mdatagen. DO NOT EDIT.
package resourceprocessor
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/confmap/confmaptest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.opentelemetry.io/collector/processor"
"go.opentelemetry.io/collector/processor/processortest"
)
func TestComponentFactoryType(t *testing.T) {
require.Equal(t, "resource", NewFactory().Type().String())
}
func TestComponentConfigStruct(t *testing.T) {
require.NoError(t, componenttest.CheckConfigStruct(NewFactory().CreateDefaultConfig()))
}
func TestComponentLifecycle(t *testing.T) {
factory := NewFactory()
tests := []struct {
name string
createFn func(ctx context.Context, set processor.Settings, cfg component.Config) (component.Component, error)
}{
{
name: "logs",
createFn: func(ctx context.Context, set processor.Settings, cfg component.Config) (component.Component, error) {
return factory.CreateLogsProcessor(ctx, set, cfg, consumertest.NewNop())
},
},
{
name: "metrics",
createFn: func(ctx context.Context, set processor.Settings, cfg component.Config) (component.Component, error) {
return factory.CreateMetricsProcessor(ctx, set, cfg, consumertest.NewNop())
},
},
{
name: "traces",
createFn: func(ctx context.Context, set processor.Settings, cfg component.Config) (component.Component, error) {
return factory.CreateTracesProcessor(ctx, set, cfg, consumertest.NewNop())
},
},
}
cm, err := confmaptest.LoadConf("metadata.yaml")
require.NoError(t, err)
cfg := factory.CreateDefaultConfig()
sub, err := cm.Sub("tests::config")
require.NoError(t, err)
require.NoError(t, sub.Unmarshal(&cfg))
for _, test := range tests {
t.Run(test.name+"-shutdown", func(t *testing.T) {
c, err := test.createFn(context.Background(), processortest.NewNopSettings(), cfg)
require.NoError(t, err)
err = c.Shutdown(context.Background())
require.NoError(t, err)
})
t.Run(test.name+"-lifecycle", func(t *testing.T) {
c, err := test.createFn(context.Background(), processortest.NewNopSettings(), cfg)
require.NoError(t, err)
host := componenttest.NewNopHost()
err = c.Start(context.Background(), host)
require.NoError(t, err)
require.NotPanics(t, func() {
switch test.name {
case "logs":
e, ok := c.(processor.Logs)
require.True(t, ok)
logs := generateLifecycleTestLogs()
if !e.Capabilities().MutatesData {
logs.MarkReadOnly()
}
err = e.ConsumeLogs(context.Background(), logs)
case "metrics":
e, ok := c.(processor.Metrics)
require.True(t, ok)
metrics := generateLifecycleTestMetrics()
if !e.Capabilities().MutatesData {
metrics.MarkReadOnly()
}
err = e.ConsumeMetrics(context.Background(), metrics)
case "traces":
e, ok := c.(processor.Traces)
require.True(t, ok)
traces := generateLifecycleTestTraces()
if !e.Capabilities().MutatesData {
traces.MarkReadOnly()
}
err = e.ConsumeTraces(context.Background(), traces)
}
})
require.NoError(t, err)
err = c.Shutdown(context.Background())
require.NoError(t, err)
})
}
}
func generateLifecycleTestLogs() plog.Logs {
logs := plog.NewLogs()
rl := logs.ResourceLogs().AppendEmpty()
rl.Resource().Attributes().PutStr("resource", "R1")
l := rl.ScopeLogs().AppendEmpty().LogRecords().AppendEmpty()
l.Body().SetStr("test log message")
l.SetTimestamp(pcommon.NewTimestampFromTime(time.Now()))
return logs
}
func generateLifecycleTestMetrics() pmetric.Metrics {
metrics := pmetric.NewMetrics()
rm := metrics.ResourceMetrics().AppendEmpty()
rm.Resource().Attributes().PutStr("resource", "R1")
m := rm.ScopeMetrics().AppendEmpty().Metrics().AppendEmpty()
m.SetName("test_metric")
dp := m.SetEmptyGauge().DataPoints().AppendEmpty()
dp.Attributes().PutStr("test_attr", "value_1")
dp.SetIntValue(123)
dp.SetTimestamp(pcommon.NewTimestampFromTime(time.Now()))
return metrics
}
func generateLifecycleTestTraces() ptrace.Traces {
traces := ptrace.NewTraces()
rs := traces.ResourceSpans().AppendEmpty()
rs.Resource().Attributes().PutStr("resource", "R1")
span := rs.ScopeSpans().AppendEmpty().Spans().AppendEmpty()
span.Attributes().PutStr("test_attr", "value_1")
span.SetName("test_span")
span.SetStartTimestamp(pcommon.NewTimestampFromTime(time.Now().Add(-1 * time.Second)))
span.SetEndTimestamp(pcommon.NewTimestampFromTime(time.Now()))
return traces
}