Skip to content

Commit

Permalink
Add docs for trace_call and trace_call_end_lazily
Browse files Browse the repository at this point in the history
  • Loading branch information
odeke-em committed Nov 28, 2024
1 parent 8db062a commit beb8bb9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
24 changes: 18 additions & 6 deletions google/cloud/spanner_v1/_opentelemetry_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ def get_tracer(tracer_provider=None):
return tracer_provider.get_tracer(TRACER_NAME, TRACER_VERSION)


def _prepare_span_attributes(
name, session=None, extra_attributes=None, observability_options=None
def _make_tracer_and_span_attributes(
session=None, extra_attributes=None, observability_options=None
):
if not HAS_OPENTELEMETRY_INSTALLED:
return None, None
Expand Down Expand Up @@ -104,8 +104,13 @@ def _prepare_span_attributes(

@contextmanager
def trace_call(name, session=None, extra_attributes=None, observability_options=None):
tracer, span_attributes = _prepare_span_attributes(
name, session, extra_attributes, observability_options
"""
trace_call is used in situations where you need to end a span with a context manager
or after a scope is exited. If you need to keep a span alive and lazily end it, please
invoke `trace_call_end_lazily`.
"""
tracer, span_attributes = _make_tracer_and_span_attributes(
session, extra_attributes, observability_options
)
if not tracer:
yield None
Expand All @@ -124,10 +129,17 @@ def trace_call(name, session=None, extra_attributes=None, observability_options=
span.set_status(Status(StatusCode.OK))


def trace_end_explicitly(
def trace_call_end_lazily(
name, session=None, extra_attributes=None, observability_options=None
):
tracer, span_attributes = _prepare_span_attributes(
"""
trace_call_end_lazily is used in situations where you won't have a context manager
and need to end a span explicitly when a specific condition happens. If you need a
context manager, please invoke `trace_call` with which you can invoke
`with trace_call(...) as span:`
It is the caller's responsibility to explicitly invoke span.end()
"""
tracer, span_attributes = _make_tracer_and_span_attributes(
session, extra_attributes, observability_options
)
if not tracer:
Expand Down
6 changes: 3 additions & 3 deletions google/cloud/spanner_v1/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
)
from google.cloud.spanner_v1._opentelemetry_tracing import (
trace_call,
trace_end_explicitly,
trace_call_end_lazily,
)
from google.cloud.spanner_v1 import RequestOptions
from google.cloud.spanner_v1._helpers import _retry
Expand All @@ -52,7 +52,7 @@ def __init__(self, session):
observability_options = getattr(
self._session._database, "observability_options", None
)
self.__span = trace_end_explicitly(
self.__span = trace_call_end_lazily(
"CloudSpanner." + type(self).__name__,
self._session,
observability_options=observability_options,
Expand Down Expand Up @@ -263,7 +263,7 @@ def __enter__(self):
observability_options = getattr(
self._session._database, "observability_options", None
)
self.__span = trace_end_explicitly(
self.__span = trace_call_end_lazily(
"CloudSpanner.Batch",
self._session,
observability_options=observability_options,
Expand Down
8 changes: 4 additions & 4 deletions google/cloud/spanner_v1/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
)
from google.cloud.spanner_v1._opentelemetry_tracing import (
trace_call,
trace_end_explicitly,
trace_call_end_lazily,
)
from google.cloud.spanner_v1.batch import Batch
from google.cloud.spanner_v1.batch import MutationGroups
Expand Down Expand Up @@ -1195,7 +1195,7 @@ def __init__(
def __enter__(self):
"""Begin ``with`` block."""
observability_options = self._database.observability_options
self.__span = trace_end_explicitly(
self.__span = trace_call_end_lazily(
"CloudSpanner.Database.batch",
None,
observability_options=observability_options,
Expand Down Expand Up @@ -1291,7 +1291,7 @@ def __enter__(self):
attributes = dict()
if self._kw:
attributes["multi_use"] = self._kw["multi_use"]
self.__span = trace_end_explicitly(
self.__span = trace_call_end_lazily(
"CloudSpanner.Database.snapshot",
None,
attributes,
Expand Down Expand Up @@ -1346,7 +1346,7 @@ def __init__(
self._exact_staleness = exact_staleness
observability_options = getattr(self._database, "observability_options", {})
self.__observability_options = observability_options
self.__span = trace_end_explicitly(
self.__span = trace_call_end_lazily(
"CloudSpanner.BatchSnapshot",
self._session,
observability_options=observability_options,
Expand Down

0 comments on commit beb8bb9

Please sign in to comment.