-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor.go
More file actions
349 lines (299 loc) · 10.2 KB
/
processor.go
File metadata and controls
349 lines (299 loc) · 10.2 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// Copyright observIQ, Inc.
//
// 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://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 metricstatsprocessor
import (
"context"
"encoding/binary"
"fmt"
"regexp"
"sync"
"time"
"github.com/observiq/bindplane-otel-contrib/processor/metricstatsprocessor/internal/stats"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/multierr"
"go.uber.org/zap"
)
type metricstatsProcessor struct {
logger *zap.Logger
mux sync.Mutex
wg sync.WaitGroup
doneChan chan struct{}
//for mocking in test
now func() time.Time
includeRegex *regexp.Regexp
flushInterval time.Duration
calcPeriodStart pcommon.Timestamp
statTypes []stats.StatType
// map resource hash to resourceMetadata
statMap map[uint64]*resourceMetadata
nextConsumer consumer.Metrics
}
func newStatsProcessor(logger *zap.Logger, cfg *Config, consumer consumer.Metrics) (*metricstatsProcessor, error) {
regex, err := regexp.Compile(cfg.Include)
if err != nil {
return nil, fmt.Errorf("failed to compile include regex: %w", err)
}
return &metricstatsProcessor{
logger: logger,
mux: sync.Mutex{},
wg: sync.WaitGroup{},
doneChan: make(chan struct{}),
now: time.Now,
includeRegex: regex,
flushInterval: cfg.Interval,
calcPeriodStart: pcommon.NewTimestampFromTime(time.Now()),
statMap: make(map[uint64]*resourceMetadata),
statTypes: cfg.StatTypes(),
nextConsumer: consumer,
}, nil
}
func (sp *metricstatsProcessor) Start(_ context.Context, _ component.Host) error {
sp.wg.Add(1)
go sp.flushLoop()
return nil
}
func (sp *metricstatsProcessor) ConsumeMetrics(ctx context.Context, md pmetric.Metrics) error {
sp.addMetricsToCalculations(md)
if md.ResourceMetrics().Len() != 0 {
// Forward metrics we didn't consume
return sp.nextConsumer.ConsumeMetrics(ctx, md)
}
return nil
}
// Add metrics that are valid and match the include regex to our calculations.
// The incoming pmetric.Metrics is modified, such that matching metrics are removed.
func (sp *metricstatsProcessor) addMetricsToCalculations(md pmetric.Metrics) {
sp.mux.Lock()
defer sp.mux.Unlock()
rms := md.ResourceMetrics()
for i := 0; i < rms.Len(); i++ {
rm := rms.At(i)
resAttrs := rm.Resource().Attributes()
resKey := mapKey(resAttrs)
sms := rm.ScopeMetrics()
for j := 0; j < sms.Len(); j++ {
sm := sms.At(j)
ms := sm.Metrics()
for k := 0; k < ms.Len(); k++ {
m := ms.At(k)
if !canAddMetricToStats(m) {
continue
}
// Metric must match regex
if !sp.includeRegex.MatchString(m.Name()) {
continue
}
ma := sp.metricMetadata(m, resKey, resAttrs)
dps := datapointsFromMetric(m)
// We remove datapoints that we add to our statistics here, so we use RemoveIf to iterate the datapoints
dps.RemoveIf(func(dp pmetric.NumberDataPoint) bool {
if dp.ValueType() != pmetric.NumberDataPointValueTypeDouble &&
dp.ValueType() != pmetric.NumberDataPointValueTypeInt {
// Ignore values that are not Double or Int (e.g. are empty)
return false
}
sp.addDatapointToStats(ma, dp)
return true
})
}
// remove the metric if we consumed all the datapoints
removeEmptyMetrics(ms)
}
// remove the scope metrics if we consumed all the metrics
removeEmptyScopeMetrics(sms)
}
// remove the resource metrics if we consumed all the ScopeMetrics
removeEmptyResourceMetrics(rms)
}
// metricMetadata gets the metricMetadata for the given metric & resource, creating it if it doesn't exist.
func (sp *metricstatsProcessor) metricMetadata(m pmetric.Metric, resKey uint64, resAttrs pcommon.Map) *metricMetadata {
rma, ok := sp.statMap[resKey]
if !ok {
// Track the resource information for this resource if we haven't already
rma = &resourceMetadata{
resource: pcommon.NewMap(),
metrics: make(map[string]*metricMetadata),
}
resAttrs.CopyTo(rma.resource)
sp.statMap[resKey] = rma
}
ma, ok := rma.metrics[m.Name()]
if !ok {
// Track the metadata for this metric if we haven't already.
ma = &metricMetadata{
name: m.Name(),
desc: m.Description(),
unit: m.Unit(),
metricType: m.Type(),
monotonic: isMonotonic(m),
datapoints: make(map[uint64]*datapointMetadata),
}
rma.metrics[m.Name()] = ma
}
return ma
}
// addDatapointToStats either adds the datapoint to all existing statistics (if one exists for the NumberDataPoint's attributes),
// or creates a new set of statistics for the datapoint.
func (sp *metricstatsProcessor) addDatapointToStats(ma *metricMetadata, dp pmetric.NumberDataPoint) {
attributeKey := mapKey(dp.Attributes())
dpa, ok := ma.datapoints[attributeKey]
if !ok {
// Create the statistics for this datapoint if we haven't already for this set of attributes.
statistics, err := sp.createStatistics(dp)
if err != nil {
sp.logger.Error("Failed to create some statistics.", zap.Error(err), zap.String("metric", ma.name))
// We continue here even if some statistics failed to be created
}
dpa = &datapointMetadata{
attributes: pcommon.NewMap(),
statistics: statistics,
}
dp.Attributes().CopyTo(dpa.attributes)
ma.datapoints[attributeKey] = dpa
// we don't need to call AddDatapoint, since the statistics are initialized with the first datapoint.
return
}
// Add datapoints to existing statistics
for _, stat := range dpa.statistics {
stat.AddDatapoint(dp)
}
}
// createStatistics creates all statistics for this datapoint based on the configuration of this processor
// The returned error here is a multierr, and may be a partial err, so the resultant map may be used even if an error is returned.
func (sp *metricstatsProcessor) createStatistics(initialVal pmetric.NumberDataPoint) (map[stats.StatType]stats.Statistic, error) {
var errs error
statistics := make(map[stats.StatType]stats.Statistic, len(sp.statTypes))
for _, statType := range sp.statTypes {
stat, err := statType.New(initialVal)
if err != nil {
errs = multierr.Append(errs, fmt.Errorf("failed to create statistic: %w", err))
continue
}
statistics[statType] = stat
}
return statistics, errs
}
// flushLoop is a goroutine that flushes all statistics every sp.flushInterval.
func (sp *metricstatsProcessor) flushLoop() {
defer sp.wg.Done()
t := time.NewTicker(sp.flushInterval)
defer t.Stop()
for {
select {
case <-t.C:
sp.flush()
case <-sp.doneChan:
return
}
}
}
// flush flushes all statistics to the next component in the collector pipeline.
func (sp *metricstatsProcessor) flush() {
sp.mux.Lock()
defer sp.mux.Unlock()
now := pcommon.NewTimestampFromTime(sp.now())
metrics := pmetric.NewMetrics()
for _, ra := range sp.statMap {
rm := metrics.ResourceMetrics().AppendEmpty()
ra.resource.CopyTo(rm.Resource().Attributes())
sm := rm.ScopeMetrics().AppendEmpty()
for _, statType := range sp.statTypes {
for _, ma := range ra.metrics {
sp.addCalculatedMetric(now, sm.Metrics(), ma, statType)
}
}
}
if metrics.DataPointCount() != 0 {
if err := sp.nextConsumer.ConsumeMetrics(context.Background(), metrics); err != nil {
sp.logger.Error("Failed to consume metrics.", zap.Error(err))
}
}
// Reset statistic map
sp.statMap = make(map[uint64]*resourceMetadata)
// Calculation period will start from when we started flush.
sp.calcPeriodStart = now
}
func (sp *metricstatsProcessor) addCalculatedMetric(now pcommon.Timestamp, ms pmetric.MetricSlice, ma *metricMetadata, statType stats.StatType) {
m := ms.AppendEmpty()
m.SetName(fmt.Sprintf("%s.%s", ma.name, statType))
m.SetDescription(ma.desc)
m.SetUnit(ma.unit)
var dps pmetric.NumberDataPointSlice
switch ma.metricType {
case pmetric.MetricTypeGauge:
g := m.SetEmptyGauge()
dps = g.DataPoints()
case pmetric.MetricTypeSum:
s := m.SetEmptySum()
s.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
s.SetIsMonotonic(ma.monotonic)
dps = s.DataPoints()
}
for _, dpa := range ma.datapoints {
stat, ok := dpa.statistics[statType]
if !ok {
// this statistics must have failed to be created, so we can't emit this as a metric
continue
}
// Construct datapoints
dp := dps.AppendEmpty()
stat.SetDatapointValue(dp)
dpa.attributes.CopyTo(dp.Attributes())
dp.SetStartTimestamp(sp.calcPeriodStart)
dp.SetTimestamp(now)
}
}
func (sp *metricstatsProcessor) Capabilities() consumer.Capabilities {
// Data is mutate, since we remove Metric payloads if they are add to a statistic
return consumer.Capabilities{MutatesData: true}
}
func (sp *metricstatsProcessor) Shutdown(ctx context.Context) error {
close(sp.doneChan)
waitDoneChan := make(chan struct{})
// wait in a goroutine so that we can select on context cancellation as well
go func() {
sp.wg.Wait()
close(waitDoneChan)
}()
select {
case <-ctx.Done():
sp.logger.Error("Context timed out while waiting for graceful shutdown.", zap.Error(ctx.Err()))
return ctx.Err()
case <-waitDoneChan: // OK
}
return nil
}
func canAddMetricToStats(m pmetric.Metric) bool {
switch m.Type() {
case pmetric.MetricTypeGauge:
return true
case pmetric.MetricTypeSum:
return m.Sum().AggregationTemporality() == pmetric.AggregationTemporalityCumulative
}
// Currently only gauges and cumulative sums are supported.
return false
}
// mapKey returns a unique key for the provided map.
func mapKey(dimension pcommon.Map) uint64 {
b := pdatautil.MapHash(dimension)
// Since the hash is 128 bits, and we want a 64 bit hash,
// we'll condense by XORing the lower and upper 64 bits.
upper := binary.BigEndian.Uint64(b[:8])
lower := binary.BigEndian.Uint64(b[8:])
return lower ^ upper
}