From d3f2eb0d64bea14da2986906bf1c69861ebf751b Mon Sep 17 00:00:00 2001 From: Pablo Baeyens Date: Mon, 26 Jun 2023 13:22:52 +0200 Subject: [PATCH] Move to helper function --- pkg/otlp/metrics/metrics_translator.go | 31 ++++++++++++++++---------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/pkg/otlp/metrics/metrics_translator.go b/pkg/otlp/metrics/metrics_translator.go index 3ade0dea..b84b3160 100644 --- a/pkg/otlp/metrics/metrics_translator.go +++ b/pkg/otlp/metrics/metrics_translator.go @@ -152,6 +152,23 @@ func getProcessStartTime() uint64 { return startTime } +// shouldConsumeInitialValue checks if the initial value of a cumulative monotonic metric +// should be consumed or dropped. +func (t *Translator) shouldConsumeInitialValue(startTs, ts uint64) bool { + switch t.cfg.InitialCumulMonoValueMode { + case InitialCumulMonoValueModeAuto: + if getProcessStartTime() < startTs && startTs != ts { + // Report the first value if the timeseries started after the Datadog Agent process started. + return true + } + case InitialCumulMonoValueModeKeep: + return true + case InitialCumulMonoValueModeDrop: + // do nothing, drop the point + } + return false +} + // mapNumberMonotonicMetrics maps monotonic datapoints into Datadog metrics func (t *Translator) mapNumberMonotonicMetrics( ctx context.Context, @@ -179,18 +196,8 @@ func (t *Translator) mapNumberMonotonicMetrics( if dx, ok := t.prevPts.MonotonicDiff(pointDims, startTs, ts, val); ok { consumer.ConsumeTimeSeries(ctx, pointDims, Count, ts, dx) - } else if i == 0 { - switch t.cfg.InitialCumulMonoValueMode { - case InitialCumulMonoValueModeAuto: - if getProcessStartTime() < startTs && startTs != ts { - // Report the first value if the timeseries started after the Datadog Agent process started. - consumer.ConsumeTimeSeries(ctx, pointDims, Count, ts, val) - } - case InitialCumulMonoValueModeKeep: - consumer.ConsumeTimeSeries(ctx, pointDims, Count, ts, val) - case InitialCumulMonoValueModeDrop: - // do nothing, drop the point - } + } else if i == 0 && t.shouldConsumeInitialValue(startTs, ts) { + consumer.ConsumeTimeSeries(ctx, pointDims, Count, ts, val) } } }