|
| 1 | +--- |
| 2 | +title: "Distributed Tracing with Context Propagation in Deno" |
| 3 | +description: "Learn how to implement end-to-end distributed tracing with automatic context propagation in Deno applications. This tutorial covers creating traced services, automatic propagation of trace context, and visualizing distributed traces." |
| 4 | +url: /examples/otel_span_propagation_tutorial/ |
| 5 | +--- |
| 6 | + |
| 7 | +Modern applications are often built as distributed systems with multiple |
| 8 | +services communicating with each other. When debugging issues or optimizing |
| 9 | +performance in these systems, it's crucial to be able to trace requests as they |
| 10 | +flow through different services. This is where distributed tracing comes in. |
| 11 | + |
| 12 | +As of Deno 2.3, the runtime now automatically preserves trace context across |
| 13 | +service boundaries, making end-to-end tracing in distributed systems simpler and |
| 14 | +more powerful. This means that when one service makes a request to another, the |
| 15 | +trace context is automatically propagated, allowing you to see the entire |
| 16 | +request flow as a single trace. |
| 17 | + |
| 18 | +## Setting up a distributed system |
| 19 | + |
| 20 | +Our example system will consist of two parts: |
| 21 | + |
| 22 | +1. A server that provides an API endpoint |
| 23 | +2. A client that makes requests to the server |
| 24 | + |
| 25 | +### The server |
| 26 | + |
| 27 | +We'll set up a simple HTTP server that responds to GET requests with a JSON |
| 28 | +message: |
| 29 | + |
| 30 | +```ts title="server.ts" |
| 31 | +import { trace } from "npm:@opentelemetry/api@1"; |
| 32 | + |
| 33 | +const tracer = trace.getTracer("api-server", "1.0.0"); |
| 34 | + |
| 35 | +// Create a simple API server with Deno.serve |
| 36 | +Deno.serve({ port: 8000 }, (req) => { |
| 37 | + return tracer.startActiveSpan("process-api-request", async (span) => { |
| 38 | + // Add attributes to the span for better context |
| 39 | + span.setAttribute("http.route", "/"); |
| 40 | + span.updateName("GET /"); |
| 41 | + |
| 42 | + // Add a span event to see in traces |
| 43 | + span.addEvent("processing_request", { |
| 44 | + request_id: crypto.randomUUID(), |
| 45 | + timestamp: Date.now(), |
| 46 | + }); |
| 47 | + |
| 48 | + // Simulate processing time |
| 49 | + await new Promise((resolve) => setTimeout(resolve, 50)); |
| 50 | + |
| 51 | + console.log("Server: Processing request in trace context"); |
| 52 | + |
| 53 | + // End the span when we're done |
| 54 | + span.end(); |
| 55 | + |
| 56 | + return new Response(JSON.stringify({ message: "Hello from server!" }), { |
| 57 | + headers: { "Content-Type": "application/json" }, |
| 58 | + }); |
| 59 | + }); |
| 60 | +}); |
| 61 | +``` |
| 62 | + |
| 63 | +### The client |
| 64 | + |
| 65 | +Now, let's create a client that will make requests to our server: |
| 66 | + |
| 67 | +```ts title="client.ts" |
| 68 | +import { SpanStatusCode, trace } from "npm:@opentelemetry/api@1"; |
| 69 | + |
| 70 | +const tracer = trace.getTracer("api-client", "1.0.0"); |
| 71 | + |
| 72 | +// Create a parent span for the client operation |
| 73 | +await tracer.startActiveSpan("call-api", async (parentSpan) => { |
| 74 | + try { |
| 75 | + console.log("Client: Starting API call"); |
| 76 | + |
| 77 | + // The fetch call inside this span will automatically: |
| 78 | + // 1. Create a child span for the fetch operation |
| 79 | + // 2. Inject the trace context into the outgoing request headers |
| 80 | + const response = await fetch("http://localhost:8000/"); |
| 81 | + const data = await response.json(); |
| 82 | + |
| 83 | + console.log(`Client: Received response: ${JSON.stringify(data)}`); |
| 84 | + |
| 85 | + parentSpan.addEvent("received_response", { |
| 86 | + status: response.status, |
| 87 | + timestamp: Date.now(), |
| 88 | + }); |
| 89 | + } catch (error) { |
| 90 | + console.error("Error calling API:", error); |
| 91 | + if (error instanceof Error) { |
| 92 | + parentSpan.recordException(error); |
| 93 | + } |
| 94 | + parentSpan.setStatus({ |
| 95 | + code: SpanStatusCode.ERROR, |
| 96 | + message: error instanceof Error ? error.message : String(error), |
| 97 | + }); |
| 98 | + } finally { |
| 99 | + parentSpan.end(); |
| 100 | + } |
| 101 | +}); |
| 102 | +``` |
| 103 | + |
| 104 | +## Tracing with OpenTelemetry |
| 105 | + |
| 106 | +Both the client and server code already include basic OpenTelemetry |
| 107 | +instrumentation: |
| 108 | + |
| 109 | +1. Create a tracer - both files create a tracer using `trace.getTracer()` with a |
| 110 | + name and version. |
| 111 | + |
| 112 | +2. Create spans - We use `startActiveSpan()` to create spans that represent |
| 113 | + operations. |
| 114 | + |
| 115 | +3. Add context - We add attributes and events to spans to provide more context. |
| 116 | + |
| 117 | +4. Ending spans - We make sure to end spans when operations are complete. |
| 118 | + |
| 119 | +## Automatic context propagation |
| 120 | + |
| 121 | +The magic happens when the client makes a request to the server. In the client |
| 122 | +code there is a fetch call to the server: |
| 123 | + |
| 124 | +```ts |
| 125 | +const response = await fetch("http://localhost:8000/"); |
| 126 | +``` |
| 127 | + |
| 128 | +Since this fetch call happens inside an active span, Deno automatically creates |
| 129 | +a child span for the fetch operation and Injects the trace context into the |
| 130 | +outgoing request headers. |
| 131 | + |
| 132 | +When the server receives this request, Deno extracts the trace context from the |
| 133 | +request headers and establishes the server span as a child of the client's span. |
| 134 | + |
| 135 | +## Running the example |
| 136 | + |
| 137 | +To run this example, first, start the server, giving your otel service a name: |
| 138 | + |
| 139 | +```sh |
| 140 | +OTEL_DENO=true OTEL_SERVICE_NAME=server deno run --unstable-otel --allow-net server.ts |
| 141 | +``` |
| 142 | + |
| 143 | +Then, in another terminal, run the client, giving the client a different service |
| 144 | +name to make observing the propagation clearer: |
| 145 | + |
| 146 | +```sh |
| 147 | +OTEL_DENO=true OTEL_SERVICE_NAME=client deno run --unstable-otel --allow-net client.ts |
| 148 | +``` |
| 149 | + |
| 150 | +You should see: |
| 151 | + |
| 152 | +1. The client logs "Client: Starting API call" |
| 153 | +2. The server logs "Server: Processing request in trace context" |
| 154 | +3. The client logs the response received from the server |
| 155 | + |
| 156 | +## Viewing traces |
| 157 | + |
| 158 | +To actually see the traces, you'll need an OpenTelemetry collector and a |
| 159 | +visualization tool, |
| 160 | +[for example Grafana Tempo](/runtime/fundamentals/open_telemetry/#quick-start). |
| 161 | + |
| 162 | +When you visualize the traces, you'll see: |
| 163 | + |
| 164 | +1. A parent span from the client |
| 165 | +2. Connected to a child span for the HTTP request |
| 166 | +3. Connected to a span from the server |
| 167 | +4. All as part of a single trace! |
| 168 | + |
| 169 | +For example, in Grafana, the trace visualization may look like this: |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | +🦕 Now that you understand distributed tracing with Deno, you could extend this |
| 174 | +to more complex systems with multiple services and async operations. |
| 175 | + |
| 176 | +With Deno's automatic context propagation, implementing distributed tracing in |
| 177 | +your applications has never been easier! |
0 commit comments