Skip to content

datarobot_genai.core.telemetry.agent_identity

agent_identity

Propagates gen_ai.agent.name across a tool-call boundary via OTel Baggage.

A tool-call span (e.g. an MCP tool served by drmcp, possibly over a network hop) has no attribute of its own for which agent invoked it - trace parentage doesn't carry span attributes across that boundary. Baggage does, via auto-instrumented HTTP clients that inject whatever is in the active context on every outgoing request, so attaching it here needs no explicit inject/extract call pairs; the receiving side just reads it back out.

attach_agent_name_baggage

attach_agent_name_baggage(agent_name: str | None) -> Token[Context] | None

Attach agent_name into OTel Baggage on the current context.

Returns the detach token, or None when agent_name is falsy (no context change made). Callers must pass whatever this returns to :func:detach_agent_name_baggage when the wrapped scope ends - always, even when it's None, so a no-op attach is a no-op detach too.

Source code in datarobot_genai/core/telemetry/agent_identity.py
def attach_agent_name_baggage(agent_name: str | None) -> Token[Context] | None:
    """Attach ``agent_name`` into OTel Baggage on the current context.

    Returns the detach token, or ``None`` when ``agent_name`` is falsy (no
    context change made). Callers must pass whatever this returns to
    :func:`detach_agent_name_baggage` when the wrapped scope ends - always,
    even when it's ``None``, so a no-op attach is a no-op detach too.
    """
    if not agent_name:
        return None
    return attach(baggage.set_baggage(GEN_AI_AGENT_NAME_BAGGAGE_KEY, agent_name))

agent_name_baggage

agent_name_baggage(agent_name: str | None) -> Iterator[None]

Context-manager form of :func:attach_agent_name_baggage for callers with a natural with scope around the agent's execution.

Source code in datarobot_genai/core/telemetry/agent_identity.py
@contextmanager
def agent_name_baggage(agent_name: str | None) -> Iterator[None]:
    """Context-manager form of :func:`attach_agent_name_baggage` for callers
    with a natural ``with`` scope around the agent's execution.
    """
    token = attach_agent_name_baggage(agent_name)
    try:
        yield
    finally:
        detach_agent_name_baggage(token)