feat(observability): enable OpenTelemetry metrics and tracing by default by sinhasubham · Pull Request #1410 · googleapis/python-spanner · GitHub
Skip to content
This repository was archived by the owner on Jun 8, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 7 additions & 20 deletions google/cloud/spanner_v1/_opentelemetry_tracing.py
2 changes: 1 addition & 1 deletion google/cloud/spanner_v1/request_id_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def with_request_id(
all_metadata = (other_metadata or []).copy()
all_metadata.append((REQ_ID_HEADER_KEY, req_id))

if span is not None:
if span:
span.set_attribute(X_GOOG_SPANNER_REQUEST_ID_SPAN_ATTR, req_id)

return all_metadata
Expand Down
81 changes: 39 additions & 42 deletions google/cloud/spanner_v1/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,9 @@ def exists(self):
span,
),
)
if span:
span.set_attribute("session_found", True)
span.set_attribute("session_found", True)
except NotFound:
if span:
span.set_attribute("session_found", False)
span.set_attribute("session_found", False)
return False

return True
Expand Down Expand Up @@ -317,18 +315,21 @@ def ping(self):
"""
if self._session_id is None:
raise ValueError("Session ID not set by back-end")

database = self._database
api = database.spanner_api
request = ExecuteSqlRequest(session=self.name, sql="SELECT 1")
api.execute_sql(
request=request,
metadata=database.metadata_with_request_id(
database._next_nth_request,
1,
_metadata_with_prefix(database.name),
),
)
self._last_use_time = datetime.now()

with trace_call("CloudSpanner.Session.ping", self) as span:
request = ExecuteSqlRequest(session=self.name, sql="SELECT 1")
api.execute_sql(
request=request,
metadata=database.metadata_with_request_id(
database._next_nth_request,
1,
_metadata_with_prefix(database.name),
span,
),
)

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

except Aborted as exc:
previous_transaction_id = txn._transaction_id
if span:
delay_seconds = _get_retry_delay(
exc.errors[0],
attempts,
default_retry_delay=default_retry_delay,
)
attributes = dict(delay_seconds=delay_seconds, cause=str(exc))
attributes.update(span_attributes)
add_span_event(
span,
"Transaction was aborted in user operation, retrying",
attributes,
)

delay_seconds = _get_retry_delay(
exc.errors[0],
attempts,
default_retry_delay=default_retry_delay,
)
attributes = dict(delay_seconds=delay_seconds, cause=str(exc))
attributes.update(span_attributes)
add_span_event(
span,
"Transaction was aborted in user operation, retrying",
attributes,
)
_delay_until_retry(
exc, deadline, attempts, default_retry_delay=default_retry_delay
)
Expand Down Expand Up @@ -611,20 +610,18 @@ def run_in_transaction(self, func, *args, **kw):

except Aborted as exc:
previous_transaction_id = txn._transaction_id
if span:
delay_seconds = _get_retry_delay(
exc.errors[0],
attempts,
default_retry_delay=default_retry_delay,
)
attributes = dict(delay_seconds=delay_seconds)
attributes.update(span_attributes)
add_span_event(
span,
"Transaction was aborted during commit, retrying",
attributes,
)

delay_seconds = _get_retry_delay(
exc.errors[0],
attempts,
default_retry_delay=default_retry_delay,
)
attributes = dict(delay_seconds=delay_seconds)
attributes.update(span_attributes)
add_span_event(
span,
"Transaction was aborted during commit, retrying",
attributes,
)
_delay_until_retry(
exc, deadline, attempts, default_retry_delay=default_retry_delay
)
Expand Down
19 changes: 8 additions & 11 deletions setup.py
Loading