ruby
36 lines · 7 steps
Per-request context with CurrentAttributes in Rails
A Rack middleware, a Current model, and tagged logging combine to attach a request ID to every log line and make it globally reachable.
Explained by
highlit
1module RequestTagging
2 class Middleware
3 def initialize(app)
4 @app = app
5 end
6
7 def call(env)
8 request_id = env["action_dispatch.request_id"]
9 Current.request_id = request_id
10 @app.call(env)
11 ensure
12 Current.reset
13 end
14 end
15end
16
17class Current < ActiveSupport::CurrentAttributes
18 attribute :request_id
19end
20
21Rails.application.configure do
22 config.log_tags = [
23 :request_id,
24 ->(req) { "ip=#{req.remote_ip}" },
25 ->(req) { "method=#{req.request_method}" }
26 ]
27
28 config.logger = ActiveSupport::TaggedLogging.new(
29 ActiveSupport::Logger.new($stdout)
30 )
31
32 config.middleware.insert_after(
33 ActionDispatch::RequestId,
34 RequestTagging::Middleware
35 )
36end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1CurrentAttributes gives you request-scoped global state that stays isolated across concurrent requests.
- 2Wrapping mutable per-request state in an ensure block guarantees cleanup even when the request raises.
- 3Tagged logging plus a shared request ID lets you trace every log line back to a single request.
Related explainers
ruby
require "shellwords" require "open3" module Backup
Building safe shell commands in Ruby
shell-out
subprocess
command-injection
Intermediate
7 steps
ruby
class UserAgentParser BROWSERS = [ [/Edg\/([\d.]+)/, "Edge"], [/OPR\/([\d.]+)/, "Opera"],
Parsing user-agent strings in Ruby
regex
pattern-matching
lookup-tables
Intermediate
8 steps
python
import time import uuid from django.utils.deprecation import MiddlewareMixin
Attaching per-request context in Django
middleware
request lifecycle
multi-tenancy
Intermediate
7 steps
ruby
class LogAggregator BUCKET_FORMAT = "%Y-%m-%dT%H:%M" def initialize(entries)
Bucketing log entries by the minute in Ruby
aggregation
hashing
enumerable
Intermediate
5 steps
ruby
class WeeklySignupsReport DEFAULT_WEEKS = 12 def initialize(weeks: DEFAULT_WEEKS, source: User.all)
Building a weekly signups report in Rails
service object
aggregation
group by
Intermediate
7 steps
ruby
class ApplicationController < ActionController::Base EXPERIMENTS = { checkout_button_color: %w[control blue green], onboarding_flow: %w[control streamlined]
How A/B test cohorts are assigned in Rails
a-b-testing
cookies
hashing
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/per-request-context-with-currentattributes-in-rails-explained-ruby-dbd0/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.