From 7719ed18d70d8736d7bd6188d1628835b85b50c3 Mon Sep 17 00:00:00 2001 From: machine424 Date: Tue, 3 Dec 2024 17:40:18 +0100 Subject: [PATCH] test: add TestTargetsFromGroupWithLabelKeepDrop to reinforce a relabelling behaviour. fixes https://github.com/prometheus/prometheus/issues/12355 Signed-off-by: machine424 --- scrape/target_test.go | 74 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/scrape/target_test.go b/scrape/target_test.go index bd2795287..0d763d738 100644 --- a/scrape/target_test.go +++ b/scrape/target_test.go @@ -354,6 +354,80 @@ func TestTargetsFromGroup(t *testing.T) { require.EqualError(t, failures[0], expectedError) } +// TestTargetsFromGroupWithLabelKeepDrop aims to demonstrate and reinforce the current behavior: relabeling's "labelkeep" and "labeldrop" +// are applied to all labels of a target, including internal ones (labels starting with "__" such as "__address__"). +// This will be helpful for cases like https://github.com/prometheus/prometheus/issues/12355. +func TestTargetsFromGroupWithLabelKeepDrop(t *testing.T) { + tests := []struct { + name string + cfgText string + targets []model.LabelSet + shouldDropTarget bool + }{ + { + name: "no relabeling", + cfgText: ` +global: + metric_name_validation_scheme: legacy +scrape_configs: + - job_name: job1 + static_configs: + - targets: ["localhost:9090"] +`, + targets: []model.LabelSet{{model.AddressLabel: "localhost:9090"}}, + }, + { + name: "labelkeep", + cfgText: ` +global: + metric_name_validation_scheme: legacy +scrape_configs: + - job_name: job1 + static_configs: + - targets: ["localhost:9090"] + relabel_configs: + - regex: 'foo' + action: labelkeep +`, + targets: []model.LabelSet{{model.AddressLabel: "localhost:9090"}}, + shouldDropTarget: true, + }, + { + name: "labeldrop", + cfgText: ` +global: + metric_name_validation_scheme: legacy +scrape_configs: + - job_name: job1 + static_configs: + - targets: ["localhost:9090"] + relabel_configs: + - regex: '__address__' + action: labeldrop +`, + targets: []model.LabelSet{{model.AddressLabel: "localhost:9090"}}, + shouldDropTarget: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + config := loadConfiguration(t, tt.cfgText) + lb := labels.NewBuilder(labels.EmptyLabels()) + targets, failures := TargetsFromGroup(&targetgroup.Group{Targets: tt.targets}, config.ScrapeConfigs[0], nil, lb) + + if tt.shouldDropTarget { + require.Len(t, failures, 1) + require.EqualError(t, failures[0], "instance 0 in group : no address") + require.Empty(t, targets) + } else { + require.Empty(t, failures) + require.Len(t, targets, 1) + } + }) + } +} + func BenchmarkTargetsFromGroup(b *testing.B) { // Simulate Kubernetes service-discovery and use subset of rules from typical Prometheus config. cfgText := `