Skip to content

Auto TraceΒΆ

Metaclass for automatic method tracing via OpenTelemetry.

ClassesΒΆ

AutoTracedMeta ΒΆ

Bases: type

Metaclass that automatically applies @traced_and_logged to all public methods.

This enables automatic OpenTelemetry span creation and debug logging for all method calls on classes using this metaclass.

Control via environment variable

FLOCK_AUTO_TRACE=true - Enable auto-tracing (default) FLOCK_AUTO_TRACE=false - Disable auto-tracing

Example

class Agent(metaclass=AutoTracedMeta): def execute(self, ctx, artifacts): # Automatically traced and logged ...

FunctionsΒΆ

__new__ ΒΆ
__new__(mcs, name, bases, namespace, **kwargs)

Create a new class with auto-traced methods.

Source code in src/flock/logging/auto_trace.py
def __new__(mcs, name, bases, namespace, **kwargs):
    """Create a new class with auto-traced methods."""
    if not ENABLE_AUTO_TRACE:
        # If auto-tracing is disabled, return the class unchanged
        return super().__new__(mcs, name, bases, namespace, **kwargs)

    # Apply @traced_and_logged to all public methods
    for attr_name, attr_value in list(namespace.items()):
        # Skip private methods (starting with _)
        if attr_name.startswith("_"):
            continue

        # Skip non-callables
        if not callable(attr_value):
            continue

        # Skip if already traced
        if getattr(attr_value, "_traced", False):
            continue

        # Skip if explicitly marked to skip tracing
        if getattr(attr_value, "_skip_trace", False):
            continue

        # Apply the decorator
        traced_func = traced_and_logged(attr_value)
        traced_func._traced = True  # Mark as traced to avoid double-wrapping
        namespace[attr_name] = traced_func

    return super().__new__(mcs, name, bases, namespace, **kwargs)

FunctionsΒΆ

skip_trace ΒΆ

skip_trace(func)

Decorator to mark a method to skip auto-tracing.

Use this for methods that are called very frequently or are not interesting for debugging purposes.

Example

class Agent(metaclass=AutoTracedMeta): @skip_trace def _internal_helper(self): # Not traced ...

Source code in src/flock/logging/auto_trace.py
def skip_trace(func):
    """Decorator to mark a method to skip auto-tracing.

    Use this for methods that are called very frequently or are not
    interesting for debugging purposes.

    Example:
        class Agent(metaclass=AutoTracedMeta):
            @skip_trace
            def _internal_helper(self):
                # Not traced
                ...
    """
    func._skip_trace = True
    return func