diff --git a/cmd/promtool/main.go b/cmd/promtool/main.go index a2f710d14..a72183f70 100644 --- a/cmd/promtool/main.go +++ b/cmd/promtool/main.go @@ -631,9 +631,9 @@ func checkRules(filename string, lintSettings lintConfig) (int, []error) { errMessage := fmt.Sprintf("%d duplicate rule(s) found.\n", len(dRules)) for _, n := range dRules { errMessage += fmt.Sprintf("Metric: %s\nLabel(s):\n", n.metric) - for _, l := range n.label { + n.label.Range(func(l labels.Label) { errMessage += fmt.Sprintf("\t%s: %s\n", l.Name, l.Value) - } + }) } errMessage += "Might cause inconsistency while recording expressions" return 0, []error{fmt.Errorf("%w %s", lintError, errMessage)} diff --git a/cmd/promtool/rules.go b/cmd/promtool/rules.go index 4dbef34eb..2b5ed1d78 100644 --- a/cmd/promtool/rules.go +++ b/cmd/promtool/rules.go @@ -158,14 +158,15 @@ func (importer *ruleImporter) importRule(ctx context.Context, ruleExpr, ruleName // Setting the rule labels after the output of the query, // so they can override query output. - for _, l := range ruleLabels { + ruleLabels.Range(func(l labels.Label) { lb.Set(l.Name, l.Value) - } + }) lb.Set(labels.MetricName, ruleName) + lbls := lb.Labels(labels.EmptyLabels()) for _, value := range sample.Values { - if err := app.add(ctx, lb.Labels(nil), timestamp.FromTime(value.Timestamp.Time()), float64(value.Value)); err != nil { + if err := app.add(ctx, lbls, timestamp.FromTime(value.Timestamp.Time()), float64(value.Value)); err != nil { return fmt.Errorf("add: %w", err) } } diff --git a/cmd/promtool/tsdb.go b/cmd/promtool/tsdb.go index 1c6ae6f6b..80b1dc42f 100644 --- a/cmd/promtool/tsdb.go +++ b/cmd/promtool/tsdb.go @@ -315,7 +315,7 @@ func readPrometheusLabels(r io.Reader, n int) ([]labels.Labels, error) { i := 0 for scanner.Scan() && i < n { - m := make(labels.Labels, 0, 10) + m := make([]labels.Label, 0, 10) r := strings.NewReplacer("\"", "", "{", "", "}", "") s := r.Replace(scanner.Text()) @@ -325,13 +325,12 @@ func readPrometheusLabels(r io.Reader, n int) ([]labels.Labels, error) { split := strings.Split(labelChunk, ":") m = append(m, labels.Label{Name: split[0], Value: split[1]}) } - // Order of the k/v labels matters, don't assume we'll always receive them already sorted. - sort.Sort(m) - h := m.Hash() + ml := labels.New(m...) // This sorts by name - order of the k/v labels matters, don't assume we'll always receive them already sorted. + h := ml.Hash() if _, ok := hashes[h]; ok { continue } - mets = append(mets, m) + mets = append(mets, ml) hashes[h] = struct{}{} i++ } @@ -479,13 +478,13 @@ func analyzeBlock(path, blockID string, limit int, runExtended bool) error { } // Amount of the block time range not covered by this series. uncovered := uint64(meta.MaxTime-meta.MinTime) - uint64(chks[len(chks)-1].MaxTime-chks[0].MinTime) - for _, lbl := range lbls { + lbls.Range(func(lbl labels.Label) { key := lbl.Name + "=" + lbl.Value labelsUncovered[lbl.Name] += uncovered labelpairsUncovered[key] += uncovered labelpairsCount[key]++ entries++ - } + }) } if p.Err() != nil { return p.Err() diff --git a/cmd/promtool/unittest.go b/cmd/promtool/unittest.go index f6b16a6bb..cc40ac9d0 100644 --- a/cmd/promtool/unittest.go +++ b/cmd/promtool/unittest.go @@ -284,8 +284,8 @@ func (tg *testGroup) test(evalInterval time.Duration, groupOrderMap map[string]i for _, a := range ar.ActiveAlerts() { if a.State == rules.StateFiring { alerts = append(alerts, labelAndAnnotation{ - Labels: append(labels.Labels{}, a.Labels...), - Annotations: append(labels.Labels{}, a.Annotations...), + Labels: a.Labels.Copy(), + Annotations: a.Annotations.Copy(), }) } }