|
| 1 | +# End-to-End OpenTelemetry Tracing with Restate |
| 2 | + |
| 3 | +This example demonstrates distributed tracing across a fictional multi-tier system: |
| 4 | + |
| 5 | +``` |
| 6 | +┌──────────┐ ┌─────────────┐ ┌─────────────────┐ ┌────────────┐ |
| 7 | +│ Client │────▶│ Restate │────▶│ Greeter Service │────▶│ Downstream │ |
| 8 | +│ App │ │ Server │ │ (SDK/Node) │ │ Service │ |
| 9 | +└──────────┘ └─────────────┘ └─────────────────┘ └────────────┘ |
| 10 | + │ │ │ │ |
| 11 | + │ │ │ │ |
| 12 | + ▼ ▼ ▼ ▼ |
| 13 | +┌────────────────────────────────────────────────────────────────────────┐ |
| 14 | +│ Jaeger │ |
| 15 | +└────────────────────────────────────────────────────────────────────────┘ |
| 16 | +``` |
| 17 | + |
| 18 | +**What gets traced:** |
| 19 | + |
| 20 | +1. **Client App** - Creates the root span and injects W3C trace context into the Restate request |
| 21 | +2. **Restate Server** - Receives trace context, emits spans for ingress requests and handler invocations |
| 22 | +3. **Greeter Service** - SDK handler that creates custom spans and propagates context to downstream calls |
| 23 | +4. **Downstream Service** - Receives and logs the propagated trace headers |
| 24 | + |
| 25 | +## Prerequisites |
| 26 | + |
| 27 | +- Node.js 18+ |
| 28 | +- Docker (for Jaeger) |
| 29 | + |
| 30 | +## Setup |
| 31 | + |
| 32 | +### 1. Start Jaeger |
| 33 | + |
| 34 | +```bash |
| 35 | +docker run -d --name jaeger \ |
| 36 | + -p 4317:4317 \ |
| 37 | + -p 16686:16686 \ |
| 38 | + jaegertracing/all-in-one:latest |
| 39 | +``` |
| 40 | + |
| 41 | +Jaeger UI will be available at `http://localhost:16686` |
| 42 | + |
| 43 | +### 2. Install dependencies |
| 44 | + |
| 45 | +```bash |
| 46 | +npm install |
| 47 | +``` |
| 48 | + |
| 49 | +### 3. Start Restate Server with tracing enabled |
| 50 | + |
| 51 | +```bash |
| 52 | +npx @restatedev/restate-server --tracing-endpoint http://localhost:4317 |
| 53 | +``` |
| 54 | + |
| 55 | +### 4. Start the downstream service (terminal 1) |
| 56 | + |
| 57 | +```bash |
| 58 | +npm run downstream |
| 59 | +``` |
| 60 | + |
| 61 | +### 5. Start the Greeter service (terminal 2) |
| 62 | + |
| 63 | +```bash |
| 64 | +npm run service |
| 65 | +``` |
| 66 | + |
| 67 | +### 6. Register the service with Restate |
| 68 | + |
| 69 | +```bash |
| 70 | +npx @restatedev/restate deployments register http://localhost:9080 |
| 71 | +``` |
| 72 | + |
| 73 | +### 7. Run the client |
| 74 | + |
| 75 | +```bash |
| 76 | +npm run client Alice |
| 77 | +``` |
| 78 | + |
| 79 | +## Viewing Traces |
| 80 | + |
| 81 | +After running the client, you'll see output like: |
| 82 | + |
| 83 | +``` |
| 84 | +Root Trace ID: abc123... |
| 85 | +View in Jaeger: `http://localhost:16686/trace/abc123...` |
| 86 | +``` |
| 87 | + |
| 88 | +Open the Jaeger link to see the complete distributed trace spanning all four components. |
| 89 | + |
| 90 | +## What You'll See in Jaeger |
| 91 | + |
| 92 | +The trace will show spans from all four services: |
| 93 | + |
| 94 | +- **client-app**: The root `client-request` span |
| 95 | +- **Greeter**: Restate server spans for ingress, invoke, and journal operations |
| 96 | +- **restate-greeter-service**: Custom `Greeter.greet` span with events |
| 97 | +- **downstream-service**: `handle-request` span (may show errors due to 50% failure rate) |
| 98 | + |
| 99 | +## Key Pattern: Extracting Trace Context in TypeScript SDK |
| 100 | + |
| 101 | +The Restate server propagates W3C trace context to handlers via HTTP headers. In the TypeScript SDK, you need to manually extract this from `ctx.request().attemptHeaders`. |
| 102 | + |
| 103 | +**Why not use Node.js auto-instrumentation?** Unlike Java and Go, Node.js does have OTEL auto-instrumentation packages (e.g. `@opentelemetry/auto-instrumentations-node`). However, they operate at the raw HTTP transport layer, which Restate wraps internally. More importantly, Restate provides durable execution — a handler may be invoked multiple times due to retries. Extracting trace context from `ctx.request().attemptHeaders` ensures exactly one span per logical invocation, correctly positioned in the trace hierarchy regardless of retries. |
| 104 | + |
| 105 | +```typescript |
| 106 | +import { context, propagation, type Context } from "@opentelemetry/api"; |
| 107 | + |
| 108 | +function extractTraceContext(ctx: restate.Context): Context { |
| 109 | + const headers = ctx.request().attemptHeaders; |
| 110 | + // TextMapGetter lets any propagator format (W3C, B3, Jaeger…) work automatically |
| 111 | + return propagation.extract(context.active(), headers, { |
| 112 | + get: (carrier, key) => { |
| 113 | + const val = carrier.get(key); |
| 114 | + return Array.isArray(val) ? val[0] : (val ?? undefined); |
| 115 | + }, |
| 116 | + keys: (carrier) => [...carrier.keys()], |
| 117 | + }); |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +Then run your handler logic within that context: |
| 122 | + |
| 123 | +```typescript |
| 124 | +const traceContext = extractTraceContext(ctx); |
| 125 | +return context.with(traceContext, () => { |
| 126 | + const span = tracer.startSpan("MyHandler"); |
| 127 | + // ... your logic here, span is now a child of Restate's span |
| 128 | +}); |
| 129 | +``` |
| 130 | + |
| 131 | +## Files |
| 132 | + |
| 133 | +- `src/client.ts` - Client app that initiates traced requests |
| 134 | +- `src/restate-service.ts` - Restate Greeter service with OpenTelemetry instrumentation |
| 135 | +- `src/downstream.ts` - HTTP server with tracing and random failure rate |
0 commit comments