forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batchperresourceattr.go
181 lines (160 loc) · 5.15 KB
/
batchperresourceattr.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
173
174
175
176
177
178
179
180
181
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package batchperresourceattr // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/batchperresourceattr"
import (
"context"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.uber.org/multierr"
)
type batchTraces struct {
attrKey string
next consumer.Traces
}
func NewBatchPerResourceTraces(attrKey string, next consumer.Traces) consumer.Traces {
return &batchTraces{
attrKey: attrKey,
next: next,
}
}
// Capabilities implements the consumer interface.
func (bt *batchTraces) Capabilities() consumer.Capabilities {
return consumer.Capabilities{MutatesData: true}
}
func (bt *batchTraces) ConsumeTraces(ctx context.Context, td ptrace.Traces) error {
rss := td.ResourceSpans()
lenRss := rss.Len()
// If zero or one resource spans just call next.
if lenRss <= 1 {
return bt.next.ConsumeTraces(ctx, td)
}
indicesByAttr := make(map[string][]int)
for i := 0; i < lenRss; i++ {
rs := rss.At(i)
var attrVal string
if attributeValue, ok := rs.Resource().Attributes().Get(bt.attrKey); ok {
attrVal = attributeValue.Str()
}
indicesByAttr[attrVal] = append(indicesByAttr[attrVal], i)
}
// If there is a single attribute value, then call next.
if len(indicesByAttr) <= 1 {
return bt.next.ConsumeTraces(ctx, td)
}
// Build the resource spans for each attribute value using CopyTo and call next for each one.
var errs error
for _, indices := range indicesByAttr {
tracesForAttr := ptrace.NewTraces()
for _, i := range indices {
rs := rss.At(i)
rs.CopyTo(tracesForAttr.ResourceSpans().AppendEmpty())
}
errs = multierr.Append(errs, bt.next.ConsumeTraces(ctx, tracesForAttr))
}
return errs
}
type batchMetrics struct {
attrKey string
next consumer.Metrics
}
func NewBatchPerResourceMetrics(attrKey string, next consumer.Metrics) consumer.Metrics {
return &batchMetrics{
attrKey: attrKey,
next: next,
}
}
// Capabilities implements the consumer interface.
func (bt *batchMetrics) Capabilities() consumer.Capabilities {
return consumer.Capabilities{MutatesData: true}
}
func (bt *batchMetrics) ConsumeMetrics(ctx context.Context, td pmetric.Metrics) error {
rms := td.ResourceMetrics()
lenRms := rms.Len()
// If zero or one resource metrics just call next.
if lenRms <= 1 {
return bt.next.ConsumeMetrics(ctx, td)
}
indicesByAttr := make(map[string][]int)
for i := 0; i < lenRms; i++ {
rm := rms.At(i)
var attrVal string
if attributeValue, ok := rm.Resource().Attributes().Get(bt.attrKey); ok {
attrVal = attributeValue.Str()
}
indicesByAttr[attrVal] = append(indicesByAttr[attrVal], i)
}
// If there is a single attribute value, then call next.
if len(indicesByAttr) <= 1 {
return bt.next.ConsumeMetrics(ctx, td)
}
// Build the resource metrics for each attribute value using CopyTo and call next for each one.
var errs error
for _, indices := range indicesByAttr {
metricsForAttr := pmetric.NewMetrics()
for _, i := range indices {
rm := rms.At(i)
rm.CopyTo(metricsForAttr.ResourceMetrics().AppendEmpty())
}
errs = multierr.Append(errs, bt.next.ConsumeMetrics(ctx, metricsForAttr))
}
return errs
}
type batchLogs struct {
attrKey string
next consumer.Logs
}
func NewBatchPerResourceLogs(attrKey string, next consumer.Logs) consumer.Logs {
return &batchLogs{
attrKey: attrKey,
next: next,
}
}
// Capabilities implements the consumer interface.
func (bt *batchLogs) Capabilities() consumer.Capabilities {
return consumer.Capabilities{MutatesData: true}
}
func (bt *batchLogs) ConsumeLogs(ctx context.Context, td plog.Logs) error {
rls := td.ResourceLogs()
lenRls := rls.Len()
// If zero or one resource logs just call next.
if lenRls <= 1 {
return bt.next.ConsumeLogs(ctx, td)
}
indicesByAttr := make(map[string][]int)
for i := 0; i < lenRls; i++ {
rl := rls.At(i)
var attrVal string
if attributeValue, ok := rl.Resource().Attributes().Get(bt.attrKey); ok {
attrVal = attributeValue.Str()
}
indicesByAttr[attrVal] = append(indicesByAttr[attrVal], i)
}
// If there is a single attribute value, then call next.
if len(indicesByAttr) <= 1 {
return bt.next.ConsumeLogs(ctx, td)
}
// Build the resource logs for each attribute value using CopyTo and call next for each one.
var errs error
for _, indices := range indicesByAttr {
logsForAttr := plog.NewLogs()
for _, i := range indices {
rl := rls.At(i)
rl.CopyTo(logsForAttr.ResourceLogs().AppendEmpty())
}
errs = multierr.Append(errs, bt.next.ConsumeLogs(ctx, logsForAttr))
}
return errs
}