Skip to content

Commit ac44fc2

Browse files
Simplify canary-exporter label check to "at least these labels" (#4562)
* Simplify canary-exporter label check to "at least these labels" * Remove count check * Remove now-unneeded sort
1 parent d7ee31e commit ac44fc2

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

cmd/profilecli/canary_exporter_probes.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"math"
1010
"net/http"
1111
"net/url"
12-
"slices"
1312
"strings"
1413
"time"
1514

@@ -493,14 +492,7 @@ func (ce *canaryExporter) testLabelNames(ctx context.Context, now time.Time) err
493492
return err
494493
}
495494

496-
expectedLabelCount := 12
497-
if len(respQuery.Msg.Names) != expectedLabelCount {
498-
level.Error(logger).Log("msg", "received an invalid number of labels", "expected", expectedLabelCount, "received", len(respQuery.Msg.Names), "labels", strings.Join(respQuery.Msg.Names, ","))
499-
return fmt.Errorf("expected %d label names, got %d", expectedLabelCount, len(respQuery.Msg.Names))
500-
}
501-
502495
labelNames := respQuery.Msg.Names
503-
slices.Sort(labelNames)
504496

505497
expectedLabelNames := []string{
506498
model.LabelNameProfileName,
@@ -510,15 +502,24 @@ func (ce *canaryExporter) testLabelNames(ctx context.Context, now time.Time) err
510502
model.LabelNameServiceNamePrivate,
511503
model.LabelNameType,
512504
model.LabelNameUnit,
513-
"ingestion_method",
514-
"instance",
515-
"job",
516505
"service.name",
517506
model.LabelNameServiceName,
518507
}
519508

520-
if !slices.Equal(labelNames, expectedLabelNames) {
521-
return fmt.Errorf("expected label names to be %s, got %s", expectedLabelNames, labelNames)
509+
// Use map as set for O(1) lookups
510+
labelNamesSet := make(map[string]struct{}, len(labelNames))
511+
for _, label := range labelNames {
512+
labelNamesSet[label] = struct{}{}
513+
}
514+
515+
missingLabels := []string{}
516+
for _, expectedLabel := range expectedLabelNames {
517+
if _, exists := labelNamesSet[expectedLabel]; !exists {
518+
missingLabels = append(missingLabels, expectedLabel)
519+
}
520+
}
521+
if len(missingLabels) > 0 {
522+
return fmt.Errorf("missing expected labels: %s", missingLabels)
522523
}
523524

524525
return nil

0 commit comments

Comments
 (0)