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
@marcroberts
Copy link

Hmm, if I remove everything inside the call method so it just returns app.call(env) I still get that error. So it's something elsewhere in my app (despite other middleware being fine). Oh well

@a-warner
Copy link
Author

@marcroberts - have you tried adding this with a rails initializer? We were doing something like:

config/initializers/queue_time_logger.rb

Rails.configuration.middleware.insert_after 'Rack::Lock', QueueTimeLogger

Or is this a sinatra/rack app?

@a-warner
Copy link
Author

Hey all - check out https://github.com/RapGenius/heroku-true-relic, which is a gem version of this gist. We'll try to keep that up to date as we get more information about how best to track dyno-level queuing.

@dblock Thanks for the tip on not reporting negative queue time numbers; I incorporated that into the gem.

@marcroberts
Copy link

Thanks @a-warner but it was a totally unrelated issue (I had been trying out ruby 2.0.0 preview and switching back to 1.9.3 had broken the session cookie).

The gem looks great (I had started writing one myself but I'll give up on that now), thanks

@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