Module: RailsOtelContext::RequestContext
- Defined in:
- lib/rails_otel_context/request_context.rb
Overview
Thread-local storage for request/job-scoped context propagated to all spans. Uses raw Thread.current — no object allocation, no mutex, ~5ns per read/write.
Controller 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
Job lifecycle:
1. Railtie's around_perform sets job at job start
2. CallContextProcessor reads it on every child span's on_start
3. around_perform's ensure block clears it when the job ends
Thread safety: each Puma/Sidekiq thread owns its slot — no sharing, no contention.
Constant Summary collapse
- CONTROLLER_KEY =
:_rails_otel_ctx_controller- ACTION_KEY =
:_rails_otel_ctx_action- JOB_KEY =
:_rails_otel_ctx_job- QUERY_COUNT_KEY =
:_rails_otel_ctx_qcount
Class Method Summary collapse
- .action ⇒ Object
- .clear! ⇒ Object
- .clear_job! ⇒ Object
- .controller ⇒ Object
-
.fetch ⇒ Object
Returns [controller, action] in a single Thread.current access.
- .job ⇒ Object
- .set(controller:, action:) ⇒ Object
- .set_job(job_class:) ⇒ Object
Class Method Details
.action ⇒ Object
46 47 48 |
# File 'lib/rails_otel_context/request_context.rb', line 46 def action Thread.current[ACTION_KEY] end |
.clear! ⇒ Object
54 55 56 57 58 59 |
# File 'lib/rails_otel_context/request_context.rb', line 54 def clear! Thread.current[CONTROLLER_KEY] = nil Thread.current[ACTION_KEY] = nil Thread.current[JOB_KEY] = nil Thread.current[QUERY_COUNT_KEY] = nil end |
.clear_job! ⇒ Object
61 62 63 64 |
# File 'lib/rails_otel_context/request_context.rb', line 61 def clear_job! Thread.current[JOB_KEY] = nil Thread.current[QUERY_COUNT_KEY] = nil end |
.controller ⇒ Object
42 43 44 |
# File 'lib/rails_otel_context/request_context.rb', line 42 def controller Thread.current[CONTROLLER_KEY] end |
.fetch ⇒ Object
Returns [controller, action] in a single Thread.current access.
37 38 39 40 |
# File 'lib/rails_otel_context/request_context.rb', line 37 def fetch t = Thread.current [t[CONTROLLER_KEY], t[ACTION_KEY]] end |
.job ⇒ Object
50 51 52 |
# File 'lib/rails_otel_context/request_context.rb', line 50 def job Thread.current[JOB_KEY] end |
.set(controller:, action:) ⇒ Object
25 26 27 28 29 |
# File 'lib/rails_otel_context/request_context.rb', line 25 def set(controller:, action:) Thread.current[CONTROLLER_KEY] = controller Thread.current[ACTION_KEY] = action Thread.current[QUERY_COUNT_KEY] = nil end |
.set_job(job_class:) ⇒ Object
31 32 33 34 |
# File 'lib/rails_otel_context/request_context.rb', line 31 def set_job(job_class:) Thread.current[JOB_KEY] = job_class Thread.current[QUERY_COUNT_KEY] = nil end |