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
typescript
type Middleware<TIn, TOut> = (ctx: TIn) => Promise<TOut> | TOut; class Pipeline<TIn, TOut> { private constructor(private readonly run: Middleware<TIn, TOut>) {}
A type-safe async middleware pipeline
generics
type-safety
middleware
Advanced
9 steps
ruby
class Spellchecker ALPHABET = ('a'..'z').to_a.freeze def initialize(dictionary)
Building a Norvig-style spellchecker in Ruby
edit-distance
levenshtein
dynamic-programming
Advanced
10 steps
go
package middleware import ( "net/http"
Wrapping Gin requests in a DB transaction
middleware
transactions
error-handling
Intermediate
8 steps
php
<?php namespace App\Http\Middleware;
Caching HTTP responses with Laravel middleware
middleware
caching
http
Intermediate
8 steps
ruby
class Order < ApplicationRecord belongs_to :customer has_many :line_items has_many :products, through: :line_items
How scopes compose in a Rails model
scopes
activerecord
subqueries
Intermediate
7 steps
ruby
require "charlock_holmes" class TextFileNormalizer DEFAULT_CONFIDENCE = 60
Normalizing text files to clean UTF-8 in Ruby
encoding
text-processing
file-io
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.