config: support env var expansion in relabel_configs#18457
Open
Mentigen wants to merge 4 commits intoprometheus:mainfrom
Open
config: support env var expansion in relabel_configs#18457Mentigen wants to merge 4 commits intoprometheus:mainfrom
Mentigen wants to merge 4 commits intoprometheus:mainfrom
Conversation
e74f140 to
0ad6b35
Compare
Member
|
I believe this should be opt-in. |
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]>
4da1123 to
d7b209a
Compare
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
Summary
Add support for expanding environment variables in
ReplacementandTargetLabelfields ofrelabel_configs,metric_relabel_configs, andalert_relabel_configs, similar to the existing support inglobal.external_labels.Changes
expandEnvVars()helper function for consistent env var expansionexpandRelabelConfigs()helper to expand Replacement and TargetLabel fieldscontainsEnvVar()to distinguish env var refs from regex capture group refs ($1, $2)TestExpandRelabelConfigswith test fixtureUsers can now use environment variables in relabel configs:
Test plan
go test ./config/...TestExpandRelabelConfigspasses with env var expansion