From 7b264b0c2359854340ebd49bee9bc92fea5f8818 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 30 Dec 2022 16:47:17 -0800 Subject: [PATCH] lib/promrelabel: allow calling Match on nil IfExpression This simplifies the caller side of IfExpression --- lib/promrelabel/if_expression.go | 3 +++ lib/promrelabel/relabel.go | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/promrelabel/if_expression.go b/lib/promrelabel/if_expression.go index 24571b865..a0a6542cc 100644 --- a/lib/promrelabel/if_expression.go +++ b/lib/promrelabel/if_expression.go @@ -78,6 +78,9 @@ func (ie *IfExpression) MarshalYAML() (interface{}, error) { // Match returns true if ie matches the given labels. func (ie *IfExpression) Match(labels []prompbmarshal.Label) bool { + if ie == nil { + return true + } for _, lf := range ie.lfs { if !lf.match(labels) { return false diff --git a/lib/promrelabel/relabel.go b/lib/promrelabel/relabel.go index c1021753f..3492074ac 100644 --- a/lib/promrelabel/relabel.go +++ b/lib/promrelabel/relabel.go @@ -158,7 +158,7 @@ func FinalizeLabels(dst, src []prompbmarshal.Label) []prompbmarshal.Label { // See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#relabel_config func (prc *parsedRelabelConfig) apply(labels []prompbmarshal.Label, labelsOffset int) []prompbmarshal.Label { src := labels[labelsOffset:] - if prc.If != nil && !prc.If.Match(labels) { + if !prc.If.Match(labels) { if prc.Action == "keep" { // Drop the target on `if` mismatch for `action: keep` return labels[:labelsOffset]