Skip to content

Commit

Permalink
fix: replaced timestamp vars in metric query range
Browse files Browse the repository at this point in the history
  • Loading branch information
mindhash committed Nov 23, 2022
1 parent 6b1b783 commit 21a7652
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
9 changes: 3 additions & 6 deletions pkg/query-service/app/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"go.signoz.io/signoz/pkg/query-service/app/parser"
"go.signoz.io/signoz/pkg/query-service/auth"
"go.signoz.io/signoz/pkg/query-service/constants"
querytemplate "go.signoz.io/signoz/pkg/query-service/utils/queryTemplate"

"go.signoz.io/signoz/pkg/query-service/dao"
am "go.signoz.io/signoz/pkg/query-service/integrations/alertManager"
Expand Down Expand Up @@ -653,12 +654,8 @@ func (aH *APIHandler) queryRangeMetricsV2(w http.ResponseWriter, r *http.Request
}
var query bytes.Buffer

// replace reserved variables
metricsQueryRangeParams.Variables["start_unix_ts"] = metricsQueryRangeParams.Start / 1000
metricsQueryRangeParams.Variables["end_unix_ts"] = metricsQueryRangeParams.End / 1000

metricsQueryRangeParams.Variables["start_datetime"] = fmt.Sprintf("toDateTime(%d)", metricsQueryRangeParams.Start/1000)
metricsQueryRangeParams.Variables["end_datetime"] = fmt.Sprintf("toDateTime(%d)", metricsQueryRangeParams.End/1000)
// replace go template variables
querytemplate.AssignReservedVars(metricsQueryRangeParams)

err = tmpl.Execute(&query, metricsQueryRangeParams.Variables)
if err != nil {
Expand Down
32 changes: 19 additions & 13 deletions pkg/query-service/rules/thresholdRule.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ import (
"bytes"
"context"
"fmt"
"go.uber.org/zap"
"math"
"reflect"
"sort"
"sync"
"text/template"
"time"

"go.uber.org/zap"

"github.com/ClickHouse/clickhouse-go/v2"
"go.signoz.io/signoz/pkg/query-service/app/metrics"
"go.signoz.io/signoz/pkg/query-service/constants"
qsmodel "go.signoz.io/signoz/pkg/query-service/model"
"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"
"go.signoz.io/signoz/pkg/query-service/utils/value"
Expand Down Expand Up @@ -338,17 +340,21 @@ func (r *ThresholdRule) CheckCondition(v float64) bool {

func (r *ThresholdRule) prepareQueryRange(ts time.Time) *qsmodel.QueryRangeParamsV2 {
// todo(amol): add 30 seconds to evalWindow for rate calc
tsEnd := ts.UnixNano() / int64(time.Millisecond)
tsStart := ts.Add(-time.Duration(r.evalWindow)).UnixNano() / int64(time.Millisecond)

// for k, v := range r.ruleCondition.CompositeMetricQuery.BuilderQueries {
// v.ReduceTo = qsmodel.RMAX
// r.ruleCondition.CompositeMetricQuery.BuilderQueries[k] = v
// }
if r.ruleCondition.QueryType() == qsmodel.CLICKHOUSE {
return &qsmodel.QueryRangeParamsV2{
Start: ts.UnixMilli(),
End: ts.Add(-time.Duration(r.evalWindow)).UnixMilli(),
Step: 30,
CompositeMetricQuery: r.ruleCondition.CompositeMetricQuery,
Variables: make(map[string]interface{}, 0),
}
}

// default mode
return &qsmodel.QueryRangeParamsV2{
Start: tsStart,
End: tsEnd,
Start: ts.UnixMilli(),
End: ts.Add(-time.Duration(r.evalWindow)).UnixMilli(),
Step: 30,
CompositeMetricQuery: r.ruleCondition.CompositeMetricQuery,
}
Expand Down Expand Up @@ -535,6 +541,9 @@ func (r *ThresholdRule) prepareClickhouseQueries(ts time.Time) (map[string]strin

params := r.prepareQueryRange(ts)

// replace reserved go template variables
querytemplate.AssignReservedVars(params)

for name, chQuery := range r.ruleCondition.CompositeMetricQuery.ClickHouseQueries {
if chQuery.Disabled {
continue
Expand All @@ -547,10 +556,7 @@ func (r *ThresholdRule) prepareClickhouseQueries(ts time.Time) (map[string]strin
return nil, err
}
var query bytes.Buffer
err = tmpl.Execute(&query, map[string]string{
"$__from_ts": fmt.Sprintf("%d", params.Start),
"$__to_ts": fmt.Sprintf("%d", params.End),
})
err = tmpl.Execute(&query, params.Variables)
if err != nil {
zap.S().Errorf("ruleid:", r.ID(), "\t msg: failed to populate clickhouse query", err)
r.SetHealth(HealthBad)
Expand Down
24 changes: 24 additions & 0 deletions pkg/query-service/utils/queryTemplate/vars.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package querytemplate

import (
"fmt"

"go.signoz.io/signoz/pkg/query-service/model"
)

// AssignReservedVars assigns values for go template vars. assumes that
// model.QueryRangeParamsV2.Start and End are Unix Nano timestamps
func AssignReservedVars(metricsQueryRangeParams *model.QueryRangeParamsV2) {
metricsQueryRangeParams.Variables["start_timestamp"] = metricsQueryRangeParams.Start / 1000
metricsQueryRangeParams.Variables["end_timestamp"] = metricsQueryRangeParams.End / 1000

metricsQueryRangeParams.Variables["start_timestamp_ms"] = metricsQueryRangeParams.Start
metricsQueryRangeParams.Variables["end_timestamp_ms"] = metricsQueryRangeParams.End

metricsQueryRangeParams.Variables["start_timestamp_nano"] = metricsQueryRangeParams.Start * 1e6
metricsQueryRangeParams.Variables["end_timestamp_nano"] = metricsQueryRangeParams.End * 1e6

metricsQueryRangeParams.Variables["start_datetime"] = fmt.Sprintf("toDateTime(%d)", metricsQueryRangeParams.Start/1000)
metricsQueryRangeParams.Variables["end_datetime"] = fmt.Sprintf("toDateTime(%d)", metricsQueryRangeParams.End/1000)

}

0 comments on commit 21a7652

Please sign in to comment.