forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics_translator.go
502 lines (449 loc) · 16.7 KB
/
metrics_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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// 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.
// nolint:gocritic
package translator // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/model/translator"
import (
"context"
"fmt"
"math"
"strconv"
"github.com/DataDog/datadog-agent/pkg/quantile"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/model/attributes"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/model/internal/instrumentationlibrary"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/model/internal/instrumentationscope"
)
const metricName string = "metric name"
// Translator is a metrics translator.
type Translator struct {
prevPts *ttlCache
logger *zap.Logger
cfg translatorConfig
}
// New creates a new translator with given options.
func New(logger *zap.Logger, options ...Option) (*Translator, error) {
cfg := translatorConfig{
HistMode: HistogramModeDistributions,
SendCountSum: false,
Quantiles: false,
SendMonotonic: true,
ResourceAttributesAsTags: false,
InstrumentationLibraryMetadataAsTags: false,
InstrumentationScopeMetadataAsTags: false,
sweepInterval: 1800,
deltaTTL: 3600,
fallbackHostnameProvider: &noHostProvider{},
}
for _, opt := range options {
err := opt(&cfg)
if err != nil {
return nil, err
}
}
if cfg.HistMode == HistogramModeNoBuckets && !cfg.SendCountSum {
return nil, fmt.Errorf("no buckets mode and no send count sum are incompatible")
}
cache := newTTLCache(cfg.sweepInterval, cfg.deltaTTL)
return &Translator{cache, logger, cfg}, nil
}
// isCumulativeMonotonic checks if a metric is a cumulative monotonic metric
func isCumulativeMonotonic(md pmetric.Metric) bool {
switch md.DataType() {
case pmetric.MetricDataTypeSum:
return md.Sum().AggregationTemporality() == pmetric.MetricAggregationTemporalityCumulative &&
md.Sum().IsMonotonic()
}
return false
}
// isSkippable checks if a value can be skipped (because it is not supported by the backend).
// It logs that the value is unsupported for debugging since this sometimes means there is a bug.
func (t *Translator) isSkippable(name string, v float64) bool {
skippable := math.IsInf(v, 0) || math.IsNaN(v)
if skippable {
t.logger.Debug("Unsupported metric value", zap.String(metricName, name), zap.Float64("value", v))
}
return skippable
}
// mapNumberMetrics maps double datapoints into Datadog metrics
func (t *Translator) mapNumberMetrics(
ctx context.Context,
consumer TimeSeriesConsumer,
dims *Dimensions,
dt MetricDataType,
slice pmetric.NumberDataPointSlice,
) {
for i := 0; i < slice.Len(); i++ {
p := slice.At(i)
pointDims := dims.WithAttributeMap(p.Attributes())
var val float64
switch p.ValueType() {
case pmetric.NumberDataPointValueTypeDouble:
val = p.DoubleVal()
case pmetric.NumberDataPointValueTypeInt:
val = float64(p.IntVal())
}
if t.isSkippable(pointDims.name, val) {
continue
}
consumer.ConsumeTimeSeries(ctx, pointDims, dt, uint64(p.Timestamp()), val)
}
}
// mapNumberMonotonicMetrics maps monotonic datapoints into Datadog metrics
func (t *Translator) mapNumberMonotonicMetrics(
ctx context.Context,
consumer TimeSeriesConsumer,
dims *Dimensions,
slice pmetric.NumberDataPointSlice,
) {
for i := 0; i < slice.Len(); i++ {
p := slice.At(i)
ts := uint64(p.Timestamp())
startTs := uint64(p.StartTimestamp())
pointDims := dims.WithAttributeMap(p.Attributes())
var val float64
switch p.ValueType() {
case pmetric.NumberDataPointValueTypeDouble:
val = p.DoubleVal()
case pmetric.NumberDataPointValueTypeInt:
val = float64(p.IntVal())
}
if t.isSkippable(pointDims.name, val) {
continue
}
if dx, ok := t.prevPts.MonotonicDiff(pointDims, startTs, ts, val); ok {
consumer.ConsumeTimeSeries(ctx, pointDims, Count, ts, dx)
}
}
}
func getBounds(p pmetric.HistogramDataPoint, idx int) (lowerBound float64, upperBound float64) {
// See https://github.com/open-telemetry/opentelemetry-proto/blob/v0.10.0/opentelemetry/proto/metrics/v1/metrics.proto#L427-L439
lowerBound = math.Inf(-1)
upperBound = math.Inf(1)
if idx > 0 {
lowerBound = p.MExplicitBounds()[idx-1]
}
if idx < len(p.MExplicitBounds()) {
upperBound = p.MExplicitBounds()[idx]
}
return
}
type histogramInfo struct {
// sum of histogram (exact)
sum float64
// count of histogram (exact)
count uint64
// ok to use
ok bool
}
func (t *Translator) getSketchBuckets(
ctx context.Context,
consumer SketchConsumer,
pointDims *Dimensions,
p pmetric.HistogramDataPoint,
histInfo histogramInfo,
delta bool,
) {
startTs := uint64(p.StartTimestamp())
ts := uint64(p.Timestamp())
as := &quantile.Agent{}
for j := range p.MBucketCounts() {
lowerBound, upperBound := getBounds(p, j)
// Compute temporary bucketTags to have unique keys in the t.prevPts cache for each bucket
// The bucketTags are computed from the bounds before the InsertInterpolate fix is done,
// otherwise in the case where p.MExplicitBounds() has a size of 1 (eg. [0]), the two buckets
// would have the same bucketTags (lower_bound:0 and upper_bound:0), resulting in a buggy behavior.
bucketDims := pointDims.AddTags(
fmt.Sprintf("lower_bound:%s", formatFloat(lowerBound)),
fmt.Sprintf("upper_bound:%s", formatFloat(upperBound)),
)
// InsertInterpolate doesn't work with an infinite bound; insert in to the bucket that contains the non-infinite bound
// https://github.com/DataDog/datadog-agent/blob/7.31.0/pkg/aggregator/check_sampler.go#L107-L111
if math.IsInf(upperBound, 1) {
upperBound = lowerBound
} else if math.IsInf(lowerBound, -1) {
lowerBound = upperBound
}
count := p.MBucketCounts()[j]
if delta {
as.InsertInterpolate(lowerBound, upperBound, uint(count))
} else if dx, ok := t.prevPts.Diff(bucketDims, startTs, ts, float64(count)); ok {
as.InsertInterpolate(lowerBound, upperBound, uint(dx))
}
}
sketch := as.Finish()
if sketch != nil {
if histInfo.ok {
// override approximate sum, count and average in sketch with exact values if available.
sketch.Basic.Cnt = int64(histInfo.count)
sketch.Basic.Sum = histInfo.sum
sketch.Basic.Avg = sketch.Basic.Sum / float64(sketch.Basic.Cnt)
}
consumer.ConsumeSketch(ctx, pointDims, ts, sketch)
}
}
func (t *Translator) getLegacyBuckets(
ctx context.Context,
consumer TimeSeriesConsumer,
pointDims *Dimensions,
p pmetric.HistogramDataPoint,
delta bool,
) {
startTs := uint64(p.StartTimestamp())
ts := uint64(p.Timestamp())
// We have a single metric, 'bucket', which is tagged with the bucket bounds. See:
// https://github.com/DataDog/integrations-core/blob/7.30.1/datadog_checks_base/datadog_checks/base/checks/openmetrics/v2/transformers/histogram.py
baseBucketDims := pointDims.WithSuffix("bucket")
for idx, val := range p.MBucketCounts() {
lowerBound, upperBound := getBounds(p, idx)
bucketDims := baseBucketDims.AddTags(
fmt.Sprintf("lower_bound:%s", formatFloat(lowerBound)),
fmt.Sprintf("upper_bound:%s", formatFloat(upperBound)),
)
count := float64(val)
if delta {
consumer.ConsumeTimeSeries(ctx, bucketDims, Count, ts, count)
} else if dx, ok := t.prevPts.Diff(bucketDims, startTs, ts, count); ok {
consumer.ConsumeTimeSeries(ctx, bucketDims, Count, ts, dx)
}
}
}
// mapHistogramMetrics maps double histogram metrics slices to Datadog metrics
//
// A Histogram metric has:
// - The count of values in the population
// - The sum of values in the population
// - A number of buckets, each of them having
// - the bounds that define the bucket
// - the count of the number of items in that bucket
// - a sample value from each bucket
//
// We follow a similar approach to our OpenMetrics check:
// we report sum and count by default; buckets count can also
// be reported (opt-in) tagged by lower bound.
func (t *Translator) mapHistogramMetrics(
ctx context.Context,
consumer Consumer,
dims *Dimensions,
slice pmetric.HistogramDataPointSlice,
delta bool,
) {
for i := 0; i < slice.Len(); i++ {
p := slice.At(i)
startTs := uint64(p.StartTimestamp())
ts := uint64(p.Timestamp())
pointDims := dims.WithAttributeMap(p.Attributes())
histInfo := histogramInfo{ok: true}
countDims := pointDims.WithSuffix("count")
if delta {
histInfo.count = p.Count()
} else if dx, ok := t.prevPts.Diff(countDims, startTs, ts, float64(p.Count())); ok {
histInfo.count = uint64(dx)
} else { // not ok
histInfo.ok = false
}
sumDims := pointDims.WithSuffix("sum")
if !t.isSkippable(sumDims.name, p.Sum()) {
if delta {
histInfo.sum = p.Sum()
} else if dx, ok := t.prevPts.Diff(sumDims, startTs, ts, p.Sum()); ok {
histInfo.sum = dx
} else { // not ok
histInfo.ok = false
}
} else { // skippable
histInfo.ok = false
}
if t.cfg.SendCountSum && histInfo.ok {
// We only send the sum and count if both values were ok.
consumer.ConsumeTimeSeries(ctx, countDims, Count, ts, float64(histInfo.count))
consumer.ConsumeTimeSeries(ctx, sumDims, Count, ts, histInfo.sum)
}
switch t.cfg.HistMode {
case HistogramModeCounters:
t.getLegacyBuckets(ctx, consumer, pointDims, p, delta)
case HistogramModeDistributions:
t.getSketchBuckets(ctx, consumer, pointDims, p, histInfo, delta)
}
}
}
// formatFloat formats a float number as close as possible to what
// we do on the Datadog Agent Python OpenMetrics check, which, in turn, tries to
// follow https://github.com/OpenObservability/OpenMetrics/blob/v1.0.0/specification/OpenMetrics.md#considerations-canonical-numbers
func formatFloat(f float64) string {
if math.IsInf(f, 1) {
return "inf"
} else if math.IsInf(f, -1) {
return "-inf"
} else if math.IsNaN(f) {
return "nan"
} else if f == 0 {
return "0"
}
// Add .0 to whole numbers
s := strconv.FormatFloat(f, 'g', -1, 64)
if f == math.Floor(f) {
s = s + ".0"
}
return s
}
// getQuantileTag returns the quantile tag for summary types.
func getQuantileTag(quantile float64) string {
return fmt.Sprintf("quantile:%s", formatFloat(quantile))
}
// mapSummaryMetrics maps summary datapoints into Datadog metrics
func (t *Translator) mapSummaryMetrics(
ctx context.Context,
consumer TimeSeriesConsumer,
dims *Dimensions,
slice pmetric.SummaryDataPointSlice,
) {
for i := 0; i < slice.Len(); i++ {
p := slice.At(i)
startTs := uint64(p.StartTimestamp())
ts := uint64(p.Timestamp())
pointDims := dims.WithAttributeMap(p.Attributes())
// count and sum are increasing; we treat them as cumulative monotonic sums.
{
countDims := pointDims.WithSuffix("count")
if dx, ok := t.prevPts.Diff(countDims, startTs, ts, float64(p.Count())); ok && !t.isSkippable(countDims.name, dx) {
consumer.ConsumeTimeSeries(ctx, countDims, Count, ts, dx)
}
}
{
sumDims := pointDims.WithSuffix("sum")
if !t.isSkippable(sumDims.name, p.Sum()) {
if dx, ok := t.prevPts.Diff(sumDims, startTs, ts, p.Sum()); ok {
consumer.ConsumeTimeSeries(ctx, sumDims, Count, ts, dx)
}
}
}
if t.cfg.Quantiles {
baseQuantileDims := pointDims.WithSuffix("quantile")
quantiles := p.QuantileValues()
for i := 0; i < quantiles.Len(); i++ {
q := quantiles.At(i)
if t.isSkippable(baseQuantileDims.name, q.Value()) {
continue
}
quantileDims := baseQuantileDims.AddTags(getQuantileTag(q.Quantile()))
consumer.ConsumeTimeSeries(ctx, quantileDims, Gauge, ts, q.Value())
}
}
}
}
// MapMetrics maps OTLP metrics into the DataDog format
func (t *Translator) MapMetrics(ctx context.Context, md pmetric.Metrics, consumer Consumer) error {
rms := md.ResourceMetrics()
for i := 0; i < rms.Len(); i++ {
rm := rms.At(i)
// Fetch tags from attributes.
attributeTags := attributes.TagsFromAttributes(rm.Resource().Attributes())
host, ok := attributes.HostnameFromAttributes(rm.Resource().Attributes(), t.cfg.previewHostnameFromAttributes)
if !ok {
var err error
host, err = t.cfg.fallbackHostnameProvider.Hostname(context.Background())
if err != nil {
return fmt.Errorf("failed to get fallback host: %w", err)
}
}
if host != "" {
// Track hosts if the consumer is a HostConsumer.
if c, ok := consumer.(HostConsumer); ok {
c.ConsumeHost(host)
}
} else {
// Track task ARN if the consumer is a TagsConsumer.
if c, ok := consumer.(TagsConsumer); ok {
tags := attributes.RunningTagsFromAttributes(rm.Resource().Attributes())
for _, tag := range tags {
c.ConsumeTag(tag)
}
}
}
ilms := rm.ScopeMetrics()
for j := 0; j < ilms.Len(); j++ {
ilm := ilms.At(j)
metricsArray := ilm.Metrics()
var additionalTags []string
if t.cfg.InstrumentationScopeMetadataAsTags {
additionalTags = append(attributeTags, instrumentationscope.TagsFromInstrumentationScopeMetadata(ilm.Scope())...)
} else if t.cfg.InstrumentationLibraryMetadataAsTags {
additionalTags = append(attributeTags, instrumentationlibrary.TagsFromInstrumentationLibraryMetadata(ilm.Scope())...)
} else {
additionalTags = attributeTags
}
for k := 0; k < metricsArray.Len(); k++ {
md := metricsArray.At(k)
baseDims := &Dimensions{
name: md.Name(),
tags: additionalTags,
host: host,
originID: attributes.OriginIDFromAttributes(rm.Resource().Attributes()),
}
switch md.DataType() {
case pmetric.MetricDataTypeGauge:
t.mapNumberMetrics(ctx, consumer, baseDims, Gauge, md.Gauge().DataPoints())
case pmetric.MetricDataTypeSum:
switch md.Sum().AggregationTemporality() {
case pmetric.MetricAggregationTemporalityCumulative:
if t.cfg.SendMonotonic && isCumulativeMonotonic(md) {
t.mapNumberMonotonicMetrics(ctx, consumer, baseDims, md.Sum().DataPoints())
} else {
t.mapNumberMetrics(ctx, consumer, baseDims, Gauge, md.Sum().DataPoints())
}
case pmetric.MetricAggregationTemporalityDelta:
t.mapNumberMetrics(ctx, consumer, baseDims, Count, md.Sum().DataPoints())
default: // pmetric.MetricAggregationTemporalityUnspecified or any other not supported type
t.logger.Debug("Unknown or unsupported aggregation temporality",
zap.String(metricName, md.Name()),
zap.Any("aggregation temporality", md.Sum().AggregationTemporality()),
)
continue
}
case pmetric.MetricDataTypeHistogram:
switch md.Histogram().AggregationTemporality() {
case pmetric.MetricAggregationTemporalityCumulative, pmetric.MetricAggregationTemporalityDelta:
delta := md.Histogram().AggregationTemporality() == pmetric.MetricAggregationTemporalityDelta
t.mapHistogramMetrics(ctx, consumer, baseDims, md.Histogram().DataPoints(), delta)
default: // pmetric.MetricAggregationTemporalityUnspecified or any other not supported type
t.logger.Debug("Unknown or unsupported aggregation temporality",
zap.String("metric name", md.Name()),
zap.Any("aggregation temporality", md.Histogram().AggregationTemporality()),
)
continue
}
case pmetric.MetricDataTypeExponentialHistogram:
switch md.ExponentialHistogram().AggregationTemporality() {
case pmetric.MetricAggregationTemporalityDelta:
delta := md.ExponentialHistogram().AggregationTemporality() == pmetric.MetricAggregationTemporalityDelta
t.mapExponentialHistogramMetrics(ctx, consumer, baseDims, md.ExponentialHistogram().DataPoints(), delta)
default: // pmetric.MetricAggregationTemporalityCumulative, pmetric.AggregationTemporalityUnspecified or any other not supported type
t.logger.Debug("Unknown or unsupported aggregation temporality",
zap.String("metric name", md.Name()),
zap.Any("aggregation temporality", md.ExponentialHistogram().AggregationTemporality()),
)
continue
}
case pmetric.MetricDataTypeSummary:
t.mapSummaryMetrics(ctx, consumer, baseDims, md.Summary().DataPoints())
default: // pmetric.MetricDataTypeNone or any other not supported type
t.logger.Debug("Unknown or unsupported metric type", zap.String(metricName, md.Name()), zap.Any("data type", md.DataType()))
continue
}
}
}
}
return nil
}