Repository: prometheus/prometheus
File: scrape/target_test.go (Go)

Code before:
	cfg := config.ScrapeConfig{
		ScrapeTimeout:  model.Duration(10 * time.Second),
		ScrapeInterval: model.Duration(1 * time.Minute),
	}
	lb := labels.NewBuilder(labels.EmptyLabels())
	targets, failures := TargetsFromGroup(&targetgroup.Group{Targets: []model.LabelSet{{}, {model.AddressLabel: "localhost:9090"}}}, &cfg, nil, lb)
	require.Len(t, targets, 1)
	require.Len(t, failures, 1)
	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 hlpful 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",

Review comment (bug):
Had to add those to get around:

```
2024/12/03 17:19:06 http: TLS handshake error from 127.0.0.1:52972: remote error: tls: bad certificate
--- FAIL: TestTargetsFromGroupWithLabelKeepDrop (0.00s)
    --- FAIL: TestTargetsFromGroupWithLabelKeepDrop/no_relabeling (0.00s)
panic: utf8 name validation requested but model.NameValidationScheme is not set to UTF8 [recovered]
	panic: utf8 name validation requested but model.NameValidationScheme is not set to UTF8 [recovered]
	panic: utf8 name validation requested but model.NameValidationScheme is not set to UTF8
```

(will look into that later, maybe we need to override a global var in this test file or sth...)

Code after:
	cfg := config.ScrapeConfig{
		ScrapeTimeout:  model.Duration(10 * time.Second),
		ScrapeInterval: model.Duration(1 * time.Minute),
	}
	lb := labels.NewBuilder(labels.EmptyLabels())
	targets, failures := TargetsFromGroup(&targetgroup.Group{Targets: []model.LabelSet{{}, {model.AddressLabel: "localhost:9090"}}}, &cfg, nil, lb)
	require.Len(t, targets, 1)
	require.Len(t, failures, 1)
	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",