Setting the Scene
In an effort to make the default Rails logging more helpful in a production environment, I have been taking advantage of Log4r and its diagnostic context, specifically MDC. In addition to the logs that come out of the Rails application itself, I'm also inserting some of my own middleware so that I can get consistent logging, even in the Rack middleware.
For example, as soon as I get access to the logged-in user via Warden, I add the MDC entry needed for that piece.
def call(env)
user = env['warden'].user
user_context = user ? user.to_log_format : 'indetermined'
MDC.put :user, user_context
@app.call(env)
end
Other logged things in the Rack middleware are parent PID, request ID, etc.
The Problem
The issue is that my log entries are noticeably incorrect. Under load, I consistently see one user's identity is getting mixed in with a request that an entirely different user would have made to the API if the requests come in close enough together.
I'm told that Log4r MDC is threadsafe and Rails 4 is threadsafe by default, but obviously something isn't right. I've also wondered if Rack is the issue, but the threadsafeness--totally a word, right?--in Rails seems to be confident enough to remove Rack::Lock, then that doesn't seem right either.
What am I missing? All the information seems to say that it's threadsafe, but I'm not convinced that it is.
Under the Hood
- Rails 4.1.10
- Log4r 1.1.10
- Passenger 4.0.59