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: add back the templating for query (#5079)

Srikanth Chekuri2f7495c6e480
diff --git a/pkg/query-service/rules/thresholdRule.go b/pkg/query-service/rules/thresholdRule.go
index 1884563305..865bac7f81 100644
--- a/pkg/query-service/rules/thresholdRule.go
+++ b/pkg/query-service/rules/thresholdRule.go
@@ -1,9 +1,11 @@
package rules
import (
+ "bytes"
"context"
"encoding/json"
"fmt"
+ "html/template"
"math"
"net/url"
"regexp"
@@ -27,6 +29,7 @@ import (
"go.signoz.io/signoz/pkg/query-service/interfaces"
v3 "go.signoz.io/signoz/pkg/query-service/model/v3"
"go.signoz.io/signoz/pkg/query-service/utils/labels"
+ querytemplate "go.signoz.io/signoz/pkg/query-service/utils/queryTemplate"
"go.signoz.io/signoz/pkg/query-service/utils/times"
"go.signoz.io/signoz/pkg/query-service/utils/timestamp"
@@ -430,7 +433,7 @@ func (r *ThresholdRule) prepareQueryRange(ts time.Time) *v3.QueryRangeParamsV3 {
end = end - (end % (60 * 1000))
if r.ruleCondition.QueryType() == v3.QueryTypeClickHouseSQL {
- return &v3.QueryRangeParamsV3{
+ params := &v3.QueryRangeParamsV3{
Start: start,
End: end,
Step: int64(math.Max(float64(common.MinAllowedStepInterval(start, end)), 60)),
@@ -438,6 +441,27 @@ func (r *ThresholdRule) prepareQueryRange(ts time.Time) *v3.QueryRangeParamsV3 {
Variables: make(map[string]interface{}, 0),
NoCache: true,
}
+ querytemplate.AssignReservedVarsV3(params)
+ for name, chQuery := range r.ruleCondition.CompositeQuery.ClickHouseQueries {
+ if chQuery.Disabled {
+ continue
+ }
+ tmpl := template.New("clickhouse-query")
+ tmpl, err := tmpl.Parse(chQuery.Query)
+ if err != nil {
+ zap.L().Error("failed to parse clickhouse query to populate vars", zap.String("ruleid", r.ID()), zap.Error(err))
+ r.SetHealth(HealthBad)
+ return params
+ }
+ var query bytes.Buffer
+ err = tmpl.Execute(&query, params.Variables)
+ if err != nil {
+ zap.L().Error("failed to populate clickhouse query", zap.String("ruleid", r.ID()), zap.Error(err))
+ r.SetHealth(HealthBad)
+ return params
+ }
+ r.ruleCondition.CompositeQuery.ClickHouseQueries[name].Query = query.String()
+ }
}
if r.ruleCondition.CompositeQuery != nil && r.ruleCondition.CompositeQuery.BuilderQueries != nil {