Last active
December 16, 2015 06:58
-
-
Save fuzzmonkey/5394997 to your computer and use it in GitHub Desktop.
A few bits of Ruby code for using ActiveSupport::Notifications to pass custom & rails provided metrics to riemann.
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
# in config/initializers/riemann.rb | |
$riemann = Riemann::Client.new | |
channels_to_subscribe = ["auth","process_action.action_controller","deliver.action_mailer"] | |
channels_to_subscribe.each do |channel| | |
ActiveSupport::Notifications.subscribe channel do |*args| | |
event = ActiveSupport::Notifications::Event.new(*args) | |
Rails.logger.debug "ActiveSupport::Notifications #{event.name}, #{event.payload.inspect}" | |
riemann_event = { | |
host: `hostname`, | |
state: event.payload[:state], | |
metric: (event.duration || event.payload[:metric].to_i), | |
ttl: 10, # or what ever TTL you want on your metrics | |
# you need to dup or you'll see weird things in tests if you expect tags to be ['sign_in','local'] then add a rails env or something in your initializer | |
tags: (event.payload[:tags].dup << Rails.env), | |
service: 'some-awesome-service' | |
} | |
$riemann << riemann_event | |
end | |
end | |
# then in your code.. | |
# instrument | |
ActiveSupport::Notifications.instrument("auth", :tags => ['sign_in','local'], :state => 'ok', :remote_ip => request.remote_ip, :metric => 99) | |
# measure | |
ActiveSupport::Notifications.instrument("auth", :tags => ['sign_in','local'], :state => 'ok', :remote_ip => request.remote_ip) do | |
# your code | |
# should populate event.duration in the subscription block | |
end | |
# some useful rails ones listed on the edge guides for RoR | |
# http://edgeguides.rubyonrails.org/active_support_instrumentation.html | |
# Highlights include | |
"process_action.action_controller" | |
{ | |
controller: "PostsController", | |
action: "index", | |
params: {"action" => "index", "controller" => "posts"}, | |
format: :html, | |
method: "GET", | |
path: "/posts", | |
status: 200, | |
view_runtime: 46.848, | |
db_runtime: 0.157 | |
} | |
"deliver.action_mailer" | |
{ | |
mailer: "Notification", | |
message_id: "[email protected]", | |
subject: "Rails Guides", | |
to: ["[email protected]", "[email protected]"], | |
from: ["[email protected]"], | |
date: Sat, 10 Mar 2012 14:18:09 +0100, | |
mail: "..." # omitted for brevity | |
} | |
# for the the activesupport ones you might want to do something like: | |
ActiveSupport::Notifications.subscribe "process_action.action_controller" do |*args| | |
event = ActiveSupport::Notifications::Event.new(*args) | |
tags = [event.payload[:controller], event.payload[:action]] | |
client.gauge (tags.dup << 'http_status'), 'ok', event.payload[:status] | |
client.gauge (tags.dup << 'view_runtime'), 'ok', event.payload[:view_runtime] | |
client.gauge (tags.dup << 'db_runtime'), 'ok', event.payload[:db_runtime] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've tidied this up somewhat by creating a riemann client wrapper so i can do
and populate things like hostname etc in the wrapper client.