Skip to content

Commit

Permalink
Add test guards against Python3.7 for which OpenTelemetry is unavaila…
Browse files Browse the repository at this point in the history
…ble + address test feedback
  • Loading branch information
odeke-em committed Dec 16, 2024
1 parent 215613f commit a6e602f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions google/cloud/spanner_v1/_opentelemetry_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ def get_tracer(tracer_provider=None):

@contextmanager
def trace_call(name, session=None, extra_attributes=None, observability_options=None):
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
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/test_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
OpenTelemetryBase,
StatusCode,
enrich_with_otel_scope,
HAS_OPENTELEMETRY_INSTALLED,
)


Expand Down Expand Up @@ -232,6 +233,9 @@ def test_get_non_expired(self):
self.assertFalse(pool._sessions.full())

def test_spans_bind_get(self):
if not HAS_OPENTELEMETRY_INSTALLED:
return

# This tests retrieving 1 out of 4 sessions from the session pool.
pool = self._make_one(size=4)
database = _Database("name")
Expand Down Expand Up @@ -271,6 +275,9 @@ def test_spans_bind_get(self):
self.assertSpanEvents("pool.Get", wantEventNames, span_list[-1])

def test_spans_bind_get_empty_pool(self):
if not HAS_OPENTELEMETRY_INSTALLED:
return

# Tests trying to invoke pool.get() from an empty pool.
pool = self._make_one(size=0)
database = _Database("name")
Expand Down Expand Up @@ -312,6 +319,9 @@ def test_spans_bind_get_empty_pool(self):
assert got_all_events == want_all_events

def test_spans_pool_bind(self):
if not HAS_OPENTELEMETRY_INSTALLED:
return

# Tests the exception generated from invoking pool.bind when
# you have an empty pool.
pool = self._make_one(size=1)
Expand Down Expand Up @@ -524,6 +534,9 @@ def test_get_empty(self):
self.assertTrue(pool._sessions.empty())

def test_spans_get_empty_pool(self):
if not HAS_OPENTELEMETRY_INSTALLED:
return

# This scenario tests a pool that hasn't been filled up
# and pool.get() acquires from a pool, waiting for a session
# to become available.
Expand Down Expand Up @@ -877,6 +890,23 @@ def test_put_full(self):

self.assertTrue(pool._sessions.full())

def test_spans_put_full(self):
if not HAS_OPENTELEMETRY_INSTALLED:
return

import queue

pool = self._make_one(size=4)
database = _Database("name")
SESSIONS = [_Session(database)] * 4
database._sessions.extend(SESSIONS)
pool.bind(database)

with self.assertRaises(queue.Full):
pool.put(_Session(database))

self.assertTrue(pool._sessions.full())

span_list = self.get_finished_spans()
got_span_names = [span.name for span in span_list]
want_span_names = ["CloudSpanner.PingingPool.BatchCreateSessions"]
Expand Down Expand Up @@ -991,6 +1021,9 @@ def test_ping_oldest_stale_and_not_exists(self):
self.assertNoSpans()

def test_spans_get_and_leave_empty_pool(self):
if not HAS_OPENTELEMETRY_INSTALLED:
return

# This scenario tests the spans generated from pulling a span
# out the pool and leaving it empty.
pool = self._make_one()
Expand Down

0 comments on commit a6e602f

Please sign in to comment.