Skip to content

Instantly share code, notes, and snippets.

@yholkamp
Forked from mm53bar/Gemfile
Created October 16, 2015 12:28
Show Gist options
  • Save yholkamp/653bc5003f465f05057b to your computer and use it in GitHub Desktop.
Save yholkamp/653bc5003f465f05057b to your computer and use it in GitHub Desktop.
Be sure to install the datadog agent on your server before applying this code to your app. Instructions are at https://app.datadoghq.com/account/settings#agent or use my Sunzi recipe at https://github.com/Shift81/sunzi-recipes/blob/master/ubuntu/precise/datadog.sh
gem 'dogstatsd-ruby'
require 'statsd'
$statsd = Statsd.new
ActiveSupport::Notifications.subscribe /process_action.action_controller/ do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
controller = "controller:#{event.payload[:controller]}"
action = "action:#{event.payload[:action]}"
format = "format:#{event.payload[:format] || 'all'}"
format = "format:all" if format == "format:*/*"
host = "host:#{ENV['INSTRUMENTATION_HOSTNAME']}"
status = event.payload[:status]
tags = [controller, action, format, host]
ActiveSupport::Notifications.instrument :performance, :action => :timing, :tags => tags, :measurement => "request.total_duration", :value => event.duration
ActiveSupport::Notifications.instrument :performance, :action => :timing, :tags => tags, :measurement => "database.query.time", :value => event.payload[:db_runtime]
ActiveSupport::Notifications.instrument :performance, :action => :timing, :tags => tags, :measurement => "web.view.time", :value => event.payload[:view_runtime]
ActiveSupport::Notifications.instrument :performance, :tags => tags, :measurement => "request.status.#{status}"
end
def send_event_to_statsd(name, payload)
action = payload[:action] || :increment
measurement = payload[:measurement]
value = payload[:value]
tags = payload[:tags]
key_name = "#{name.to_s.capitalize}.#{measurement}"
if action == :increment
$statsd.increment key_name, :tags => tags
else
$statsd.histogram key_name, value, :tags => tags
end
end
ActiveSupport::Notifications.subscribe /performance/ do |name, start, finish, id, payload|
send_event_to_statsd(name, payload) if Rails.env == 'production'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment