Module: BrainzLab::Pulse::Propagation
- Defined in:
- lib/brainzlab/pulse/propagation.rb
Overview
Distributed tracing context propagation using W3C Trace Context format www.w3.org/TR/trace-context/
Defined Under Namespace
Classes: Context
Constant Summary collapse
- TRACEPARENT_HEADER =
W3C Trace Context header names
'traceparent'- TRACESTATE_HEADER =
'tracestate'- HTTP_TRACEPARENT =
HTTP header versions (with HTTP_ prefix for Rack env)
'HTTP_TRACEPARENT'- HTTP_TRACESTATE =
'HTTP_TRACESTATE'- B3_TRACE_ID =
Also support B3 format for compatibility
'X-B3-TraceId'- B3_SPAN_ID =
'X-B3-SpanId'- B3_SAMPLED =
'X-B3-Sampled'- B3_PARENT_SPAN_ID =
'X-B3-ParentSpanId'
Class Method Summary collapse
-
.child_context(parent: nil) ⇒ Object
Create a child context for a new span.
-
.clear! ⇒ Object
Clear current context.
-
.current ⇒ Object
Get current propagation context from thread local.
-
.current=(context) ⇒ Object
Set current propagation context.
-
.extract(headers) ⇒ Context?
Extract trace context from incoming HTTP headers (Rack env or plain headers).
-
.extract!(headers) ⇒ Object
Extract and set as current context Returns the context for chaining.
-
.inject(headers, context: nil, format: :w3c) ⇒ Object
Inject trace context into outgoing HTTP headers.
-
.start(trace_id: nil, parent_span_id: nil) ⇒ Object
Create new context and set as current.
Class Method Details
.child_context(parent: nil) ⇒ Object
Create a child context for a new span
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/brainzlab/pulse/propagation.rb', line 122 def child_context(parent: nil) parent ||= current return Context.new unless parent&.valid? Context.new( trace_id: parent.trace_id, parent_span_id: parent.span_id, sampled: parent.sampled, tracestate: parent.tracestate ) end |
.clear! ⇒ Object
Clear current context
78 79 80 |
# File 'lib/brainzlab/pulse/propagation.rb', line 78 def clear! Thread.current[:brainzlab_propagation_context] = nil end |
.current ⇒ Object
Get current propagation context from thread local
60 61 62 |
# File 'lib/brainzlab/pulse/propagation.rb', line 60 def current Thread.current[:brainzlab_propagation_context] end |
.current=(context) ⇒ Object
Set current propagation context
65 66 67 |
# File 'lib/brainzlab/pulse/propagation.rb', line 65 def current=(context) Thread.current[:brainzlab_propagation_context] = context end |
.extract(headers) ⇒ Context?
Extract trace context from incoming HTTP headers (Rack env or plain headers)
106 107 108 109 110 111 112 113 |
# File 'lib/brainzlab/pulse/propagation.rb', line 106 def extract(headers) # Try W3C format first ctx = extract_w3c(headers) return ctx if ctx # Fall back to B3 format extract_b3(headers) end |
.extract!(headers) ⇒ Object
Extract and set as current context Returns the context for chaining
117 118 119 |
# File 'lib/brainzlab/pulse/propagation.rb', line 117 def extract!(headers) self.current = extract(headers) end |
.inject(headers, context: nil, format: :w3c) ⇒ Object
Inject trace context into outgoing HTTP headers
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/brainzlab/pulse/propagation.rb', line 86 def inject(headers, context: nil, format: :w3c) ctx = context || current return headers unless ctx&.valid? case format when :w3c inject_w3c(headers, ctx) when :b3 inject_b3(headers, ctx) when :all inject_w3c(headers, ctx) inject_b3(headers, ctx) end headers end |