fix: use same time window as the logs/traces query (#4682)

This commit is contained in:
Srikanth Chekuri 2024-03-12 17:30:01 +05:30 committed by GitHub
parent c6c2b9d809
commit c9816cce18
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 10 additions and 26 deletions

View File

@ -182,26 +182,6 @@ func (r *PromRule) Annotations() qslabels.BaseLabels {
return r.annotations return r.annotations
} }
func (r *PromRule) sample(alert *Alert, ts time.Time) pql.Sample {
lb := plabels.NewBuilder(r.labels)
alertLabels := alert.Labels.(plabels.Labels)
for _, l := range alertLabels {
lb.Set(l.Name, l.Value)
}
lb.Set(qslabels.MetricNameLabel, alertMetricName)
lb.Set(qslabels.AlertNameLabel, r.name)
lb.Set(qslabels.AlertStateLabel, alert.State.String())
s := pql.Sample{
Metric: lb.Labels(),
T: timestamp.FromTime(ts),
F: 1,
}
return s
}
// GetEvaluationDuration returns the time in seconds it took to evaluate the alerting rule. // GetEvaluationDuration returns the time in seconds it took to evaluate the alerting rule.
func (r *PromRule) GetEvaluationDuration() time.Duration { func (r *PromRule) GetEvaluationDuration() time.Duration {
r.mtx.Lock() r.mtx.Lock()
@ -388,6 +368,7 @@ func (r *PromRule) Eval(ctx context.Context, ts time.Time, queriers *Queriers) (
if !shouldAlert { if !shouldAlert {
continue continue
} }
zap.S().Debugf("rule: %s, alerting for series: %v", r.Name(), series)
thresholdFormatter := formatter.FromUnit(r.ruleCondition.TargetUnit) thresholdFormatter := formatter.FromUnit(r.ruleCondition.TargetUnit)
threshold := thresholdFormatter.Format(r.targetVal(), r.ruleCondition.TargetUnit) threshold := thresholdFormatter.Format(r.targetVal(), r.ruleCondition.TargetUnit)
@ -454,6 +435,7 @@ func (r *PromRule) Eval(ctx context.Context, ts time.Time, queriers *Queriers) (
} }
} }
zap.S().Debugf("For rule: %s, found %d alerts", r.Name(), len(alerts))
// alerts[h] is ready, add or update active list now // alerts[h] is ready, add or update active list now
for h, a := range alerts { for h, a := range alerts {
// Check whether we already have alerting state for the identifying label set. // Check whether we already have alerting state for the identifying label set.

View File

@ -823,10 +823,11 @@ func (r *ThresholdRule) prepareLinksToLogs(ts time.Time, lbls labels.Labels) str
return "" return ""
} }
q := r.prepareQueryRange(ts)
// Logs list view expects time in milliseconds // Logs list view expects time in milliseconds
tr := timeRange{ tr := timeRange{
Start: ts.Add(-time.Duration(r.evalWindow)).UnixMilli(), Start: q.Start,
End: ts.UnixMilli(), End: q.End,
PageSize: 100, PageSize: 100,
} }
@ -886,10 +887,11 @@ func (r *ThresholdRule) prepareLinksToTraces(ts time.Time, lbls labels.Labels) s
return "" return ""
} }
q := r.prepareQueryRange(ts)
// Traces list view expects time in nanoseconds // Traces list view expects time in nanoseconds
tr := timeRange{ tr := timeRange{
Start: ts.Add(-time.Duration(r.evalWindow)).UnixNano(), Start: q.Start * time.Second.Microseconds(),
End: ts.UnixNano(), End: q.End * time.Second.Microseconds(),
PageSize: 100, PageSize: 100,
} }

View File

@ -376,7 +376,7 @@ func TestPrepareLinksToLogs(t *testing.T) {
ts := time.UnixMilli(1705469040000) ts := time.UnixMilli(1705469040000)
link := rule.prepareLinksToLogs(ts, labels.Labels{}) link := rule.prepareLinksToLogs(ts, labels.Labels{})
assert.Contains(t, link, "&timeRange=%7B%22start%22%3A1705468740000%2C%22end%22%3A1705469040000%2C%22pageSize%22%3A100%7D&startTime=1705468740000&endTime=1705469040000") assert.Contains(t, link, "&timeRange=%7B%22start%22%3A1705468620000%2C%22end%22%3A1705468920000%2C%22pageSize%22%3A100%7D&startTime=1705468620000&endTime=1705468920000")
} }
func TestPrepareLinksToTraces(t *testing.T) { func TestPrepareLinksToTraces(t *testing.T) {
@ -418,5 +418,5 @@ func TestPrepareLinksToTraces(t *testing.T) {
ts := time.UnixMilli(1705469040000) ts := time.UnixMilli(1705469040000)
link := rule.prepareLinksToTraces(ts, labels.Labels{}) link := rule.prepareLinksToTraces(ts, labels.Labels{})
assert.Contains(t, link, "&timeRange=%7B%22start%22%3A1705468740000000000%2C%22end%22%3A1705469040000000000%2C%22pageSize%22%3A100%7D&startTime=1705468740000000000&endTime=1705469040000000000") assert.Contains(t, link, "&timeRange=%7B%22start%22%3A1705468620000000000%2C%22end%22%3A1705468920000000000%2C%22pageSize%22%3A100%7D&startTime=1705468620000000000&endTime=1705468920000000000")
} }