-
Notifications
You must be signed in to change notification settings - Fork 929
opentelemetry-sdk: activate instrumentors from declarative config #5372
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
ocelotl
wants to merge
12
commits into
open-telemetry:main
Choose a base branch
from
ocelotl:issue_5361
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.
+395
−23
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5f72ea5
opentelemetry-sdk: implement instrumentation.python declarative confi…
ocelotl 8b37ec5
opentelemetry-sdk: normalize slash in YAML keys during dict-to-datacl…
ocelotl 58488d2
opentelemetry-sdk: validate instrumentation opts via config_dataclass
ocelotl bc14db0
refactor: inline _coerce_opts into configure_instrumentation
ocelotl a2a88df
refactor: rename _instrumentation to instrumentation, use from-import…
ocelotl 5d54326
changelog: add fragment for PR 5372
ocelotl 08b992f
fix(ci): fix pylint and ruff failures in instrumentation files
ocelotl 72d74b7
fix(ci): move pylint disable inside class body so it applies to methods
ocelotl d58e2f1
refactor: rename config_dataclass attribute to configuration
ocelotl 76ab381
ci: retrigger
ocelotl b58e602
fix: address review comments on instrumentation declarative config
ocelotl 1d6fa79
fix: use from inspect import isclass instead of import inspect
ocelotl 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`: Add support for activating instrumentors from a declarative configuration file via the `instrumentation/development.python` section. Instrumentors can declare a `configuration` attribute to have their options validated through the same type-coercion pipeline used for SDK component configuration. |
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
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
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
78 changes: 78 additions & 0 deletions
78
opentelemetry-sdk/src/opentelemetry/sdk/_configuration/instrumentation.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,78 @@ | ||
| # Copyright The OpenTelemetry Authors | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import fields, is_dataclass | ||
| from inspect import isclass | ||
| from logging import getLogger | ||
|
|
||
| from opentelemetry.sdk._configuration._common import load_entry_point | ||
| from opentelemetry.sdk._configuration._conversion import _dict_to_dataclass | ||
| from opentelemetry.sdk._configuration._exceptions import ConfigurationError | ||
| from opentelemetry.sdk._configuration.models import ExperimentalInstrumentation | ||
|
|
||
| _logger = getLogger(__name__) | ||
|
|
||
|
|
||
| def configure_instrumentation( | ||
| configuration: ExperimentalInstrumentation | None, | ||
| ) -> None: | ||
| """Activate instrumentors listed under ``instrumentation/development.python``. | ||
|
|
||
| For each entry in ``configuration.python`` the matching | ||
| ``opentelemetry_instrumentor`` entry point is loaded. If the instrumentor | ||
| class exposes a ``configuration`` attribute that is a dataclass type, the | ||
| raw options are validated through ``_dict_to_dataclass`` before being | ||
| forwarded to ``instrument()``. An ``enabled: false`` value suppresses | ||
| instrumentation without raising. | ||
|
|
||
| If an instrumentor is already active (e.g. ``opentelemetry-instrument`` | ||
| ran before the SDK was configured from the file) its ``instrument()`` call | ||
| is skipped to avoid a double-instrumentation warning. | ||
|
|
||
| Absent or unknown entry points are logged as warnings; runtime errors from | ||
| an instrumentor are logged as exceptions. Neither stops the remaining | ||
| instrumentors from being applied. | ||
| """ | ||
| if configuration is None or configuration.python is None: | ||
| return | ||
|
|
||
| for name, options in configuration.python.items(): | ||
| options = dict(options) | ||
| if not options.pop("enabled", True): | ||
| _logger.debug( | ||
| "Instrumentation '%s' is disabled in declarative config; skipping", | ||
| name, | ||
| ) | ||
| continue | ||
|
|
||
| try: | ||
| cls = load_entry_point("opentelemetry_instrumentor", name) | ||
| configuration_cls = getattr(cls, "configuration", None) | ||
| if isclass(configuration_cls) and is_dataclass(configuration_cls): | ||
| configuration_obj = _dict_to_dataclass( | ||
| options, configuration_cls | ||
| ) | ||
| options = { | ||
| f.name: value | ||
| for f in fields(configuration_obj) | ||
| if (value := getattr(configuration_obj, f.name)) | ||
| is not None | ||
| } | ||
| instance = cls() | ||
| if getattr(instance, "is_instrumented_by_opentelemetry", False): | ||
| _logger.debug("Skipping '%s': already instrumented", name) | ||
| else: | ||
| instance.instrument(**options) | ||
| _logger.debug("Instrumented '%s' via declarative config", name) | ||
| except ConfigurationError as exc: | ||
| _logger.warning( | ||
| "Skipping instrumentation '%s' in declarative config: %s", | ||
| name, | ||
| exc, | ||
| ) | ||
| except Exception: # pylint: disable=broad-except | ||
| _logger.exception( | ||
| "Failed to instrument '%s' via declarative config", name | ||
| ) | ||
Oops, something went wrong.
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.