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

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.
This commit is contained in:
Jorge Luis Betancourt
2022-10-11 20:40:18 +08:00
committed by GitHub
parent 0a183d1ba2
commit 95b9fa3346
2 changed files with 16 additions and 1 deletions
+1 -1
View File
@@ -107,7 +107,7 @@ func (c *EvalContext) shouldUpdateAlertState() bool {
// GetDurationMs returns the duration of the alert evaluation.
func (c *EvalContext) GetDurationMs() float64 {
return float64(c.EndTime.Nanosecond()-c.StartTime.Nanosecond()) / float64(1000000)
return float64(c.EndTime.Sub(c.StartTime).Nanoseconds()) / float64(time.Millisecond)
}
// GetNotificationTitle returns the title of the alert rule including alert state.