Skip to content

Instantly share code, notes, and snippets.

@killercup
Created March 16, 2012 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save killercup/2049630 to your computer and use it in GitHub Desktop.
Save killercup/2049630 to your computer and use it in GitHub Desktop.
Update Stathat from Rails in Background
# config/initializers/stathat.rb
if Rails.env.production?
ezkey = # SET ME!
server = # SET ME!
def stathat(ezkey, stat, value, type)
# This is probably the most insane code you have ever seen
# http://ragefac.es/53
system "curl -silent -d 'email=#{ezkey}&stat=#{stat}&#{type}=#{value}' 'http://api.stathat.com/ez' > /dev/null &"
return
end
ActiveSupport::Notifications.subscribe "process_action.action_controller" do |name, start, finish, id, payload|
stathat(ezkey, "#{server} page view", "1", "count")
stathat(ezkey, "#{server} request duration", (finish - start) * 1000, "value")
unless payload[:view_runtime].nil?
stathat(ezkey, "#{server} request view duration", payload[:view_runtime], "value")
end
unless payload[:db_runtime].nil?
stathat(ezkey, "#{server} request db duration", payload[:db_runtime], "value")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment