Compare commits

...

3 Commits

Author SHA1 Message Date
nityanandagohain
a4bf34c996 fix: minor changes 2026-03-05 09:42:18 +05:30
nityanandagohain
2da1e975fb fix: address cursor comments 2026-03-05 09:30:46 +05:30
nityanandagohain
612d50ec32 fix: address comments 2026-03-05 09:21:21 +05:30
7 changed files with 44 additions and 22 deletions

View File

@@ -1,12 +1,19 @@
package instrumentation
import "time"
import (
"math"
"time"
)
// DurationBucket returns a human-readable bucket label for the duration between fromMS and toMS.
// fromMS and toMS are Unix timestamps (same unit as used by time.Unix).
// Returns labels like "<1h", "<6h", "<24h", "<3D", "<1W", "<2W", "<1M", or ">=1M".
func DurationBucket(fromMS, toMS uint64) string {
diff := time.Unix(0, int64(toMS)).Sub(time.Unix(0, int64(fromMS)))
func DurationBucket(from, to uint64) string {
// make sure it's nanoseconds regardless of the unit
fromNS := toNanoSecs(from)
toNS := toNanoSecs(to)
diff := time.Unix(0, int64(toNS)).Sub(time.Unix(0, int64(fromNS)))
buckets := []struct {
d time.Duration
@@ -29,3 +36,19 @@ func DurationBucket(fromMS, toMS uint64) string {
return ">=1M"
}
// (todo): move this to a common package to be shared with querybuilder.
// toNanoSecs takes epoch and returns it in ns
func toNanoSecs(epoch uint64) uint64 {
temp := epoch
count := 0
if epoch == 0 {
count = 1
} else {
for epoch != 0 {
epoch /= 10
count++
}
}
return temp * uint64(math.Pow(10, float64(19-count)))
}

View File

@@ -141,9 +141,9 @@ func (client *client) queryToClickhouseQuery(_ context.Context, query *prompb.Qu
func (client *client) getFingerprintsFromClickhouseQuery(ctx context.Context, query string, args []any) (map[uint64][]prompb.Label, error) {
ctx = ctxtypes.AddCommentsToContext(ctx, map[string]string{
"signal": telemetrytypes.SignalMetrics.StringValue(),
"module_name": "clickhouse-prometheus",
"function_name": "getFingerprintsFromClickhouseQuery",
instrumentation.TelemetrySignal: telemetrytypes.SignalMetrics.StringValue(),
instrumentation.CodeNamespace: "clickhouse-prometheus",
instrumentation.CodeFunctionName: "getFingerprintsFromClickhouseQuery",
})
rows, err := client.telemetryStore.ClickhouseDB().Query(ctx, query, args...)
if err != nil {
@@ -258,9 +258,9 @@ func (client *client) querySamples(ctx context.Context, start int64, end int64,
func (client *client) queryRaw(ctx context.Context, query string, ts int64) (*prompb.QueryResult, error) {
ctx = ctxtypes.AddCommentsToContext(ctx, map[string]string{
"signal": telemetrytypes.SignalMetrics.StringValue(),
"module_name": "clickhouse-prometheus",
"function_name": "queryRaw",
instrumentation.TelemetrySignal: telemetrytypes.SignalMetrics.StringValue(),
instrumentation.CodeNamespace: "clickhouse-prometheus",
instrumentation.CodeFunctionName: "queryRaw",
})
rows, err := client.telemetryStore.ClickhouseDB().Query(ctx, query)

View File

@@ -215,8 +215,8 @@ func (q *builderQuery[T]) Execute(ctx context.Context) (*qbtypes.Result, error)
// executeWithContext executes the query with query window and step context for partial value detection
func (q *builderQuery[T]) executeWithContext(ctx context.Context, query string, args []any) (*qbtypes.Result, error) {
ctx = ctxtypes.AddCommentsToContext(ctx, map[string]string{
"signal": q.spec.Signal.StringValue(),
instrumentation.QueryDuration: instrumentation.DurationBucket(q.fromMS, q.toMS),
instrumentation.TelemetrySignal: q.spec.Signal.StringValue(),
instrumentation.QueryDuration: instrumentation.DurationBucket(q.fromMS, q.toMS),
})
totalRows := uint64(0)

View File

@@ -101,7 +101,6 @@ func (q *chSQLQuery) renderVars(query string, vars map[string]qbtypes.VariableIt
func (q *chSQLQuery) Execute(ctx context.Context) (*qbtypes.Result, error) {
ctx = ctxtypes.AddCommentsToContext(ctx, map[string]string{
"module_name": "clickhouse-query",
instrumentation.QueryDuration: instrumentation.DurationBucket(q.fromMS, q.toMS),
})

View File

@@ -189,10 +189,10 @@ func (q *promqlQuery) renderVars(query string, vars map[string]qbv5.VariableItem
func (q *promqlQuery) Execute(ctx context.Context) (*qbv5.Result, error) {
comment := ctxtypes.CommentFromContext(ctx)
comment.Set("signal", telemetrytypes.SignalMetrics.StringValue())
comment.Set("duration", instrumentation.DurationBucket(q.tr.From, q.tr.To))
ctx = ctxtypes.NewContextWithComment(ctx, comment)
ctx = ctxtypes.AddCommentsToContext(ctx, map[string]string{
instrumentation.TelemetrySignal: telemetrytypes.SignalMetrics.StringValue(),
instrumentation.QueryDuration: instrumentation.DurationBucket(q.tr.From, q.tr.To),
})
start := int64(querybuilder.ToNanoSecs(q.tr.From))
end := int64(querybuilder.ToNanoSecs(q.tr.To))

View File

@@ -506,6 +506,11 @@ func (r *ThresholdRule) buildAndRunQueryV5(ctx context.Context, orgID valuer.UUI
var results []*v3.Result
ctx = ctxtypes.AddCommentsToContext(ctx, map[string]string{
instrumentation.CodeNamespace: "rules",
instrumentation.CodeFunctionName: "buildAndRunQueryV5",
})
v5Result, err := r.querierV5.QueryRange(ctx, orgID, params)
if err != nil {
r.logger.ErrorContext(ctx, "failed to get alert query result", "rule_name", r.Name(), "error", err)

View File

@@ -110,12 +110,7 @@ func AddCommentsToContext(ctx context.Context, comments map[string]string) conte
return ctx
}
comment := CommentFromContext(ctx)
if comment == nil {
comment = NewComment()
}
for k, v := range comments {
comment.Set(k, v)
}
comment.Merge(comments)
return NewContextWithComment(ctx, comment)
}