Skip to content

Commit a7988b0

Browse files
committed
test(gapic): add rigorous assertions to negative and fallback tracing tests
1 parent 5a36f52 commit a7988b0

1 file changed

Lines changed: 70 additions & 5 deletions

File tree

packages/google-api-core/tests/unit/gapic/test_method.py

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -352,20 +352,40 @@ def test__deduplicate_metadata_tokens(headers, expected):
352352

353353
def test_wrap_method_otel_tracing_disabled(monkeypatch):
354354
"""Proves that when OpenTelemetry tracing is disabled, no span is created."""
355-
monkeypatch.setenv("GOOGLE_SDK_EXPERIMENTAL_PYTHON_TRACING_ENABLED", "false")
356355
mock_target = mock.Mock(return_value="success")
356+
mock_trace = mock.Mock()
357357

358-
with mock.patch(
359-
"google.api_core._observability.is_otel_capabilities_enabled",
360-
return_value=False,
358+
with (
359+
mock.patch(
360+
"google.api_core._observability.is_otel_capabilities_enabled",
361+
return_value=False,
362+
),
363+
mock.patch.dict(
364+
sys.modules,
365+
{
366+
"opentelemetry": mock.Mock(trace=mock_trace),
367+
"opentelemetry.trace": mock_trace,
368+
},
369+
),
361370
):
362371
wrapped = google.api_core.gapic_v1.method.wrap_method(
363372
mock_target,
364373
method_name="google.cloud.secretmanager.v1.SecretManagerService/ListSecrets",
365374
)
366-
assert wrapped() == "success"
375+
result = wrapped()
376+
377+
# 1. Prove the RPC executed successfully
378+
assert result == "success"
367379
mock_target.assert_called_once()
368380

381+
# 2. Prove the OpenTelemetry API was never invoked
382+
mock_trace.get_tracer.assert_not_called()
383+
384+
# 3. Prove the callable holds no tracer or span configuration
385+
assert wrapped._tracer is None
386+
assert wrapped._span_name is None
387+
assert wrapped._span_attributes is None
388+
369389

370390
def test_wrap_method_otel_tracing_omitted_method_name_skips_span(monkeypatch):
371391
"""Proves that when method_name is omitted (e.g. streaming or uninstrumented), no span is created."""
@@ -390,9 +410,18 @@ def test_wrap_method_otel_tracing_omitted_method_name_skips_span(monkeypatch):
390410
wrapped = google.api_core.gapic_v1.method.wrap_method(mock_target)
391411
result = wrapped()
392412

413+
# 1. Prove the RPC executed successfully
393414
assert result == "success"
415+
mock_target.assert_called_once()
416+
417+
# 2. Prove the OpenTelemetry API was never invoked
394418
mock_trace.get_tracer.assert_not_called()
395419

420+
# 3. Prove the callable holds no tracer or span configuration
421+
assert wrapped._tracer is None
422+
assert wrapped._span_name is None
423+
assert wrapped._span_attributes is None
424+
396425

397426
def test_wrap_method_otel_tracing_streaming_skips_span(monkeypatch):
398427
"""Proves that when is_streaming=True is passed, no Tier 3 span is created."""
@@ -421,9 +450,18 @@ def test_wrap_method_otel_tracing_streaming_skips_span(monkeypatch):
421450
)
422451
result = wrapped()
423452

453+
# 1. Prove the RPC executed successfully
424454
assert result == "success"
455+
mock_target.assert_called_once()
456+
457+
# 2. Prove the OpenTelemetry API was never invoked
425458
mock_trace.get_tracer.assert_not_called()
426459

460+
# 3. Prove the callable holds no tracer or span configuration
461+
assert wrapped._tracer is None
462+
assert wrapped._span_name is None
463+
assert wrapped._span_attributes is None
464+
427465

428466
def test_wrap_method_otel_tracing_enabled_success(monkeypatch):
429467
"""Proves that when OpenTelemetry tracing is enabled and method_name is passed, a T3 client span is started."""
@@ -628,9 +666,15 @@ def test_wrap_method_otel_tracing_import_error(monkeypatch):
628666
)
629667
result = wrapped()
630668

669+
# 1. Prove the RPC executed successfully
631670
assert result == "success"
632671
mock_target.assert_called_once()
633672

673+
# 2. Prove the callable holds no tracer or span configuration
674+
assert wrapped._tracer is None
675+
assert wrapped._span_name is None
676+
assert wrapped._span_attributes is None
677+
634678

635679
def test_wrap_method_otel_tracing_provider_attribute_error(monkeypatch):
636680
"""Proves that if tracer_provider raises AttributeError, execution proceeds gracefully."""
@@ -654,9 +698,15 @@ def test_wrap_method_otel_tracing_provider_attribute_error(monkeypatch):
654698
)
655699
result = wrapped()
656700

701+
# 1. Prove the RPC executed successfully
657702
assert result == "success"
658703
mock_target.assert_called_once()
659704

705+
# 2. Prove the callable holds no tracer or span configuration
706+
assert wrapped._tracer is None
707+
assert wrapped._span_name is None
708+
assert wrapped._span_attributes is None
709+
660710

661711
def test_wrap_method_otel_tracing_provider_type_error(monkeypatch):
662712
"""Proves that if tracer_provider raises TypeError, execution proceeds gracefully."""
@@ -680,9 +730,15 @@ def test_wrap_method_otel_tracing_provider_type_error(monkeypatch):
680730
)
681731
result = wrapped()
682732

733+
# 1. Prove the RPC executed successfully
683734
assert result == "success"
684735
mock_target.assert_called_once()
685736

737+
# 2. Prove the callable holds no tracer or span configuration
738+
assert wrapped._tracer is None
739+
assert wrapped._span_name is None
740+
assert wrapped._span_attributes is None
741+
686742

687743
def test_wrap_method_otel_tracing_start_span_error_bypasses_tracing(monkeypatch):
688744
"""Proves that if start_as_current_span raises an Exception, execution proceeds gracefully with nullcontext."""
@@ -804,5 +860,14 @@ def test_wrap_method_async_otel_tracing_streaming_skips_span(monkeypatch):
804860
)
805861
result = wrapped()
806862

863+
# 1. Prove the RPC executed successfully
807864
assert result == "async_success"
865+
mock_target.assert_called_once()
866+
867+
# 2. Prove the OpenTelemetry API was never invoked
808868
mock_trace.get_tracer.assert_not_called()
869+
870+
# 3. Prove the callable holds no tracer or span configuration
871+
assert wrapped._tracer is None
872+
assert wrapped._span_name is None
873+
assert wrapped._span_attributes is None

0 commit comments

Comments
 (0)