Alerting: Fix duration calculation when testing a rule (#56616) (#56705)

When testing a rule within the legacy alerting the `timeMs` field could
sometimes show negative (and/or wrongly calculated) durations. This happens if the alert evaluation crosses a second boundary. This change uses the full timestamp to compute the eval duration.

(cherry picked from commit 95b9fa3346)

Co-authored-by: Jorge Luis Betancourt <jorge-luis.betancourt@trivago.com>
This commit is contained in:
Grot (@grafanabot)
2022-10-12 11:38:33 +08:00
committed by GitHub
co-authored by Jorge Luis Betancourt
parent bd81793292
commit d02b7fc0ba
2 changed files with 16 additions and 1 deletions
@@ -404,3 +404,18 @@ func TestEvaluateNotificationTemplateFields(t *testing.T) {
})
}
}
func TestGetDurationFromEvalContext(t *testing.T) {
startTime, err := time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", "2022-10-03 11:33:14.438803 +0200 CEST")
require.NoError(t, err)
endTime, err := time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", "2022-10-03 11:33:15.291075 +0200 CEST")
require.NoError(t, err)
evalContext := EvalContext{
StartTime: startTime,
EndTime: endTime,
}
assert.Equal(t, float64(852.272), evalContext.GetDurationMs())
}