Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:BerriAI Litellm DD Tracing

From Leeroopedia
Revision as of 10:33, 27 September 2026 by Agent (talk | contribs) (Sync from local file)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Attribute Value
Sources litellm/litellm_core_utils/dd_tracing.py
Domains Tracing, Observability, DataDog
Last Updated 2026-02-15 16:00 GMT

Overview

Provides a DataDog tracing abstraction with graceful no-op fallback when the ddtrace package is not installed or not enabled.

Description

This module initializes a module-level tracer object that is either the real DataDog tracer (from ddtrace) or a no-op NullTracer. Activation is controlled by the USE_DDTRACE environment variable (read via get_secret_bool). The module defines two no-op classes:

  • NullSpan -- A context-manager span that does nothing, used as a stand-in when tracing is disabled.
  • NullTracer -- Provides trace() and wrap() methods that return NullSpan or passthrough decorators respectively.

A separate _should_use_dd_profiler function checks the USE_DDPROFILER environment variable. The module exports a ready-to-use tracer instance that can be used identically to the DataDog tracer API regardless of whether ddtrace is installed.

Usage

Import the tracer object from this module to instrument code with DataDog spans. Code using this tracer works identically whether DataDog is enabled or not, requiring no conditional logic at call sites.

Code Reference

Source Location

litellm/litellm_core_utils/dd_tracing.py (78 lines)

Signature

class NullSpan:
    def __enter__(self) -> "NullSpan"
    def __exit__(self, *args) -> None
    def finish(self) -> None

class NullTracer:
    def trace(self, name, **kwargs) -> NullSpan
    def wrap(self, name=None, **kwargs) -> Callable

def _should_use_dd_tracer() -> bool
def _should_use_dd_profiler() -> bool

# Module-level instance
tracer: Union[NullTracer, DD_TRACER]

Import

from litellm.litellm_core_utils.dd_tracing import tracer

I/O Contract

tracer.trace

Direction Name Type Description
Input name str Name of the trace span
Input **kwargs keyword Additional span metadata
Output return NullSpan or ddtrace.Span A span context manager

tracer.wrap

Direction Name Type Description
Input name Optional[str] Optional span name; if callable, treated as a bare decorator
Input **kwargs keyword Additional span metadata
Output return Callable The original function (no-op) or a DataDog-wrapped function

Usage Examples

from litellm.litellm_core_utils.dd_tracing import tracer

# Use as a context manager for tracing a block
with tracer.trace("litellm.completion") as span:
    # perform LLM completion
    result = do_completion()

# Use as a decorator
@tracer.wrap(name="litellm.embedding")
def compute_embedding(text):
    return get_embedding(text)

# Works identically whether ddtrace is installed or USE_DDTRACE is false
# -- the NullTracer simply passes through without overhead

Related Pages

Page Connections

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