-
Notifications
You must be signed in to change notification settings - Fork 25
/
processor.go
292 lines (269 loc) · 8.87 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
package rollupprocessor
import (
"context"
"encoding/base64"
"fmt"
"time"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/processor"
"github.com/fluxninja/aperture/v2/pkg/log"
"github.com/fluxninja/aperture/v2/pkg/metrics"
"github.com/fluxninja/aperture/v2/pkg/otelcollector"
otelconsts "github.com/fluxninja/aperture/v2/pkg/otelcollector/consts"
"github.com/fluxninja/datasketches-go/sketches"
"github.com/prometheus/client_golang/prometheus"
)
var defaultRollupGroups = []RollupGroup{
{
FromField: otelconsts.WorkloadDurationLabel,
WithDatasketch: true,
},
{
FromField: otelconsts.FlowDurationLabel,
WithDatasketch: true,
},
{
FromField: otelconsts.ApertureProcessingDurationLabel,
WithDatasketch: true,
},
{
FromField: otelconsts.HTTPRequestContentLength,
},
{
FromField: otelconsts.HTTPResponseContentLength,
},
}
type rollupProcessor struct {
cfg *Config
logsNextConsumer consumer.Logs
rollupHistogram *prometheus.HistogramVec
rollups []*Rollup
rollupFromFields map[string]struct{} // Set of all all FromField names.
}
const (
defaultAttributeCardinalityLimit = 10
// RedactedAttributeValue is a value that replaces actual attribute value
// in case it exceeds cardinality limit.
RedactedAttributeValue = "REDACTED_VIA_CARDINALITY_LIMIT"
)
var _ consumer.Logs = (*rollupProcessor)(nil)
func newRollupProcessor(set processor.CreateSettings, cfg *Config) (*rollupProcessor, error) {
rollupHistogram := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: metrics.RollupMetricName,
Help: "Latency of the requests processed by the server",
Buckets: cfg.RollupBuckets,
}, []string{})
rollups := NewRollups(defaultRollupGroups)
rollupFromFields := map[string]struct{}{}
for _, rollup := range rollups {
rollupFromFields[rollup.FromField] = struct{}{}
}
err := cfg.promRegistry.Register(rollupHistogram)
if err != nil {
// Ignore already registered error, as this is not harmful. Metrics may
// be registered by other running processor.
if _, ok := err.(prometheus.AlreadyRegisteredError); !ok {
return nil, fmt.Errorf("could not register prometheus metrics: %w", err)
}
}
return &rollupProcessor{
cfg: cfg,
rollupHistogram: rollupHistogram,
rollups: rollups,
rollupFromFields: rollupFromFields,
}, nil
}
// Capabilities returns the capabilities of the processor with MutatesData set to true.
func (rp *rollupProcessor) Capabilities() consumer.Capabilities {
return consumer.Capabilities{MutatesData: true}
}
// Start is invoked during service startup.
func (rp *rollupProcessor) Start(context.Context, component.Host) error {
return nil
}
// Shutdown is invoked during service shutdown.
func (rp *rollupProcessor) Shutdown(context.Context) error {
return nil
}
// ConsumeLogs implements LogsProcessor.
func (rp *rollupProcessor) ConsumeLogs(ctx context.Context, ld plog.Logs) error {
rp.applyCardinalityLimits(ld, rp.cfg.AttributeCardinalityLimit)
rollupData := make(map[string]pcommon.Map)
datasketches := make(map[string]map[string]*sketches.HeapDoublesSketch)
log.Trace().Int("count", ld.LogRecordCount()).Msg("Before rollup")
otelcollector.IterateLogRecords(ld, func(logRecord plog.LogRecord) otelcollector.IterAction {
attributes := logRecord.Attributes()
key := key(attributes, rp.rollupFromFields)
_, exists := rollupData[key]
if !exists {
rollupData[key] = attributes
rollupData[key].PutInt(RollupCountKey, 0)
}
_, exists = datasketches[key]
if !exists {
datasketches[key] = make(map[string]*sketches.HeapDoublesSketch)
}
rawCount, _ := rollupData[key].Get(RollupCountKey)
rollupData[key].PutInt(RollupCountKey, rawCount.Int()+1)
rp.rollupAttributes(datasketches[key], rollupData[key], attributes)
return otelcollector.Keep
})
for k, v := range datasketches {
attributes := rollupData[k]
for toField, sketch := range v {
serializedBytes, err := sketch.Compact().Serialize()
if err != nil {
// Serialize() should never return error, unless there's a bug in sketches library.
log.Bug().Err(err).Msg("Sketch.Serialize() failed")
}
serialized := base64.StdEncoding.EncodeToString(serializedBytes)
attributes.PutStr(toField, serialized)
}
}
return rp.exportLogs(ctx, rollupData)
}
func (rp *rollupProcessor) applyCardinalityLimits(ld plog.Logs, limit int) {
attributeValues := map[string]map[string]struct{}{}
otelcollector.IterateLogRecords(ld, func(logRecord plog.LogRecord) otelcollector.IterAction {
logRecord.Attributes().Range(func(k string, v pcommon.Value) bool {
// Applying the cardinality limits only to string attributes, as
// all user-created attributes where we risk high-cardinality are
// string attributes.
if v.Type() != pcommon.ValueTypeStr {
return true
}
if _, toRollup := rp.rollupFromFields[k]; toRollup {
return true
}
value := v.Str()
values, exist := attributeValues[k]
if !exist {
attributeValues[k] = map[string]struct{}{value: {}}
return true
}
if _, exists := values[value]; !exists {
if len(values) < limit {
values[value] = struct{}{}
} else {
v.SetStr(RedactedAttributeValue)
}
}
return true
})
return otelcollector.Keep
})
}
func (rp *rollupProcessor) rollupAttributes(
datasketches map[string]*sketches.HeapDoublesSketch,
baseAttributes,
attributes pcommon.Map,
) {
// TODO tgill: need to track latest timestamp from attributes as the timestamp in baseAttributes
for _, rollup := range rp.rollups {
switch rollup.Type {
case RollupSum:
newValue, found := rollup.GetFromFieldValue(attributes)
if !found {
continue
}
rollupSum, exists := rollup.GetToFieldValue(baseAttributes)
if !exists {
rollupSum = 0
baseAttributes.PutDouble(rollup.ToField, rollupSum)
}
baseAttributes.PutDouble(rollup.ToField, rollupSum+newValue)
case RollupSumOfSquares:
newValue, found := rollup.GetFromFieldValue(attributes)
if !found {
continue
}
rollupSos, exists := rollup.GetToFieldValue(baseAttributes)
if !exists {
rollupSos = 0
baseAttributes.PutDouble(rollup.ToField, rollupSos)
}
baseAttributes.PutDouble(rollup.ToField, rollupSos+newValue*newValue)
case RollupMin:
newValue, found := rollup.GetFromFieldValue(attributes)
if !found {
continue
}
rollupMin, exists := rollup.GetToFieldValue(baseAttributes)
if !exists {
rollupMin = newValue
baseAttributes.PutDouble(rollup.ToField, rollupMin)
}
newMin := otelcollector.Min(rollupMin, newValue)
baseAttributes.PutDouble(rollup.ToField, newMin)
case RollupMax:
newValue, found := rollup.GetFromFieldValue(attributes)
if !found {
continue
}
rollupMax, exists := rollup.GetToFieldValue(baseAttributes)
if !exists {
rollupMax = newValue
baseAttributes.PutDouble(rollup.ToField, rollupMax)
}
newMax := otelcollector.Max(rollupMax, newValue)
baseAttributes.PutDouble(rollup.ToField, newMax)
case RollupDatasketch:
newValue, found := rollup.GetFromFieldValue(attributes)
if !found {
continue
}
_, exists := datasketches[rollup.ToField]
if !exists {
ds, err := sketches.NewDoublesSketch(128)
if err != nil {
log.Warn().Msg("Failed creating an empty HeapDoublesSketch")
continue
}
datasketches[rollup.ToField] = ds
}
datasketch := datasketches[rollup.ToField]
err := datasketch.Update(newValue)
if err != nil {
log.Warn().Float64("value", newValue).Msg("Failed updating datasketch with value")
}
}
}
// Exclude list
for fromField := range rp.rollupFromFields {
baseAttributes.Remove(fromField)
}
}
func (rp *rollupProcessor) setLogsNextConsumer(c consumer.Logs) {
rp.logsNextConsumer = c
}
func (rp *rollupProcessor) exportLogs(ctx context.Context, rollupData map[string]pcommon.Map) error {
ld := plog.NewLogs()
logs := ld.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords()
for _, v := range rollupData {
logRecord := logs.AppendEmpty()
// TODO tgill: need to get timestamp from v
logRecord.SetTimestamp(pcommon.NewTimestampFromTime(time.Now().UTC()))
v.CopyTo(logRecord.Attributes())
rawCount, _ := v.Get(RollupCountKey)
hist, err := rp.rollupHistogram.GetMetricWith(nil)
if err != nil {
log.Debug().Msgf("Could not extract rollup histogram metric from registry: %v", err)
} else {
hist.Observe(float64(rawCount.Int()))
}
}
log.Trace().Int("count", ld.LogRecordCount()).Msg("After rollup")
return rp.logsNextConsumer.ConsumeLogs(ctx, ld)
}
// newRollupLogsProcessor creates a new rollup processor that rollupes logs.
func newRollupLogsProcessor(set processor.CreateSettings, next consumer.Logs, cfg *Config) (*rollupProcessor, error) {
rp, err := newRollupProcessor(set, cfg)
if err != nil {
return nil, err
}
rp.setLogsNextConsumer(next)
return rp, nil
}