Skip to content

Commit

Permalink
Add option to trigger stats when exceptions occur
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinAiken committed Feb 14, 2014
1 parent 1f7f007 commit 4bf2f52
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# DEVELOP
* FEATURE: Stats for exceptions

# 0.0.2
* BUGFIX: Log location relative to `Adhearsion.root`

Expand Down
15 changes: 12 additions & 3 deletions lib/adhearsion-stats/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ class << self
class Plugin < Adhearsion::Plugin

config :statsd do
host "127.0.0.1", desc: "The host that statsd is found at"
port 8125, desc: "The port that statsd is found at"
log_metrics false, desc: "Whether or not to log stats sent to statsd"
host "127.0.0.1", desc: "The host that statsd is found at"
port 8125, desc: "The port that statsd is found at"
log_metrics false, desc: "Whether or not to log stats sent to statsd"
exception_prefix nil, desc: "A string to prefix exceptions with - leave nil to disable"
end

init :statsd do
Expand All @@ -20,6 +21,14 @@ class Plugin < Adhearsion::Plugin
AdhearsionStats.statsd = Statsd.new Adhearsion.config.statsd.host, Adhearsion.config.statsd.port
AdhearsionStats.loaded = true

if Adhearsion.config.statsd.exception_prefix
Adhearsion::Events.register_callback(:exception) do |e, logger|
AdhearsionStats.increment "#{Adhearsion.config.statsd.exception_prefix}.#{e.class}"
end

logger.info "Adhearsion-Stats is watching for exceptions"
end

logger.info "Adhearsion-Stats has been loaded"
end
end
Expand Down
26 changes: 23 additions & 3 deletions spec/adhearsion-stats/plugin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
end

describe "initialization" do
let(:logfile) { double "File", 'sync=' => true }
let!(:dummy_logger) { AdhearsionStats::MetricsLogger.new(STDOUT) }
let(:exception_prefix) { nil }
let(:logfile) { double "File", 'sync=' => true }
let!(:dummy_logger) { AdhearsionStats::MetricsLogger.new(STDOUT) }

before do
File.stub(:open).and_return logfile
AdhearsionStats::MetricsLogger.stub(:new).with(logfile).and_return dummy_logger
Adhearsion.config[:statsd].log_metrics = true
Adhearsion.config[:statsd].log_metrics = true
Adhearsion.config[:statsd].exception_prefix = exception_prefix
Adhearsion::Plugin.initializers.each { |plugin_initializer| plugin_initializer.run }
end

Expand Down Expand Up @@ -55,6 +57,24 @@
subject.timing "bar", 100
end
end

describe "Exceptions" do
context "when exception_prefix is nil" do
it "don't trigger a stat" do
subject.should_not_receive :increment
Adhearsion::Events.trigger_immediately :exception, StandardError.new
end
end

context "when exception_prefix is set" do
let(:exception_prefix) { "oh_noes" }

it "trigger a stat" do
subject.should_receive(:increment).with "oh_noes.StandardError"
Adhearsion::Events.trigger_immediately :exception, StandardError.new
end
end
end
end
end
end

0 comments on commit 4bf2f52

Please sign in to comment.