-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathprocessor.go
297 lines (265 loc) · 9.57 KB
/
processor.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package redactionprocessor // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/redactionprocessor"
import (
"context"
"fmt"
"regexp"
"sort"
"strings"
"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.uber.org/zap"
)
const attrValuesSeparator = ","
type redaction struct {
// Attribute keys allowed in a span
allowList map[string]string
// Attribute keys ignored in a span
ignoreList map[string]string
// Attribute values blocked in a span
blockRegexList map[string]*regexp.Regexp
// Redaction processor configuration
config *Config
// Logger
logger *zap.Logger
}
// newRedaction creates a new instance of the redaction processor
func newRedaction(ctx context.Context, config *Config, logger *zap.Logger) (*redaction, error) {
allowList := makeAllowList(config)
ignoreList := makeIgnoreList(config)
blockRegexList, err := makeBlockRegexList(ctx, config)
if err != nil {
// TODO: Placeholder for an error metric in the next PR
return nil, fmt.Errorf("failed to process block list: %w", err)
}
return &redaction{
allowList: allowList,
ignoreList: ignoreList,
blockRegexList: blockRegexList,
config: config,
logger: logger,
}, nil
}
// processTraces implements ProcessMetricsFunc. It processes the incoming data
// and returns the data to be sent to the next component
func (s *redaction) processTraces(ctx context.Context, batch ptrace.Traces) (ptrace.Traces, error) {
for i := 0; i < batch.ResourceSpans().Len(); i++ {
rs := batch.ResourceSpans().At(i)
s.processResourceSpan(ctx, rs)
}
return batch, nil
}
func (s *redaction) processLogs(ctx context.Context, logs plog.Logs) (plog.Logs, error) {
for i := 0; i < logs.ResourceLogs().Len(); i++ {
rl := logs.ResourceLogs().At(i)
s.processResourceLog(ctx, rl)
}
return logs, nil
}
func (s *redaction) processMetrics(ctx context.Context, metrics pmetric.Metrics) (pmetric.Metrics, error) {
for i := 0; i < metrics.ResourceMetrics().Len(); i++ {
rm := metrics.ResourceMetrics().At(i)
s.processResourceMetric(ctx, rm)
}
return metrics, nil
}
// processResourceSpan processes the RS and all of its spans and then returns the last
// view metric context. The context can be used for tests
func (s *redaction) processResourceSpan(ctx context.Context, rs ptrace.ResourceSpans) {
rsAttrs := rs.Resource().Attributes()
// Attributes can be part of a resource span
s.processAttrs(ctx, rsAttrs)
for j := 0; j < rs.ScopeSpans().Len(); j++ {
ils := rs.ScopeSpans().At(j)
for k := 0; k < ils.Spans().Len(); k++ {
span := ils.Spans().At(k)
spanAttrs := span.Attributes()
// Attributes can also be part of span
s.processAttrs(ctx, spanAttrs)
}
}
}
// processResourceLog processes the log resource and all of its logs and then returns the last
// view metric context. The context can be used for tests
func (s *redaction) processResourceLog(ctx context.Context, rl plog.ResourceLogs) {
rsAttrs := rl.Resource().Attributes()
s.processAttrs(ctx, rsAttrs)
for j := 0; j < rl.ScopeLogs().Len(); j++ {
ils := rl.ScopeLogs().At(j)
for k := 0; k < ils.LogRecords().Len(); k++ {
log := ils.LogRecords().At(k)
s.processAttrs(ctx, log.Attributes())
}
}
}
func (s *redaction) processResourceMetric(ctx context.Context, rm pmetric.ResourceMetrics) {
rsAttrs := rm.Resource().Attributes()
s.processAttrs(ctx, rsAttrs)
for j := 0; j < rm.ScopeMetrics().Len(); j++ {
ils := rm.ScopeMetrics().At(j)
for k := 0; k < ils.Metrics().Len(); k++ {
metric := ils.Metrics().At(k)
switch metric.Type() {
case pmetric.MetricTypeGauge:
dps := metric.Gauge().DataPoints()
for i := 0; i < dps.Len(); i++ {
s.processAttrs(ctx, dps.At(i).Attributes())
}
case pmetric.MetricTypeSum:
dps := metric.Sum().DataPoints()
for i := 0; i < dps.Len(); i++ {
s.processAttrs(ctx, dps.At(i).Attributes())
}
case pmetric.MetricTypeHistogram:
dps := metric.Histogram().DataPoints()
for i := 0; i < dps.Len(); i++ {
s.processAttrs(ctx, dps.At(i).Attributes())
}
case pmetric.MetricTypeExponentialHistogram:
dps := metric.ExponentialHistogram().DataPoints()
for i := 0; i < dps.Len(); i++ {
s.processAttrs(ctx, dps.At(i).Attributes())
}
case pmetric.MetricTypeSummary:
dps := metric.Summary().DataPoints()
for i := 0; i < dps.Len(); i++ {
s.processAttrs(ctx, dps.At(i).Attributes())
}
case pmetric.MetricTypeEmpty:
}
}
}
}
// processAttrs redacts the attributes of a resource span or a span
func (s *redaction) processAttrs(_ context.Context, attributes pcommon.Map) {
// TODO: Use the context for recording metrics
var toDelete []string
var toBlock []string
var ignoring []string
// Identify attributes to redact and mask in the following sequence
// 1. Make a list of attribute keys to redact
// 2. Mask any blocked values for the other attributes
// 3. Delete the attributes from 1
//
// This sequence satisfies these performance constraints:
// - Only range through all attributes once
// - Don't mask any values if the whole attribute is slated for deletion
attributes.Range(func(k string, value pcommon.Value) bool {
// don't delete or redact the attribute if it should be ignored
if _, ignored := s.ignoreList[k]; ignored {
ignoring = append(ignoring, k)
// Skip to the next attribute
return true
}
// Make a list of attribute keys to redact
if !s.config.AllowAllKeys {
if _, allowed := s.allowList[k]; !allowed {
toDelete = append(toDelete, k)
// Skip to the next attribute
return true
}
}
// Mask any blocked values for the other attributes
strVal := value.Str()
var matched bool
for _, compiledRE := range s.blockRegexList {
match := compiledRE.MatchString(strVal)
if match {
if !matched {
matched = true
toBlock = append(toBlock, k)
}
maskedValue := compiledRE.ReplaceAllString(strVal, "****")
value.SetStr(maskedValue)
strVal = maskedValue
}
}
return true
})
// Delete the attributes on the redaction list
for _, k := range toDelete {
attributes.Remove(k)
}
// Add diagnostic information to the span
s.addMetaAttrs(toDelete, attributes, redactedKeys, redactedKeyCount)
s.addMetaAttrs(toBlock, attributes, maskedValues, maskedValueCount)
s.addMetaAttrs(ignoring, attributes, "", ignoredKeyCount)
}
// addMetaAttrs adds diagnostic information about redacted or masked attribute keys
func (s *redaction) addMetaAttrs(redactedAttrs []string, attributes pcommon.Map, valuesAttr, countAttr string) {
redactedCount := int64(len(redactedAttrs))
if redactedCount == 0 {
return
}
// Record summary as span attributes, empty string for ignored items
if s.config.Summary == debug && len(valuesAttr) > 0 {
if existingVal, found := attributes.Get(valuesAttr); found && existingVal.Str() != "" {
redactedAttrs = append(redactedAttrs, strings.Split(existingVal.Str(), attrValuesSeparator)...)
}
sort.Strings(redactedAttrs)
attributes.PutStr(valuesAttr, strings.Join(redactedAttrs, attrValuesSeparator))
}
if s.config.Summary == info || s.config.Summary == debug {
if existingVal, found := attributes.Get(countAttr); found {
redactedCount += existingVal.Int()
}
attributes.PutInt(countAttr, redactedCount)
}
}
const (
debug = "debug"
info = "info"
redactedKeys = "redaction.redacted.keys"
redactedKeyCount = "redaction.redacted.count"
maskedValues = "redaction.masked.keys"
maskedValueCount = "redaction.masked.count"
ignoredKeyCount = "redaction.ignored.count"
)
// makeAllowList sets up a lookup table of allowed span attribute keys
func makeAllowList(c *Config) map[string]string {
// redactionKeys are additional span attributes created by the processor to
// summarize the changes it made to a span. If the processor removes
// 2 attributes from a span (e.g. `birth_date`, `mothers_maiden_name`),
// then it will list them in the `redaction.redacted.keys` span attribute
// and set the `redaction.redacted.count` attribute to 2
//
// If the processor finds and masks values matching a blocked regex in 2
// span attributes (e.g. `notes`, `description`), then it will those
// attribute keys in `redaction.masked.keys` and set the
// `redaction.masked.count` to 2
redactionKeys := []string{redactedKeys, redactedKeyCount, maskedValues, maskedValueCount, ignoredKeyCount}
// allowList consists of the keys explicitly allowed by the configuration
// as well as of the new span attributes that the processor creates to
// summarize its changes
allowList := make(map[string]string, len(c.AllowedKeys)+len(redactionKeys))
for _, key := range c.AllowedKeys {
allowList[key] = key
}
for _, key := range redactionKeys {
allowList[key] = key
}
return allowList
}
func makeIgnoreList(c *Config) map[string]string {
ignoreList := make(map[string]string, len(c.IgnoredKeys))
for _, key := range c.IgnoredKeys {
ignoreList[key] = key
}
return ignoreList
}
// makeBlockRegexList precompiles all the blocked regex patterns
func makeBlockRegexList(_ context.Context, config *Config) (map[string]*regexp.Regexp, error) {
blockRegexList := make(map[string]*regexp.Regexp, len(config.BlockedValues))
for _, pattern := range config.BlockedValues {
re, err := regexp.Compile(pattern)
if err != nil {
// TODO: Placeholder for an error metric in the next PR
return nil, fmt.Errorf("error compiling regex in block list: %w", err)
}
blockRegexList[pattern] = re
}
return blockRegexList, nil
}