Skip to content

Runtime

Context

Async-safe trace/span context using ContextVar.

Provides minimal helpers + a context manager for ergonomic usage without locks.

get_current_parent_span_id()

Return the current span id (preferred) or trace id as a fallback.

Rationale
  • Adapters that need to link DB events to traces can call this single, dependency-free helper.
  • This centralizes span/trace access and keeps telemetry plumbing testable.

use_span(trace_id=None, span_id=None)

Temporarily set trace/span IDs (async-safe). Resets on exit.

Parameters may be None to leave a value unchanged.

IDs

ID generation with zero locks.

  • span_id(): 64-bit random value encoded as 16-char lowercase hex

span_id()

Return a 16-char, lowercase hex string representing a 64-bit ID.

Ensures the ID is non-zero to avoid sentinel collisions in downstream systems.

Clock

High-resolution, monotonic clock.

  • now_ns(): nanoseconds from an arbitrary, monotonic reference (perf_counter_ns)

now_ns()

Return a monotonically increasing timestamp in nanoseconds.

Uses time.perf_counter_ns() for high-resolution timing unaffected by system clock adjustments. Suitable for interval measurement.