|
| 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 | + |
| 100 | + |
| 101 | +## Key Pattern: Extracting Trace Context in TypeScript SDK |
| 102 | + |
| 103 | +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`: |
| 104 | + |
| 105 | +```typescript |
| 106 | +import { context, propagation, Context } from "@opentelemetry/api"; |
| 107 | + |
| 108 | +function extractTraceContext(ctx: restate.Context): Context { |
| 109 | + const headers = ctx.request().attemptHeaders; |
| 110 | + const traceparent = headers.get("traceparent"); |
| 111 | + const tracestate = headers.get("tracestate"); |
| 112 | + |
| 113 | + const carrier: Record<string, string> = {}; |
| 114 | + if (traceparent) { |
| 115 | + carrier["traceparent"] = Array.isArray(traceparent) |
| 116 | + ? traceparent[0] |
| 117 | + : traceparent; |
| 118 | + } |
| 119 | + if (tracestate) { |
| 120 | + carrier["tracestate"] = Array.isArray(tracestate) |
| 121 | + ? tracestate[0] |
| 122 | + : tracestate; |
| 123 | + } |
| 124 | + return propagation.extract(context.active(), carrier); |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +Then run your handler logic within that context: |
| 129 | + |
| 130 | +```typescript |
| 131 | +const traceContext = extractTraceContext(ctx); |
| 132 | +return context.with(traceContext, () => { |
| 133 | + const span = tracer.startSpan("MyHandler"); |
| 134 | + // ... your logic here, span is now a child of Restate's span |
| 135 | +}); |
| 136 | +``` |
| 137 | + |
| 138 | +## Files |
| 139 | + |
| 140 | +- `src/client.ts` - Client app that initiates traced requests |
| 141 | +- `src/restate-service.ts` - Restate Greeter service with OpenTelemetry instrumentation |
| 142 | +- `src/downstream.ts` - HTTP server with tracing and random failure rate |
0 commit comments