forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metric_translator.go
475 lines (429 loc) · 14.2 KB
/
metric_translator.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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// Copyright 2020, 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
//
// 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 awsemfexporter
import (
"bytes"
"crypto/sha1"
"encoding/json"
"fmt"
"sort"
"time"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/translator/conventions"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsemfexporter/mapwithexpiry"
)
const (
CleanInterval = 5 * time.Minute
MinTimeDiff = 50 * time.Millisecond // We assume 50 milli-seconds is the minimal gap between two collected data sample to be valid to calculate delta
// OTel instrumentation lib name as dimension
OTellibDimensionKey = "OTelLib"
defaultNameSpace = "default"
noInstrumentationLibraryName = "Undefined"
// See: https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutLogEvents.html
maximumLogEventsPerPut = 10000
// DimensionRollupOptions
ZeroAndSingleDimensionRollup = "ZeroAndSingleDimensionRollup"
SingleDimensionRollupOnly = "SingleDimensionRollupOnly"
FakeMetricValue = 0
)
var currentState = mapwithexpiry.NewMapWithExpiry(CleanInterval)
type rateState struct {
value interface{}
timestamp int64
}
// CWMetrics defines
type CWMetrics struct {
Measurements []CwMeasurement
Timestamp int64
Fields map[string]interface{}
}
// CwMeasurement defines
type CwMeasurement struct {
Namespace string
Dimensions [][]string
Metrics []map[string]string
}
// CWMetric stats defines
type CWMetricStats struct {
Max float64
Min float64
Count uint64
Sum float64
}
// Wrapper interface for:
// - pdata.IntDataPointSlice
// - pdata.DoubleDataPointSlice
// - pdata.IntHistogramDataPointSlice
// - pdata.DoubleHistogramDataPointSlice
// - pdata.DoubleSummaryDataPointSlice
type DataPoints interface {
Len() int
At(int) DataPoint
}
// DataPoint is a wrapper interface for:
// - pdata.IntDataPoint
// - pdata.DoubleDataPoint
// - pdata.IntHistogramDataPoint
// - pdata.DoubleHistogramDataPoint
// - pdata.DoubleSummaryDataPointSlice
type DataPoint interface {
IsNil() bool
LabelsMap() pdata.StringMap
}
// Define wrapper interfaces such that At(i) returns a `DataPoint`
type IntDataPointSlice struct {
pdata.IntDataPointSlice
}
type DoubleDataPointSlice struct {
pdata.DoubleDataPointSlice
}
type DoubleHistogramDataPointSlice struct {
pdata.DoubleHistogramDataPointSlice
}
type DoubleSummaryDataPointSlice struct {
pdata.DoubleSummaryDataPointSlice
}
func (dps IntDataPointSlice) At(i int) DataPoint {
return dps.IntDataPointSlice.At(i)
}
func (dps DoubleDataPointSlice) At(i int) DataPoint {
return dps.DoubleDataPointSlice.At(i)
}
func (dps DoubleHistogramDataPointSlice) At(i int) DataPoint {
return dps.DoubleHistogramDataPointSlice.At(i)
}
func (dps DoubleSummaryDataPointSlice) At(i int) DataPoint {
return dps.DoubleSummaryDataPointSlice.At(i)
}
// TranslateOtToCWMetric converts OT metrics to CloudWatch Metric format
func TranslateOtToCWMetric(rm *pdata.ResourceMetrics, config *Config) ([]*CWMetrics, int) {
var cwMetricList []*CWMetrics
totalDroppedMetrics := 0
namespace := config.Namespace
var instrumentationLibName string
if len(namespace) == 0 {
serviceName, svcNameOk := rm.Resource().Attributes().Get(conventions.AttributeServiceName)
serviceNamespace, svcNsOk := rm.Resource().Attributes().Get(conventions.AttributeServiceNamespace)
if svcNameOk && svcNsOk && serviceName.Type() == pdata.AttributeValueSTRING && serviceNamespace.Type() == pdata.AttributeValueSTRING {
namespace = fmt.Sprintf("%s/%s", serviceNamespace.StringVal(), serviceName.StringVal())
} else if svcNameOk && serviceName.Type() == pdata.AttributeValueSTRING {
namespace = serviceName.StringVal()
} else if svcNsOk && serviceNamespace.Type() == pdata.AttributeValueSTRING {
namespace = serviceNamespace.StringVal()
}
}
if len(namespace) == 0 {
namespace = defaultNameSpace
}
ilms := rm.InstrumentationLibraryMetrics()
for j := 0; j < ilms.Len(); j++ {
ilm := ilms.At(j)
if ilm.IsNil() {
continue
}
if ilm.InstrumentationLibrary().Name() == "" {
instrumentationLibName = noInstrumentationLibraryName
} else {
instrumentationLibName = ilm.InstrumentationLibrary().Name()
}
metrics := ilm.Metrics()
for k := 0; k < metrics.Len(); k++ {
metric := metrics.At(k)
if metric.IsNil() {
totalDroppedMetrics++
continue
}
cwMetrics := getCWMetrics(&metric, namespace, instrumentationLibName, config)
cwMetricList = append(cwMetricList, cwMetrics...)
}
}
return cwMetricList, totalDroppedMetrics
}
// TranslateCWMetricToEMF converts CloudWatch Metric format to EMF.
func TranslateCWMetricToEMF(cwMetricLists []*CWMetrics, logger *zap.Logger) []*LogEvent {
// convert CWMetric into map format for compatible with PLE input
ples := make([]*LogEvent, 0, maximumLogEventsPerPut)
for _, met := range cwMetricLists {
cwmMap := make(map[string]interface{})
fieldMap := met.Fields
if len(met.Measurements) > 0 {
// Create `_aws` section only if there are measurements
cwmMap["CloudWatchMetrics"] = met.Measurements
cwmMap["Timestamp"] = met.Timestamp
fieldMap["_aws"] = cwmMap
} else {
str, _ := json.Marshal(fieldMap)
logger.Debug("Dropped metric due to no matching metric declarations", zap.String("labels", string(str)))
}
pleMsg, err := json.Marshal(fieldMap)
if err != nil {
continue
}
metricCreationTime := met.Timestamp
logEvent := NewLogEvent(
metricCreationTime,
string(pleMsg),
)
logEvent.LogGeneratedTime = time.Unix(0, metricCreationTime*int64(time.Millisecond))
ples = append(ples, logEvent)
}
return ples
}
// getCWMetrics translates OTLP Metric to a list of CW Metrics
func getCWMetrics(metric *pdata.Metric, namespace string, instrumentationLibName string, config *Config) (cwMetrics []*CWMetrics) {
if metric == nil {
return
}
// metric measure data from OT
metricMeasure := make(map[string]string)
metricMeasure["Name"] = metric.Name()
metricMeasure["Unit"] = metric.Unit()
// metric measure slice could include multiple metric measures
metricSlice := []map[string]string{metricMeasure}
// Retrieve data points
var dps DataPoints
switch metric.DataType() {
case pdata.MetricDataTypeIntGauge:
dps = IntDataPointSlice{metric.IntGauge().DataPoints()}
case pdata.MetricDataTypeDoubleGauge:
dps = DoubleDataPointSlice{metric.DoubleGauge().DataPoints()}
case pdata.MetricDataTypeIntSum:
dps = IntDataPointSlice{metric.IntSum().DataPoints()}
case pdata.MetricDataTypeDoubleSum:
dps = DoubleDataPointSlice{metric.DoubleSum().DataPoints()}
case pdata.MetricDataTypeDoubleHistogram:
dps = DoubleHistogramDataPointSlice{metric.DoubleHistogram().DataPoints()}
case pdata.MetricDataTypeDoubleSummary:
dps = DoubleSummaryDataPointSlice{metric.DoubleSummary().DataPoints()}
default:
config.logger.Warn(
"Unhandled metric data type.",
zap.String("DataType", metric.DataType().String()),
zap.String("Name", metric.Name()),
zap.String("Unit", metric.Unit()),
)
return
}
if dps.Len() == 0 {
return
}
for m := 0; m < dps.Len(); m++ {
dp := dps.At(m)
if dp.IsNil() {
continue
}
cwMetric := buildCWMetric(dp, metric, namespace, metricSlice, instrumentationLibName, config)
if cwMetric != nil {
cwMetrics = append(cwMetrics, cwMetric)
}
}
return
}
// buildCWMetric builds CWMetric from DataPoint
func buildCWMetric(dp DataPoint, pmd *pdata.Metric, namespace string, metricSlice []map[string]string, instrumentationLibName string, config *Config) *CWMetrics {
dimensionRollupOption := config.DimensionRollupOption
metricDeclarations := config.MetricDeclarations
labelsMap := dp.LabelsMap()
labelsSlice := make([]string, labelsMap.Len(), labelsMap.Len()+1)
// `labels` contains label key/value pairs
labels := make(map[string]string, labelsMap.Len()+1)
// `fields` contains metric and dimensions key/value pairs
fields := make(map[string]interface{}, labelsMap.Len()+2)
idx := 0
labelsMap.ForEach(func(k, v string) {
fields[k] = v
labels[k] = v
labelsSlice[idx] = k
idx++
})
// Apply single/zero dimension rollup to labels
rollupDimensionArray := dimensionRollup(dimensionRollupOption, labelsSlice, instrumentationLibName)
// Add OTel instrumentation lib name as an additional dimension if it is defined
if instrumentationLibName != noInstrumentationLibraryName {
labels[OTellibDimensionKey] = instrumentationLibName
fields[OTellibDimensionKey] = instrumentationLibName
}
// Create list of dimension sets
var dimensions [][]string
if len(metricDeclarations) > 0 {
// If metric declarations are defined, extract dimension sets from them
dimensions = processMetricDeclarations(metricDeclarations, pmd, labels, rollupDimensionArray)
} else {
// If no metric declarations defined, create a single dimension set containing
// the list of labels
dims := labelsSlice
if instrumentationLibName != noInstrumentationLibraryName {
// If OTel instrumentation lib name is defined, add instrumentation lib
// name as a dimension
dims = append(dims, OTellibDimensionKey)
}
if len(rollupDimensionArray) > 0 {
// Perform de-duplication check for edge case with a single label and single roll-up
// is activated
if len(labelsSlice) > 1 || (dimensionRollupOption != SingleDimensionRollupOnly &&
dimensionRollupOption != ZeroAndSingleDimensionRollup) {
dimensions = [][]string{dims}
}
dimensions = append(dimensions, rollupDimensionArray...)
} else {
dimensions = [][]string{dims}
}
}
// Build list of CW Measurements
var cwMeasurements []CwMeasurement
if len(dimensions) > 0 {
cwMeasurements = []CwMeasurement{
{
Namespace: namespace,
Dimensions: dimensions,
Metrics: metricSlice,
},
}
}
timestamp := time.Now().UnixNano() / int64(time.Millisecond)
// Extract metric
var metricVal interface{}
switch metric := dp.(type) {
case pdata.IntDataPoint:
// Put a fake but identical metric value here in order to add metric name into fields
// since calculateRate() needs metric name as one of metric identifiers
fields[pmd.Name()] = int64(FakeMetricValue)
metricVal = metric.Value()
if needsCalculateRate(pmd) {
metricVal = calculateRate(fields, metric.Value(), timestamp)
}
case pdata.DoubleDataPoint:
fields[pmd.Name()] = float64(FakeMetricValue)
metricVal = metric.Value()
if needsCalculateRate(pmd) {
metricVal = calculateRate(fields, metric.Value(), timestamp)
}
case pdata.DoubleHistogramDataPoint:
metricVal = &CWMetricStats{
Count: metric.Count(),
Sum: metric.Sum(),
}
case pdata.DoubleSummaryDataPoint:
metricStat := &CWMetricStats{
Count: metric.Count(),
Sum: metric.Sum(),
}
quantileValues := metric.QuantileValues()
if quantileValues.Len() > 0 {
metricStat.Min = quantileValues.At(0).Value()
metricStat.Max = quantileValues.At(quantileValues.Len() - 1).Value()
}
metricVal = metricStat
}
if metricVal == nil {
return nil
}
fields[pmd.Name()] = metricVal
cwMetric := &CWMetrics{
Measurements: cwMeasurements,
Timestamp: timestamp,
Fields: fields,
}
return cwMetric
}
// rate is calculated by valDelta / timeDelta
func calculateRate(fields map[string]interface{}, val interface{}, timestamp int64) interface{} {
keys := make([]string, 0, len(fields))
var b bytes.Buffer
var metricRate interface{}
// hash the key of str: metric + dimension key/value pairs (sorted alpha)
for k := range fields {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
switch v := fields[k].(type) {
case int64:
b.WriteString(k)
continue
case string:
b.WriteString(k)
b.WriteString(v)
default:
continue
}
}
h := sha1.New()
h.Write(b.Bytes())
bs := h.Sum(nil)
hashStr := string(bs)
// get previous Metric content from map. Need to lock the map until set the new state
currentState.Lock()
if state, ok := currentState.Get(hashStr); ok {
prevStats := state.(*rateState)
deltaTime := timestamp - prevStats.timestamp
var deltaVal interface{}
if _, ok := val.(float64); ok {
deltaVal = val.(float64) - prevStats.value.(float64)
if deltaTime > MinTimeDiff.Milliseconds() && deltaVal.(float64) >= 0 {
metricRate = deltaVal.(float64) * 1e3 / float64(deltaTime)
}
} else {
deltaVal = val.(int64) - prevStats.value.(int64)
if deltaTime > MinTimeDiff.Milliseconds() && deltaVal.(int64) >= 0 {
metricRate = deltaVal.(int64) * 1e3 / deltaTime
}
}
}
content := &rateState{
value: val,
timestamp: timestamp,
}
currentState.Set(hashStr, content)
currentState.Unlock()
if metricRate == nil {
metricRate = 0
}
return metricRate
}
// dimensionRollup creates rolled-up dimensions from the metric's label set.
func dimensionRollup(dimensionRollupOption string, originalDimensionSlice []string, instrumentationLibName string) [][]string {
var rollupDimensionArray [][]string
dimensionZero := []string{}
if instrumentationLibName != noInstrumentationLibraryName {
dimensionZero = append(dimensionZero, OTellibDimensionKey)
}
if dimensionRollupOption == ZeroAndSingleDimensionRollup {
//"Zero" dimension rollup
if len(originalDimensionSlice) > 0 {
rollupDimensionArray = append(rollupDimensionArray, dimensionZero)
}
}
if dimensionRollupOption == ZeroAndSingleDimensionRollup || dimensionRollupOption == SingleDimensionRollupOnly {
//"One" dimension rollup
for _, dimensionKey := range originalDimensionSlice {
rollupDimensionArray = append(rollupDimensionArray, append(dimensionZero, dimensionKey))
}
}
return rollupDimensionArray
}
func needsCalculateRate(pmd *pdata.Metric) bool {
switch pmd.DataType() {
case pdata.MetricDataTypeIntSum:
if pmd.IntSum().AggregationTemporality() == pdata.AggregationTemporalityCumulative {
return true
}
case pdata.MetricDataTypeDoubleSum:
if pmd.DoubleSum().AggregationTemporality() == pdata.AggregationTemporalityCumulative {
return true
}
}
return false
}