Skip to content

Commit 9c4a066

Browse files
ruslan-mikhailovknylander-grafana
authored andcommitted
[bugfix] avg_over_time calculation fix (grafana#6252)
1 parent a4da7a2 commit 9c4a066

4 files changed

Lines changed: 63 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## main / unreleased
2+
* [BUGFIX] Correct avg_over_time calculation [#6252](https://github.com/grafana/tempo/pull/6252) (@ruslan-mikhailov)
23

34
# v2.10.0-rc.0
45

pkg/traceql/engine_metrics_average.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ func (k *averageSeries) addWeigthedMean(interval int, mean float64, weight float
268268
meanDelta -= currentMean.compensation
269269

270270
currentMean.add(meanDelta)
271+
currentMean.weight = sumWeights
271272
k.values[interval] = currentMean
272273
}
273274

pkg/traceql/engine_metrics_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,43 @@ func TestAvgOverTimeForDurationWithoutAggregation(t *testing.T) {
12941294
assert.Equal(t, 200., avg.Values[2]*float64(time.Second))
12951295
}
12961296

1297+
func TestAvgOverTimeCombine(t *testing.T) {
1298+
req := &tempopb.QueryRangeRequest{
1299+
Start: 1,
1300+
End: uint64(3 * time.Second),
1301+
Step: uint64(1 * time.Second),
1302+
Query: "{ } | avg_over_time(duration)",
1303+
}
1304+
1305+
// Three set of spans with different data for a single time interval
1306+
// Each set will be passed to level 2 independently
1307+
in1 := []Span{
1308+
newMockSpan(nil).WithStartTime(uint64(1 * time.Second)).WithDuration(uint64(100 * time.Second)),
1309+
}
1310+
1311+
in2 := []Span{
1312+
newMockSpan(nil).WithStartTime(uint64(1 * time.Second)).WithDuration(uint64(200 * time.Second)),
1313+
newMockSpan(nil).WithStartTime(uint64(1 * time.Second)).WithDuration(uint64(400 * time.Second)),
1314+
}
1315+
1316+
in3 := []Span{
1317+
newMockSpan(nil).WithStartTime(uint64(1 * time.Second)).WithDuration(uint64(500 * time.Second)),
1318+
newMockSpan(nil).WithStartTime(uint64(1 * time.Second)).WithDuration(uint64(600 * time.Second)),
1319+
newMockSpan(nil).WithStartTime(uint64(1 * time.Second)).WithDuration(uint64(700 * time.Second)),
1320+
}
1321+
1322+
result, seriesCount, err := runTraceQLMetric(req, in1, in2, in3)
1323+
require.NoError(t, err)
1324+
require.Equal(t, len(result), seriesCount)
1325+
1326+
avg := result[LabelsFromArgs("__name__", "avg_over_time").MapKey()]
1327+
1328+
expectedResult := (100 + 200 + 400 + 500 + 600 + 700) / 6.
1329+
assert.Equal(t, expectedResult, avg.Values[0])
1330+
assert.True(t, math.IsNaN(avg.Values[1]), "expected NaN, got %f", avg.Values[1])
1331+
assert.True(t, math.IsNaN(avg.Values[2]), "expected NaN, got %f", avg.Values[2])
1332+
}
1333+
12971334
func TestAvgOverTimeForSpanAttribute(t *testing.T) {
12981335
req := &tempopb.QueryRangeRequest{
12991336
Start: 1,

tempodb/tempodb_metrics_test.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,7 @@ var queryRangeTestCases = []struct {
244244
},
245245
},
246246
},
247-
expectedL2: nil,
248-
expectedL3: []*tempopb.TimeSeries{
247+
expectedL2: []*tempopb.TimeSeries{
249248
{
250249
Labels: []common_v1.KeyValue{tempopb.MakeKeyValueString("__name__", "avg_over_time")},
251250
Samples: []tempopb.Sample{
@@ -255,6 +254,29 @@ var queryRangeTestCases = []struct {
255254
{TimestampMs: 60_000, Value: 240 / 5.0}, // sum from 46 to 50 is 240
256255
},
257256
},
257+
{
258+
Labels: []common_v1.KeyValue{
259+
tempopb.MakeKeyValueString("__name__", "avg_over_time"),
260+
tempopb.MakeKeyValueString("__meta_type", "__count"),
261+
},
262+
Samples: []tempopb.Sample{
263+
{TimestampMs: 15_000, Value: 2 * 15},
264+
{TimestampMs: 30_000, Value: 2 * 15},
265+
{TimestampMs: 45_000, Value: 2 * 15},
266+
{TimestampMs: 60_000, Value: 2 * 5},
267+
},
268+
},
269+
},
270+
expectedL3: []*tempopb.TimeSeries{
271+
{
272+
Labels: []common_v1.KeyValue{tempopb.MakeKeyValueString("__name__", "avg_over_time")},
273+
Samples: []tempopb.Sample{
274+
{TimestampMs: 15_000, Value: 120 / 15.0},
275+
{TimestampMs: 30_000, Value: 345 / 15.0},
276+
{TimestampMs: 45_000, Value: 570 / 15.0},
277+
{TimestampMs: 60_000, Value: 240 / 5.0},
278+
},
279+
},
258280
},
259281
},
260282
{

0 commit comments

Comments
 (0)