From eb32fce003031c2104c1f1a4c8705a288998b9d8 Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Mon, 1 Jul 2024 08:15:34 +0530 Subject: [PATCH] fix: use the correct formatter for the description --- pkg/query-service/model/v3/v3.go | 7 +++++-- pkg/query-service/rules/promRule.go | 14 +++++++++++--- pkg/query-service/rules/thresholdRule.go | 14 +++++++++++--- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/pkg/query-service/model/v3/v3.go b/pkg/query-service/model/v3/v3.go index 7b9fd8d9891..7facd2ff504 100644 --- a/pkg/query-service/model/v3/v3.go +++ b/pkg/query-service/model/v3/v3.go @@ -401,8 +401,11 @@ type CompositeQuery struct { PromQueries map[string]*PromQuery `json:"promQueries,omitempty"` PanelType PanelType `json:"panelType"` QueryType QueryType `json:"queryType"` - Unit string `json:"unit,omitempty"` - FillGaps bool `json:"fillGaps,omitempty"` + // Unit for the time series data shown in the graph + // This is used in alerts to format the value and threshold + Unit string `json:"unit,omitempty"` + // FillGaps is used to fill the gaps in the time series data + FillGaps bool `json:"fillGaps,omitempty"` } func (c *CompositeQuery) EnabledQueries() int { diff --git a/pkg/query-service/rules/promRule.go b/pkg/query-service/rules/promRule.go index a998de243e9..fd00363abf3 100644 --- a/pkg/query-service/rules/promRule.go +++ b/pkg/query-service/rules/promRule.go @@ -111,13 +111,22 @@ func (r *PromRule) Condition() *RuleCondition { return r.ruleCondition } +// targetVal returns the target value for the rule condition +// when the y-axis and target units are non-empty, it +// converts the target value to the y-axis unit func (r *PromRule) targetVal() float64 { if r.ruleCondition == nil || r.ruleCondition.Target == nil { return 0 } + // get the converter for the target unit unitConverter := converter.FromUnit(converter.Unit(r.ruleCondition.TargetUnit)) - value := unitConverter.Convert(converter.Value{F: *r.ruleCondition.Target, U: converter.Unit(r.ruleCondition.TargetUnit)}, converter.Unit(r.Unit())) + // convert the target value to the y-axis unit + value := unitConverter.Convert(converter.Value{ + F: *r.ruleCondition.Target, + U: converter.Unit(r.ruleCondition.TargetUnit), + }, converter.Unit(r.Unit())) + return value.F } @@ -370,8 +379,7 @@ func (r *PromRule) Eval(ctx context.Context, ts time.Time, queriers *Queriers) ( } zap.L().Debug("alerting for series", zap.String("name", r.Name()), zap.Any("series", series)) - thresholdFormatter := formatter.FromUnit(r.ruleCondition.TargetUnit) - threshold := thresholdFormatter.Format(r.targetVal(), r.ruleCondition.TargetUnit) + threshold := valueFormatter.Format(r.targetVal(), r.Unit()) tmplData := AlertTemplateData(l, valueFormatter.Format(alertSmpl.F, r.Unit()), threshold) // Inject some convenience variables that are easier to remember for users diff --git a/pkg/query-service/rules/thresholdRule.go b/pkg/query-service/rules/thresholdRule.go index 051476e6b87..e642fb3a0a0 100644 --- a/pkg/query-service/rules/thresholdRule.go +++ b/pkg/query-service/rules/thresholdRule.go @@ -165,13 +165,22 @@ func (r *ThresholdRule) PreferredChannels() []string { return r.preferredChannels } +// targetVal returns the target value for the rule condition +// when the y-axis and target units are non-empty, it +// converts the target value to the y-axis unit func (r *ThresholdRule) targetVal() float64 { if r.ruleCondition == nil || r.ruleCondition.Target == nil { return 0 } + // get the converter for the target unit unitConverter := converter.FromUnit(converter.Unit(r.ruleCondition.TargetUnit)) - value := unitConverter.Convert(converter.Value{F: *r.ruleCondition.Target, U: converter.Unit(r.ruleCondition.TargetUnit)}, converter.Unit(r.Unit())) + // convert the target value to the y-axis unit + value := unitConverter.Convert(converter.Value{ + F: *r.ruleCondition.Target, + U: converter.Unit(r.ruleCondition.TargetUnit), + }, converter.Unit(r.Unit())) + return value.F } @@ -874,8 +883,7 @@ func (r *ThresholdRule) Eval(ctx context.Context, ts time.Time, queriers *Querie } value := valueFormatter.Format(smpl.V, r.Unit()) - thresholdFormatter := formatter.FromUnit(r.ruleCondition.TargetUnit) - threshold := thresholdFormatter.Format(r.targetVal(), r.ruleCondition.TargetUnit) + threshold := valueFormatter.Format(r.targetVal(), r.Unit()) zap.L().Debug("Alert template data for rule", zap.String("name", r.Name()), zap.String("formatter", valueFormatter.Name()), zap.String("value", value), zap.String("threshold", threshold)) tmplData := AlertTemplateData(l, value, threshold)