-
Notifications
You must be signed in to change notification settings - Fork 910
feat(config): support OTEL_CONFIG_FILE in the SDK configurator #5271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MikeGoldsmith
wants to merge
24
commits into
open-telemetry:main
Choose a base branch
from
MikeGoldsmith:mike/config-file-env-routing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+123
−0
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
e5d6932
recursively convert parsed dicts to typed dataclasses in loader
MikeGoldsmith 582c37f
rename changelog fragment to PR #5269
MikeGoldsmith b302f93
tighten typing on conversion module
MikeGoldsmith 3a6fd21
isolate typing.get_type_hints call to placate astroid 3.x on py3.14
MikeGoldsmith 131378c
inline the typing.get_type_hints wrap
MikeGoldsmith 3720621
add configure_sdk orchestrator for declarative config
MikeGoldsmith fd6c20a
rename changelog fragment to PR #5270
MikeGoldsmith 7066419
honor OTEL_CONFIG_FILE in the SDK configurator
MikeGoldsmith da80d63
rename changelog fragment to PR #5271
MikeGoldsmith b6e4702
use ExemplarFilter for enum coercion test fixture; allow 'astroid' in…
MikeGoldsmith 83e17bd
Merge branch 'mike/config-recursive-dict-conversion' into mike/config…
MikeGoldsmith 52365e2
Merge branch 'mike/config-orchestrator' into mike/config-file-env-rou…
MikeGoldsmith 3fc2669
fix lint on test_sdk.py: hoist import, disable no-self-use
MikeGoldsmith 119bc83
Merge branch 'mike/config-orchestrator' into mike/config-file-env-rou…
MikeGoldsmith 417d451
silence pylint/ruff on intentional lazy imports
MikeGoldsmith 41667ca
remove extra blank line after imports (ruff I001)
MikeGoldsmith 297da35
Merge branch 'mike/config-orchestrator' into mike/config-file-env-rou…
MikeGoldsmith 2b2d47b
collapse multi-line @patch decorators (ruff format)
MikeGoldsmith 70c93d9
add end-to-end loader tests covering YAML -> typed config -> factory
MikeGoldsmith 828c54b
Merge branch 'mike/config-recursive-dict-conversion' into mike/config…
MikeGoldsmith 62cdfa4
Merge branch 'mike/config-orchestrator' into mike/config-file-env-rou…
MikeGoldsmith 111fc4a
Merge branch 'main' into mike/config-file-env-routing
MikeGoldsmith e111ebf
Merge branch 'main' into mike/config-file-env-routing
MikeGoldsmith 73982d9
address review feedback on OTEL_CONFIG_FILE routing
MikeGoldsmith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| `opentelemetry-sdk`: the SDK configurator now honors the `OTEL_CONFIG_FILE` environment variable. When set, the SDK loads and applies the referenced declarative configuration file (YAML or JSON) in place of the env-var-based init path. |
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
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
89 changes: 89 additions & 0 deletions
89
opentelemetry-sdk/tests/_configuration/test_configurator_file_routing.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| # Copyright The OpenTelemetry Authors | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| # Tests access private members of SDK classes to assert correct configuration. | ||
| # pylint: disable=protected-access,no-self-use | ||
|
|
||
| import unittest | ||
| from unittest.mock import patch | ||
|
|
||
| from opentelemetry.sdk._configuration import _OTelSDKConfigurator | ||
| from opentelemetry.sdk._configuration._exceptions import ConfigurationError | ||
| from opentelemetry.sdk.environment_variables import OTEL_CONFIG_FILE | ||
|
|
||
|
|
||
| class TestConfiguratorFileRouting(unittest.TestCase): | ||
| def tearDown(self): | ||
| # _BaseConfigurator caches instances via a singleton; reset so sibling | ||
| # tests (e.g. test_configurator.py's CustomConfigurator subclass) are | ||
| # not affected by this class's singleton state. | ||
| _OTelSDKConfigurator._instance = None | ||
|
|
||
| @patch.dict("os.environ", {}, clear=True) | ||
| @patch("opentelemetry.sdk._configuration._initialize_components") | ||
| def test_env_var_unset_runs_env_var_path(self, mock_init_components): | ||
| _OTelSDKConfigurator()._configure(auto_instrumentation_version="X") | ||
| mock_init_components.assert_called_once_with( | ||
| auto_instrumentation_version="X" | ||
| ) | ||
|
|
||
| @patch.dict("os.environ", {OTEL_CONFIG_FILE: "/tmp/otel.yaml"}) | ||
| @patch("opentelemetry.sdk._configuration._sdk.configure_sdk") | ||
| @patch("opentelemetry.sdk._configuration.file._loader.load_config_file") | ||
| @patch("opentelemetry.sdk._configuration._initialize_components") | ||
| def test_env_var_set_routes_to_declarative_path( | ||
| self, mock_init_components, mock_load, mock_configure_sdk | ||
| ): | ||
| sentinel_config = object() | ||
| mock_load.return_value = sentinel_config | ||
|
|
||
| _OTelSDKConfigurator()._configure() | ||
|
|
||
| mock_load.assert_called_once_with("/tmp/otel.yaml") | ||
| mock_configure_sdk.assert_called_once_with(sentinel_config) | ||
| mock_init_components.assert_not_called() | ||
|
|
||
| @patch.dict("os.environ", {OTEL_CONFIG_FILE: "/does/not/exist.yaml"}) | ||
| @patch("opentelemetry.sdk._configuration._initialize_components") | ||
| def test_env_var_set_missing_file_propagates(self, mock_init_components): | ||
| with self.assertRaises(ConfigurationError): | ||
| _OTelSDKConfigurator()._configure() | ||
| mock_init_components.assert_not_called() | ||
|
|
||
| @patch.dict("os.environ", {OTEL_CONFIG_FILE: "/tmp/otel.yaml"}) | ||
| @patch("opentelemetry.sdk._configuration._sdk.configure_sdk") | ||
| @patch("opentelemetry.sdk._configuration.file._loader.load_config_file") | ||
| def test_env_var_set_with_kwargs_warns_and_ignores( | ||
| self, mock_load, mock_configure_sdk | ||
| ): | ||
| mock_load.return_value = object() | ||
|
|
||
| with self.assertLogs( | ||
| "opentelemetry.sdk._configuration", level="WARNING" | ||
| ) as captured: | ||
| _OTelSDKConfigurator()._configure( | ||
| sampler="X", auto_instrumentation_version="Y" | ||
| ) | ||
|
|
||
| self.assertTrue( | ||
| any( | ||
| "OTEL_CONFIG_FILE" in msg and "sampler" in msg | ||
| for msg in captured.output | ||
| ), | ||
| f"Expected warning about ignored kwargs, got: {captured.output}", | ||
| ) | ||
| mock_configure_sdk.assert_called_once() | ||
|
|
||
| @patch.dict("os.environ", {}, clear=True) | ||
| @patch("opentelemetry.sdk._configuration._initialize_components") | ||
| def test_distro_override_pattern_still_works(self, mock_init_components): | ||
| class CustomConfigurator(_OTelSDKConfigurator): | ||
| def _configure(self, **kwargs): | ||
| kwargs["sampler"] = "TEST_SAMPLER" | ||
| super()._configure(**kwargs) | ||
|
|
||
| CustomConfigurator()._configure(auto_instrumentation_version="V") | ||
|
|
||
| mock_init_components.assert_called_once_with( | ||
| auto_instrumentation_version="V", sampler="TEST_SAMPLER" | ||
| ) | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.