Tracking custom statistics in your Rails apps is super easy with StatHat. We have a gem that you need to add to your Gemfile:
gem 'stathat'
Then make calls to StatHat in your controllers or models. You do not need to create the stats first using the web interface.
StatHat::API.ez_post_value("load average", "steve@stathat.com", 0.92) StatHat::API.ez_post_count("messages sent - female to male", "steve@stathat.com", 13)
The StatHat gem uses a pool of threads servicing a queue, so these calls will not block waiting for the requests to complete.
You can use ActiveSupport::Notifications
to track some interesting internal
stats in your Rails app. For example, to track SQL query duration, create a file in
config/initializers
named notifications.rb
and put this code
in it:
if Rails.env.production? ActiveSupport::Notifications.subscribe "sql.active_record" do |name, start, finish, id, payload| if payload[:name] == "SQL" duration = (finish - start) * 1000 StatHat::API.ez_post_value("rails sql query duration", "steve@stathat.com", duration) if duration > 500 StatHat::API.ez_post_count("rails slow sql query", "steve@stathat.com", 1) end end end end
You can also track Rails request durations easily using a similar method:
ActiveSupport::Notifications.subscribe "process_action.action_controller" do |name, start, finish, id, payload| duration = (finish - start) * 1000 unless payload[:view_runtime].nil? view_time = payload[:view_runtime] StatHat::API.ez_post_value("rails request view duration", "steve@stathat.com", view_time) if view_time > 100 StatHat::API.ez_post_count("rails slow view duration", "steve@stathat.com", 1) end end unless payload[:db_runtime].nil? db_time = payload[:db_runtime] StatHat::API.ez_post_value("rails request db duration", "steve@stathat.com", db_time) if db_time > 100 StatHat::API.ez_post_count("rails slow db duration", "steve@stathat.com", 1) end end if duration > 500 StatHat::API.ez_post_count("rails slow request", "steve@stathat.com", 1) end end
© 2011-2017 Numerotron Inc