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

Commit 529d7c8

Browse files
committed
Attach observability_options directly before trace_call
1 parent 6f179a4 commit 529d7c8

File tree

7 files changed

+70
-28
lines changed

7 files changed

+70
-28
lines changed

google/cloud/spanner_v1/_opentelemetry_tracing.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,6 @@ def trace_call(name, session, extra_attributes=None, observability_options=None)
5959

6060
tracer_provider = None
6161
enable_extended_tracing = False
62-
if observability_options is None and getattr(session, "_database", None):
63-
observability_options = getattr(
64-
session._database, "observability_options", None
65-
)
6662

6763
if observability_options:
6864
tracer_provider = observability_options.tracer_provider
@@ -85,7 +81,9 @@ def trace_call(name, session, extra_attributes=None, observability_options=None)
8581

8682
# TODO(@odeke-em) enable after discussion with team and agreement
8783
# over extended tracing changes as the legacy default is always to
88-
# record SQL statements on spans.
84+
# record SQL statements on spans, because the prior behavior was
85+
# to always record the SQL statement and changing it is 's considered
86+
# a breaking change by the Python-Spanner team.
8987
if False and not enable_extended_tracing:
9088
attributes.pop("db.statement", False)
9189
attributes.pop("sql", False)

google/cloud/spanner_v1/batch.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,13 @@ def commit(
205205
max_commit_delay=max_commit_delay,
206206
request_options=request_options,
207207
)
208-
with trace_call("CloudSpanner.Commit", self._session, trace_attributes):
208+
observability_options = getattr(database, "observability_options", None)
209+
with trace_call(
210+
"CloudSpanner.Commit",
211+
self._session,
212+
trace_attributes,
213+
observability_options=observability_options,
214+
):
209215
method = functools.partial(
210216
api.commit,
211217
request=request,
@@ -318,7 +324,13 @@ def batch_write(self, request_options=None, exclude_txn_from_change_streams=Fals
318324
request_options=request_options,
319325
exclude_txn_from_change_streams=exclude_txn_from_change_streams,
320326
)
321-
with trace_call("CloudSpanner.BatchWrite", self._session, trace_attributes):
327+
observability_options = getattr(database, "observability_options", None)
328+
with trace_call(
329+
"CloudSpanner.BatchWrite",
330+
self._session,
331+
trace_attributes,
332+
observability_options=observability_options,
333+
):
322334
method = functools.partial(
323335
api.batch_write,
324336
request=request,

google/cloud/spanner_v1/database.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,7 @@ def __init__(
180180
self._proto_descriptors = proto_descriptors
181181

182182
if pool is None:
183-
pool = BurstyPool(
184-
database_role=database_role,
185-
)
183+
pool = BurstyPool(database_role=database_role)
186184

187185
self._pool = pool
188186
pool.bind(self)
@@ -744,11 +742,7 @@ def session(self, labels=None, database_role=None):
744742
# If role is specified in param, then that role is used
745743
# instead.
746744
role = database_role or self._database_role
747-
return Session(
748-
self,
749-
labels=labels,
750-
database_role=role,
751-
)
745+
return Session(self, labels=labels, database_role=role)
752746

753747
def snapshot(self, **kw):
754748
"""Return an object which wraps a snapshot.
@@ -1719,7 +1713,7 @@ def close(self):
17191713

17201714
@property
17211715
def observability_options(self):
1722-
return self._instance.observability_options
1716+
return self._instance._observability_options
17231717

17241718

17251719
def _check_ddl_statements(value):

google/cloud/spanner_v1/instance.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,3 @@ def _item_to_operation(self, operation_pb):
733733
return google.api_core.operation.from_gapic(
734734
operation_pb, operations_client, response_type, metadata_type=metadata_type
735735
)
736-
737-
@property
738-
def observability_options(self):
739-
return self._client.observability_options

google/cloud/spanner_v1/session.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,13 @@ def create(self):
142142
if self._labels:
143143
request.session.labels = self._labels
144144

145-
with trace_call("CloudSpanner.CreateSession", self, self._labels):
145+
observability_options = getattr(self._database, "observability_options", None)
146+
with trace_call(
147+
"CloudSpanner.CreateSession",
148+
self,
149+
self._labels,
150+
observability_options=observability_options,
151+
):
146152
session_pb = api.create_session(
147153
request=request,
148154
metadata=metadata,
@@ -169,7 +175,10 @@ def exists(self):
169175
)
170176
)
171177

172-
with trace_call("CloudSpanner.GetSession", self) as span:
178+
observability_options = getattr(self._database, "observability_options", None)
179+
with trace_call(
180+
"CloudSpanner.GetSession", self, observability_options=observability_options
181+
) as span:
173182
try:
174183
api.get_session(name=self.name, metadata=metadata)
175184
if span:
@@ -194,7 +203,12 @@ def delete(self):
194203
raise ValueError("Session ID not set by back-end")
195204
api = self._database.spanner_api
196205
metadata = _metadata_with_prefix(self._database.name)
197-
with trace_call("CloudSpanner.DeleteSession", self):
206+
observability_options = getattr(self._database, "observability_options", None)
207+
with trace_call(
208+
"CloudSpanner.DeleteSession",
209+
self,
210+
observability_options=observability_options,
211+
):
198212
api.delete_session(name=self.name, metadata=metadata)
199213

200214
def ping(self):

google/cloud/spanner_v1/snapshot.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,14 @@ def _restart_on_unavailable(
8484
)
8585

8686
request.transaction = transaction_selector
87-
with trace_call(trace_name, session, attributes):
87+
observability_options = None
88+
if session and session._database:
89+
observability_options = getattr(
90+
session._database, "observability_options", None
91+
)
92+
with trace_call(
93+
trace_name, session, attributes, observability_options=observability_options
94+
):
8895
iterator = method(request=request)
8996
while True:
9097
try:

google/cloud/spanner_v1/transaction.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,12 @@ def _execute_request(
110110
"""
111111
transaction = self._make_txn_selector()
112112
request.transaction = transaction
113-
with trace_call(trace_name, session, attributes):
113+
observability_options = getattr(
114+
self._session._database, "observability_options", None
115+
)
116+
with trace_call(
117+
trace_name, session, attributes, observability_options=observability_options
118+
):
114119
method = functools.partial(method, request=request)
115120
response = _retry(
116121
method,
@@ -147,7 +152,12 @@ def begin(self):
147152
read_write=TransactionOptions.ReadWrite(),
148153
exclude_txn_from_change_streams=self.exclude_txn_from_change_streams,
149154
)
150-
with trace_call("CloudSpanner.BeginTransaction", self._session):
155+
observability_options = getattr(database, "observability_options", None)
156+
with trace_call(
157+
"CloudSpanner.BeginTransaction",
158+
self._session,
159+
observability_options=observability_options,
160+
):
151161
method = functools.partial(
152162
api.begin_transaction,
153163
session=self._session.name,
@@ -175,7 +185,12 @@ def rollback(self):
175185
database._route_to_leader_enabled
176186
)
177187
)
178-
with trace_call("CloudSpanner.Rollback", self._session):
188+
observability_options = getattr(database, "observability_options", None)
189+
with trace_call(
190+
"CloudSpanner.Rollback",
191+
self._session,
192+
observability_options=observability_options,
193+
):
179194
method = functools.partial(
180195
api.rollback,
181196
session=self._session.name,
@@ -248,7 +263,13 @@ def commit(
248263
max_commit_delay=max_commit_delay,
249264
request_options=request_options,
250265
)
251-
with trace_call("CloudSpanner.Commit", self._session, trace_attributes):
266+
observability_options = getattr(database, "observability_options", None)
267+
with trace_call(
268+
"CloudSpanner.Commit",
269+
self._session,
270+
trace_attributes,
271+
observability_options=observability_options,
272+
):
252273
method = functools.partial(
253274
api.commit,
254275
request=request,

0 commit comments

Comments
 (0)