Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pkg/opentelemetry-mapping-go/otlp/metrics/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type translatorConfig struct {
InitialCumulMonoValueMode InitialCumulMonoValueMode
InstrumentationLibraryMetadataAsTags bool
InstrumentationScopeMetadataAsTags bool
InferDeltaInterval bool

originProduct OriginProduct

Expand Down Expand Up @@ -232,3 +233,12 @@ func WithInitialCumulMonoValueMode(mode InitialCumulMonoValueMode) TranslatorOpt
return nil
}
}

// WithInferDeltaInterval infers the interval for delta sums.
// By default the interval is set to 0.
func WithInferDeltaInterval() TranslatorOption {
return func(t *translatorConfig) error {
t.InferDeltaInterval = true
return nil
}
}
44 changes: 42 additions & 2 deletions pkg/opentelemetry-mapping-go/otlp/metrics/metrics_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ import (
const (
metricName string = "metric name"
errNoBucketsNoSumCount string = "no buckets mode and no send count sum are incompatible"

// intervalTolerance is the tolerance for interval calculation in seconds
// We use 0.05 seconds as tolerance to allow for some jitter.
intervalTolerance float64 = 0.05
)

var (
Expand All @@ -62,6 +66,32 @@ var (
}
)

// inferDeltaInterval calculates the interval for Datadog counts from OTLP delta sums.
// It returns the interval in seconds if the time difference between start and end timestamps
// is close to a whole number of seconds (within intervalTolerance), otherwise returns 0.
func inferDeltaInterval(startTimestamp, timestamp uint64) int64 {
if startTimestamp == 0 {
// We can't infer the interval without the startTimestamp
return 0
}

if startTimestamp > timestamp {
// malformed data
return 0
}

// Convert nanoseconds to seconds
deltaSeconds := float64(timestamp-startTimestamp) / 1e9
roundedDelta := math.Round(deltaSeconds)

if math.Abs(roundedDelta-deltaSeconds) < intervalTolerance {
return int64(roundedDelta)
}

// delta is outside of tolerance range
return 0
}

var _ source.Provider = (*noSourceProvider)(nil)

type noSourceProvider struct{}
Expand Down Expand Up @@ -169,7 +199,13 @@ func (t *Translator) mapNumberMetrics(
continue
}

consumer.ConsumeTimeSeries(ctx, pointDims, dt, uint64(p.Timestamp()), 0, val)
// Calculate interval for Count type metrics (from OTLP delta sums)
var interval int64
if t.cfg.InferDeltaInterval && dt == Count {
interval = inferDeltaInterval(uint64(p.StartTimestamp()), uint64(p.Timestamp()))
}

consumer.ConsumeTimeSeries(ctx, pointDims, dt, uint64(p.Timestamp()), interval, val)
}
}

Expand Down Expand Up @@ -417,7 +453,11 @@ func (t *Translator) getSketchBuckets(
sketch.Basic.Max = math.Min(p.Max(), sketch.Basic.Max)
}

consumer.ConsumeSketch(ctx, pointDims, ts, 0, sketch)
var interval int64
if t.cfg.InferDeltaInterval && delta {
interval = inferDeltaInterval(startTs, ts)
}
consumer.ConsumeSketch(ctx, pointDims, ts, interval, sketch)
}
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2346,3 +2346,55 @@ var statsPayloads = []*pb.ClientStatsPayload{
},
},
}

func TestInferInterval(t *testing.T) {
tests := []struct {
name string
startTs, ts uint64
expected int64
}{
{
name: "exact difference",
startTs: 1e9,
ts: 11e9,
expected: 10,
},
{
name: "under within tolerance",
startTs: 1e9,
ts: 11e9 - 30e6,
expected: 10,
},
{
name: "over within tolerance",
startTs: 1e9,
ts: 11e9 + 30e6,
expected: 10,
},
{
name: "outside tolerance",
startTs: 1e9,
ts: 11e9 + 50e7,
expected: 0,
},
{
name: "no starttimestamp",
startTs: 0,
ts: 11e9,
expected: 0,
},
{
name: "malformed data",
startTs: 710000000,
ts: 0,
expected: 0,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := inferDeltaInterval(tt.startTs, tt.ts)
assert.Equal(t, tt.expected, got)
})
}
}
13 changes: 13 additions & 0 deletions pkg/opentelemetry-mapping-go/otlp/metrics/mixed_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,19 @@ func TestMapMetrics(t *testing.T) {
expectedUnknownMetricType: 1,
expectedUnsupportedAggregationTemporality: 2,
},
{
name: "with-infer-delta-interval",
otlpfile: "test/otlp/mixed/interval.json",
ddogfile: "test/datadog/mixed/interval_inferred.json",
options: []TranslatorOption{
WithInferDeltaInterval(),
},
},
{
name: "with-infer-delta-interval",
otlpfile: "test/otlp/mixed/interval.json",
ddogfile: "test/datadog/mixed/interval_not_inferred.json",
},
}

