Update package model/relabel for new labels.Labels type

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham 2022-03-09 22:13:03 +00:00
parent b10fd9aea3
commit 8ad7b64c0f

View file

@ -203,20 +203,20 @@ func (re Regexp) String() string {
// Process returns a relabeled copy of the given label set. The relabel configurations // Process returns a relabeled copy of the given label set. The relabel configurations
// are applied in order of input. // are applied in order of input.
// If a label set is dropped, nil is returned. // If a label set is dropped, EmptyLabels and false is returned.
// May return the input labelSet modified. // May return the input labelSet modified.
func Process(lbls labels.Labels, cfgs ...*Config) labels.Labels { func Process(lbls labels.Labels, cfgs ...*Config) (ret labels.Labels, keep bool) {
lb := labels.NewBuilder(nil) lb := labels.NewBuilder(labels.EmptyLabels())
for _, cfg := range cfgs { for _, cfg := range cfgs {
lbls = relabel(lbls, cfg, lb) lbls, keep = relabel(lbls, cfg, lb)
if lbls == nil { if !keep {
return nil return labels.EmptyLabels(), false
} }
} }
return lbls return lbls, true
} }
func relabel(lset labels.Labels, cfg *Config, lb *labels.Builder) labels.Labels { func relabel(lset labels.Labels, cfg *Config, lb *labels.Builder) (ret labels.Labels, keep bool) {
var va [16]string var va [16]string
values := va[:0] values := va[:0]
if len(cfg.SourceLabels) > cap(values) { if len(cfg.SourceLabels) > cap(values) {
@ -232,19 +232,19 @@ func relabel(lset labels.Labels, cfg *Config, lb *labels.Builder) labels.Labels
switch cfg.Action { switch cfg.Action {
case Drop: case Drop:
if cfg.Regex.MatchString(val) { if cfg.Regex.MatchString(val) {
return nil return labels.EmptyLabels(), false
} }
case Keep: case Keep:
if !cfg.Regex.MatchString(val) { if !cfg.Regex.MatchString(val) {
return nil return labels.EmptyLabels(), false
} }
case DropEqual: case DropEqual:
if lset.Get(cfg.TargetLabel) == val { if lset.Get(cfg.TargetLabel) == val {
return nil return labels.EmptyLabels(), false
} }
case KeepEqual: case KeepEqual:
if lset.Get(cfg.TargetLabel) != val { if lset.Get(cfg.TargetLabel) != val {
return nil return labels.EmptyLabels(), false
} }
case Replace: case Replace:
indexes := cfg.Regex.FindStringSubmatchIndex(val) indexes := cfg.Regex.FindStringSubmatchIndex(val)
@ -271,29 +271,29 @@ func relabel(lset labels.Labels, cfg *Config, lb *labels.Builder) labels.Labels
mod := sum64(md5.Sum([]byte(val))) % cfg.Modulus mod := sum64(md5.Sum([]byte(val))) % cfg.Modulus
lb.Set(cfg.TargetLabel, fmt.Sprintf("%d", mod)) lb.Set(cfg.TargetLabel, fmt.Sprintf("%d", mod))
case LabelMap: case LabelMap:
for _, l := range lset { lset.Range(func(l labels.Label) {
if cfg.Regex.MatchString(l.Name) { if cfg.Regex.MatchString(l.Name) {
res := cfg.Regex.ReplaceAllString(l.Name, cfg.Replacement) res := cfg.Regex.ReplaceAllString(l.Name, cfg.Replacement)
lb.Set(res, l.Value) lb.Set(res, l.Value)
} }
} })
case LabelDrop: case LabelDrop:
for _, l := range lset { lset.Range(func(l labels.Label) {
if cfg.Regex.MatchString(l.Name) { if cfg.Regex.MatchString(l.Name) {
lb.Del(l.Name) lb.Del(l.Name)
} }
} })
case LabelKeep: case LabelKeep:
for _, l := range lset { lset.Range(func(l labels.Label) {
if !cfg.Regex.MatchString(l.Name) { if !cfg.Regex.MatchString(l.Name) {
lb.Del(l.Name) lb.Del(l.Name)
} }
} })
default: default:
panic(fmt.Errorf("relabel: unknown relabel action type %q", cfg.Action)) panic(fmt.Errorf("relabel: unknown relabel action type %q", cfg.Action))
} }
return lb.Labels(lset) return lb.Labels(lset), true
} }
// sum64 sums the md5 hash to an uint64. // sum64 sums the md5 hash to an uint64.