Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,12 @@
_metadata_with_span_context,
)

try:
from opentelemetry import trace
from opentelemetry.trace.status import Status, StatusCode
from opentelemetry.semconv.attributes.otel_attributes import (
OTEL_SCOPE_NAME,
OTEL_SCOPE_VERSION,
)

HAS_OPENTELEMETRY_INSTALLED = True
except ImportError:
HAS_OPENTELEMETRY_INSTALLED = False
from opentelemetry import trace
from opentelemetry.trace.status import Status, StatusCode
from opentelemetry.semconv.attributes.otel_attributes import (
OTEL_SCOPE_NAME,
OTEL_SCOPE_VERSION,
)

from google.cloud.spanner_v1.metrics.metrics_capture import MetricsCapture

Expand Down Expand Up @@ -70,11 +65,6 @@ def trace_call(
if session:
session._last_use_time = datetime.now()

if not (HAS_OPENTELEMETRY_INSTALLED and name):
# Empty context manager. Users will have to check if the generated value is None or a span
yield None
return

tracer_provider = None

# By default enable_extended_tracing=True because in a bid to minimize
Expand Down Expand Up @@ -155,11 +145,8 @@ def trace_call(


def get_current_span():
if not HAS_OPENTELEMETRY_INSTALLED:
return None
return trace.get_current_span()


def add_span_event(span, event_name, event_attributes=None):
if span:
span.add_event(event_name, event_attributes)
span.add_event(event_name, event_attributes)
3 changes: 1 addition & 2 deletions google/cloud/spanner_v1/request_id_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +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:
span.set_attribute(X_GOOG_SPANNER_REQUEST_ID_SPAN_ATTR, req_id)
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 @@ -311,18 +309,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 @@ -557,20 +558,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 @@ -602,20 +601,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
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,15 @@
"proto-plus >= 1.22.2, <2.0.0; python_version>='3.11'",
"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",
"grpc-interceptor >= 0.15.4",
# Make OpenTelemetry a core dependency
"opentelemetry-api >= 1.22.0",
"opentelemetry-sdk >= 1.22.0",
"opentelemetry-semantic-conventions >= 0.43b0",
"opentelemetry-resourcedetector-gcp >= 1.8.0a0",
"google-cloud-monitoring >= 2.16.0",
"mmh3 >= 4.1.0 ",
]
extras = {
"tracing": [
"opentelemetry-api >= 1.22.0",
"opentelemetry-sdk >= 1.22.0",
"opentelemetry-semantic-conventions >= 0.43b0",
"opentelemetry-resourcedetector-gcp >= 1.8.0a0",
"google-cloud-monitoring >= 2.16.0",
"mmh3 >= 4.1.0 ",
],
"libcst": "libcst >= 0.2.5",
}
extras = {"libcst": "libcst >= 0.2.5"}

url = "https://github.com/googleapis/python-spanner"

Expand Down
Loading
Loading