From 195f0b0f7a18a151f8e1f17d9d1e1574cb5fa8aa Mon Sep 17 00:00:00 2001 From: waiho-gumloop Date: Tue, 31 Mar 2026 16:44:26 -0700 Subject: [PATCH] fix: remove redundant MetricsCapture from trace_call The bare MetricsCapture() inside trace_call creates a MetricsTracer without project_id or instance_id. Since every caller already provides its own MetricsCapture(resource_info) with correct labels, the one inside trace_call is redundant and records operation metrics with incomplete resource labels on every operation. Fixes googleapis/google-cloud-python#16173 --- .../spanner_v1/_opentelemetry_tracing.py | 46 +++++++++---------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/packages/google-cloud-spanner/google/cloud/spanner_v1/_opentelemetry_tracing.py b/packages/google-cloud-spanner/google/cloud/spanner_v1/_opentelemetry_tracing.py index 62033ddfcb7b..f0f871a8f6f9 100644 --- a/packages/google-cloud-spanner/google/cloud/spanner_v1/_opentelemetry_tracing.py +++ b/packages/google-cloud-spanner/google/cloud/spanner_v1/_opentelemetry_tracing.py @@ -30,7 +30,6 @@ _metadata_with_span_context, ) from google.cloud.spanner_v1.gapic_version import __version__ as gapic_version -from google.cloud.spanner_v1.metrics.metrics_capture import MetricsCapture from google.cloud.spanner_v1.services.spanner.client import SpannerClient TRACER_NAME = "cloud.google.com/python/spanner" @@ -129,29 +128,28 @@ def trace_call( with tracer.start_as_current_span( name, kind=trace.SpanKind.CLIENT, attributes=attributes ) as span: - with MetricsCapture(): - try: - if enable_end_to_end_tracing: - _metadata_with_span_context(metadata) - yield span - except Exception as error: - span.set_status(Status(StatusCode.ERROR, str(error))) - # OpenTelemetry-Python imposes invoking span.record_exception on __exit__ - # on any exception. We should file a bug later on with them to only - # invoke .record_exception if not already invoked, hence we should not - # invoke .record_exception on our own else we shall have 2 exceptions. - raise - else: - # All spans still have set_status available even if for example - # NonRecordingSpan doesn't have "_status". - absent_span_status = getattr(span, "_status", None) is None - if absent_span_status or span._status.status_code == StatusCode.UNSET: - # OpenTelemetry-Python only allows a status change - # if the current code is UNSET or ERROR. At the end - # of the generator's consumption, only set it to OK - # it wasn't previously set otherwise. - # https://github.com/googleapis/python-spanner/issues/1246 - span.set_status(Status(StatusCode.OK)) + try: + if enable_end_to_end_tracing: + _metadata_with_span_context(metadata) + yield span + except Exception as error: + span.set_status(Status(StatusCode.ERROR, str(error))) + # OpenTelemetry-Python imposes invoking span.record_exception on __exit__ + # on any exception. We should file a bug later on with them to only + # invoke .record_exception if not already invoked, hence we should not + # invoke .record_exception on our own else we shall have 2 exceptions. + raise + else: + # All spans still have set_status available even if for example + # NonRecordingSpan doesn't have "_status". + absent_span_status = getattr(span, "_status", None) is None + if absent_span_status or span._status.status_code == StatusCode.UNSET: + # OpenTelemetry-Python only allows a status change + # if the current code is UNSET or ERROR. At the end + # of the generator's consumption, only set it to OK + # it wasn't previously set otherwise. + # https://github.com/googleapis/python-spanner/issues/1246 + span.set_status(Status(StatusCode.OK)) def get_current_span():