Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

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

Srikanth Chekuric9816cce181f
diff --git a/pkg/query-service/rules/promRule.go b/pkg/query-service/rules/promRule.go
index 5607366e6b..1a4a89e3d2 100644
--- a/pkg/query-service/rules/promRule.go
+++ b/pkg/query-service/rules/promRule.go
@@ -182,26 +182,6 @@ func (r *PromRule) Annotations() qslabels.BaseLabels {
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.
func (r *PromRule) GetEvaluationDuration() time.Duration {
r.mtx.Lock()
@@ -388,6 +368,7 @@ func (r *PromRule) Eval(ctx context.Context, ts time.Time, queriers *Queriers) (
if !shouldAlert {
continue
}
+ zap.S().Debugf("rule: %s, alerting for series: %v", r.Name(), series)
thresholdFormatter := formatter.FromUnit(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
for h, a := range alerts {
// Check whether we already have alerting state for the identifying label set.
diff --git a/pkg/query-service/rules/thresholdRule.go b/pkg/query-service/rules/thresholdRule.go
index b9e885cbc1..e62897c996 100644
--- a/pkg/query-service/rules/thresholdRule.go
+++ b/pkg/query-service/rules/thresholdRule.go
@@ -823,10 +823,11 @@ func (r *ThresholdRule) prepareLinksToLogs(ts time.Time, lbls labels.Labels) str
return ""
}
+ q := r.prepareQueryRange(ts)
// Logs list view expects time in milliseconds
tr := timeRange{
- Start: ts.Add(-time.Duration(r.evalWindow)).UnixMilli(),
- End: ts.UnixMilli(),
+ Start: q.Start,
+ End: q.End,
PageSize: 100,
}
@@ -886,10 +887,11 @@ func (r *ThresholdRule) prepareLinksToTraces(ts time.Time, lbls labels.Labels) s
return ""
}
+ q := r.prepareQueryRange(ts)
// Traces list view expects time in nanoseconds
tr := timeRange{
- Start: ts.Add(-time.Duration(r.evalWindow)).UnixNano(),
- End: ts.UnixNano(),
+ Start: q.Start * time.Second.Microseconds(),
+ End: q.End * time.Second.Microseconds(),
PageSize: 100,
}
diff --git a/pkg/query-service/rules/thresholdRule_test.go b/pkg/query-service/rules/thresholdRule_test.go
index 2b39084bec..fde35364bc 100644
--- a/pkg/query-service/rules/thresholdRule_test.go
+++ b/pkg/query-service/rules/thresholdRule_test.go
@@ -376,7 +376,7 @@ func TestPrepareLinksToLogs(t *testing.T) {
ts := time.UnixMilli(1705469040000)
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) {
@@ -418,5 +418,5 @@ func TestPrepareLinksToTraces(t *testing.T) {
ts := time.UnixMilli(1705469040000)
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")
}