Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.

Commit da44aff

Browse files
committed
Adjustments to handle-disabled behaviour of MetricsCapture
1 parent c07edca commit da44aff

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

google/cloud/spanner_v1/metrics/metrics_capture.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ def __enter__(self):
4444
SpannerMetricsTracerFactory.current_metrics_tracer = (
4545
factory.create_metrics_tracer()
4646
)
47-
SpannerMetricsTracerFactory.current_metrics_tracer.record_operation_start()
47+
if SpannerMetricsTracerFactory.current_metrics_tracer:
48+
SpannerMetricsTracerFactory.current_metrics_tracer.record_operation_start()
4849
return self
4950

5051
def __exit__(self, exc_type, exc_value, traceback):
@@ -61,5 +62,6 @@ def __exit__(self, exc_type, exc_value, traceback):
6162
Returns:
6263
bool: False to propagate the exception if any occurred.
6364
"""
64-
SpannerMetricsTracerFactory.current_metrics_tracer.record_operation_completion()
65+
if SpannerMetricsTracerFactory.current_metrics_tracer:
66+
SpannerMetricsTracerFactory.current_metrics_tracer.record_operation_completion()
6567
return False # Propagate the exception if any

google/cloud/spanner_v1/metrics/metrics_tracer_factory.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,9 @@ def create_metrics_tracer(self) -> MetricsTracer:
259259
Returns:
260260
MetricsTracer: A MetricsTracer instance with default settings and client attributes.
261261
"""
262+
if not HAS_OPENTELEMETRY_INSTALLED:
263+
return None
264+
262265
metrics_tracer = MetricsTracer(
263266
enabled=self.enabled and HAS_OPENTELEMETRY_INSTALLED,
264267
instrument_attempt_latency=self._instrument_attempt_latency,

google/cloud/spanner_v1/metrics/spanner_metrics_tracer_factory.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ def __new__(
7676
cls._metrics_tracer_factory = MetricsTracerFactory(
7777
enabled, SPANNER_SERVICE_NAME
7878
)
79+
if not HAS_OPENTELEMETRY_INSTALLED:
80+
return cls._metrics_tracer_factory
7981

8082
client_uid = cls._generate_client_uid()
8183
cls._metrics_tracer_factory.set_client_uid(client_uid)

0 commit comments

Comments
 (0)