|
| 1 | +# Langfuse Tracing |
| 2 | + |
| 3 | +This sample shows the recommended way to get Temporal workflow traces into |
| 4 | +[Langfuse](https://langfuse.com/): Temporal's |
| 5 | +[`OpenTelemetryPlugin`](https://python.temporal.io/temporalio.contrib.opentelemetry.html) |
| 6 | +plus a standard OTLP/HTTP exporter pointed at Langfuse's native OpenTelemetry |
| 7 | +endpoint. No Langfuse SDK or Langfuse-specific plugin is involved, workflow |
| 8 | +code stays deterministic and sandboxed, and traces are correctly nested, |
| 9 | +correctly typed, and duplicate-free across replay and worker restarts. |
| 10 | + |
| 11 | +Contents: |
| 12 | + |
| 13 | +- **[ticket_triage/](ticket_triage/)** — the recommended pattern: an LLM |
| 14 | + ticket-triage workflow (two LLM activities, one plain activity, one human |
| 15 | + approval delivered as a workflow update). |
| 16 | +- **[verify_trace.py](verify_trace.py)** — checks a trace via the Langfuse |
| 17 | + public API: whole-tree equality, observation types, token usage, and |
| 18 | + no-duplicates. |
| 19 | +- **[langfuse/docker-compose.yml](langfuse/docker-compose.yml)** — pinned |
| 20 | + self-hosted Langfuse with org/project/API keys provisioned headlessly. |
| 21 | +- **[telemetry.py](telemetry.py)** — the OpenTelemetry wiring (replay-safe |
| 22 | + tracer provider, OTLP exporter with Langfuse auth, LLM instrumentation). |
| 23 | + |
| 24 | +## Prerequisites |
| 25 | + |
| 26 | +- Docker (for Langfuse), a local Temporal server |
| 27 | + (`temporal server start-dev`), and `uv`. |
| 28 | +- An OpenAI-compatible LLM endpoint: either a real `OPENAI_API_KEY`, or any |
| 29 | + OpenAI-compatible gateway via `OPENAI_BASE_URL`. |
| 30 | + |
| 31 | +## Run it |
| 32 | + |
| 33 | +```bash |
| 34 | +# 1. Start Langfuse (first pull takes a few minutes) |
| 35 | +cd langfuse_tracing/langfuse |
| 36 | +docker compose up -d |
| 37 | +curl -sf http://localhost:3000/api/public/health # repeat until {"status":"OK",...} |
| 38 | +# UI: http://localhost:3000 — login demo@temporal.io / langfuse-demo-pw-1 |
| 39 | + |
| 40 | +# 2. Install dependencies and set environment (repo root) |
| 41 | +cd ../.. |
| 42 | +uv sync --group langfuse-tracing |
| 43 | +cp langfuse_tracing/.env.example langfuse_tracing/.env # edit the LLM settings |
| 44 | +set -a; source langfuse_tracing/.env; set +a |
| 45 | + |
| 46 | +# 3. Run the sample (two terminals, same environment) |
| 47 | +uv run python -m langfuse_tracing.ticket_triage.worker |
| 48 | +uv run python -m langfuse_tracing.ticket_triage.starter |
| 49 | + |
| 50 | +# 4. Verify the trace through the Langfuse API (uses the printed trace ID) |
| 51 | +uv run python -m langfuse_tracing.verify_trace --trace-id <printed trace id> |
| 52 | +``` |
| 53 | + |
| 54 | +The starter prints a direct link to the trace in the Langfuse UI. You should |
| 55 | +see one trace shaped like this (types as Langfuse derives them): |
| 56 | + |
| 57 | +``` |
| 58 | +ticket-triage SPAN (root; session/user/tags) |
| 59 | +├─ StartWorkflow:TicketTriageWorkflow SPAN |
| 60 | +│ └─ RunWorkflow:TicketTriageWorkflow SPAN |
| 61 | +│ ├─ triage SPAN (custom span from workflow code) |
| 62 | +│ │ ├─ StartActivity:classify_ticket → RunActivity:classify_ticket |
| 63 | +│ │ │ └─ ChatCompletion GENERATION (model, tokens, cost) |
| 64 | +│ │ └─ StartActivity:lookup_account → RunActivity:lookup_account |
| 65 | +│ └─ StartActivity:draft_reply → RunActivity:draft_reply |
| 66 | +│ └─ ChatCompletion GENERATION |
| 67 | +└─ StartWorkflowUpdate:approve SPAN |
| 68 | + ├─ ValidateUpdate:approve SPAN |
| 69 | + └─ HandleUpdate:approve SPAN |
| 70 | +``` |
| 71 | + |
| 72 | +## Prove the replay-safety claims |
| 73 | + |
| 74 | +Durable execution means workflow code re-executes (replays) on worker |
| 75 | +restarts and cache evictions. These two experiments show tracing is |
| 76 | +unaffected — each run still verifies cleanly with the same tree shape and no |
| 77 | +duplicate observations: |
| 78 | + |
| 79 | +```bash |
| 80 | +# Replay stress: disable the workflow cache so EVERY workflow task replays |
| 81 | +# the workflow from the start of history. |
| 82 | +uv run python -m langfuse_tracing.ticket_triage.worker --replay-stress |
| 83 | +uv run python -m langfuse_tracing.ticket_triage.starter |
| 84 | +uv run python -m langfuse_tracing.verify_trace --trace-id <printed trace id> |
| 85 | + |
| 86 | +# Worker restart mid-workflow: the starter waits 20s before sending the |
| 87 | +# approval. Give the triage activities a few seconds to finish, then kill the |
| 88 | +# worker while the workflow durably awaits approval; start a new worker and |
| 89 | +# watch the workflow (and its trace) complete cleanly. |
| 90 | +uv run python -m langfuse_tracing.ticket_triage.starter --pause-before-approval 20 |
| 91 | +# ... after ~5s, ctrl+c the worker, then start it again in another terminal |
| 92 | +uv run python -m langfuse_tracing.verify_trace --trace-id <printed trace id> |
| 93 | +``` |
| 94 | + |
| 95 | +## Where spans come from |
| 96 | + |
| 97 | +| Span | Emitted by | Where it runs | |
| 98 | +|---|---|---| |
| 99 | +| `ticket-triage` (root) + `langfuse.*` trace attributes | starter code | starter | |
| 100 | +| `StartWorkflow:*`, `StartWorkflowUpdate:*` | `OpenTelemetryPlugin` | starter (client side) | |
| 101 | +| `RunWorkflow:*`, `StartActivity:*`, `ValidateUpdate:*`, `HandleUpdate:*` | `OpenTelemetryPlugin` | worker (workflow) | |
| 102 | +| `triage` | plain OpenTelemetry API in workflow code | worker (workflow) | |
| 103 | +| `RunActivity:*` | `OpenTelemetryPlugin` | worker (activity) | |
| 104 | +| `ChatCompletion` / `chat <model>` GENERATIONs | OpenAI auto-instrumentation | worker (activity) | |
| 105 | + |
| 106 | +## Where tracing works |
| 107 | + |
| 108 | +| Location | Works? | Notes | |
| 109 | +|---|---|---| |
| 110 | +| Activity bodies | ✅ | Plain OpenTelemetry + any auto-instrumentation, no restrictions. This is where LLM calls (and their GENERATION spans) belong. | |
| 111 | +| Workflow bodies | ✅ | Plain OpenTelemetry APIs are replay-safe under the plugin: deterministic span IDs, no re-export on replay. Spans export when they end; the `RunWorkflow` span exports when the run completes. | |
| 112 | +| Signal/query/update handlers | ✅ | Handled by the plugin automatically (`HandleUpdate:*` etc.). | |
| 113 | +| Client / starter code | ✅ | Standard OpenTelemetry; put Langfuse trace-level attributes on your root span. | |
| 114 | + |
| 115 | +## LLM instrumentation flavors |
| 116 | + |
| 117 | +`LLM_INSTRUMENTATION` selects how OpenAI calls are instrumented (both are |
| 118 | +verified against Langfuse by this sample): |
| 119 | + |
| 120 | +| | `openinference` (default) | `openai-v2` | |
| 121 | +|---|---|---| |
| 122 | +| Package | `openinference-instrumentation-openai` | `opentelemetry-instrumentation-openai-v2` | |
| 123 | +| Semantic conventions | OpenInference | OpenTelemetry GenAI (`gen_ai.*`) | |
| 124 | +| GENERATION type, model, token usage, cost | ✅ | ✅ | |
| 125 | +| Prompt/completion content | ✅ by default | Requires `OTEL_SEMCONV_STABILITY_OPT_IN=gen_ai_latest_experimental` and `OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=span_only` | |
| 126 | +| GENERATION span name | `ChatCompletion` | `chat <model>` | |
| 127 | + |
| 128 | +## Operational notes |
| 129 | + |
| 130 | +- Langfuse's OTLP endpoint is HTTP-only — this sample uses |
| 131 | + `opentelemetry-exporter-otlp-proto-http` (the gRPC exporter will not work). |
| 132 | +- Short-lived processes must flush: the starter and worker call |
| 133 | + `force_flush()` on exit (see `telemetry.py`). |
| 134 | +- Use a fresh workflow ID per run: the starter reports it as the Langfuse |
| 135 | + session ID, so each run groups cleanly in the Sessions view. (The trace |
| 136 | + itself is keyed by the starter's root span, which is new on every run.) |
| 137 | +- `OTEL_SDK_DISABLED=true` turns off export without code changes. |
| 138 | +- Ingestion is asynchronous; `verify_trace.py` polls until the trace is |
| 139 | + stable. |
| 140 | + |
| 141 | +## Tests |
| 142 | + |
| 143 | +`tests/langfuse_tracing/` runs without Langfuse, Docker, or an LLM: mocked |
| 144 | +activities, an in-memory span exporter, a worker with the workflow cache |
| 145 | +disabled, whole-tree span assertions, and a `Replayer` pass asserting that |
| 146 | +replaying the finished workflow's history emits zero new spans. |
| 147 | + |
| 148 | +```bash |
| 149 | +uv run --group langfuse-tracing pytest tests/langfuse_tracing -v |
| 150 | +``` |
| 151 | + |
| 152 | +## Using this outside samples-python |
| 153 | + |
| 154 | +The sample is self-contained: copy the `langfuse_tracing/` directory, change |
| 155 | +the absolute imports (`langfuse_tracing.ticket_triage.activities` → |
| 156 | +`ticket_triage.activities` or similar), and install the dependencies listed |
| 157 | +under `langfuse-tracing` in this repo's `pyproject.toml`. |
0 commit comments