Performance bar doesn't work reliably with Puma
We use Peek for our performance bar. This keys performance data for HTTP requests by their X-Request-Id
header.
The library uses a module variable pointing to a Concurrent::AtomicReference
to refer to its current request ID: https://github.com/peek/peek/blob/master/lib/peek.rb#L11-L13
This is set in a before_action
for all controllers: https://github.com/peek/peek/blob/master/lib/peek/controller_helpers.rb#L13
It's cleared after the results are saved to the adapter (in our case, Redis): https://github.com/peek/peek/blob/master/lib/peek/railtie.rb#L23-L24
This works fine under Thin and Unicorn, but not Puma as it's multi-threaded.
By adding some logging in peek.rb
, I can see that Peek.request_id
can get changed from one value to another, rather than going via the empty string (i.e. Peek.clear
):
PEEK: change request ID from "" to "PSd8wnSTS84"
PEEK: change request ID from "PSd8wnSTS84" to ''
PEEK: change request ID from "" to "AiF4IH2iyS3"
PEEK: change request ID from "AiF4IH2iyS3" to ''
PEEK: change request ID from "" to "fhUI45nU0c2"
PEEK: change request ID from "fhUI45nU0c2" to ''
PEEK: change request ID from "" to "D9MMGjMMyP9"
PEEK: change request ID from "D9MMGjMMyP9" to ''
PEEK: change request ID from "" to "YGdfnloG7P"
PEEK: change request ID from "" to "ZEa6xeka8u6"
PEEK: change request ID from "YGdfnloG7P" to ''
PEEK: change request ID from "" to "lHhJw7lJCx3"
PEEK: change request ID from "ZEa6xeka8u6" to "kyEkxHJKf92"
PEEK: change request ID from "kyEkxHJKf92" to "CB3er0zsSW"
PEEK: change request ID from "CB3er0zsSW" to ''
PEEK: change request ID from "lHhJw7lJCx3" to "46AYKJcnhO9"
PEEK: change request ID from "46AYKJcnhO9" to ''
PEEK: change request ID from "" to ''
PEEK: change request ID from "" to ''
PEEK: change request ID from "" to ''
In my case, this means that the results for request IDs ZEa6xeka8u6
, kyEkxHJKf92
, et al are not available in the performance bar as they haven't been saved: by the time that Peek tries to save it, it's no longer the 'current' request ID.