mirror of
https://github.com/jimeh/airbrake-statsd.git
synced 2026-02-19 02:46:43 +00:00
initial import of airbrake-statsd gem
This commit is contained in:
17
.gitignore
vendored
Normal file
17
.gitignore
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
*.gem
|
||||
*.rbc
|
||||
.bundle
|
||||
.config
|
||||
.yardoc
|
||||
Gemfile.lock
|
||||
InstalledFiles
|
||||
_yardoc
|
||||
coverage
|
||||
doc/
|
||||
lib/bundler/man
|
||||
pkg
|
||||
rdoc
|
||||
spec/reports
|
||||
test/tmp
|
||||
test/version_tmp
|
||||
tmp
|
||||
4
Gemfile
Normal file
4
Gemfile
Normal file
@@ -0,0 +1,4 @@
|
||||
source 'http://rubygems.org'
|
||||
|
||||
# Specify your gem's dependencies in airbrake-statsd.gemspec
|
||||
gemspec
|
||||
20
Rakefile
Normal file
20
Rakefile
Normal file
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env rake
|
||||
require "bundler/gem_tasks"
|
||||
|
||||
# Default task
|
||||
task :default => 'spec'
|
||||
|
||||
# Rspec
|
||||
require "rspec/core/rake_task"
|
||||
|
||||
RSpec::Core::RakeTask.new(:spec)
|
||||
|
||||
namespace :spec do
|
||||
RSpec::Core::RakeTask.new(:integration) do |spec|
|
||||
spec.pattern = 'spec/integration/**/*_spec.rb'
|
||||
end
|
||||
|
||||
RSpec::Core::RakeTask.new(:unit) do |spec|
|
||||
spec.pattern = 'spec/unit/**/*_spec.rb'
|
||||
end
|
||||
end
|
||||
26
airbrake-statsd.gemspec
Normal file
26
airbrake-statsd.gemspec
Normal file
@@ -0,0 +1,26 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
require File.expand_path('../lib/airbrake-statsd/version', __FILE__)
|
||||
|
||||
Gem::Specification.new do |gem|
|
||||
gem.authors = ['Jim Myhrberg']
|
||||
gem.email = ['contact@jimeh.me']
|
||||
gem.description = 'Increment an exception counter in StatsD whenever ' +
|
||||
'the Airbrake gem reports and exception.'
|
||||
gem.summary = 'Increment an exception counter in StatsD whenever ' +
|
||||
'the Airbrake gem reports and exception.'
|
||||
gem.homepage = 'http://github.com/jimeh/airbrake-statsd'
|
||||
|
||||
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
||||
gem.files = `git ls-files`.split("\n")
|
||||
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
||||
gem.name = 'airbrake-statsd'
|
||||
gem.require_paths = ['lib']
|
||||
gem.version = Airbrake::Statsd::VERSION
|
||||
|
||||
gem.add_development_dependency 'rake'
|
||||
gem.add_development_dependency 'rspec'
|
||||
gem.add_development_dependency 'simplecov'
|
||||
|
||||
gem.add_runtime_dependency 'airbrake'
|
||||
gem.add_runtime_dependency 'statsd-ruby'
|
||||
end
|
||||
40
lib/airbrake-statsd.rb
Normal file
40
lib/airbrake-statsd.rb
Normal file
@@ -0,0 +1,40 @@
|
||||
require 'airbrake'
|
||||
require 'statsd'
|
||||
|
||||
require 'airbrake-statsd/version'
|
||||
require 'airbrake-statsd/configuration'
|
||||
require 'airbrake-statsd/airbrake_ext'
|
||||
|
||||
module Airbrake
|
||||
module Statsd
|
||||
class << self
|
||||
|
||||
def configure(&block)
|
||||
@configured = true
|
||||
block.call(config) if block_given?
|
||||
end
|
||||
|
||||
def configured?
|
||||
!!@configured
|
||||
end
|
||||
|
||||
def config
|
||||
@config ||= Configuration.new
|
||||
end
|
||||
|
||||
def client
|
||||
@client ||= begin
|
||||
client = ::Statsd.new(config.host, config.port)
|
||||
client.namespace = config.namespace if config.namespace
|
||||
client
|
||||
end
|
||||
end
|
||||
|
||||
def increment
|
||||
return unless configured?
|
||||
client.increment('exceptions')
|
||||
end
|
||||
|
||||
end # << self
|
||||
end # Statsd
|
||||
end # Airbrake
|
||||
13
lib/airbrake-statsd/airbrake_ext.rb
Normal file
13
lib/airbrake-statsd/airbrake_ext.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
module Airbrake
|
||||
class << self
|
||||
|
||||
def notify_with_statds(*args)
|
||||
Airbrake::Statsd.increment
|
||||
notify_without_statsd(*args)
|
||||
end
|
||||
|
||||
alias :notify_without_statsd :notify
|
||||
alias :notify :notify_with_statds
|
||||
|
||||
end
|
||||
end
|
||||
19
lib/airbrake-statsd/configuration.rb
Normal file
19
lib/airbrake-statsd/configuration.rb
Normal file
@@ -0,0 +1,19 @@
|
||||
module Airbrake
|
||||
module Statsd
|
||||
class Configuration
|
||||
|
||||
attr_accessor :namespace
|
||||
|
||||
def host
|
||||
@host ||= 'localhost'
|
||||
end
|
||||
attr_writer :host
|
||||
|
||||
def port
|
||||
@port ||= 8125
|
||||
end
|
||||
attr_writer :port
|
||||
|
||||
end # Configuration
|
||||
end # Statsd
|
||||
end # Airbrake
|
||||
5
lib/airbrake-statsd/version.rb
Normal file
5
lib/airbrake-statsd/version.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
module Airbrake
|
||||
module Statsd
|
||||
VERSION = "0.0.1"
|
||||
end
|
||||
end
|
||||
10
spec/spec_helper.rb
Normal file
10
spec/spec_helper.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
require 'rspec'
|
||||
require 'rspec/autorun'
|
||||
|
||||
require 'simplecov'
|
||||
SimpleCov.start do
|
||||
add_filter 'spec'
|
||||
add_filter 'vendor'
|
||||
end
|
||||
|
||||
require 'airbrake-statsd'
|
||||
34
spec/unit/airbrake-statsd/airbrake_ext_spec.rb
Normal file
34
spec/unit/airbrake-statsd/airbrake_ext_spec.rb
Normal file
@@ -0,0 +1,34 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe Airbrake do
|
||||
|
||||
subject { Airbrake }
|
||||
|
||||
it '#notify is an alias to #notify_with_statsd' do
|
||||
subject.method(:notify).should == subject.method(:notify_with_statds)
|
||||
end
|
||||
|
||||
describe '#notify_without_statsd' do
|
||||
|
||||
it 'does not call Airbrake::Statsd.increment' do
|
||||
Airbrake.stub(:send_notice).and_return(nil)
|
||||
|
||||
Airbrake.should_receive(:build_notice_for).with('oops', {})
|
||||
Airbrake::Statsd.should_not_receive(:increment)
|
||||
|
||||
Airbrake.notify_without_statsd('oops', {})
|
||||
end
|
||||
end
|
||||
|
||||
describe '#notify' do
|
||||
it 'calls Airbrake::Statsd.increment' do
|
||||
Airbrake.stub(:send_notice).and_return(nil)
|
||||
|
||||
Airbrake.should_receive(:build_notice_for).with('oops', {})
|
||||
Airbrake::Statsd.should_receive(:increment)
|
||||
|
||||
Airbrake.notify('oops', {})
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
42
spec/unit/airbrake-statsd/configuration_spec.rb
Normal file
42
spec/unit/airbrake-statsd/configuration_spec.rb
Normal file
@@ -0,0 +1,42 @@
|
||||
require 'spec_helper'
|
||||
|
||||
module Airbrake
|
||||
module Statsd
|
||||
describe Configuration do
|
||||
|
||||
describe '`host` option' do
|
||||
it 'defaults to "localhost"' do
|
||||
subject.host.should == 'localhost'
|
||||
end
|
||||
|
||||
it 'can be set' do
|
||||
subject.host = 'not-default.com'
|
||||
subject.host.should == 'not-default.com'
|
||||
end
|
||||
end
|
||||
|
||||
describe '`port` option' do
|
||||
it 'defaults to 8125' do
|
||||
subject.port.should == 8125
|
||||
end
|
||||
|
||||
it 'can be set' do
|
||||
subject.port = 88125
|
||||
subject.port.should == 88125
|
||||
end
|
||||
end
|
||||
|
||||
describe '`namespace` option' do
|
||||
it 'defaults to "nil"' do
|
||||
subject.namespace.should be_nil
|
||||
end
|
||||
|
||||
it 'can be set' do
|
||||
subject.namespace = 'my_awesome_app'
|
||||
subject.namespace.should == 'my_awesome_app'
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
72
spec/unit/airbrake-statsd_spec.rb
Normal file
72
spec/unit/airbrake-statsd_spec.rb
Normal file
@@ -0,0 +1,72 @@
|
||||
require 'spec_helper'
|
||||
|
||||
module Airbrake
|
||||
describe Statsd do
|
||||
|
||||
subject { Statsd }
|
||||
|
||||
before do
|
||||
subject.instance_variable_set('@configured', nil)
|
||||
subject.instance_variable_set('@config', nil)
|
||||
subject.instance_variable_set('@client', nil)
|
||||
end
|
||||
|
||||
describe '#configured?' do
|
||||
context 'when #configure has been called' do
|
||||
it 'returns true' do
|
||||
subject.configure
|
||||
subject.configured?.should be_true
|
||||
end
|
||||
end # configure called
|
||||
|
||||
context 'when #configure has not been called' do
|
||||
it 'returns false' do
|
||||
subject.configured?.should be_false
|
||||
end
|
||||
end # configure not called
|
||||
end # configured?
|
||||
|
||||
describe '#config' do
|
||||
it 'creates new Configuration instance if not set' do
|
||||
subject.config.should be_a(Statsd::Configuration)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#client' do
|
||||
it 'creates a new ::Statsd client instance if not set' do
|
||||
subject.config.should_receive(:host).once.and_return('testhost')
|
||||
subject.config.should_receive(:port).once.and_return(1234)
|
||||
::Statsd.should_receive(:new).with('testhost', 1234)
|
||||
subject.client
|
||||
end
|
||||
|
||||
context 'when a namespace is set in #config instance' do
|
||||
before do
|
||||
subject.configure { |config| config.namespace = 'test' }
|
||||
end
|
||||
|
||||
it 'sets namespace on client' do
|
||||
::Statsd.any_instance.should_receive(:namespace=).with('test').once
|
||||
subject.client
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#increment' do
|
||||
context 'when #configure has been called' do
|
||||
before { subject.configure }
|
||||
|
||||
it 'calls #increment on #client instance' do
|
||||
subject.client.should_receive(:increment).once
|
||||
subject.increment
|
||||
end
|
||||
|
||||
it 'default bucket name used is "exceptions"' do
|
||||
subject.client.should_receive(:increment).with('exceptions').once
|
||||
subject.increment
|
||||
end
|
||||
end # configure called
|
||||
end # increment
|
||||
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user