Skip to content

Commit d297b66

Browse files
committed
Add docs for trace_call and trace_call_end_lazily
1 parent 8db062a commit d297b66

File tree

4 files changed

+46
-26
lines changed

4 files changed

+46
-26
lines changed

google/cloud/spanner_v1/_opentelemetry_tracing.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ def get_tracer(tracer_provider=None):
5454
return tracer_provider.get_tracer(TRACER_NAME, TRACER_VERSION)
5555

5656

57-
def _prepare_span_attributes(
58-
name, session=None, extra_attributes=None, observability_options=None
57+
def _make_tracer_and_span_attributes(
58+
session=None, extra_attributes=None, observability_options=None
5959
):
6060
if not HAS_OPENTELEMETRY_INSTALLED:
6161
return None, None
@@ -104,8 +104,13 @@ def _prepare_span_attributes(
104104

105105
@contextmanager
106106
def trace_call(name, session=None, extra_attributes=None, observability_options=None):
107-
tracer, span_attributes = _prepare_span_attributes(
108-
name, session, extra_attributes, observability_options
107+
"""
108+
trace_call is used in situations where you need to end a span with a context manager
109+
or after a scope is exited. If you need to keep a span alive and lazily end it, please
110+
invoke `trace_call_end_lazily`.
111+
"""
112+
tracer, span_attributes = _make_tracer_and_span_attributes(
113+
session, extra_attributes, observability_options
109114
)
110115
if not tracer:
111116
yield None
@@ -124,14 +129,27 @@ def trace_call(name, session=None, extra_attributes=None, observability_options=
124129
span.set_status(Status(StatusCode.OK))
125130

126131

127-
def trace_end_explicitly(
132+
def trace_call_end_lazily(
128133
name, session=None, extra_attributes=None, observability_options=None
129134
):
130-
tracer, span_attributes = _prepare_span_attributes(
135+
"""
136+
trace_call_end_lazily is used in situations where you won't have a context manager
137+
and need to end a span explicitly when a specific condition happens. If you need a
138+
context manager, please invoke `trace_call` with which you can invoke
139+
`with trace_call(...) as span:`
140+
It is the caller's responsibility to explicitly invoke span.end()
141+
"""
142+
tracer, span_attributes = _make_tracer_and_span_attributes(
131143
session, extra_attributes, observability_options
132144
)
133145
if not tracer:
134146
return None
135147
return tracer.start_span(
136148
name, kind=trace.SpanKind.CLIENT, attributes=span_attributes
137149
)
150+
151+
152+
def get_current_span():
153+
if not HAS_OPENTELEMETRY_INSTALLED:
154+
return None
155+
return trace.get_current_span()

google/cloud/spanner_v1/batch.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727
_metadata_with_leader_aware_routing,
2828
)
2929
from google.cloud.spanner_v1._opentelemetry_tracing import (
30+
get_current_span,
3031
trace_call,
31-
trace_end_explicitly,
32+
trace_call_end_lazily,
3233
)
3334
from google.cloud.spanner_v1 import RequestOptions
3435
from google.cloud.spanner_v1._helpers import _retry
@@ -52,7 +53,7 @@ def __init__(self, session):
5253
observability_options = getattr(
5354
self._session._database, "observability_options", None
5455
)
55-
self.__span = trace_end_explicitly(
56+
self.__span = trace_call_end_lazily(
5657
"CloudSpanner." + type(self).__name__,
5758
self._session,
5859
observability_options=observability_options,
@@ -102,11 +103,11 @@ def update(self, table, columns, values):
102103
"update mutations inserted", dict(table=table, columns=columns)
103104
)
104105

105-
def add_event_on_current_span(self, event_commentary, attributes=None):
106+
def _add_event_on_current_span(self, event_commentary, attributes=None):
106107
current_span = get_current_span()
107108
if not current_span:
108109
return
109-
span.add_event(event_commentary, attributes)
110+
current_span.add_event(event_commentary, attributes)
110111

111112
def insert_or_update(self, table, columns, values):
112113
"""Insert/update one or more table rows.
@@ -155,9 +156,7 @@ def delete(self, table, keyset):
155156
"""
156157
delete = Mutation.Delete(table=table, key_set=keyset._to_pb())
157158
self._mutations.append(Mutation(delete=delete))
158-
self._add_event_on_current_span(
159-
"delete mutations inserted", dict(table=table, columns=columns)
160-
)
159+
self._add_event_on_current_span("delete mutations inserted", dict(table=table))
161160

162161

163162
class Batch(_BatchBase):
@@ -263,7 +262,7 @@ def __enter__(self):
263262
observability_options = getattr(
264263
self._session._database, "observability_options", None
265264
)
266-
self.__span = trace_end_explicitly(
265+
self.__span = trace_call_end_lazily(
267266
"CloudSpanner.Batch",
268267
self._session,
269268
observability_options=observability_options,

google/cloud/spanner_v1/database.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
)
5656
from google.cloud.spanner_v1._opentelemetry_tracing import (
5757
trace_call,
58-
trace_end_explicitly,
58+
trace_call_end_lazily,
5959
)
6060
from google.cloud.spanner_v1.batch import Batch
6161
from google.cloud.spanner_v1.batch import MutationGroups
@@ -1195,7 +1195,7 @@ def __init__(
11951195
def __enter__(self):
11961196
"""Begin ``with`` block."""
11971197
observability_options = self._database.observability_options
1198-
self.__span = trace_end_explicitly(
1198+
self.__span = trace_call_end_lazily(
11991199
"CloudSpanner.Database.batch",
12001200
None,
12011201
observability_options=observability_options,
@@ -1291,7 +1291,7 @@ def __enter__(self):
12911291
attributes = dict()
12921292
if self._kw:
12931293
attributes["multi_use"] = self._kw["multi_use"]
1294-
self.__span = trace_end_explicitly(
1294+
self.__span = trace_call_end_lazily(
12951295
"CloudSpanner.Database.snapshot",
12961296
None,
12971297
attributes,
@@ -1346,7 +1346,7 @@ def __init__(
13461346
self._exact_staleness = exact_staleness
13471347
observability_options = getattr(self._database, "observability_options", {})
13481348
self.__observability_options = observability_options
1349-
self.__span = trace_end_explicitly(
1349+
self.__span = trace_call_end_lazily(
13501350
"CloudSpanner.BatchSnapshot",
13511351
self._session,
13521352
observability_options=observability_options,

tests/unit/test_snapshot.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ def test_read_other_error(self):
616616
list(derived.read(TABLE_NAME, COLUMNS, keyset))
617617

618618
self.assertSpanAttributes(
619-
"CloudSpanner.ReadOnlyTransaction.read",
619+
"CloudSpanner._Derived.read",
620620
status=StatusCode.ERROR,
621621
attributes=dict(
622622
BASE_ATTRIBUTES, table_id=TABLE_NAME, columns=tuple(COLUMNS)
@@ -773,7 +773,7 @@ def _read_helper(
773773
)
774774

775775
self.assertSpanAttributes(
776-
"CloudSpanner.ReadOnlyTransaction.read",
776+
"CloudSpanner._Derived.read",
777777
attributes=dict(
778778
BASE_ATTRIBUTES, table_id=TABLE_NAME, columns=tuple(COLUMNS)
779779
),
@@ -1195,10 +1195,13 @@ def _partition_read_helper(
11951195
)
11961196

11971197
self.assertSpanAttributes(
1198-
"CloudSpanner.PartitionReadOnlyTransaction",
1198+
"CloudSpanner.partition_read",
11991199
status=StatusCode.OK,
12001200
attributes=dict(
1201-
BASE_ATTRIBUTES, table_id=TABLE_NAME, columns=tuple(COLUMNS)
1201+
BASE_ATTRIBUTES,
1202+
table_id=TABLE_NAME,
1203+
columns=tuple(COLUMNS),
1204+
index="0",
12021205
),
12031206
)
12041207

@@ -1226,7 +1229,7 @@ def test_partition_read_other_error(self):
12261229
list(derived.partition_read(TABLE_NAME, COLUMNS, keyset))
12271230

12281231
self.assertSpanAttributes(
1229-
"CloudSpanner.PartitionReadOnlyTransaction",
1232+
"CloudSpanner.partition_read",
12301233
status=StatusCode.ERROR,
12311234
attributes=dict(
12321235
BASE_ATTRIBUTES, table_id=TABLE_NAME, columns=tuple(COLUMNS)
@@ -1369,7 +1372,7 @@ def _partition_query_helper(
13691372
)
13701373

13711374
self.assertSpanAttributes(
1372-
"CloudSpanner.PartitionReadWriteTransaction",
1375+
"CloudSpanner.partition_query",
13731376
status=StatusCode.OK,
13741377
attributes=dict(BASE_ATTRIBUTES, **{"db.statement": SQL_QUERY_WITH_PARAM}),
13751378
)
@@ -1755,7 +1758,7 @@ def test_begin_ok_exact_staleness(self):
17551758
)
17561759

17571760
self.assertSpanAttributes(
1758-
"CloudSpanner.BeginTransaction",
1761+
"CloudSpanner.begin",
17591762
status=StatusCode.OK,
17601763
attributes=BASE_ATTRIBUTES,
17611764
)
@@ -1791,7 +1794,7 @@ def test_begin_ok_exact_strong(self):
17911794
)
17921795

17931796
self.assertSpanAttributes(
1794-
"CloudSpanner.BeginTransaction",
1797+
"CloudSpanner.begin",
17951798
status=StatusCode.OK,
17961799
attributes=BASE_ATTRIBUTES,
17971800
)

0 commit comments

Comments
 (0)