Skip to content

feat(config): add MeterProvider creation from declarative config#4987

Draft
MikeGoldsmith wants to merge 9 commits intoopen-telemetry:mainfrom
MikeGoldsmith:mike/config-meter-provider
Draft

feat(config): add MeterProvider creation from declarative config#4987
MikeGoldsmith wants to merge 9 commits intoopen-telemetry:mainfrom
MikeGoldsmith:mike/config-meter-provider

Conversation

@MikeGoldsmith
Copy link
Member

@MikeGoldsmith MikeGoldsmith commented Mar 16, 2026

Description

Implements create_meter_provider() and configure_meter_provider() for the declarative configuration pipeline, as part of the ongoing work tracked in the following issue:

Note

This PR is based on #4979 (Resource & Propagator creation) which must be merged first. The extra commits at the base of this branch will be dropped once that PR lands.

What's included

  • _meter_provider.py: creates an SDK MeterProvider from declarative config
  • Supports PeriodicExportingMetricReader with interval/timeout config
  • Supports OTLP HTTP, OTLP gRPC, and Console metric exporters
  • Lazy imports for optional OTLP packages with a ConfigurationError if not installed
  • Temporality preference mapping (cumulative/delta/low_memory) with explicit per-instrument dicts
  • Default histogram aggregation mapping (explicit_bucket_histogram/base2_exponential_bucket_histogram)
  • Full View support: selector (instrument type/name/unit/meter info) and stream (name/description/aggregation/attribute_keys)
  • All aggregation types with detail parameters (boundaries, max_size, max_scale, record_min_max)
  • ExemplarFilter mapping; default = TraceBasedExemplarFilter per OTel spec
  • configure_meter_provider(None) is a no-op per spec/Java/JS behavior
  • Pull readers (prometheus_development) raise ConfigurationError — aligned with Java's fail-fast behavior and the spec's SHOULD-throw guidance
  • 47 new tests covering all paths including env-var suppression

Env-var suppression

Python's PeriodicExportingMetricReader reads OTEL_METRIC_EXPORT_INTERVAL/OTEL_METRIC_EXPORT_TIMEOUT when params are None, and metric exporters read OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE when preferred_temporality=None. To match the spec's "what you see is what you get" semantics, we always pass explicit values — defaulting to spec defaults (60s/30s, cumulative temporality, trace_based exemplar filter).

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

  • 47 unit tests in tests/_configuration/test_meter_provider.py
  • Includes explicit tests that env vars (OTEL_METRIC_EXPORT_INTERVAL, OTEL_METRICS_EXEMPLAR_FILTER, OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE) are not read when config is used

Does This PR Require a Contrib Repo Change?

  • Yes.
  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

Assisted-by: Claude Sonnet 4.6

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
- 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
…erge

- 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
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
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.

1 participant