-
-
Save clevertechru/dd3d9d7092da4c1daf65 to your computer and use it in GitHub Desktop.
datadog installation for rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem 'dogstatsd-ruby' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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