Skip to content

Instantly share code, notes, and snippets.

@skymob
Created March 29, 2013 21:11
Show Gist options
  • Save skymob/5273674 to your computer and use it in GitHub Desktop.
Save skymob/5273674 to your computer and use it in GitHub Desktop.
Increment a statsd counter for each metric received (not yet working!)
# Increment a counter in Statsd
# ==
#
# Copyright 2013 Bethany Erskine bethany@paperlesspost.com
#
require 'rubygems' if RUBY_VERSION < '1.9.0'
require 'statsd'
module Sensu
module Extension
class StastdIncrementCounter < Handler
def name
'statsd_increment_counter'
end
def description
'sends event output to statsd'
end
def definition
{
:type => 'handler',
:name => name
}
end
def run(event, settings, &block)
opts = settings['statsd']
opts['statsd_host'] ||= "localhost"
opts['statsd_port'] ||= 8125
metric_count = event['check']['output'].split("\n").count
# need to pull these from a snippet or dynamically set them
namespace = "sensu"
key = "metric_count"
statsd = Statsd.new(opts['statsd_host'], opts['statsd_port']).tap{
|sd| sd.namespace = namespace }
statsd.increment key,metric_count
block.call("incremented counter #{namespace}.#{key} #{metric_count} times", 0)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment