formatted-metrics 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dd05373a844e69ff2e42dc4e24c8c741667a7eb3
4
- data.tar.gz: c0ef9a68d1c6a28239d366150f6b38896a3b2f84
3
+ metadata.gz: b72967d33d3eaeaefa60e66a94b4df665733bb66
4
+ data.tar.gz: 80529c68a14a80c2f7127385440f090a9f446383
5
5
  SHA512:
6
- metadata.gz: 4bf4bf9259751cad5a97fbf7aeed66b24ae5900159b798a18e54a171ea87c6a521962c369bbdd9754e5610eb2eec7548104f0992a2e828a6784079d80cba6e98
7
- data.tar.gz: e3daad6ab06807c864a4706a423164719e97015ae21338c983d55a69f637a057fea6452ef835c3f05ff3ddb614d462e46734133645ddc244d9133b1bd867370d
6
+ metadata.gz: 488dfe4484cbc9d34340ae4c307de143472ad257b0db4a0eae5002ca1f55069785ae1967f1d19aed1616e860790ee136b6211bb89d9665f6ff91544cf165bceb
7
+ data.tar.gz: 957744dc87ce70b5663d4cef80a816e46537b4a7a4c90bad0495b9750c1721a8bd1dd9ad678d99af65b56f09576add7550b8c2eda7cf6fe68b059c018706a7e9
@@ -21,4 +21,5 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency 'bundler', '~> 1.3'
22
22
  spec.add_development_dependency 'rake'
23
23
  spec.add_development_dependency 'rspec', '~> 2.14'
24
+ spec.add_development_dependency 'faraday', '~> 0.9.0'
24
25
  end
