-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
resource_processor_test.go
172 lines (156 loc) · 5.44 KB
/
resource_processor_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package resourceprocessor
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.opentelemetry.io/collector/processor/processortest"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/attraction"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/testdata"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/plogtest"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/ptracetest"
)
var (
cfg = &Config{
AttributesActions: []attraction.ActionKeyValue{
{Key: "cloud.availability_zone", Value: "zone-1", Action: attraction.UPSERT},
{Key: "k8s.cluster.name", FromAttribute: "k8s-cluster", Action: attraction.INSERT},
{Key: "redundant-attribute", Action: attraction.DELETE},
},
}
)
func TestResourceProcessorAttributesUpsert(t *testing.T) {
tests := []struct {
name string
config *Config
sourceAttributes map[string]string
wantAttributes map[string]string
}{
{
name: "config_with_attributes_applied_on_nil_resource",
config: cfg,
sourceAttributes: nil,
wantAttributes: map[string]string{
"cloud.availability_zone": "zone-1",
},
},
{
name: "config_with_attributes_applied_on_empty_resource",
config: cfg,
sourceAttributes: map[string]string{},
wantAttributes: map[string]string{
"cloud.availability_zone": "zone-1",
},
},
{
name: "config_attributes_applied_on_existing_resource_attributes",
config: cfg,
sourceAttributes: map[string]string{
"cloud.availability_zone": "to-be-replaced",
"k8s-cluster": "test-cluster",
"redundant-attribute": "to-be-removed",
},
wantAttributes: map[string]string{
"cloud.availability_zone": "zone-1",
"k8s-cluster": "test-cluster",
"k8s.cluster.name": "test-cluster",
},
},
{
name: "config_attributes_replacement",
config: &Config{
AttributesActions: []attraction.ActionKeyValue{
{Key: "k8s.cluster.name", FromAttribute: "k8s-cluster", Action: attraction.INSERT},
{Key: "k8s-cluster", Action: attraction.DELETE},
},
},
sourceAttributes: map[string]string{
"k8s-cluster": "test-cluster",
},
wantAttributes: map[string]string{
"k8s.cluster.name": "test-cluster",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Test trace consumer
ttn := new(consumertest.TracesSink)
factory := NewFactory()
rtp, err := factory.CreateTracesProcessor(context.Background(), processortest.NewNopSettings(), tt.config, ttn)
require.NoError(t, err)
assert.True(t, rtp.Capabilities().MutatesData)
sourceTraceData := generateTraceData(tt.sourceAttributes)
wantTraceData := generateTraceData(tt.wantAttributes)
err = rtp.ConsumeTraces(context.Background(), sourceTraceData)
require.NoError(t, err)
traces := ttn.AllTraces()
require.Len(t, traces, 1)
assert.NoError(t, ptracetest.CompareTraces(wantTraceData, traces[0]))
// Test metrics consumer
tmn := new(consumertest.MetricsSink)
rmp, err := factory.CreateMetricsProcessor(context.Background(), processortest.NewNopSettings(), tt.config, tmn)
require.NoError(t, err)
assert.True(t, rtp.Capabilities().MutatesData)
sourceMetricData := generateMetricData(tt.sourceAttributes)
wantMetricData := generateMetricData(tt.wantAttributes)
err = rmp.ConsumeMetrics(context.Background(), sourceMetricData)
require.NoError(t, err)
metrics := tmn.AllMetrics()
require.Len(t, metrics, 1)
assert.NoError(t, pmetrictest.CompareMetrics(wantMetricData, metrics[0]))
// Test logs consumer
tln := new(consumertest.LogsSink)
rlp, err := factory.CreateLogsProcessor(context.Background(), processortest.NewNopSettings(), tt.config, tln)
require.NoError(t, err)
assert.True(t, rtp.Capabilities().MutatesData)
sourceLogData := generateLogData(tt.sourceAttributes)
wantLogData := generateLogData(tt.wantAttributes)
err = rlp.ConsumeLogs(context.Background(), sourceLogData)
require.NoError(t, err)
logs := tln.AllLogs()
require.Len(t, logs, 1)
assert.NoError(t, plogtest.CompareLogs(wantLogData, logs[0]))
})
}
}
func generateTraceData(attributes map[string]string) ptrace.Traces {
td := testdata.GenerateTracesOneSpanNoResource()
if attributes == nil {
return td
}
resource := td.ResourceSpans().At(0).Resource()
for k, v := range attributes {
resource.Attributes().PutStr(k, v)
}
return td
}
func generateMetricData(attributes map[string]string) pmetric.Metrics {
md := testdata.GenerateMetricsOneMetricNoResource()
if attributes == nil {
return md
}
resource := md.ResourceMetrics().At(0).Resource()
for k, v := range attributes {
resource.Attributes().PutStr(k, v)
}
return md
}
func generateLogData(attributes map[string]string) plog.Logs {
ld := testdata.GenerateLogsOneLogRecordNoResource()
if attributes == nil {
return ld
}
resource := ld.ResourceLogs().At(0).Resource()
for k, v := range attributes {
resource.Attributes().PutStr(k, v)
}
return ld
}