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

Commit 2ba8794

Browse files
committed
Make OpenTelemetry default
1 parent f81fbd5 commit 2ba8794

File tree

6 files changed

+250
-322
lines changed

6 files changed

+250
-322
lines changed

google/cloud/spanner_v1/_opentelemetry_tracing.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,12 @@
2424
_metadata_with_span_context,
2525
)
2626

27-
try:
28-
from opentelemetry import trace
29-
from opentelemetry.trace.status import Status, StatusCode
30-
from opentelemetry.semconv.attributes.otel_attributes import (
31-
OTEL_SCOPE_NAME,
32-
OTEL_SCOPE_VERSION,
33-
)
34-
35-
HAS_OPENTELEMETRY_INSTALLED = True
36-
except ImportError:
37-
HAS_OPENTELEMETRY_INSTALLED = False
27+
from opentelemetry import trace
28+
from opentelemetry.trace.status import Status, StatusCode
29+
from opentelemetry.semconv.attributes.otel_attributes import (
30+
OTEL_SCOPE_NAME,
31+
OTEL_SCOPE_VERSION,
32+
)
3833

3934
from google.cloud.spanner_v1.metrics.metrics_capture import MetricsCapture
4035

@@ -70,11 +65,6 @@ def trace_call(
7065
if session:
7166
session._last_use_time = datetime.now()
7267

73-
if not (HAS_OPENTELEMETRY_INSTALLED and name):
74-
# Empty context manager. Users will have to check if the generated value is None or a span
75-
yield None
76-
return
77-
7868
tracer_provider = None
7969

8070
# By default enable_extended_tracing=True because in a bid to minimize
@@ -155,11 +145,8 @@ def trace_call(
155145

156146

157147
def get_current_span():
158-
if not HAS_OPENTELEMETRY_INSTALLED:
159-
return None
160148
return trace.get_current_span()
161149

162150

163151
def add_span_event(span, event_name, event_attributes=None):
164-
if span:
165-
span.add_event(event_name, event_attributes)
152+
span.add_event(event_name, event_attributes)

google/cloud/spanner_v1/request_id_header.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ def with_request_id(
4343
all_metadata = (other_metadata or []).copy()
4444
all_metadata.append((REQ_ID_HEADER_KEY, req_id))
4545

46-
if span is not None:
47-
span.set_attribute(X_GOOG_SPANNER_REQUEST_ID_SPAN_ATTR, req_id)
46+
span.set_attribute(X_GOOG_SPANNER_REQUEST_ID_SPAN_ATTR, req_id)
4847

4948
return all_metadata
5049

google/cloud/spanner_v1/session.py

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,9 @@ def exists(self):
251251
span,
252252
),
253253
)
254-
if span:
255-
span.set_attribute("session_found", True)
254+
span.set_attribute("session_found", True)
256255
except NotFound:
257-
if span:
258-
span.set_attribute("session_found", False)
256+
span.set_attribute("session_found", False)
259257
return False
260258

261259
return True
@@ -311,18 +309,21 @@ def ping(self):
311309
"""
312310
if self._session_id is None:
313311
raise ValueError("Session ID not set by back-end")
312+
314313
database = self._database
315314
api = database.spanner_api
316-
request = ExecuteSqlRequest(session=self.name, sql="SELECT 1")
317-
api.execute_sql(
318-
request=request,
319-
metadata=database.metadata_with_request_id(
320-
database._next_nth_request,
321-
1,
322-
_metadata_with_prefix(database.name),
323-
),
324-
)
325-
self._last_use_time = datetime.now()
315+
316+
with trace_call("CloudSpanner.Session.ping", self) as span:
317+
request = ExecuteSqlRequest(session=self.name, sql="SELECT 1")
318+
api.execute_sql(
319+
request=request,
320+
metadata=database.metadata_with_request_id(
321+
database._next_nth_request,
322+
1,
323+
_metadata_with_prefix(database.name),
324+
span,
325+
),
326+
)
326327

327328
def snapshot(self, **kw):
328329
"""Create a snapshot to perform a set of reads with shared staleness.
@@ -557,20 +558,18 @@ def run_in_transaction(self, func, *args, **kw):
557558

558559
except Aborted as exc:
559560
previous_transaction_id = txn._transaction_id
560-
if span:
561-
delay_seconds = _get_retry_delay(
562-
exc.errors[0],
563-
attempts,
564-
default_retry_delay=default_retry_delay,
565-
)
566-
attributes = dict(delay_seconds=delay_seconds, cause=str(exc))
567-
attributes.update(span_attributes)
568-
add_span_event(
569-
span,
570-
"Transaction was aborted in user operation, retrying",
571-
attributes,
572-
)
573-
561+
delay_seconds = _get_retry_delay(
562+
exc.errors[0],
563+
attempts,
564+
default_retry_delay=default_retry_delay,
565+
)
566+
attributes = dict(delay_seconds=delay_seconds, cause=str(exc))
567+
attributes.update(span_attributes)
568+
add_span_event(
569+
span,
570+
"Transaction was aborted in user operation, retrying",
571+
attributes,
572+
)
574573
_delay_until_retry(
575574
exc, deadline, attempts, default_retry_delay=default_retry_delay
576575
)
@@ -602,20 +601,18 @@ def run_in_transaction(self, func, *args, **kw):
602601

603602
except Aborted as exc:
604603
previous_transaction_id = txn._transaction_id
605-
if span:
606-
delay_seconds = _get_retry_delay(
607-
exc.errors[0],
608-
attempts,
609-
default_retry_delay=default_retry_delay,
610-
)
611-
attributes = dict(delay_seconds=delay_seconds)
612-
attributes.update(span_attributes)
613-
add_span_event(
614-
span,
615-
"Transaction was aborted during commit, retrying",
616-
attributes,
617-
)
618-
604+
delay_seconds = _get_retry_delay(
605+
exc.errors[0],
606+
attempts,
607+
default_retry_delay=default_retry_delay,
608+
)
609+
attributes = dict(delay_seconds=delay_seconds)
610+
attributes.update(span_attributes)
611+
add_span_event(
612+
span,
613+
"Transaction was aborted during commit, retrying",
614+
attributes,
615+
)
619616
_delay_until_retry(
620617
exc, deadline, attempts, default_retry_delay=default_retry_delay
621618
)

setup.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,15 @@
4444
"proto-plus >= 1.22.2, <2.0.0; python_version>='3.11'",
4545
"protobuf>=3.20.2,<7.0.0,!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5",
4646
"grpc-interceptor >= 0.15.4",
47+
# Make OpenTelemetry a core dependency
48+
"opentelemetry-api >= 1.22.0",
49+
"opentelemetry-sdk >= 1.22.0",
50+
"opentelemetry-semantic-conventions >= 0.43b0",
51+
"opentelemetry-resourcedetector-gcp >= 1.8.0a0",
52+
"google-cloud-monitoring >= 2.16.0",
53+
"mmh3 >= 4.1.0 ",
4754
]
48-
extras = {
49-
"tracing": [
50-
"opentelemetry-api >= 1.22.0",
51-
"opentelemetry-sdk >= 1.22.0",
52-
"opentelemetry-semantic-conventions >= 0.43b0",
53-
"opentelemetry-resourcedetector-gcp >= 1.8.0a0",
54-
"google-cloud-monitoring >= 2.16.0",
55-
"mmh3 >= 4.1.0 ",
56-
],
57-
"libcst": "libcst >= 0.2.5",
58-
}
55+
extras = {"libcst": "libcst >= 0.2.5"}
5956

6057
url = "https://github.com/googleapis/python-spanner"
6158

0 commit comments

Comments
 (0)