Skip to content

Context

provide.foundation.tracer.context

TODO: Add module docstring.

Classes

SpanContext

SpanContext(span: Span)

Context manager for managing span lifecycle.

Automatically sets and clears the current span.

Source code in provide/foundation/tracer/context.py
def __init__(self, span: Span) -> None:
    self.span = span
    self.previous_span: Span | None = None
Functions
__enter__
__enter__() -> Span

Enter the span context.

Source code in provide/foundation/tracer/context.py
def __enter__(self) -> Span:
    """Enter the span context."""
    self.previous_span = get_current_span()
    set_current_span(self.span)
    return self.span
__exit__
__exit__(
    exc_type: type[BaseException] | None,
    exc_val: BaseException | None,
    exc_tb: Any,
) -> None

Exit the span context.

Source code in provide/foundation/tracer/context.py
def __exit__(
    self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: Any
) -> None:
    """Exit the span context."""
    if exc_type is not None:
        self.span.set_error(f"{exc_type.__name__}: {exc_val}")
    self.span.finish()
    set_current_span(self.previous_span)

Functions

create_child_span

create_child_span(
    name: str, parent: Span | None = None
) -> Span

Create a child span.

Parameters:

Name Type Description Default
name str

Name of the span

required
parent Span | None

Parent span, defaults to current span

None

Returns:

Type Description
Span

New child span

Source code in provide/foundation/tracer/context.py
def create_child_span(name: str, parent: Span | None = None) -> Span:
    """Create a child span.

    Args:
        name: Name of the span
        parent: Parent span, defaults to current span

    Returns:
        New child span

    """
    if parent is None:
        parent = get_current_span()

    if parent:
        return Span(name=name, parent_id=parent.span_id, trace_id=parent.trace_id)
    return Span(name=name)

get_current_span

get_current_span() -> Span | None

Get the currently active span.

Source code in provide/foundation/tracer/context.py
def get_current_span() -> Span | None:
    """Get the currently active span."""
    return _current_span.get(None)

get_current_trace_id

get_current_trace_id() -> str | None

Get the current trace ID.

Source code in provide/foundation/tracer/context.py
def get_current_trace_id() -> str | None:
    """Get the current trace ID."""
    return _current_trace_id.get(None)

get_trace_context

get_trace_context() -> dict[str, Any]

Get the current trace context information.

Returns:

Type Description
dict[str, Any]

Dictionary with trace context information

Source code in provide/foundation/tracer/context.py
def get_trace_context() -> dict[str, Any]:
    """Get the current trace context information.

    Returns:
        Dictionary with trace context information

    """
    current_span = get_current_span()
    trace_id = get_current_trace_id()

    return {
        "trace_id": trace_id,
        "span_id": current_span.span_id if current_span else None,
        "span_name": current_span.name if current_span else None,
    }

set_current_span

set_current_span(span: Span | None) -> None

Set the current active span.

Source code in provide/foundation/tracer/context.py
def set_current_span(span: Span | None) -> None:
    """Set the current active span."""
    _current_span.set(span)
    if span:
        _current_trace_id.set(span.trace_id)

with_span

with_span(name: str) -> SpanContext

Create a new span context.

Parameters:

Name Type Description Default
name str

Name of the span

required

Returns:

Type Description
SpanContext

SpanContext that can be used as a context manager

Source code in provide/foundation/tracer/context.py
def with_span(name: str) -> SpanContext:
    """Create a new span context.

    Args:
        name: Name of the span

    Returns:
        SpanContext that can be used as a context manager

    """
    span = create_child_span(name)
    return SpanContext(span)