Module: RailsOtelContext::RequestContext
- Defined in:
- lib/rails_otel_context/request_context.rb
Overview
Thread-local storage for request-scoped context that gets propagated to all spans within a request. Uses raw Thread.current for minimal overhead —no object allocation, no mutex, ~5ns per read/write.
Lifecycle:
1. Railtie's around_action sets controller + action at request start
2. CallContextProcessor reads them on every child span's on_start
3. around_action's ensure block clears them when the request ends
Thread safety: each Puma thread has its own slot — no sharing, no contention.
Constant Summary collapse
- CONTROLLER_KEY =
:_rails_otel_ctx_controller- ACTION_KEY =
:_rails_otel_ctx_action- QUERY_COUNT_KEY =
:_rails_otel_ctx_qcount
Class Method Summary collapse
- .action ⇒ Object
- .clear! ⇒ Object
- .controller ⇒ Object
-
.fetch ⇒ Object
Returns [controller, action] in a single Thread.current access.
- .set(controller:, action:) ⇒ Object
Class Method Details
.action ⇒ Object
36 37 38 |
# File 'lib/rails_otel_context/request_context.rb', line 36 def action Thread.current[ACTION_KEY] end |
.clear! ⇒ Object
40 41 42 43 44 |
# File 'lib/rails_otel_context/request_context.rb', line 40 def clear! Thread.current[CONTROLLER_KEY] = nil Thread.current[ACTION_KEY] = nil Thread.current[QUERY_COUNT_KEY] = nil end |
.controller ⇒ Object
32 33 34 |
# File 'lib/rails_otel_context/request_context.rb', line 32 def controller Thread.current[CONTROLLER_KEY] end |
.fetch ⇒ Object
Returns [controller, action] in a single Thread.current access.
27 28 29 30 |
# File 'lib/rails_otel_context/request_context.rb', line 27 def fetch t = Thread.current [t[CONTROLLER_KEY], t[ACTION_KEY]] end |
.set(controller:, action:) ⇒ Object
20 21 22 23 24 |
# File 'lib/rails_otel_context/request_context.rb', line 20 def set(controller:, action:) Thread.current[CONTROLLER_KEY] = controller Thread.current[ACTION_KEY] = action Thread.current[QUERY_COUNT_KEY] = nil end |