|
| 1 | +# Context Propagation |
| 2 | + |
| 3 | +This guide explains how to propagate trace context between different execution contexts within your application using `Traces.current_context` and `Traces.with_context`. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The `traces` library provides two complementary approaches for managing trace context: |
| 8 | + |
| 9 | +- **Local context propagation** (`Traces.current_context` / `Traces.with_context`): For passing context between execution contexts within the same process (threads, fibers, async tasks). |
| 10 | +- **Distributed context propagation** (`Traces.inject` / `Traces.extract`): For transmitting context across process and service boundaries via serialization (HTTP headers, message metadata, etc.). |
| 11 | + |
| 12 | +There is a legacy interface `Traces.trace_context` and `Traces.trace_context=` but you should prefer to use the new methods outlined above. |
| 13 | + |
| 14 | +## Local Context Propagation |
| 15 | + |
| 16 | +Local context propagation involves passing trace context between different execution contexts within the same process. This is essential for maintaining trace continuity when code execution moves between threads, fibers, async tasks, or other concurrent execution contexts. Unlike distributed propagation which requires serialization over network boundaries, local propagation uses Context objects directly. |
| 17 | + |
| 18 | +### Capturing the Current Context |
| 19 | + |
| 20 | +Use `Traces.current_context` to capture the current trace context as a Context object: |
| 21 | + |
| 22 | +~~~ ruby |
| 23 | +current_context = Traces.current_context |
| 24 | +# Returns a Traces::Context object or nil if no active trace |
| 25 | +~~~ |
| 26 | + |
| 27 | +### Using the Context |
| 28 | + |
| 29 | +Use `Traces.with_context(context)` to execute code within a specific trace context: |
| 30 | + |
| 31 | +~~~ ruby |
| 32 | +# With block (automatic restoration): |
| 33 | +Traces.with_context(context) do |
| 34 | + # Code runs with the specified context. |
| 35 | +end |
| 36 | + |
| 37 | +# Without block (permanent switch): |
| 38 | +Traces.with_context(context) |
| 39 | +# Context remains active. |
| 40 | +~~~ |
| 41 | + |
| 42 | +### Use Cases |
| 43 | + |
| 44 | +#### Thread-Safe Context Propagation |
| 45 | + |
| 46 | +When spawning background threads, you often want them to inherit the current trace context: |
| 47 | + |
| 48 | +~~~ ruby |
| 49 | +require 'traces' |
| 50 | + |
| 51 | +# Main thread has active tracing |
| 52 | +Traces.trace("main_operation") do |
| 53 | + # Capture current context before spawning thread: |
| 54 | + current_context = Traces.current_context |
| 55 | + |
| 56 | + # Spawn background thread: |
| 57 | + Thread.new do |
| 58 | + # Restore context in the new thread: |
| 59 | + Traces.with_context(current_context) do |
| 60 | + # This thread now has the same trace context as main thread: |
| 61 | + Traces.trace("background_work") do |
| 62 | + perform_heavy_computation |
| 63 | + end |
| 64 | + end |
| 65 | + end.join |
| 66 | +end |
| 67 | +~~~ |
| 68 | + |
| 69 | +#### Fiber-Based Async Operations |
| 70 | + |
| 71 | +For fiber-based concurrency (like in async frameworks), context propagation ensures trace continuity: |
| 72 | + |
| 73 | +~~~ ruby |
| 74 | +require 'traces' |
| 75 | + |
| 76 | +Traces.trace("main_operation") do |
| 77 | + current_context = Traces.current_context |
| 78 | + |
| 79 | + # Create fiber for async work: |
| 80 | + fiber = Fiber.new do |
| 81 | + Traces.with_context(current_context) do |
| 82 | + # Fiber inherits the trace context: |
| 83 | + Traces.trace("fiber_work") do |
| 84 | + perform_async_operation |
| 85 | + end |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + fiber.resume |
| 90 | +end |
| 91 | +~~~ |
| 92 | + |
| 93 | +### Context Propagation vs. New Spans |
| 94 | + |
| 95 | +Remember that context propagation maintains the same trace, while `trace()` creates new spans: |
| 96 | + |
| 97 | +~~~ ruby |
| 98 | +Traces.trace("parent") do |
| 99 | + context = Traces.current_context |
| 100 | + |
| 101 | + Thread.new do |
| 102 | + # This maintains the same trace context: |
| 103 | + Traces.with_context(context) do |
| 104 | + # This creates a NEW span within the same trace: |
| 105 | + Traces.trace("child") do |
| 106 | + # Child span, same trace as parent |
| 107 | + end |
| 108 | + end |
| 109 | + end |
| 110 | +end |
| 111 | +~~~ |
| 112 | + |
| 113 | +## Distributed Context Propagation |
| 114 | + |
| 115 | +Distributed context propagation involves transmitting trace context across process and service boundaries. Unlike local propagation which works within a single process, distributed propagation requires serializing context data and transmitting it over network protocols. |
| 116 | + |
| 117 | +### Injecting Context into Headers |
| 118 | + |
| 119 | +Use `Traces.inject(headers, context = nil)` to add W3C Trace Context headers to a headers hash for transmission over network boundaries: |
| 120 | + |
| 121 | +~~~ ruby |
| 122 | +require 'traces' |
| 123 | + |
| 124 | +# Capture current context: |
| 125 | +context = Traces.current_context |
| 126 | +headers = {'Content-Type' => 'application/json'} |
| 127 | + |
| 128 | +# Inject trace headers: |
| 129 | +Traces.inject(headers, context) |
| 130 | +# headers now contains: {'Content-Type' => '...', 'traceparent' => '00-...'} |
| 131 | + |
| 132 | +# Or use current context by default: |
| 133 | +Traces.inject(headers) # Uses current trace context |
| 134 | +~~~ |
| 135 | + |
| 136 | +### Extracting Context from Headers |
| 137 | + |
| 138 | +Use `Traces.extract(headers)` to extract trace context from W3C headers received over the network: |
| 139 | + |
| 140 | +~~~ ruby |
| 141 | +# Receive headers from incoming request: |
| 142 | +incoming_headers = request.headers |
| 143 | + |
| 144 | +# Extract context: |
| 145 | +context = Traces.extract(incoming_headers) |
| 146 | +# Returns a Traces::Context object or nil if no valid context |
| 147 | + |
| 148 | +# Use the extracted context: |
| 149 | +if context |
| 150 | + Traces.with_context(context) do |
| 151 | + # Process request with distributed trace context |
| 152 | + end |
| 153 | +end |
| 154 | +~~~ |
| 155 | + |
| 156 | +### Use Cases |
| 157 | + |
| 158 | +#### Outgoing HTTP Requests |
| 159 | + |
| 160 | +~~~ ruby |
| 161 | +require 'traces' |
| 162 | + |
| 163 | +class ApiClient |
| 164 | + def make_request(endpoint, data) |
| 165 | + Traces.trace("api_request", attributes: {endpoint: endpoint}) do |
| 166 | + headers = { |
| 167 | + 'Content-Type' => 'application/json' |
| 168 | + } |
| 169 | + |
| 170 | + # Add trace context to outgoing request: |
| 171 | + Traces.inject(headers) |
| 172 | + |
| 173 | + http_client.post(endpoint, |
| 174 | + body: data.to_json, |
| 175 | + headers: headers |
| 176 | + ) |
| 177 | + end |
| 178 | + end |
| 179 | +end |
| 180 | +~~~ |
| 181 | + |
| 182 | +#### Incoming HTTP Requests |
| 183 | + |
| 184 | +~~~ ruby |
| 185 | +require 'traces' |
| 186 | + |
| 187 | +class WebController |
| 188 | + def handle_request(request) |
| 189 | + # Extract trace context from incoming headers: |
| 190 | + context = Traces.extract(request.headers) |
| 191 | + |
| 192 | + # Process request with inherited context: |
| 193 | + if context |
| 194 | + Traces.with_context(context) do |
| 195 | + Traces.trace("web_request", attributes: { |
| 196 | + path: request.path, |
| 197 | + method: request.method |
| 198 | + }) do |
| 199 | + process_business_logic |
| 200 | + end |
| 201 | + end |
| 202 | + else |
| 203 | + Traces.trace("web_request", attributes: { |
| 204 | + path: request.path, |
| 205 | + method: request.method |
| 206 | + }) do |
| 207 | + process_business_logic |
| 208 | + end |
| 209 | + end |
| 210 | + end |
| 211 | +end |
| 212 | +~~~ |
0 commit comments