Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Principle:Wandb Weave Op Decoration

From Leeroopedia
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:

  1. Intercepts the call: Captures function arguments before execution.
  2. Executes the original: Delegates to the wrapped function transparently.
  3. Records the result: Captures the return value or exception after execution.
  4. 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.

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment