Skip to content

Commit dbbd1bc

Browse files
feat(config): Add MeterProvider support for declarative config (#4987)
* config: add resource and propagator creation from declarative config 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 * update changelog with PR number Assisted-by: Claude Sonnet 4.6 * fix pylint, pyright and ruff errors in resource/propagator config - _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 * address review feedback: use _DEFAULT_RESOURCE, fix bool_array coercion - replace _sdk_default_attributes() with _DEFAULT_RESOURCE from resources module - move _coerce_bool into dispatch tables for both scalar and array bool types, fixing a bug where bool_array with string values like "false" would coerce incorrectly via plain bool() (non-empty string -> True) - add test for bool_array with string values to cover the bug Assisted-by: Claude Sonnet 4.6 * fix linter * address review feedback: single coercion table, simplify attributes merge - collapse _SCALAR_COERCIONS and _ARRAY_COERCIONS into a single _COERCIONS dict using an _array() factory, reducing _coerce_attribute_value to two lines - process attributes_list before attributes so explicit attributes naturally overwrite list entries without needing an explicit guard Assisted-by: Claude Sonnet 4.6 * use Callable type annotation on _array helper Assisted-by: Claude Sonnet 4.6 * add detection infrastructure foundations for resource detectors Adds _run_detectors() stub and _filter_attributes() to create_resource(), providing the shared scaffolding for detector PRs to build on. Detectors are opt-in: nothing runs unless explicitly listed under detection_development.detectors in the config. The include/exclude attribute filter mirrors other SDK behaviour. Assisted-by: Claude Sonnet 4.6 * move service.name default into base resource Merges service.name=unknown_service into base before running detectors, so detectors (e.g. service) can override it. Previously it was added to config_attrs and merged last, which would have silently overridden any detector-provided service.name. Assisted-by: Claude Sonnet 4.6 * remove unused logging import from _propagator.py Assisted-by: Claude Sonnet 4.6 * add MeterProvider creation from declarative config Implements create_meter_provider() and configure_meter_provider() following the same patterns as _tracer_provider.py. - PeriodicExportingMetricReader with explicit interval/timeout defaults (60s/30s) to suppress env var reading - OTLP HTTP and gRPC metric exporters (lazy imports) - Console metric exporter - Temporality preference mapping (cumulative/delta/low_memory) with explicit dicts to suppress OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE - Default histogram aggregation mapping with env var suppression - View creation with selector (instrument type/name/unit/meter info) and stream (name/description/aggregation/attribute_keys) - ExemplarFilter mapping; default = TraceBasedExemplarFilter (spec default) - configure_meter_provider(None) is a no-op per spec/Java/JS behavior - attribute_keys.excluded logs a warning (SDK View only supports inclusion) - Pull readers raise ConfigurationError (marked _development in spec) Assisted-by: Claude Sonnet 4.6 * add changelog entry for meter provider declarative config (#4987) Assisted-by: Claude Sonnet 4.6 * fix linter errors * address review feedback: simplify resource filter, propagator loading, and type annotations Assisted-by: Claude Sonnet 4.6 * fix ruff formatting Assisted-by: Claude Sonnet 4.6 * fix pyright: wrap EntryPoints in iter() for next() compatibility Assisted-by: Claude Sonnet 4.6
1 parent 6faa58c commit dbbd1bc

File tree

4 files changed

+1132
-0
lines changed

4 files changed

+1132
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2929
- `opentelemetry-sdk`: Add shared `_parse_headers` helper for declarative config OTLP exporters
3030
([#5021](https://github.com/open-telemetry/opentelemetry-python/pull/5021))
3131
- `opentelemetry-api`: Replace a broad exception in attribute cleaning tests to satisfy pylint in the `lint-opentelemetry-api` CI job
32+
- `opentelemetry-sdk`: Add `create_meter_provider`/`configure_meter_provider` to declarative file configuration, enabling MeterProvider instantiation from config files without reading env vars
33+
([#4987](https://github.com/open-telemetry/opentelemetry-python/pull/4987))
3234
- `opentelemetry-sdk`: Add `create_resource` and `create_propagator`/`configure_propagator` to declarative file configuration, enabling Resource and propagator instantiation from config files without reading env vars
3335
([#4979](https://github.com/open-telemetry/opentelemetry-python/pull/4979))
3436
- `opentelemetry-sdk`: Map Python `CRITICAL` log level to OTel `FATAL` severity text per the specification

0 commit comments

Comments
 (0)