Skip to content

config: support env var expansion in relabel_configs#18457

Open
Mentigen wants to merge 4 commits intoprometheus:mainfrom
Mentigen:add-relabel-config-env-expansion
Open

config: support env var expansion in relabel_configs#18457
Mentigen wants to merge 4 commits intoprometheus:mainfrom
Mentigen:add-relabel-config-env-expansion

Conversation

@Mentigen
Copy link
Copy Markdown

@Mentigen Mentigen commented Apr 5, 2026

Which issue(s) does the PR fix:

Fixes #12563

Release notes for end users (ALL commits must be considered).

Reviewers should verify clarity and quality.

[ENHANCEMENT] Config: Add environment variable expansion support to relabel_configs fields (replacement, target_label), similar to existing support in global.external_labels.

Summary

Add support for expanding environment variables in Replacement and TargetLabel fields of relabel_configs, metric_relabel_configs, and alert_relabel_configs, similar to the existing support in global.external_labels.

Changes

  • Created expandEnvVars() helper function for consistent env var expansion
  • Created expandRelabelConfigs() helper to expand Replacement and TargetLabel fields
  • Created containsEnvVar() to distinguish env var refs from regex capture group refs ($1, $2)
  • Apply expansion to: ScrapeConfig, AlertingConfig, AlertmanagerConfig, RemoteWriteConfig
  • Refactored external_labels expansion to use shared helper
  • Added TestExpandRelabelConfigs with test fixture

Users can now use environment variables in relabel configs:

relabel_configs:
  - replacement: ${MY_LABEL_PREFIX}_${JOB}
    target_label: expanded_label

Test plan

  • Run go test ./config/...
  • Verify TestExpandRelabelConfigs passes with env var expansion
  • Verify regex references like $1 are preserved (not expanded)
  • Test with custom environment variables set

@Mentigen Mentigen requested a review from a team as a code owner April 5, 2026 17:02
@Mentigen Mentigen requested a review from brancz April 5, 2026 17:02
@Mentigen Mentigen force-pushed the add-relabel-config-env-expansion branch 3 times, most recently from e74f140 to 0ad6b35 Compare April 5, 2026 18:10
@roidelapluie
Copy link
Copy Markdown
Member

I believe this should be opt-in.

@Mentigen Mentigen requested a review from dgl as a code owner April 9, 2026 22:34
Add support for expanding environment variables in Replacement and TargetLabel
fields of relabel_configs, metric_relabel_configs, and alert_relabel_configs.

This allows users to reference environment variables like $JOB_NAME or ${CUSTOM_LABEL}
in relabel configurations, enabling dynamic configuration based on deployment environment.

Preserves regex capture group references ($1, $2, etc.) which are not treated as
environment variables. Expansion happens after YAML parsing and before configuration
validation to ensure all values are properly expanded.

Fixes prometheus#12563

Signed-off-by: Ilya Kiselev <[email protected]>
Address reviewer feedback: expansion of environment variables in
relabel_configs is now opt-in, disabled by default.

Enable with --enable-feature=expand-relabel-env-vars.

Without the flag, behaviour is unchanged: $VAR references in replacement
and target_label are treated as literals (or regex capture groups).
With the flag, they are expanded via os.Expand before the config is
applied, matching the existing behaviour of global.external_labels.

Changes:
- config.Load and config.LoadFile accept a new expandRelabelEnv bool
- cmd/prometheus: parse the new feature flag; thread it through
  reloadConfig into LoadFile
- cmd/promtool: pass false (no expansion in config-check / sd-check)
- docs/feature_flags.md: document the new flag

Signed-off-by: Ilya Kiselev <[email protected]>
@Mentigen Mentigen force-pushed the add-relabel-config-env-expansion branch from 4da1123 to d7b209a Compare April 9, 2026 22:52
Add missing bool parameter (expandRelabelEnv=false) to config.Load
calls in scrape/manager_test.go and scrape/scrape_test.go.

Signed-off-by: Ilya Kiselev <[email protected]>
- Combine adjacent bool params in reloadConfig signature to satisfy
  gocritic paramTypeCombine rule
- Regenerate docs/command-line/prometheus.md to include the new
  expand-relabel-env-vars feature flag (required by TestDocumentation)

Signed-off-by: Ilya Kiselev <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expand environment variables in relabel_configs

2 participants