fix(telemetry): merge SDK resource attrs into pre-installed MeterProvider - #320
Open
jeanscherf wants to merge 5 commits into
Open
fix(telemetry): merge SDK resource attrs into pre-installed MeterProvider#320jeanscherf wants to merge 5 commits into
jeanscherf wants to merge 5 commits into
Conversation
…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).
NicoleMGomes
approved these changes
Sep 9, 2026
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.
Problem
Reported:
sap.internal_sdk.versionandsap.cloud_sdk.namewere 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
MeterProviderbeforeauto_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 SDKMeterProvideris 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.resourceis mutable in-place._measurement_consumerholds 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 andset_meter_provideris not called againTestSetupMeterProvider.test_existing_provider_no_new_reader— verifies no extraPeriodicExportingMetricReaderis created on the reuse path