Skip to content

Trace

provide.foundation.logger.processors.trace

TODO: Add module docstring.

Functions

inject_trace_context

inject_trace_context(
    logger: Any,
    method_name: str,
    event_dict: dict[str, Any],
) -> dict[str, Any]

Processor to inject trace context into log records.

Parameters:

Name Type Description Default
logger Any

Logger instance

required
method_name str

Method name being called

required
event_dict dict[str, Any]

Current event dictionary

required

Returns:

Type Description
dict[str, Any]

Event dictionary with trace context added

Source code in provide/foundation/logger/processors/trace.py
def inject_trace_context(logger: Any, method_name: str, event_dict: dict[str, Any]) -> dict[str, Any]:
    """Processor to inject trace context into log records.

    Args:
        logger: Logger instance
        method_name: Method name being called
        event_dict: Current event dictionary

    Returns:
        Event dictionary with trace context added

    """
    # Try OpenTelemetry trace context first
    if _inject_otel_trace_context(event_dict):
        return event_dict

    # Fallback to Foundation's simple tracer context
    _inject_foundation_trace_context(event_dict)

    return event_dict

should_inject_trace_context

should_inject_trace_context() -> bool

Check if trace context injection is available.

Returns:

Type Description
bool

True if trace context can be injected

Source code in provide/foundation/logger/processors/trace.py
def should_inject_trace_context() -> bool:
    """Check if trace context injection is available.

    Returns:
        True if trace context can be injected

    """
    # Check if OpenTelemetry is available and has active span
    if _HAS_OTEL and otel_trace_runtime:
        try:
            current_span = otel_trace_runtime.get_current_span()
            if current_span and current_span.is_recording():
                return True
        except Exception:
            pass

    # Check if Foundation tracer has active context
    try:
        from provide.foundation.tracer.context import (
            get_current_span,
            get_current_trace_id,
        )

        return get_current_span() is not None or get_current_trace_id() is not None
    except Exception:
        pass

    return False