Skip to content

Instantly share code, notes, and snippets.

@clevertechru
Forked from mm53bar/Gemfile
Last active October 15, 2015 23:30
Show Gist options
  • Save clevertechru/dd3d9d7092da4c1daf65 to your computer and use it in GitHub Desktop.
Save clevertechru/dd3d9d7092da4c1daf65 to your computer and use it in GitHub Desktop.
datadog installation for rails
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