Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.
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
16 changes: 16 additions & 0 deletions .chloggen/mx-psi_infer-interval.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component (e.g. pkg/quantile)
component: pkg/otlp/metrics

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add `WithInferDeltaInterval` option to infer the interval for delta metrics

# The PR related to this change
issues: [765]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
10 changes: 10 additions & 0 deletions pkg/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/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 @@ -418,7 +454,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
52 changes: 52 additions & 0 deletions pkg/otlp/metrics/metrics_translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2345,3 +2345,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/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: "testdata/otlpdata/mixed/interval.json",
ddogfile: "testdata/datadogdata/mixed/interval_inferred.json",
options: []TranslatorOption{
WithInferDeltaInterval(),
},
},
{
name: "with-infer-delta-interval",
otlpfile: "testdata/otlpdata/mixed/interval.json",
ddogfile: "testdata/datadogdata/mixed/interval_not_inferred.json",
},
}

for _, testinstance := range tests {
Expand Down
110 changes: 110 additions & 0 deletions pkg/otlp/metrics/testdata/datadogdata/mixed/interval_inferred.json
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": {
"": {}
}
}
Loading
Loading