test: add TestTargetsFromGroupWithLabelKeepDrop to reinforce a relabelling behaviour.

fixes https://github.com/prometheus/prometheus/issues/12355

Signed-off-by: machine424 <ayoubmrini424@gmail.com>
pull/15529/head
machine424 2024-12-03 17:40:18 +01:00
parent 7f26371af8
commit 7719ed18d7
No known key found for this signature in database
GPG Key ID: A4B001A4FDEE017D
1 changed files with 74 additions and 0 deletions

View File

@ -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 := `