for _, testinstance := range tests {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
{
"Metrics": {
"Sketches": [
{
"Name": "delta_histogram",
"Tags": [],
"Host": "",
"OriginID": "",
"OriginProduct": 10,
"OriginSubProduct": 17,
"OriginProductDetail": 0,
"Interval": 20,
"Timestamp": 1755603980235511877,
"Summary": {
"Min": -100,
"Max": 99.95733062532716,
"Sum": 3.141592653589793,
"Avg": 0.15707963267948966,
"Cnt": 20
},
"Keys": [
-1635,
-1590,
0,
1453,
1498,
1524,
1543,
1558,
1570,
1580,
1589,
1597,
1604,
1610,
1616,
1621,
1626,
1631,
1635
],
"Counts": [
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
2
]
}
],
"TimeSeries": [
{
"Name": "random_delta_sum",
"Tags": [],
"Host": "",
"OriginID": "",
"OriginProduct": 10,
"OriginSubProduct": 17,
"OriginProductDetail": 0,
"Type": "count",
"Interval": 20,
"Timestamp": 1755603940234853944,
"Value": 8617
},
{
"Name": "random_delta_sum",
"Tags": [],
"Host": "",
"OriginID": "",
"OriginProduct": 10,
"OriginSubProduct": 17,
"OriginProductDetail": 0,
"Type": "count",
"Interval": 20,
"Timestamp": 1755603960236383064,
"Value": 9227
},
{
"Name": "random_delta_sum",
"Tags": [],
"Host": "",
"OriginID": "",
"OriginProduct": 10,
"OriginSubProduct": 17,
"OriginProductDetail": 0,
"Type": "count",
"Interval": 20,
"Timestamp": 1755603980235511877,
"Value": 9125
}
]
},
"Hosts": {
"": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
{
"Metrics": {
"Sketches": [
{
"Name": "delta_histogram",
"Tags": [],
"Host": "",
"OriginID": "",
"OriginProduct": 10,
"OriginSubProduct": 17,
"OriginProductDetail": 0,
"Interval": 0,
"Timestamp": 1755603980235511877,
"Summary": {
"Min": -100,
"Max": 99.95733062532716,
"Sum": 3.141592653589793,
"Avg": 0.15707963267948966,
"Cnt": 20
},
"Keys": [
-1635,
-1590,
0,
1453,
1498,
1524,
1543,
1558,
1570,
1580,
1589,
1597,
1604,
1610,
1616,
1621,
1626,
1631,
1635
],
"Counts": [
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
2
]
}
],
"TimeSeries": [
{
"Name": "random_delta_sum",
"Tags": [],
"Host": "",
"OriginID": "",
"OriginProduct": 10,
"OriginSubProduct": 17,
"OriginProductDetail": 0,
"Type": "count",
"Interval": 0,
"Timestamp": 1755603940234853944,
"Value": 8617
},
{
"Name": "random_delta_sum",
"Tags": [],
"Host": "",
"OriginID": "",
"OriginProduct": 10,
"OriginSubProduct": 17,
"OriginProductDetail": 0,
"Type": "count",
"Interval": 0,
"Timestamp": 1755603960236383064,
"Value": 9227
},
{
"Name": "random_delta_sum",
"Tags": [],
"Host": "",
"OriginID": "",
"OriginProduct": 10,
"OriginSubProduct": 17,
"OriginProductDetail": 0,
"Type": "count",
"Interval": 0,
"Timestamp": 1755603980235511877,
"Value": 9125
}
]
},
"Hosts": {
"": {}
}
}
Loading
Loading