Principle:Wandb Weave Op Decoration
| Knowledge Sources | |
|---|---|
| Domains | Observability, Instrumentation |
| Last Updated | 2026-02-14 00:00 GMT |
Overview
A decorator-based instrumentation pattern that wraps Python functions to transparently capture their inputs, outputs, and execution metadata.
Description
Op Decoration applies the Decorator design pattern to observability. By annotating a function with a tracing decorator, the function's behavior is augmented to record call data (arguments, return values, timing, exceptions) without modifying the function's core logic. This approach provides non-invasive instrumentation that preserves the original function signature and return type.
The pattern supports sync functions, async functions, sync generators, and async generators, making it applicable to virtually any Python callable.
Usage
Use this principle when you want to trace individual function calls in an application. It is the primary mechanism for adding observability to user-defined logic, model inference functions, data processing steps, or any callable that should appear in the trace timeline.
Theoretical Basis
The decorator pattern wraps a target function with a proxy that:
- Intercepts the call: Captures function arguments before execution.
- Executes the original: Delegates to the wrapped function transparently.
- Records the result: Captures the return value or exception after execution.
- Reports metadata: Sends timing, inputs, outputs, and call hierarchy to the tracing backend.
Pseudo-code:
# Abstract decorator pattern for tracing
def trace_decorator(func):
def wrapper(*args, **kwargs):
call = start_trace(func, args, kwargs)
try:
result = func(*args, **kwargs)
end_trace(call, output=result)
return result
except Exception as e:
end_trace(call, exception=e)
raise
return wrapper
Sampling can be applied to reduce overhead in high-throughput scenarios, where only a fraction of calls are actually traced.