Skip to content

Add YAML aware variable substitution#8342

Open
AlphaOne1 wants to merge 5 commits intoopen-telemetry:mainfrom
AlphaOne1:add_yaml_aware_variable_substitution
Open

Add YAML aware variable substitution#8342
AlphaOne1 wants to merge 5 commits intoopen-telemetry:mainfrom
AlphaOne1:add_yaml_aware_variable_substitution

Conversation

@AlphaOne1
Copy link
Copy Markdown

In the current implementation the environment variable substitution does not follow the specification found in https://opentelemetry.io/docs/specs/otel/configuration/data-model/. Specifically it does not obey the rule that substitution can only occur in YAML values. Further, according to the published specification examples, injected YAML-structured content leads to errors rather than correctly escaped/formatted strings.
This pull request solves these issues by using the yaml.Node structure of the yaml library to operate on already parsed YAML content. Further the redacted examples of the specification are included as further tests.

@AlphaOne1 AlphaOne1 requested review from a team and pellared as code owners December 31, 2025 10:47
@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla bot commented Dec 31, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@github-actions github-actions bot requested a review from codeboten December 31, 2025 10:47
@pellared
Copy link
Copy Markdown
Member

pellared commented Jan 8, 2026

@AlphaOne1, can you please provide examples of current incorrect behavior? It would be nice if you create an issue (bug) for tracking.

@pellared
Copy link
Copy Markdown
Member

@AlphaOne1, bump.

@AlphaOne1
Copy link
Copy Markdown
Author

The issue 8405 was created to track this bug.

@AlphaOne1
Copy link
Copy Markdown
Author

The last commit addresses the issues pointed out during the last pipeline run. Could you run it again?

@codecov
Copy link
Copy Markdown

codecov bot commented Jan 26, 2026

Codecov Report

❌ Patch coverage is 86.11111% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.1%. Comparing base (257448a) to head (d649069).
⚠️ Report is 15 commits behind head on main.

Files with missing lines Patch % Lines
otelconf/internal/provider/envprovider.go 86.1% 5 Missing and 5 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@          Coverage Diff          @@
##            main   #8342   +/-   ##
=====================================
  Coverage   82.1%   82.1%           
=====================================
  Files        179     179           
  Lines      13662   13719   +57     
=====================================
+ Hits       11229   11276   +47     
- Misses      2032    2037    +5     
- Partials     401     406    +5     
Files with missing lines Coverage Δ
otelconf/internal/provider/envprovider.go 89.7% <86.1%> (-7.0%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Member

@pellared pellared left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have forgotten to publish some comments 2 weeks ago...

@AlphaOne1
Copy link
Copy Markdown
Author

Comments of @pellared now included.

Copy link
Copy Markdown
Contributor

@codeboten codeboten left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @AlphaOne1! This looks great, just a few questions in the comments

{ // 24
yamlInput: "key: ${UNDEFINED_KEY:-$${UNDEFINED_KEY}}",
// yamlOutput: `key: ${UNDEFINED_KEY:-${UNDEFINED_KEY}}`,
// this is what the spec tells, but I object,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there already an issue in the spec around this objection? It would be great to have consistency across implementations

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not (yet) file an issue to them. I did not yet dig into their contribution guidelines. As an initial step, do you agree with my argumentation there?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pinging @mx-psi as he did a lot of the initial spec work. wdyt about this issue?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At a first glance the argumentation seems sound and key: ${UNDEFINED_KEY} is what I would expect.

This does not seem to be what the Collector does (see open-telemetry/opentelemetry-collector/pull/14720), but I think what the Collector does is clearly a bug.

I think it would be worthwhile to raise an issue at the spec level.

@AlphaOne1
Copy link
Copy Markdown
Author

I included the changes @codeboten proposed.

Signed-off-by: Alexander Adam <alphaone23@gmail.com>
…rsing:

- Adjust `envVarRegexp` to handle multiple dollar signs more effectively.
- Improve node retyping logic for better YAML type detection.
- Remove `checkRawConfType` function and related references.
- Treat injected or invalid YAML types as strings rather than causing errors.
- Update tests to reflect adjusted behavior for YAML injection and substitution handling.

Signed-off-by: Alexander Adam <alphaone23@gmail.com>
- Add `preserveYAMLLines` to retain original line numbers for improved debugging.
- Replace errors in `retypeNode` with `errors.New` for consistency.
- Enhance test cases to account for escaped newlines in YAML values.

Signed-off-by: Alexander Adam <alphaone23@gmail.com>

# Conflicts:
#	otelconf/config_test.go
Signed-off-by: Alexander Adam <alphaone23@gmail.com>
Signed-off-by: Alexander Adam <alphaone23@gmail.com>
@AlphaOne1 AlphaOne1 force-pushed the add_yaml_aware_variable_substitution branch from 68bfd76 to d649069 Compare February 15, 2026 08:35
@AlphaOne1
Copy link
Copy Markdown
Author

I rebased onto main, to resolve the conflicts.

Copy link
Copy Markdown
Contributor

@codeboten codeboten left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @AlphaOne1, one of those comments can be resolved

{ // 24
yamlInput: "key: ${UNDEFINED_KEY:-$${UNDEFINED_KEY}}",
// yamlOutput: `key: ${UNDEFINED_KEY:-${UNDEFINED_KEY}}`,
// this is what the spec tells, but I object,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pinging @mx-psi as he did a lot of the initial spec work. wdyt about this issue?

github-merge-queue bot pushed a commit to open-telemetry/opentelemetry-collector that referenced this pull request Mar 11, 2026
…14720)

<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description

Relates to
open-telemetry/opentelemetry-go-contrib#8342 (comment)
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.

4 participants