Update package rules for new labels.Labels type

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2022-02-27 14:12:38 +00:00
parent 047585360b
commit cdbe7f462b
2 changed files with 22 additions and 27 deletions

View file

@ -146,10 +146,7 @@ func NewAlertingRule(
labels, annotations, externalLabels labels.Labels, externalURL string, labels, annotations, externalLabels labels.Labels, externalURL string,
restored bool, logger log.Logger, restored bool, logger log.Logger,
) *AlertingRule { ) *AlertingRule {
el := make(map[string]string, len(externalLabels)) el := externalLabels.Map()
for _, lbl := range externalLabels {
el[lbl.Name] = lbl.Value
}
return &AlertingRule{ return &AlertingRule{
name: name, name: name,
@ -217,16 +214,16 @@ func (r *AlertingRule) Annotations() labels.Labels {
func (r *AlertingRule) sample(alert *Alert, ts time.Time) promql.Sample { func (r *AlertingRule) sample(alert *Alert, ts time.Time) promql.Sample {
lb := labels.NewBuilder(r.labels) lb := labels.NewBuilder(r.labels)
for _, l := range alert.Labels { alert.Labels.Range(func(l labels.Label) {
lb.Set(l.Name, l.Value) lb.Set(l.Name, l.Value)
} })
lb.Set(labels.MetricName, alertMetricName) lb.Set(labels.MetricName, alertMetricName)
lb.Set(labels.AlertName, r.name) lb.Set(labels.AlertName, r.name)
lb.Set(alertStateLabel, alert.State.String()) lb.Set(alertStateLabel, alert.State.String())
s := promql.Sample{ s := promql.Sample{
Metric: lb.Labels(nil), Metric: lb.Labels(labels.EmptyLabels()),
Point: promql.Point{T: timestamp.FromTime(ts), V: 1}, Point: promql.Point{T: timestamp.FromTime(ts), V: 1},
} }
return s return s
@ -236,15 +233,15 @@ func (r *AlertingRule) sample(alert *Alert, ts time.Time) promql.Sample {
func (r *AlertingRule) forStateSample(alert *Alert, ts time.Time, v float64) promql.Sample { func (r *AlertingRule) forStateSample(alert *Alert, ts time.Time, v float64) promql.Sample {
lb := labels.NewBuilder(r.labels) lb := labels.NewBuilder(r.labels)
for _, l := range alert.Labels { alert.Labels.Range(func(l labels.Label) {
lb.Set(l.Name, l.Value) lb.Set(l.Name, l.Value)
} })
lb.Set(labels.MetricName, alertForStateMetricName) lb.Set(labels.MetricName, alertForStateMetricName)
lb.Set(labels.AlertName, r.name) lb.Set(labels.AlertName, r.name)
s := promql.Sample{ s := promql.Sample{
Metric: lb.Labels(nil), Metric: lb.Labels(labels.EmptyLabels()),
Point: promql.Point{T: timestamp.FromTime(ts), V: v}, Point: promql.Point{T: timestamp.FromTime(ts), V: v},
} }
return s return s
@ -254,13 +251,13 @@ func (r *AlertingRule) forStateSample(alert *Alert, ts time.Time, v float64) pro
func (r *AlertingRule) QueryforStateSeries(alert *Alert, q storage.Querier) (storage.Series, error) { func (r *AlertingRule) QueryforStateSeries(alert *Alert, q storage.Querier) (storage.Series, error) {
smpl := r.forStateSample(alert, time.Now(), 0) smpl := r.forStateSample(alert, time.Now(), 0)
var matchers []*labels.Matcher var matchers []*labels.Matcher
for _, l := range smpl.Metric { smpl.Metric.Range(func(l labels.Label) {
mt, err := labels.NewMatcher(labels.MatchEqual, l.Name, l.Value) mt, err := labels.NewMatcher(labels.MatchEqual, l.Name, l.Value)
if err != nil { if err != nil {
panic(err) panic(err)
} }
matchers = append(matchers, mt) matchers = append(matchers, mt)
} })
sset := q.Select(false, nil, matchers...) sset := q.Select(false, nil, matchers...)
var s storage.Series var s storage.Series
@ -268,7 +265,7 @@ func (r *AlertingRule) QueryforStateSeries(alert *Alert, q storage.Querier) (sto
// Query assures that smpl.Metric is included in sset.At().Labels(), // Query assures that smpl.Metric is included in sset.At().Labels(),
// hence just checking the length would act like equality. // hence just checking the length would act like equality.
// (This is faster than calling labels.Compare again as we already have some info). // (This is faster than calling labels.Compare again as we already have some info).
if len(sset.At().Labels()) == len(matchers) { if sset.At().Labels().Len() == len(matchers) {
s = sset.At() s = sset.At()
break break
} }
@ -327,10 +324,7 @@ func (r *AlertingRule) Eval(ctx context.Context, ts time.Time, query QueryFunc,
alerts := make(map[uint64]*Alert, len(res)) alerts := make(map[uint64]*Alert, len(res))
for _, smpl := range res { for _, smpl := range res {
// Provide the alert information to the template. // Provide the alert information to the template.
l := make(map[string]string, len(smpl.Metric)) l := smpl.Metric.Map()
for _, lbl := range smpl.Metric {
l[lbl.Name] = lbl.Value
}
tmplData := template.AlertTemplateData(l, r.externalLabels, r.externalURL, smpl.V) tmplData := template.AlertTemplateData(l, r.externalLabels, r.externalURL, smpl.V)
// Inject some convenience variables that are easier to remember for users // Inject some convenience variables that are easier to remember for users
@ -363,17 +357,18 @@ func (r *AlertingRule) Eval(ctx context.Context, ts time.Time, query QueryFunc,
lb := labels.NewBuilder(smpl.Metric).Del(labels.MetricName) lb := labels.NewBuilder(smpl.Metric).Del(labels.MetricName)
for _, l := range r.labels { r.labels.Range(func(l labels.Label) {
lb.Set(l.Name, expand(l.Value)) lb.Set(l.Name, expand(l.Value))
} })
lb.Set(labels.AlertName, r.Name()) lb.Set(labels.AlertName, r.Name())
annotations := make(labels.Labels, 0, len(r.annotations)) sb := labels.ScratchBuilder{}
for _, a := range r.annotations { r.annotations.Range(func(a labels.Label) {
annotations = append(annotations, labels.Label{Name: a.Name, Value: expand(a.Value)}) sb.Add(a.Name, expand(a.Value))
} })
annotations := sb.Labels()
lbs := lb.Labels(nil) lbs := lb.Labels(labels.EmptyLabels())
h := lbs.Hash() h := lbs.Hash()
resultFPs[h] = struct{}{} resultFPs[h] = struct{}{}

View file

@ -85,11 +85,11 @@ func (rule *RecordingRule) Eval(ctx context.Context, ts time.Time, query QueryFu
lb.Set(labels.MetricName, rule.name) lb.Set(labels.MetricName, rule.name)
for _, l := range rule.labels { rule.labels.Range(func(l labels.Label) {
lb.Set(l.Name, l.Value) lb.Set(l.Name, l.Value)
} })
sample.Metric = lb.Labels(nil) sample.Metric = lb.Labels(labels.EmptyLabels())
} }
// Check that the rule does not produce identical metrics after applying // Check that the rule does not produce identical metrics after applying