formatted-metrics 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,9 +14,18 @@ module Metrics
14
14
  block.call(self)
15
15
  end
16
16
 
17
+ def increment(metric)
18
+ instrument metric, 1
19
+ end
20
+
17
21
  def instrument(metric, *args, &block)
18
22
  metric = "#{namespace}.#{metric}" if namespace
19
- instrumenters << Instrumenter.instrument(metric, *args, &block)
23
+ instrumenters.push(Instrumenter.instrument(metric, *args, &block))
24
+ end
25
+
26
+ def group(nested_namespace = nil, &block)
27
+ ns = nested_namespace ? "#{namespace}.#{nested_namespace}" : namespace
28
+ instrumenters.push(*Grouping.instrument(ns, &block))
20
29
  end
21
30
  end
22
31
  end
@@ -1,3 +1,3 @@
1
1
  module FormattedMetrics
2
- VERSION = '0.2.3'
2
+ VERSION = '0.2.4'
3
3
  end
data/lib/metrics.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'rack/instrumentation'
1
2
  require 'metrics/railtie' if defined?(Rails)
2
3
 
3
4
  module Metrics
@@ -0,0 +1,55 @@
1
+ module Rack
2
+ class Instrumentation
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ begin
9
+ header_metrics env
10
+
11
+ time = Time.now
12
+ response = @app.call(env)
13
+ duration = (Time.now - time) * 1000.0
14
+
15
+ request_metrics response.first, duration
16
+
17
+ response
18
+ rescue Exception => raised
19
+ instrument 'exception', 1, source: 'rack'
20
+ raise
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def header_metrics(env)
27
+ return unless env.keys.include?('HTTP_X_HEROKU_QUEUE_WAIT_TIME')
28
+
29
+ instrument 'rack.heroku.queue.wait_time', env['HTTP_X_HEROKU_QUEUE_WAIT_TIME'].to_f, units: 'ms'
30
+ end
31
+
32
+ def request_metrics(status, duration)
33
+ group 'rack.request' do |group|
34
+ group.instrument 'time', duration, units: 'ms'
35
+
36
+ group.group 'status' do |group|
37
+ group.increment status
38
+ group.increment "#{status.to_s[0]}xx"
39
+
40
+ group.instrument "#{status}.time", duration, units: 'ms'
41
+ group.instrument "#{status.to_s[0]}xx.time", duration, units: 'ms'
42
+ end
43
+ end
44
+ end
45
+
46
+ def instrument(*args, &block)
47
+ Metrics.instrument(*args, &block)
48
+ end
49
+
50
+ def group(*args, &block)
51
+ Metrics.group(*args, &block)
52
+ end
53
+ end
54
+ end
55
+
@@ -18,5 +18,25 @@ describe Metrics::Grouping do
18
18
 
19
19
  expect(group.instrumenters.first.metric).to eq 'rack.request.time'
20
20
  end
21
+
22
+ it 'allows for nested groups' do
23
+ group = described_class.new 'rack' do |group|
24
+ group.group do |g|
25
+ g.instrument 'request.time', 500, units: 'ms'
26
+ end
27
+ end
28
+
29
+ expect(group.instrumenters.first.metric).to eq 'rack.request.time'
30
+ end
31
+
32
+ it 'allows for nested groups with namespaces' do
33
+ group = described_class.new 'rack' do |group|
34
+ group.group 'request' do |g|
35
+ g.instrument 'time', 500, units: 'ms'
36
+ end
37
+ end
38
+
39
+ expect(group.instrumenters.first.metric).to eq 'rack.request.time'
40
+ end
21
41
  end
22
42
  end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::Instrumentation do
4
+ let(:app ) { double 'app', call: response }
5
+ let(:env ) { Hash.new }
6
+ let(:response ) { [200, {}, []] }
7
+ subject(:middleware) { described_class.new app }
8
+
9
+ describe '.call' do
10
+ context 'when no error is raised' do
11
+ it 'calls the app' do
12
+ app.should_receive(:call).and_return(response)
13
+ expect(middleware.call(env)).to eq response
14
+ end
15
+
16
+ it 'reports heroku queue time' do
17
+ env['HTTP_X_HEROKU_QUEUE_WAIT_TIME'] = '36'
18
+ middleware.call(env)
19
+ end
20
+ end
21
+
22
+ context 'when an error is raised' do
23
+ it 'raises the error' do
24
+ app.should_receive(:call).and_raise
25
+ expect { middleware.call(env) }.to raise_error
26
+ end
27
+ end
28
+ end
29
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: formatted-metrics
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -85,11 +85,13 @@ files:
85
85
  - lib/metrics/instrumenter.rb
86
86
  - lib/metrics/railtie.rb
87
87
  - lib/metrics/version.rb
88
+ - lib/rack/instrumentation.rb
88
89
  - spec/metrics/formatters/l2met_spec.rb
89
90
  - spec/metrics/grouping_spec.rb
90
91
  - spec/metrics/instrumentable_spec.rb
91
92
  - spec/metrics/instrumenter_spec.rb
92
93
  - spec/metrics_spec.rb
94
+ - spec/rack/instrumentation_spec.rb
93
95
  - spec/spec_helper.rb
94
96
  homepage: http://github.com/remind101/formatted-metrics
95
97
  licenses:
@@ -106,7 +108,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
106
108
  version: '0'
107
109
  segments:
108
110
  - 0
109
- hash: 4334694595459848801
111
+ hash: 147122982979217444
110
112
  required_rubygems_version: !ruby/object:Gem::Requirement
111
113
  none: false
112
114
  requirements:
@@ -115,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
117
  version: '0'
116
118
  segments:
117
119
  - 0
118
- hash: 4334694595459848801
120
+ hash: 147122982979217444
119
121
  requirements: []
120
122
  rubyforge_project:
121
123
  rubygems_version: 1.8.23
@@ -128,4 +130,5 @@ test_files:
128
130
  - spec/metrics/instrumentable_spec.rb
129
131
  - spec/metrics/instrumenter_spec.rb
130
132
  - spec/metrics_spec.rb
133
+ - spec/rack/instrumentation_spec.rb
131
134
  - spec/spec_helper.rb