Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Jun 8, 2026. It is now read-only.

Commit bb5095d

Browse filesBrowse files
authored
feat(observability): enable OpenTelemetry metrics and tracing by default (#1410)
This change enables OpenTelemetry metrics and tracing by default. [Fix for [Issues 1222](https://github.com/googleapis/python-spanner/issues/1222)]
1 parent 9535e5e commit bb5095d
Copy full SHA for bb5095d

6 files changed

+244-322Lines changed: 244 additions & 322 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎google/cloud/spanner_v1/_opentelemetry_tracing.py‎

Copy file name to clipboardExpand all lines: google/cloud/spanner_v1/_opentelemetry_tracing.py
+7-20Lines 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)
Collapse file

‎google/cloud/spanner_v1/request_id_header.py‎

Copy file name to clipboardExpand all lines: google/cloud/spanner_v1/request_id_header.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +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:
46+
if span:
4747
span.set_attribute(X_GOOG_SPANNER_REQUEST_ID_SPAN_ATTR, req_id)
4848

4949
return all_metadata
Collapse file

‎google/cloud/spanner_v1/session.py‎

Copy file name to clipboardExpand all lines: google/cloud/spanner_v1/session.py
+39-42Lines 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
@@ -317,18 +315,21 @@ def ping(self):
317315
"""
318316
if self._session_id is None:
319317
raise ValueError("Session ID not set by back-end")
318+
320319
database = self._database
321320
api = database.spanner_api
322-
request = ExecuteSqlRequest(session=self.name, sql="SELECT 1")
323-
api.execute_sql(
324-
request=request,
325-
metadata=database.metadata_with_request_id(
326-
database._next_nth_request,
327-
1,
328-
_metadata_with_prefix(database.name),
329-
),
330-
)
331-
self._last_use_time = datetime.now()
321+
322+
with trace_call("CloudSpanner.Session.ping", self) as span:
323+
request = ExecuteSqlRequest(session=self.name, sql="SELECT 1")
324+
api.execute_sql(
325+
request=request,
326+
metadata=database.metadata_with_request_id(
327+
database._next_nth_request,
328+
1,
329+
_metadata_with_prefix(database.name),
330+
span,
331+
),
332+
)
332333

333334
def snapshot(self, **kw):
334335
"""Create a snapshot to perform a set of reads with shared staleness.
@@ -566,20 +567,18 @@ def run_in_transaction(self, func, *args, **kw):
566567

567568
except Aborted as exc:
568569
previous_transaction_id = txn._transaction_id
569-
if span:
570-
delay_seconds = _get_retry_delay(
571-
exc.errors[0],
572-
attempts,
573-
default_retry_delay=default_retry_delay,
574-
)
575-
attributes = dict(delay_seconds=delay_seconds, cause=str(exc))
576-
attributes.update(span_attributes)
577-
add_span_event(
578-
span,
579-
"Transaction was aborted in user operation, retrying",
580-
attributes,
581-
)
582-
570+
delay_seconds = _get_retry_delay(
571+
exc.errors[0],
572+
attempts,
573+
default_retry_delay=default_retry_delay,
574+
)
575+
attributes = dict(delay_seconds=delay_seconds, cause=str(exc))
576+
attributes.update(span_attributes)
577+
add_span_event(
578+
span,
579+
"Transaction was aborted in user operation, retrying",
580+
attributes,
581+
)
583582
_delay_until_retry(
584583
exc, deadline, attempts, default_retry_delay=default_retry_delay
585584
)
@@ -611,20 +610,18 @@ def run_in_transaction(self, func, *args, **kw):
611610

612611
except Aborted as exc:
613612
previous_transaction_id = txn._transaction_id
614-
if span:
615-
delay_seconds = _get_retry_delay(
616-
exc.errors[0],
617-
attempts,
618-
default_retry_delay=default_retry_delay,
619-
)
620-
attributes = dict(delay_seconds=delay_seconds)
621-
attributes.update(span_attributes)
622-
add_span_event(
623-
span,
624-
"Transaction was aborted during commit, retrying",
625-
attributes,
626-
)
627-
613+
delay_seconds = _get_retry_delay(
614+
exc.errors[0],
615+
attempts,
616+
default_retry_delay=default_retry_delay,
617+
)
618+
attributes = dict(delay_seconds=delay_seconds)
619+
attributes.update(span_attributes)
620+
add_span_event(
621+
span,
622+
"Transaction was aborted during commit, retrying",
623+
attributes,
624+
)
628625
_delay_until_retry(
629626
exc, deadline, attempts, default_retry_delay=default_retry_delay
630627
)
Collapse file

‎setup.py‎

Copy file name to clipboardExpand all lines: setup.py
+8-11Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.