feat(config): add resource and propagator creation from declarative config#4979
Open
MikeGoldsmith wants to merge 3 commits intoopen-telemetry:mainfrom
Open
feat(config): add resource and propagator creation from declarative config#4979MikeGoldsmith wants to merge 3 commits intoopen-telemetry:mainfrom
MikeGoldsmith wants to merge 3 commits intoopen-telemetry:mainfrom
Conversation
Implements create_resource() and create_propagator()/configure_propagator() for the declarative file configuration. Resource creation does not read OTEL_RESOURCE_ATTRIBUTES or run any detectors (matches Java/JS SDK behavior). Propagator configuration always calls set_global_textmap to override Python's default tracecontext+baggage, setting a noop CompositePropagator when no propagator is configured. Assisted-by: Claude Sonnet 4.6
Assisted-by: Claude Sonnet 4.6
- _resource.py: refactor _coerce_attribute_value to dispatch table to avoid too-many-return-statements; fix short variable names k/v -> attr_key/attr_val; fix return type of _sdk_default_attributes to dict[str, str] to satisfy pyright - _propagator.py: rename short variable names e -> exc, p -> propagator - test_resource.py: move imports to top level; split TestCreateResource (25 methods) into three focused classes to satisfy too-many-public-methods - test_propagator.py: add pylint disable for protected-access Assisted-by: Claude Sonnet 4.6
pmcollins
reviewed
Mar 13, 2026
|
|
||
| def _sdk_default_attributes() -> dict[str, str]: | ||
| """Return the SDK telemetry attributes (equivalent to Java's Resource.getDefault()).""" | ||
| return { |
Member
There was a problem hiding this comment.
Looks like this these same values are passed into a Resource as
_DEFAULT_RESOURCE = Resource(
{
TELEMETRY_SDK_LANGUAGE: "python",
TELEMETRY_SDK_NAME: "opentelemetry",
TELEMETRY_SDK_VERSION: _OPENTELEMETRY_SDK_VERSION,
}
)
in opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py
Might be nice to share this definition.
| return value | ||
|
|
||
|
|
||
| def _parse_attributes_list(attributes_list: str) -> dict[str, str]: |
Member
There was a problem hiding this comment.
Looks like comma delimited k=v parsing is done in OTELResourceDetector as well. Can they be consolidated?
| if attr_type is None: | ||
| return value | ||
| if attr_type == AttributeType.bool: | ||
| return _coerce_bool(value) |
Member
There was a problem hiding this comment.
Looks like we're treating bool as a special case, but only for scalars. For arrays, we have bool() in the dispatch table (which may not give us what we want). Maybe _coerce_bool could go into the dispatch table for both cases and the if attr_type == AttributeType.bool carveout could be removed.
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.
Description
Implements step 3 of the declarative configuration work (see below), resource and propagator creation from a parsed config file.
Adds:
create_resource(config)— builds an SDKResourcefrom the declarative config model without readingOTEL_RESOURCE_ATTRIBUTESor running any resource detectors (matches Java/JS SDK behavior). Starts from SDK telemetry defaults (telemetry.sdk.*), mergesattributes(withAttributeTypecoercion) andattributes_list(URL-decoded, lower priority), and addsservice.name=unknown_serviceif not specified.create_propagator(config)— builds aCompositePropagatorfrom the config'scompositelist and/orcomposite_liststring. Deduplicates by propagator type. Returns an emptyCompositePropagator(noop) when no propagator is configured.configure_propagator(config)— callsset_global_textmapto override Python's default env-var-based propagator setup.ConfigurationErrormoved to_exceptions.pyto break a circular import introduced whenfile/__init__.pyre-exports the new functions.All functions are exported from
opentelemetry.sdk._configuration.file.Type of change
How Has This Been Tested?
43 new unit tests covering:
AttributeTypecoercions (string, bool, int, double, and array variants)attributes_listparsing: priority vs explicit attributes, URL decoding,=in values, invalid pairsschema_urlpassthroughOTEL_RESOURCE_ATTRIBUTESenv var is NOT readtracecontext,baggage,b3,b3multi)composite+composite_listmerging and deduplicationnoneand empty entries incomposite_listskippedConfigurationErrorconfigure_propagatorcallsset_global_textmapDoes This PR Require a Contrib Repo Change?
Checklist:
Assisted-by: Claude Sonnet 4.6