formatted-metrics 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/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in formatted-metrics.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Eric J. Holmes
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,53 @@
1
+ # Formatted Metrics
2
+
3
+ Easily produce metrics that can be consumed by [l2met](https://github.com/ryandotsmith/l2met).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'formatted-metrics'
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ### Rails
16
+
17
+ You're done!
18
+
19
+ ### Rack
20
+
21
+ Call `Metrics.setup` when you app boots.
22
+
23
+ ### Instrument
24
+
25
+ Use `Metrics.instrument` to instrument events to STDOUT.
26
+
27
+ ```ruby
28
+ Metrics.instrument 'rack.request' do
29
+ @app.call(env)
30
+ end
31
+ # => 'source=app measure.rack.request=50ms'
32
+
33
+ Metrics.instrument 'workers.busy', 10, units: 'workers'
34
+ # => 'source=app measure.workers.busy=10workers'
35
+
36
+ Metrics.instrument 'sidekiq.queue', source: 'background' do
37
+ yield
38
+ end
39
+ # => 'source=app.background measure.sidekiq.queue=500ms'
40
+ ```
41
+
42
+ ## TODO
43
+
44
+ * Add Rack middleware for outputting rack performance metrics.
45
+ * Instrument some default rails stuff.
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ task :default => [:spec]
5
+
6
+ require 'rspec/core/rake_task'
7
+ desc "Run specs"
8
+ RSpec::Core::RakeTask.new do |t|
9
+ t.pattern = 'spec/**/*_spec.rb'
10
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'formatted-metrics/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "formatted-metrics"
8
+ spec.version = FormattedMetrics::VERSION
9
+ spec.authors = ["Eric J. Holmes"]
10
+ spec.email = ["eric@ejholmes.net"]
11
+ spec.description = %q{Easily output formatted metrics to stdout}
12
+ spec.summary = %q{Easily output formatted metrics to stdout}
13
+ spec.homepage = "http://github.com/remind101/formatted-metrics"
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_dependency 'activesupport', '~> 3.1'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.3'
24
+ spec.add_development_dependency 'rake'
25
+ spec.add_development_dependency 'rspec', '~> 2.14'
26
+ end
@@ -0,0 +1,25 @@
1
+ module Metrics
2
+ class Configuration
3
+ # The stream source to write to. Defaults to STDOUT.
4
+ attr_accessor :stream
5
+
6
+ # The base source for all metrics.
7
+ attr_accessor :source
8
+
9
+ # The formatter to use. Only needs to be a class that responds to `to_s`.
10
+ # Defaults to Metrics::Formatter.
11
+ attr_accessor :formatter
12
+
13
+ def stream
14
+ @stream ||= STDOUT
15
+ end
16
+
17
+ def source
18
+ @source ||= ENV['METRICS_SOURCE'] || ENV['APP_NAME'] || `hostname`.chomp.underscore
19
+ end
20
+
21
+ def formatter
22
+ @formatter ||= Metrics::Formatter
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,53 @@
1
+ require 'active_support/core_ext/module/delegation'
2
+ require 'active_support/core_ext/string/inflections'
3
+
4
+ module Metrics
5
+
6
+ # Internal: Responsible for taking an event and formatting it to be consumed
7
+ # by l2met.
8
+ #
9
+ # Example
10
+ #
11
+ # Formatter.new(event).to_s
12
+ # # => "source=my-app measure.rack.request=50ms"
13
+ #
14
+ # Returns a Metrics::Formatter.to_s
15
+ class Formatter
16
+ DEFAULT_UNITS = 'ms'.freeze
17
+
18
+ # An object that conforms to the same public interface as
19
+ # ActiveSupport::Notifications::Event
20
+ attr_reader :event
21
+
22
+ def initialize(event)
23
+ @event = event
24
+ end
25
+
26
+ def to_s
27
+ "source=#{source} measure.#{event_name}=#{value}"
28
+ end
29
+
30
+ private
31
+
32
+ delegate :name, :payload, :duration, to: :event, prefix: true
33
+ delegate :configuration, to: :'Metrics'
34
+
35
+ def value
36
+ case measurement = event_payload[:measure]
37
+ when true
38
+ [event_duration, DEFAULT_UNITS].join('')
39
+ else
40
+ if units = event_payload[:units]
41
+ [measurement, units].join('')
42
+ else
43
+ measurement
44
+ end
45
+ end
46
+ end
47
+
48
+ def source
49
+ [configuration.source, Array(event_payload[:source])].flatten.join('.')
50
+ end
51
+ end
52
+
53
+ end
@@ -0,0 +1,35 @@
1
+ require 'active_support/core_ext/module/delegation'
2
+
3
+ module Metrics
4
+
5
+ # Internal: Responsible for handling an ActiveSupport::Notifications message.
6
+ class Handler
7
+ attr_reader :args
8
+
9
+ def initialize(*args)
10
+ @args = args
11
+ end
12
+
13
+ def handle
14
+ log if trackable?
15
+ end
16
+
17
+ private
18
+
19
+ delegate :configuration, to: :'Metrics'
20
+
21
+ def trackable?
22
+ event.payload[:measure]
23
+ end
24
+
25
+ def event
26
+ @event ||= ActiveSupport::Notifications::Event.new(*args)
27
+ end
28
+
29
+ def log
30
+ configuration.stream.puts configuration.formatter.new(event).to_s
31
+ end
32
+
33
+ end
34
+
35
+ end
@@ -0,0 +1,67 @@
1
+ require 'active_support/notifications'
2
+ require 'active_support/core_ext/array/extract_options'
3
+
4
+ require 'formatted-metrics/railtie' if defined?(Rails)
5
+
6
+ module Metrics
7
+ autoload :Configuration, 'formatted-metrics/configuration'
8
+ autoload :Handler, 'formatted-metrics/handler'
9
+ autoload :Formatter, 'formatted-metrics/formatter'
10
+
11
+ class << self
12
+
13
+ # Public: Instrument a metric.
14
+ #
15
+ # metric - The name of the metric (e.g. rack.request)
16
+ # source - A source to append to the default source.
17
+ #
18
+ # Example
19
+ #
20
+ # # Instrument the duration of an event.
21
+ # Metrics.instrument 'rack.request' do
22
+ # @app.call(env)
23
+ # end
24
+ #
25
+ # # Instrument a specific value.
26
+ # Metrics.instrument 'workers.busy', 10, units: 'workers'
27
+ #
28
+ # # Instrument something with a specific source.
29
+ # Metrics.instrument 'sidekiq.queue', source: 'background' do
30
+ # yield
31
+ # end
32
+ #
33
+ # Returns nothing.
34
+ def instrument(metric, *args, &block)
35
+ options = args.extract_options!
36
+ measure = !args.empty? ? args.first : true
37
+ ActiveSupport::Notifications.instrument \
38
+ metric,
39
+ options.merge(measure: measure, source: options[:source]),
40
+ &block
41
+ end
42
+
43
+ # Public: Subscribe to all ActiveSupport::Notifications events. Only events
44
+ # that have a payload with a :measure key that is truthy will be processed
45
+ # and logged to stdout.
46
+ #
47
+ # Example
48
+ #
49
+ # Metrics.setup
50
+ #
51
+ # Returns nothing.
52
+ def subscribe
53
+ ActiveSupport::Notifications.subscribe /.*/ do |*args|
54
+ Metrics::Handler.new(*args).handle
55
+ end
56
+ end
57
+
58
+ def configuration
59
+ @configuration || Configuration.new
60
+ end
61
+
62
+ def configure
63
+ yield configuration
64
+ end
65
+
66
+ end
67
+ end
@@ -0,0 +1,7 @@
1
+ module Metrics
2
+ class Railtie < ::Rails::Railtie
3
+ initializer 'metrics.subscribe' do
4
+ Metrics.subscribe
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module FormattedMetrics
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1 @@
1
+ require 'formatted-metrics/metrics'
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe Metrics::Formatter do
4
+ let(:event ) { double ActiveSupport::Notifications::Event, name: 'rack.request', payload: { measure: true } }
5
+ let(:formatter) { described_class.new event }
6
+
7
+ before do
8
+ formatter.stub_chain :configuration, source: 'app'
9
+ end
10
+
11
+ describe '.to_s' do
12
+ subject { formatter.to_s }
13
+
14
+ context 'with a duration' do
15
+ before { event.stub duration: 10 }
16
+ it { should eq 'source=app measure.rack.request=10ms' }
17
+ end
18
+
19
+ context 'with a measurement' do
20
+ context 'with units' do
21
+ before { event.stub payload: { measure: 1, units: 's' } }
22
+ it { should eq 'source=app measure.rack.request=1s' }
23
+ end
24
+
25
+ context 'without units' do
26
+ before { event.stub payload: { measure: 50 } }
27
+ it { should eq 'source=app measure.rack.request=50' }
28
+ end
29
+ end
30
+
31
+ context 'with a source' do
32
+ before { event.stub name: 'workers.busy', payload: { measure: 10, source: 'sidekiq' } }
33
+ it { should eq 'source=app.sidekiq measure.workers.busy=10' }
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe Metrics::Handler do
4
+ let(:args ) { ['event args'] * 5 }
5
+ let(:handler ) { described_class.new *args }
6
+
7
+ let(:event ) { double ActiveSupport::Notifications::Event }
8
+ let(:formatter) { double Metrics.configuration.formatter }
9
+
10
+ describe '.handle' do
11
+ before do
12
+ ActiveSupport::Notifications::Event.stub new: event
13
+ Metrics.configuration.formatter.stub new: formatter
14
+ end
15
+
16
+ context 'when the event is trackable' do
17
+ before do
18
+ event.stub payload: { measure: true }
19
+ end
20
+
21
+ it 'should handle the event' do
22
+ Metrics.configuration.stream.should_receive(:puts).with(formatter.to_s)
23
+ handler.handle
24
+ end
25
+ end
26
+
27
+ context 'when the event is not trackable' do
28
+ before do
29
+ event.stub payload: { }
30
+ end
31
+
32
+ it 'should not handle the event' do
33
+ Metrics.configuration.stream.should_receive(:puts).never
34
+ handler.handle
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe Metrics do
4
+ let(:instrumenter) { ActiveSupport::Notifications }
5
+
6
+ describe '#instrument' do
7
+ context 'with a block' do
8
+ it 'instruments the duration' do
9
+ instrumenter.should_receive(:instrument).with('rack.request', measure: true, source: nil)
10
+ Metrics.instrument 'rack.request' do
11
+ 'foo'
12
+ end
13
+ end
14
+
15
+ it 'instruments the duration with a source' do
16
+ instrumenter.should_receive(:instrument).with('rack.request', measure: true, source: 'foo')
17
+ Metrics.instrument 'rack.request', source: 'foo' do
18
+ 'do something long'
19
+ end
20
+ end
21
+ end
22
+
23
+ context 'with a measurement' do
24
+ it 'instruments the measurement' do
25
+ instrumenter.should_receive(:instrument).with('rack.request', measure: 10, source: nil)
26
+ Metrics.instrument 'rack.request', 10
27
+ end
28
+
29
+ it 'instruments the measurement with a source' do
30
+ instrumenter.should_receive(:instrument).with('rack.request', measure: 10, source: 'foo')
31
+ Metrics.instrument 'rack.request', 10, source: 'foo'
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,2 @@
1
+ require 'bundler/setup'
2
+ Bundler.require
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: formatted-metrics
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Eric J. Holmes
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.1'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '2.14'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '2.14'
78
+ description: Easily output formatted metrics to stdout
79
+ email:
80
+ - eric@ejholmes.net
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .rspec
87
+ - Gemfile
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - formatted-metrics.gemspec
92
+ - lib/formatted-metrics.rb
93
+ - lib/formatted-metrics/configuration.rb
94
+ - lib/formatted-metrics/formatter.rb
95
+ - lib/formatted-metrics/handler.rb
96
+ - lib/formatted-metrics/metrics.rb
97
+ - lib/formatted-metrics/railtie.rb
98
+ - lib/formatted-metrics/version.rb
99
+ - spec/formatted-metrics/formatter_spec.rb
100
+ - spec/formatted-metrics/handler_spec.rb
101
+ - spec/formatted-metrics/metrics_spec.rb
102
+ - spec/spec_helper.rb
103
+ homepage: http://github.com/remind101/formatted-metrics
104
+ licenses:
105
+ - MIT
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ segments:
117
+ - 0
118
+ hash: -1041896361416376265
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ segments:
126
+ - 0
127
+ hash: -1041896361416376265
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 1.8.23
131
+ signing_key:
132
+ specification_version: 3
133
+ summary: Easily output formatted metrics to stdout
134
+ test_files:
135
+ - spec/formatted-metrics/formatter_spec.rb
136
+ - spec/formatted-metrics/handler_spec.rb
137
+ - spec/formatted-metrics/metrics_spec.rb
138
+ - spec/spec_helper.rb