@@ -0,0 +1,36 @@
1
+ require 'metrics'
2
+ require 'faraday'
3
+
4
+ module Faraday
5
+ class Instrumentation < Faraday::Middleware
6
+ include Metrics::MiddlewareHelpers
7
+
8
+ def initialize(app, options = {})
9
+ super(app)
10
+ @options = { metric: 'faraday.request' }.merge(options)
11
+ end
12
+
13
+ def call(env)
14
+ time = Time.now
15
+ response = @app.call(env)
16
+ duration = (Time.now - time) * 1000.0
17
+
18
+ request_metrics response.status, duration, metric: metric, source: source(env)
19
+
20
+ response
21
+ end
22
+
23
+ private
24
+ attr_reader :options
25
+
26
+ def source(env)
27
+ method = env[:method]
28
+ path = env[:url].path.gsub(/\//, '.')
29
+ "#{method}#{path}"
30
+ end
31
+
32
+ def metric
33
+ options[:metric]
34
+ end
35
+ end
36
+ end
data/lib/metrics.rb CHANGED
@@ -3,15 +3,26 @@ require 'metrics/core_ext'
3
3
  require 'metrics/railtie' if defined?(Rails)
4
4
 
5
5
  module Metrics
6
- autoload :Configuration, 'metrics/configuration'
7
- autoload :Instrumentable, 'metrics/instrumentable'
8
- autoload :Instrumenter, 'metrics/instrumenter'
9
- autoload :Grouping, 'metrics/grouping'
10
- autoload :Handler, 'metrics/handler'
6
+ autoload :MiddlewareHelpers, 'metrics/middleware_helpers'
7
+ autoload :Configuration, 'metrics/configuration'
8
+ autoload :Instrumentable, 'metrics/instrumentable'
9
+ autoload :Instrumenter, 'metrics/instrumenter'
10
+ autoload :Grouping, 'metrics/grouping'
11
+ autoload :Handler, 'metrics/handler'
11
12
 
12
13
  module Formatters
13
- autoload :Base, 'metrics/formatters/base'
14
- autoload :L2Met, 'metrics/formatters/l2met'
14
+ autoload :Base, 'metrics/formatters/base'
15
+ autoload :L2Met, 'metrics/formatters/l2met'
16
+ end
17
+
18
+ module Helpers
19
+ def self.extract_options!(options)
20
+ if options.last.is_a?(Hash)
21
+ options.pop
22
+ else
23
+ {}
24
+ end
25
+ end
15
26
  end
16
27
 
17
28
  class << self
@@ -2,30 +2,44 @@ module Metrics
2
2
  # Public: Starts a new Grouping context, which allows for multiple
3
3
  # instruments to output on a single line.
4
4
  class Grouping
5
- attr_reader :namespace, :instrumenters
5
+ attr_reader :namespace, :instrumenters, :options
6
6
 
7
7
  def self.instrument(*args, &block)
8
8
  new(*args, &block).instrumenters
9
9
  end
10
10
 
11
- def initialize(namespace = nil, &block)
11
+ def initialize(namespace = nil, options = {}, &block)
12
12
  @instrumenters = []
13
13
  @namespace = namespace
14
+ @options = options
14
15
  block.call(self)
15
16
  end
16
17
 
17
18
  def increment(metric)
18
- instrument metric, 1, type: 'count'
19
+ instrument metric, 1, options.merge(type: 'count')
19
20
  end
20
21
 
21
22
  def instrument(metric, *args, &block)
22
23
  metric = "#{namespace}.#{metric}" if namespace
23
- instrumenters.push(Instrumenter.instrument(metric, *args, &block))
24
+ instrumenters.push(Instrumenter.instrument(metric, *merge_options(args), &block))
24
25
  end
25
26
 
26
- def group(nested_namespace = nil, &block)
27
+ def group(nested_namespace = nil, *args, &block)
27
28
  ns = nested_namespace ? "#{namespace}.#{nested_namespace}" : namespace
28
- instrumenters.push(*Grouping.instrument(ns, &block))
29
+ instrumenters.push(*Grouping.instrument(ns, *merge_options(args), &block))
29
30
  end
31
+
32
+ private
33
+
34
+ def merge_options(args)
35
+ opts = extract_options!(args)
36
+ args << options.merge(opts)
37
+ args
38
+ end
39
+
40
+ def extract_options!(options)
41
+ Metrics::Helpers.extract_options!(options)
42
+ end
43
+
30
44
  end
31
45
  end
@@ -11,7 +11,7 @@ module Metrics
11
11
  class Instrumenter
12
12
  TIME_UNITS = 'ms'.freeze
13
13
 
14
- attr_reader :metric
14
+ attr_reader :metric, :options
15
15
 
16
16
  def self.instrument(*args, &block)
17
17
  instrument = new(*args, &block)
@@ -53,7 +53,7 @@ module Metrics
53
53
  end
54
54
 
55
55
  private
56
- attr_reader :options, :block
56
+ attr_reader :block
57
57
 
58
58
  def timing?
59
59
  !block.nil?
@@ -68,11 +68,7 @@ module Metrics
68
68
  end
69
69
 
70
70
  def extract_options!(options)
71
- if options.last.is_a?(Hash)
72
- options.pop
73
- else
74
- {}
75
- end
71
+ Metrics::Helpers.extract_options!(options)
76
72
  end
77
73
  end
78
74
  end
@@ -0,0 +1,32 @@
1
+ require 'metrics/middleware_helpers'
2
+
3
+ module Metrics
4
+ module MiddlewareHelpers
5
+ private
6
+
7
+ def request_metrics(status, duration, options = {})
8
+ metric = options[:metric]
9
+ source = options[:source]
10
+
11
+ group metric, source: source do |group|
12
+ group.instrument 'time', duration, units: 'ms'
13
+
14
+ group.group 'status' do |group|
15
+ group.increment status
16
+ group.increment "#{status.to_s[0]}xx"
17
+
18
+ group.instrument "#{status}.time", duration, units: 'ms'
19
+ group.instrument "#{status.to_s[0]}xx.time", duration, units: 'ms'
20
+ end
21
+ end
22
+ end
23
+
24
+ def instrument(*args, &block)
25
+ Metrics.instrument(*args, &block)
26
+ end
27
+
28
+ def group(*args, &block)
29
+ Metrics.group(*args, &block)
30
+ end
31
+ end
32
+ end
@@ -1,3 +1,3 @@
1
1
  module FormattedMetrics
2
- VERSION = '1.1.1'
2
+ VERSION = '1.1.2'
3
3
  end
@@ -1,5 +1,9 @@
1
+ require 'metrics/middleware_helpers'
2
+
1
3
  module Rack
2
4
  class Instrumentation
5
+ include Metrics::MiddlewareHelpers
6
+
3
7
  def initialize(app)
4
8
  @app = app
5
9
  end
@@ -12,7 +16,7 @@ module Rack
12
16
  response = @app.call(env)
13
17
  duration = (Time.now - time) * 1000.0
14
18
 
15
- request_metrics response.first, duration
19
+ request_metrics response.first, duration, metric: 'rack.request'
16
20
 
17
21
  response
18
22
  rescue Exception => raised
@@ -28,28 +32,6 @@ module Rack
28
32
 
29
33
  instrument 'rack.heroku.queue.wait_time', env['HTTP_X_HEROKU_QUEUE_WAIT_TIME'].to_f, units: 'ms'
30
34
  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
35
  end
54
36
  end
55
37
 
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+ require 'faraday/instrumentation'
3
+
4
+ describe Faraday::Instrumentation do
5
+ let(:response) { double('response', status: 200) }
6
+ let(:app) { double('app', call: response) }
7
+ let(:middleware) { described_class.new(app) }
8
+
9
+ describe '.call' do
10
+ let(:uri) { URI.parse('http://google.com/v1/foo?param=1') }
11
+
12
+ it 'returns the response' do
13
+ expect(middleware.call(url: uri)).to eq response
14
+ end
15
+
16
+ it 'sets the source' do
17
+ Metrics.should_receive(:group).with('faraday.request', source: 'get.v1.foo').and_call_original
18
+ expect(middleware.call(method: :get, url: uri)).to eq response
19
+ end
20
+ end
21
+ end
@@ -38,5 +38,23 @@ describe Metrics::Grouping do
38
38
 
39
39
  expect(group.instrumenters.first.metric).to eq 'rack.request.time'
40
40
  end
41
+
42
+ it 'allows for options to be passed' do
43
+ group = described_class.new 'rack', source: 'foo' do |group|
44
+ group.instrument 'request.time', 500, units: 'ms'
45
+ end
46
+
47
+ expect(group.instrumenters.first.options).to eq(source: 'foo', units: 'ms')
48
+ end
49
+
50
+ it 'allows for top level options to be passed to nested groups' do
51
+ group = described_class.new 'rack', source: 'foo' do |group|
52
+ group.group 'request' do |g|
53
+ g.instrument 'time', 500, units: 'ms'
54
+ end
55
+ end
56
+
57
+ expect(group.instrumenters.first.options).to eq(source: 'foo', units: 'ms')
58
+ end
41
59
  end
42
60
  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: 1.1.1
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric J. Holmes
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ~>
53
53
  - !ruby/object:Gem::Version
54
54
  version: '2.14'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.9.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 0.9.0
55
69
  description: Easily output formatted metrics to stdout
56
70
  email:
57
71
  - eric@ejholmes.net
@@ -67,6 +81,7 @@ files:
67
81
  - README.md
68
82
  - Rakefile
69
83
  - formatted-metrics.gemspec
84
+ - lib/faraday/instrumentation.rb
70
85
  - lib/formatted-metrics.rb
71
86
  - lib/metrics.rb
72
87
  - lib/metrics/configuration.rb
@@ -77,10 +92,12 @@ files:
77
92
  - lib/metrics/handler.rb
78
93
  - lib/metrics/instrumentable.rb
79
94
  - lib/metrics/instrumenter.rb
95
+ - lib/metrics/middleware_helpers.rb
80
96
  - lib/metrics/railtie.rb
81
97
  - lib/metrics/version.rb
82
98
  - lib/rack/instrumentation.rb
83
99
  - lib/sidekiq/middleware/server/instrumentation.rb
100
+ - spec/faraday/instrumentation_spec.rb
84
101
  - spec/metrics/formatters/l2met_spec.rb
85
102
  - spec/metrics/grouping_spec.rb
86
103
  - spec/metrics/instrumentable_spec.rb
@@ -113,6 +130,7 @@ signing_key:
113
130
  specification_version: 4
114
131
  summary: Easily output formatted metrics to stdout
115
132
  test_files:
133
+ - spec/faraday/instrumentation_spec.rb
116
134
  - spec/metrics/formatters/l2met_spec.rb
117
135
  - spec/metrics/grouping_spec.rb
118
136
  - spec/metrics/instrumentable_spec.rb