Skip to content

fix(telemetry): merge SDK resource attrs into pre-installed MeterProvider - #320

Open
jeanscherf wants to merge 5 commits into
mainfrom
fix/metrics-resource-merge
Open

fix(telemetry): merge SDK resource attrs into pre-installed MeterProvider#320
jeanscherf wants to merge 5 commits into
mainfrom
fix/metrics-resource-merge

Conversation

@jeanscherf

@jeanscherf jeanscherf commented Sep 9, 2026

Copy link
Copy Markdown
Member

Problem

Reported: sap.internal_sdk.version and sap.cloud_sdk.name were missing from metrics when OTel auto-instrumentation was active, even though they appeared correctly on traces and logs.

Root cause: when the OTel auto-instrumentation wrapper installs a MeterProvider before auto_instrument() is called, metrics.set_meter_provider() is a silent no-op (WARNING: Overriding of current MeterProvider is not allowed). The new provider with our resource attributes is created but never registered, so the global provider (owned by the wrapper) never gets our attributes.

Traces and logs already had a fix for this — they detect the pre-installed provider and merge resource attributes into it. Metrics had no equivalent.

Fix

Mirror the log/trace pattern: at _setup_meter_provider() time, check if an SDK MeterProvider is already set globally. If so, merge our resource attributes into it instead of trying to create a new one.

SdkConfiguration (which stores the resource) is a dataclass, so _sdk_config.resource is mutable in-place. _measurement_consumer holds a reference to the same object, so it sees the update automatically — no additional patching needed.

Tests

  • TestMergeSdkResourceIntoMeterProvider — unit tests for the merge function itself (resource update, measurement consumer sees it, SDK attrs win on collision)
  • TestSetupMeterProvider.test_reuses_existing_sdk_meter_provider — verifies merge path is taken and set_meter_provider is not called again
  • TestSetupMeterProvider.test_existing_provider_no_new_reader — verifies no extra PeriodicExportingMetricReader is created on the reuse path

…ider

When OTel auto-instrumentation installs a MeterProvider before auto_instrument()
is called, metrics.set_meter_provider() is a no-op and the SDK resource attrs
(sap.internal_sdk.version, sap.cloud_sdk.name) are never applied to metrics.

Apply the same merge-on-existing pattern already used for LoggerProvider and
TracerProvider: detect an SDK MeterProvider via isinstance, then mutate
_sdk_config.resource in-place (SdkConfiguration is a dataclass; _measurement_consumer
holds the same object reference so it sees the update automatically).
@jeanscherf
jeanscherf requested a review from a team as a code owner September 9, 2026 12:34
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.

2 participants