rigor_logger 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rigor_logger.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Kyle Conarro
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,122 @@
1
+ # RigorLogger
2
+
3
+ Wrapper to send metrics and events to DataDog
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rigor_logger'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rigor_logger
18
+
19
+ ## Usage
20
+
21
+ ### Configuration
22
+ Configure your API key to send to DataDog. For Rails, this could go in ```config/initializers/rigor_logger.rb```
23
+ ```ruby
24
+ RigorLogger.config.merge! :api_key => 'my-datadog-apikey', # required
25
+ # hostname tag (Socket.gethostname default)
26
+ :host => 'hostname',
27
+ # the server running dogstatsd (localhost default)
28
+ :server_host => 'myserver.com',
29
+ # the port for dogstatsd server (8125 default)
30
+ :server_port => 1234,
31
+ # an environment tag ('development' default)
32
+ :environment => Rails.env,
33
+ # an app tag (nil default)
34
+ :app => 'my.app.com',
35
+ # what tags to include by default
36
+ :default_tags => [:environment, :host, :app]
37
+ ```
38
+
39
+ ### Metrics
40
+ ```ruby
41
+ # create a metric
42
+ metric = RigorLogger::Metric.new('namespace.my_cool_metric')
43
+
44
+ # send it to DataDog
45
+ metric.increment
46
+
47
+ # or use convenience method
48
+ RigorLogger::Metric.increment('namespace.my_cool_metric')
49
+ ```
50
+
51
+ __Metric types__
52
+
53
+ ```ruby
54
+ # increment a counter
55
+ RigorLogger::Metric.increment('name')
56
+
57
+ # update state
58
+ RigorLogger::Metric.gauge('name', 123)
59
+
60
+ # track distributions
61
+ RigorLogger::Metric.histogram('name', 123)
62
+
63
+ # add to a set
64
+ RigorLogger::Metric.set('name', 123)
65
+
66
+ # time a block of code
67
+ RigorLogger::Metric.time('name') { puts 'time this code block!' }
68
+ ```
69
+ __Default Options__
70
+
71
+ By default, metrics are sent with host and environment tags. These can be passed as options as well.
72
+ ```ruby
73
+ options = {
74
+ :host => 'some_hostname', # defaults to Socket.gethostname
75
+ :environment => 'staging' # defaults to 'development'
76
+ }
77
+ ```
78
+
79
+ __Additional Options__
80
+ ```ruby
81
+ metric_options = {
82
+ :tags => ['my_tag'], # tags for reporting dimensions
83
+ :sample_rate => 0.5, # only send data half the time
84
+ }
85
+ ```
86
+
87
+ ### Events
88
+ __[DataDog API docs](http://docs.datadoghq.com/api/#events)__
89
+ ```ruby
90
+ # create an event
91
+ event = RigorLogger::Event.new('my_event')
92
+
93
+ # send it to DataDog
94
+ event.submit
95
+
96
+ # or use convenience method
97
+ RigorLogger::Event.submit('my_event')
98
+ ```
99
+
100
+ __Default Options__
101
+
102
+ By default, events are sent with host and environment tags. These can be passed as options as well.
103
+ ```ruby
104
+ options = {
105
+ :host => 'some_hostname', # defaults to Socket.gethostname
106
+ :environment => 'staging' # defaults to 'development'
107
+ }
108
+
109
+ RigorLogger::Event.new 'my_event', options
110
+ ```
111
+ __Additional Options__
112
+ ```ruby
113
+ event_options = {
114
+ :msg_title => 'My Event Title', # default: ''
115
+ :date_happened => 1.day.ago, # default: now
116
+ :priority => 'normal', # 'normal' or 'low' - default 'normal'
117
+ :tags => ['my_tag'], # tags for reporting dimensions
118
+ :alert_type => 'info', # 'info', 'warning', 'success', or 'error'
119
+ :aggregation_key => 'some_key', # string to use for aggregation
120
+ :source_type_name => 'event_source' # event type: nagios, hudson, jenkins, user, my apps, feed, chef, puppet, git, bitbucket, fabric, capistrano
121
+ }
122
+ ```
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ require "rigor_logger/version"
2
+ require 'rigor_logger/base'
3
+ require 'rigor_logger/event'
4
+ require 'rigor_logger/metric'
5
+
6
+ module RigorLogger
7
+ @config = {
8
+ :api_key => nil,
9
+ :host => Socket.gethostname,
10
+ :environment => 'development',
11
+ :server_host => 'localhost',
12
+ :app => nil,
13
+ :server_port => 8125,
14
+ :default_tags => [:environment, :host]
15
+ }
16
+
17
+ class << self
18
+ attr_accessor :config
19
+ end
20
+
21
+ class ConfigurationError < Exception
22
+ end
23
+ end
@@ -0,0 +1,34 @@
1
+ require 'rubygems'
2
+ require 'statsd'
3
+ require 'dogapi'
4
+
5
+ module RigorLogger
6
+
7
+ class Base
8
+ attr_reader :environment, :host, :name, :options
9
+
10
+ def initialize name, options={}
11
+ raise(ConfigurationError, 'Please provide an API key!') unless RigorLogger.config[:api_key]
12
+ @host = options[:host] || RigorLogger.config[:host]
13
+ @environment = options[:environment] || RigorLogger.config[:environment]
14
+ @name = name
15
+ @options = set_options(options)
16
+ end
17
+
18
+ protected
19
+
20
+ def set_tags opts
21
+ t = opts[:tags] ? opts[:tags].concat(default_tags) : default_tags
22
+ {:tags => t}
23
+ end
24
+
25
+ def default_tags
26
+ RigorLogger.config[:default_tags].map {|tag| "#{tag}:#{self.send(tag)}"}
27
+ end
28
+
29
+ def set_options options
30
+ options.merge(set_tags(options))
31
+ end
32
+ end
33
+
34
+ end
@@ -0,0 +1,22 @@
1
+ module RigorLogger
2
+
3
+ class Event < Base
4
+
5
+ # convenience methods
6
+ def self.submit name, options={}
7
+ new(name, options).submit
8
+ end
9
+
10
+ attr_reader :name, :options, :client
11
+
12
+ def initialize name, options={}
13
+ @client = Dogapi::Client.new(RigorLogger.config[:api_key])
14
+ super
15
+ end
16
+
17
+ def submit
18
+ @client.emit_event @name, @options
19
+ end
20
+ end
21
+
22
+ end
@@ -0,0 +1,58 @@
1
+ module RigorLogger
2
+
3
+ class Metric < Base
4
+
5
+ # convenience methods
6
+ def self.increment name, options={}
7
+ new(name, options).increment
8
+ end
9
+
10
+ def self.gauge name, value, options={}
11
+ new(name, options.merge(:value => value)).gauge
12
+ end
13
+
14
+ def self.histogram name, value, options={}
15
+ new(name, options.merge(:value => value)).histogram
16
+ end
17
+
18
+ def self.set name, value, options={}
19
+ new(name, options.merge(:value => value)).set
20
+ end
21
+
22
+ def self.time name, options={}, &block
23
+ new(name, options).time &block
24
+ end
25
+
26
+ attr_reader :name, :value, :options, :client, :server_host, :server_port
27
+
28
+ def initialize name, options={}
29
+ @server_host = options[:server_host] || RigorLogger.config[:server_host]
30
+ @server_port = options[:server_port] || RigorLogger.config[:server_port]
31
+ @client = Statsd.new(@server_host, @server_port)
32
+ @value = options[:value]
33
+ super
34
+ end
35
+
36
+ def increment
37
+ @client.increment @name, @options
38
+ end
39
+
40
+ def gauge
41
+ @client.gauge @name, @value, @options
42
+ end
43
+
44
+ def histogram
45
+ @client.histogram @name, @value, @options
46
+ end
47
+
48
+ def set
49
+ @client.set @name, @value, @options
50
+ end
51
+
52
+ def time &block
53
+ raise(LocalJumpError, 'no block given') unless block_given?
54
+ @client.time @name, @options, &block
55
+ end
56
+ end
57
+
58
+ end
@@ -0,0 +1,3 @@
1
+ module RigorLogger
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rigor_logger/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rigor_logger"
8
+ spec.version = RigorLogger::VERSION
9
+ spec.authors = ["Kyle Conarro"]
10
+ spec.email = ["kyle.conarro@rigor.com"]
11
+ spec.description = %q{Metrics and Events wrapper for DataDog}
12
+ spec.summary = %q{Wrapper to send metrics and events to DataDog}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+
25
+ spec.add_runtime_dependency "dogstatsd-ruby"
26
+ spec.add_runtime_dependency "dogapi"
27
+ end
data/spec/base_spec.rb ADDED
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe RigorLogger::Base do
4
+
5
+ describe 'using defaults' do
6
+ it 'should require an api key' do
7
+ expect { RigorLogger::Base.new 'test'}.to raise_error RigorLogger::ConfigurationError
8
+ RigorLogger.config[:api_key] = 'my_key'
9
+ expect { RigorLogger::Base.new 'test' }.to_not raise_error RigorLogger::ConfigurationError
10
+ end
11
+
12
+ it 'should require a name' do
13
+ expect { RigorLogger::Base.new }.to raise_error ArgumentError
14
+ expect { RigorLogger::Base.new 'test'}.to_not raise_error ArgumentError
15
+ RigorLogger::Base.new('test').name.should eql('test')
16
+ end
17
+
18
+ it 'should have a default environment with tag' do
19
+ logger = RigorLogger::Base.new('test')
20
+ logger.environment.should eql('development')
21
+ logger.options[:tags].include?('environment:development').should be_true
22
+ end
23
+
24
+ it 'should have a default host with tag' do
25
+ logger = RigorLogger::Base.new('test')
26
+ logger.host.should eql(Socket.gethostname)
27
+ logger.options[:tags].include?("host:#{Socket.gethostname}").should be_true
28
+ end
29
+ end
30
+
31
+ describe 'overriding defaults' do
32
+ it 'should use options for environment' do
33
+ logger = RigorLogger::Base.new('test', :environment => 'lala-land')
34
+ logger.environment.should eql('lala-land')
35
+ logger.options[:tags].include?('environment:lala-land').should be_true
36
+ end
37
+
38
+ it 'should use options for host' do
39
+ logger = RigorLogger::Base.new('test', :host => 'my_host')
40
+ logger.host.should eql('my_host')
41
+ logger.options[:tags].include?("host:my_host").should be_true
42
+ end
43
+
44
+ it 'should have configurable defaults' do
45
+ defaults = RigorLogger.config.dup
46
+ overrides = {
47
+ :api_key => 'some_other_key',
48
+ :host => 'different_host',
49
+ :environment => 'staging',
50
+ :server_host => 'statsd.com',
51
+ :server_port => 12345,
52
+ :default_tags => [:host]
53
+ }
54
+
55
+ defaults.each do |option, default_value|
56
+ default_value.should_not eql(RigorLogger.config.merge(overrides)[option])
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe RigorLogger::Event do
4
+
5
+ let(:logger) { RigorLogger::Event.new('test') }
6
+
7
+ describe 'using defaults' do
8
+ it 'should have base attributes' do
9
+ logger.name.should eql('test')
10
+ logger.options.should be_a(Hash)
11
+ end
12
+
13
+ it 'should have an event client' do
14
+ logger.client.should_not be_nil
15
+ logger.client.should be_an_instance_of(Dogapi::Client)
16
+ end
17
+ end
18
+
19
+ describe '#submit' do
20
+ it 'should submit events using the client' do
21
+ Dogapi::Client.any_instance.stub(:emit_event).and_return('submitted!')
22
+ logger.submit.should eql('submitted!')
23
+ end
24
+
25
+ it 'should submit conveniently' do
26
+ Dogapi::Client.any_instance.stub(:emit_event).and_return('submitted!')
27
+ RigorLogger::Event.submit('test.event').should eql('submitted!')
28
+ end
29
+ end
30
+
31
+ end
@@ -0,0 +1,104 @@
1
+ require 'spec_helper'
2
+
3
+ describe RigorLogger::Metric do
4
+
5
+ let(:logger) { RigorLogger::Metric.new('test') }
6
+
7
+ describe 'using defaults' do
8
+ it 'should have base attributes' do
9
+ logger.name.should eql('test')
10
+ logger.options.should be_a(Hash)
11
+ end
12
+
13
+ it 'should have a metric client' do
14
+ logger.client.should_not be_nil
15
+ logger.client.should be_an_instance_of(Statsd)
16
+ end
17
+
18
+ it 'should have a default metric server' do
19
+ logger.server_host.should eql('localhost')
20
+ logger.server_port.should == 8125
21
+ end
22
+ end
23
+
24
+ describe '#increment' do
25
+ before { Statsd.any_instance.stub(:increment).and_return('incremented!') }
26
+
27
+ it 'should increment a counter' do
28
+ logger.increment.should eql('incremented!')
29
+ end
30
+
31
+ it 'should increment conveniently' do
32
+ RigorLogger::Metric.increment('test.event').should eql('incremented!')
33
+ end
34
+ end
35
+
36
+ describe '#gauge' do
37
+ before { Statsd.any_instance.stub(:gauge).and_return('gauged!') }
38
+
39
+ it 'should require a value' do
40
+ expect { RigorLogger::Metric.gauge 'test' }.to raise_error(ArgumentError)
41
+ expect { RigorLogger::Metric.gauge 'test', :value => 123 }.to_not raise_error
42
+ end
43
+
44
+ it 'should gauge a metric' do
45
+ logger.gauge.should eql('gauged!')
46
+ end
47
+
48
+ it 'should gauge conveniently' do
49
+ RigorLogger::Metric.gauge('test.event', 123).should eql('gauged!')
50
+ end
51
+ end
52
+
53
+ describe '#histogram' do
54
+ before { Statsd.any_instance.stub(:histogram).and_return('histogrammed!') }
55
+
56
+ it 'should require a value' do
57
+ expect { RigorLogger::Metric.histogram 'test' }.to raise_error(ArgumentError)
58
+ expect { RigorLogger::Metric.histogram 'test', :value => 123 }.to_not raise_error
59
+ end
60
+
61
+ it 'should histogram a metric' do
62
+ logger.histogram.should eql('histogrammed!')
63
+ end
64
+
65
+ it 'should histogram conveniently' do
66
+ RigorLogger::Metric.histogram('test.event', 123).should eql('histogrammed!')
67
+ end
68
+ end
69
+
70
+ describe '#set' do
71
+ before { Statsd.any_instance.stub(:set).and_return('set!') }
72
+
73
+ it 'should require a value' do
74
+ expect { RigorLogger::Metric.set 'test' }.to raise_error(ArgumentError)
75
+ expect { RigorLogger::Metric.set 'test', :value => 123 }.to_not raise_error
76
+ end
77
+
78
+ it 'should set a metric' do
79
+ logger.set.should eql('set!')
80
+ end
81
+
82
+ it 'should set conveniently' do
83
+ RigorLogger::Metric.set('test.event', 123).should eql('set!')
84
+ end
85
+ end
86
+
87
+ describe '#time' do
88
+ before { Statsd.any_instance.stub(:time).and_return('timed!') }
89
+
90
+ it 'should require a block' do
91
+ expect { RigorLogger::Metric.time 'test' }.to raise_error(LocalJumpError)
92
+ expect { RigorLogger::Metric.time('test') { a = 'hi' } }.to_not raise_error
93
+ end
94
+
95
+ it 'should time a block' do
96
+ logger.time { a = 'hi' }.should eql('timed!')
97
+ end
98
+
99
+ it 'should time conveniently' do
100
+ RigorLogger::Metric.time('test.event') { a = 'hi' }.should eql('timed!')
101
+ end
102
+ end
103
+
104
+ end
@@ -0,0 +1,2 @@
1
+ require 'rigor_logger'
2
+ require 'bundler/setup'
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rigor_logger
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Kyle Conarro
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2013-11-08 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: bundler
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 9
29
+ segments:
30
+ - 1
31
+ - 3
32
+ version: "1.3"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rake
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: rspec
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :development
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: dogstatsd-ruby
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ type: :runtime
76
+ version_requirements: *id004
77
+ - !ruby/object:Gem::Dependency
78
+ name: dogapi
79
+ prerelease: false
80
+ requirement: &id005 !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ hash: 3
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ type: :runtime
90
+ version_requirements: *id005
91
+ description: Metrics and Events wrapper for DataDog
92
+ email:
93
+ - kyle.conarro@rigor.com
94
+ executables: []
95
+
96
+ extensions: []
97
+
98
+ extra_rdoc_files: []
99
+
100
+ files:
101
+ - .gitignore
102
+ - Gemfile
103
+ - LICENSE.txt
104
+ - README.md
105
+ - Rakefile
106
+ - lib/rigor_logger.rb
107
+ - lib/rigor_logger/base.rb
108
+ - lib/rigor_logger/event.rb
109
+ - lib/rigor_logger/metric.rb
110
+ - lib/rigor_logger/version.rb
111
+ - rigor_logger.gemspec
112
+ - spec/base_spec.rb
113
+ - spec/event_spec.rb
114
+ - spec/metric_spec.rb
115
+ - spec/spec_helper.rb
116
+ homepage: ""
117
+ licenses:
118
+ - MIT
119
+ post_install_message:
120
+ rdoc_options: []
121
+
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ none: false
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ hash: 3
130
+ segments:
131
+ - 0
132
+ version: "0"
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ none: false
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ hash: 3
139
+ segments:
140
+ - 0
141
+ version: "0"
142
+ requirements: []
143
+
144
+ rubyforge_project:
145
+ rubygems_version: 1.8.15
146
+ signing_key:
147
+ specification_version: 3
148
+ summary: Wrapper to send metrics and events to DataDog
149
+ test_files:
150
+ - spec/base_spec.rb
151
+ - spec/event_spec.rb
152
+ - spec/metric_spec.rb
153
+ - spec/spec_helper.rb
154
+ has_rdoc: