Skip to Content
Judgment Labs
DocumentationTracingIntegrations

Trace Temporal

Trace Temporal workflows in Python so the request that starts a workflow and every activity it runs land in one trace.

Temporal's OpenTelemetry TracingInterceptor carries trace context from the code that starts a workflow into the workflow and its activities. With Judgment installed as the global OpenTelemetry provider, the request that starts the workflow and all the work it runs show up as one trace. Activities that finish after that request has returned still join the trace.

This is the recommended setup. OpenTelemetry recommends letting a library's own instrumentation propagate context, and calling inject and continue_trace by hand only where none exists. For Temporal, that instrumentation is the interceptor: it carries the context through workflow and activity headers, so you don't pass it yourself. Temporal also ships a newer OpenTelemetryPlugin, but it is experimental and needs a tracer provider created by Temporal, so it doesn't work with the Judgment SDK's provider. Use TracingInterceptor.

pip install judgeval "temporalio[opentelemetry]"

Before you begin

Complete tracing setup.

Add the interceptor to every process

Do this in every process that starts workflows or runs a worker. Initialize the tracer, install Judgment as the global provider, then connect the client with the interceptor. The interceptor creates its spans through the global provider, so without that call they don't reach Judgment. Workers built from this client use the interceptor too.

app/temporal_client.py
from judgeval import JudgmentTracerProvider, Tracer
from temporalio.client import Client
from temporalio.contrib.opentelemetry import TracingInterceptor

Tracer.init(project_name="lunch-order-agent")
JudgmentTracerProvider.install_as_global_tracer_provider()


async def connect() -> Client:
    return await Client.connect("localhost:7233", interceptors=[TracingInterceptor()])

Trace work inside activities

Workflow code can replay, so don't start spans in workflow code. Put observed functions and wrapped model clients in activities, where their spans nest under Temporal's RunActivity span.

The workflow sandbox rejects judgeval's imports. In a module that defines a workflow, import judgeval inside workflow.unsafe.imports_passed_through().

Set the conversation ID inside the activity

The interceptor carries the trace, but not values you set with Tracer.set_propagating_attribute, such as the conversation ID. When the workflow runs your agent on a conversation that already has an identifier, pass that identifier in the workflow input and set it as gen_ai.conversation.id in the activity that runs the agent. Don't create an ID for work that has no conversation. See Conversations.

workflows.py
from datetime import timedelta

from temporalio import activity, workflow

with workflow.unsafe.imports_passed_through():
    from judgeval import Tracer


@Tracer.observe(span_type="agent")
async def run_agent(message: str, conversation_id: str) -> str:
    Tracer.set_propagating_attribute("gen_ai.conversation.id", conversation_id)
    return await answer(message)  # wrapped model calls and observed tools


@activity.defn
async def respond(message: str, conversation_id: str) -> str:
    return await run_agent(message, conversation_id)


@workflow.defn
class ChatWorkflow:
    @workflow.run
    async def run(self, message: str, conversation_id: str) -> str:
        return await workflow.execute_activity(
            respond,
            args=[message, conversation_id],
            start_to_close_timeout=timedelta(minutes=5),
        )

Start the workflow from an observed function so the request has a root span:

app/chat.py
from uuid import uuid4

from judgeval import Tracer

from app.temporal_client import connect
from workflows import ChatWorkflow


@Tracer.observe(span_type="agent", span_name="chat_turn")
async def chat_turn(chat_id: str, message: str) -> str:
    client = await connect()
    return await client.execute_workflow(
        ChatWorkflow.run,
        args=[message, chat_id],
        id=f"chat-{chat_id}-{uuid4()}",
        task_queue="chat",
    )

What you'll see in Traces

One trace per request: your root span, then StartWorkflow:ChatWorkflow, then RunActivity:respond with your agent, model, and tool spans inside it. Every activity the workflow runs appears under the same root, including activities that end after the root does. The model calls inside the agent activity record the conversation.

If something looks wrong

  • Activities show up as separate traces. The process that started the workflow or the worker process is missing the interceptor, or didn't call install_as_global_tracer_provider().
  • The worker fails with "Failed validating workflow". A workflow module imports judgeval outside workflow.unsafe.imports_passed_through().
  • Model calls inside an activity aren't recorded as a conversation. Set gen_ai.conversation.id inside the activity. A value set before the workflow started doesn't reach it.

On this page