Skip to content

feat: current_trace_id() — read the caller's active trace id (RIUS-466) - #64

Draft
sudopower wants to merge 1 commit into
mainfrom
kiran/rius-demo-current-trace-id
Draft

feat: current_trace_id() — read the caller's active trace id (RIUS-466)#64
sudopower wants to merge 1 commit into
mainfrom
kiran/rius-demo-current-trace-id

Conversation

@sudopower

Copy link
Copy Markdown
Member

The only SDK change the healing-agent demo needs. Part of RIUS-463, tracked as RIUS-466.

log_resolution attributes a resolution to the run that produced it, so the healing agent needs its own trace id — not a trace it is investigating.

def current_trace_id() -> str | None:
    span_context = trace.get_current_span().get_span_context()
    if not span_context.is_valid:
        return None
    return format_trace_id(span_context.trace_id)

Two details that matter

Uses OTel's own format_trace_id rather than hand-rolled hex padding. Verified against the pinned opentelemetry-api 1.43.0 that it emits bare 32-hex with no 0x prefix — older releases did prefix it, so rolling our own would have been a subtle format bug.

span_context.is_valid covers both required cases in one check. INVALID_SPAN — what get_current_span() returns with nothing active — has an all-zeros context, so the result is None rather than a string of 32 zeros. A valid non-recording span (a propagated or sampled-out remote context) still returns its real trace id, which is the correct answer to "what trace is the caller inside".

Worth knowing for callers: it returns None unless init() has run, because with no provider OTel hands back INVALID_SPAN. Correct semantics, but a headless agent has to init before it can attribute anything.

Verification

All four gates from CLAUDE.md:

uv run ruff check .          → All checks passed!
uv run ruff format --check . → 35 files already formatted
uv run mypy                  → Success: no issues found in 15 source files
uv run pytest -q             → 230 passed, 6 skipped

The 6 skips are pre-existing (test_instrumentation_registry.py outside an --all-extras environment).

Also verified live rather than only by unit test: inside a span it returns a real 32-hex id equal to the span's own context, nested spans report the same trace, and it is None outside any span and after the block exits.

Version left at 0.12.0 and CHANGELOG.md untouched — release-please generates both from squashed PR titles, per CLAUDE.md.

Unrelated drift noticed, deliberately not fixed here

CLAUDE.md still documents the pre-rename package (PyPI glassflow-ai, import glassflow, GLASSFLOW_ env vars) when main is glassflow-rius / rius / RIUS_*. And heartbeat.py hand-rolls format(trace_id, "032x") in two places that could now use the helper this PR adopts. Both left alone to keep the diff to one feature.

@sudopower
sudopower marked this pull request as draft August 24, 2026 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant