GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — tracing: spans are unconditionally set to a status of OK even after setting to ERROR after invoking `with trace_call` and that fails due to unconditional overriding because OpenTelemetry-Python can change the status after ERROR to OK · Issue #1246 · googleapis/python-spanner

This repository was archived by the owner on Jun 8, 2026. It is now read-only.
This repository was archived by the owner on Jun 8, 2026. It is now read-only.

tracing: spans are unconditionally set to a status of OK even after setting to ERROR after invoking with trace_call and that fails due to unconditional overriding because OpenTelemetry-Python can change the status after ERROR to OK #1246

Copy link
Copy link
Closed
#1258
#1248
Closed
tracing: spans are unconditionally set to a status of OK even after setting to ERROR after invoking with trace_call and that fails due to unconditional overriding because OpenTelemetry-Python can change the status after ERROR to OK#1246
#1258
#1248
Copy link
Assignees
harshachinta
Labels
api: spannerIssues related to the googleapis/python-spanner API.

Description

@odeke-em
odeke-em
opened on Dec 3, 2024
Issue body actions

Noticed while writing extensive tests for modernized tracing functionality that we've got this code

python-spanner/google/cloud/spanner_v1/_opentelemetry_tracing.py

Lines 98 to 108 in ccae6e0

with tracer.start_as_current_span(
name, kind=trace.SpanKind.CLIENT, attributes=attributes
) as span:
try:
yield span
except Exception as error:
span.set_status(Status(StatusCode.ERROR, str(error)))
span.record_exception(error)
raise
else:
span.set_status(Status(StatusCode.OK))

which doesn't do exactly what you expect with OpenTelemetry

Once yield span is invoked, if no exception was encountered, we unconditionally set the span's value to OK even if it previously were set to ERROR and here is a test

Reproduction

from tests import _helpers as ot_helpers
        
@pytest.mark.skipif(
    not ot_helpers.HAS_OPENTELEMETRY_INSTALLED,
    reason="Tracing requires OpenTelemetry",
)               
def test_trace_call_status():
    from opentelemetry.sdk.trace.export import SimpleSpanProcessor
    from opentelemetry.sdk.trace.export.in_memory_span_exporter import (
        InMemorySpanExporter,
    )
    from google.cloud.spanner_v1._opentelemetry_tracing import trace_call
    from opentelemetry.trace.status import Status, StatusCode
    from opentelemetry.sdk.trace import TracerProvider
    from opentelemetry.sdk.trace.sampling import ALWAYS_ON
    from opentelemetry import trace
        
    tracer_provider = TracerProvider(sampler=ALWAYS_ON)
    trace_exporter = InMemorySpanExporter()
    tracer_provider.add_span_processor(SimpleSpanProcessor(trace_exporter))
    observability_options = dict(tracer_provider=tracer_provider)
        
    with trace_call(
        "VerifyBehavior", observability_options=observability_options
    ) as span:
        span.set_status(Status(StatusCode.ERROR, "Our error exhibit"))
        
    span_list = trace_exporter.get_finished_spans()
    got_statuses = []
        
    for span in span_list:
        got_statuses.append(
            (span.name, span.status.status_code, span.status.description)
        )
        
    want_statuses = [
        ("VerifyBehavior", StatusCode.ERROR, "Our error exhibit"),
    ]
    assert got_statuses == want_statuses

which would fail with

$ nox -s system-3.8 -- -k test_trace_call_status
>       assert got_statuses == want_statuses
E       AssertionError: assert [('VerifyBeha...OK: 1>, None)] == [('VerifyBeha...ror exhibit')]
E         
E         At index 0 diff: ('VerifyBehavior', <StatusCode.OK: 1>, None) != ('VerifyBehavior', <StatusCode.ERROR: 2>, 'Our error exhibit')
E         Use -v to get more diff

tests/system/test_observability_options.py:180: AssertionError

Fix

diff --git a/google/cloud/spanner_v1/_opentelemetry_tracing.py b/google/cloud/spanner_v1/_opentelemetry_tracing.py
index 9472251..65b3441 100644
--- a/google/cloud/spanner_v1/_opentelemetry_tracing.py
+++ b/google/cloud/spanner_v1/_opentelemetry_tracing.py
@@ -142,6 +142,9 @@ def trace_call_end_lazily(
     ctx_manager.__enter__()
 
     def discard(exc_type=None, exc_value=None, exc_traceback=None):
+        if not exc_type:
+            span.set_status(Status(StatusCode.OK))
+
         ctx_manager.__exit__(exc_type, exc_value, exc_traceback)
 
     return discard
@@ -175,8 +178,12 @@ def trace_call(name, session=None, extra_attributes=None, observability_options=
             span.record_exception(error)
             raise
         else:
-            span.set_status(Status(StatusCode.OK))
-
+            if span._status.status_code == StatusCode.UNSET:
+                # OpenTelemetry-Python only allows a status change
+                # if the current code is UNSET or ERROR. At the end
+                # of the generator's consumption, only set it to OK
+                # it wasn't previously set otherwise
+                span.set_status(Status(StatusCode.OK))

/cc @harshachinta

Activity

  1. product-auto-label
    added
    api: spannerIssues related to the googleapis/python-spanner API.
    on Dec 3, 2024
  2. blunderbuss-gcf
    assigned
    harshachinta
    on Dec 3, 2024
  3. odeke-em commented on Dec 3, 2024

    @odeke-em
    odeke-em
    on Dec 3, 2024
    ContributorAuthor
    More actions

    I have a fix for it.

  4. odeke-em
    added 7 commits that reference this issue on Dec 3, 2024

    All add much deeper tests to check for span statuses plus retries+abort

    This issue will close once commit 8d6dad0 is merged into the 'main' branch.
    8d6dad0

    fix(tracing): only set span.status=OK if UNSET

    This issue will close once commit d1018ac is merged into the 'main' branch.
    d1018ac

    All add much deeper tests to check for span statuses plus retries+abort

    This issue will close once commit cfe27c8 is merged into the 'main' branch.
    cfe27c8

    feat(observability): more descriptive and value adding spans

    This issue will close once commit 41ddb55 is merged into the 'main' branch.
    41ddb55

    fix(tracing): only set span.status=OK if UNSET

    This issue will close once commit f1d41ee is merged into the 'main' branch.
    f1d41ee

    fix(tracing): only set span.status=OK if UNSET

    This issue will close once commit 981fab5 is merged into the 'main' branch.
    981fab5

    All add much deeper tests to check for span statuses plus retries+abort

    This issue will close once commit f129154 is merged into the 'main' branch.
    f129154
  5. harshachinta
    closed this as completedin #1248on Dec 6, 2024
  6. harshachinta
    closed this as completedin 1d393feon Dec 6, 2024
  7. renovate-bot
    added a commit that references this issue on Dec 6, 2024

    fix(tracing): only set span.status=OK if UNSET (googleapis#1248)

    This issue will close once commit 1d393fe is merged into the 'main' branch.
    1d393fe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @harshachinta
    harshachinta

Labels

api: spannerIssues related to the googleapis/python-spanner API.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.