Skip to content

Instantly share code, notes, and snippets.

@a-warner
Last active December 13, 2015 19:18
Show Gist options
  • Save a-warner/f5db30857ed3423cea79 to your computer and use it in GitHub Desktop.
Save a-warner/f5db30857ed3423cea79 to your computer and use it in GitHub Desktop.
# combination of https://gist.github.com/daveyeu/4960893
# and https://gist.github.com/jasonrclark/d82a1ea7695daac0b9ee
class QueueTimeLogger
attr_reader :app
def initialize(app, options = {})
@app = app
end
def call(env)
now = Time.now.to_f
# Delete Heroku's queue time header because it's incorrect and useless
env.delete("HTTP_X_HEROKU_QUEUE_WAIT_TIME")
microseconds = (now * 1_000_000).to_i
env["HTTP_X_MIDDLEWARE_START"] = "t=#{microseconds}"
if env["HTTP_X_REQUEST_START"]
# HTTP_X_REQUEST_START is expected by New Relic to have a t= leading
# but currently doesn't, so it can just get to_i applied.
request_start_microseconds = env["HTTP_X_REQUEST_START"].to_i * 1_000
queue_time_microseconds = microseconds - request_start_microseconds
env["HTTP_X_QUEUE_TIME"] = "t=#{queue_time_microseconds}"
end
app.call(env)
end
end
@jeffday
Copy link

jeffday commented Feb 20, 2013

do i need to modify my new relic or heroku configuration for the gem to work out of the box? i've just deployed this to a staging app I have running on the cedar stack with new relic standard, and am throwing a ton of requests at it with ab, but am seeing 0 queueing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment