formatted-metrics 0.0.2 → 0.0.3

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.
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - jruby-19mode
6
+ - rbx-19mode
7
+ script: bundle exec rake
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Formatted Metrics
2
2
 
3
+ [![Build Status](https://travis-ci.org/remind101/formatted-metrics.png)](https://travis-ci.org/remind101/formatted-metrics) [![Code Climate](https://codeclimate.com/github/remind101/formatted-metrics.png)](https://codeclimate.com/github/remind101/formatted-metrics)
4
+
3
5
  Easily produce metrics that can be consumed by [l2met](https://github.com/ryandotsmith/l2met).
4
6
 
5
7
  ## Installation
@@ -18,7 +20,7 @@ You're done!
18
20
 
19
21
  ### Rack
20
22
 
21
- Call `Metrics.setup` when you app boots.
23
+ Call `Metrics.subscribe` when your app boots.
22
24
 
23
25
  ### Instrument
24
26
 
@@ -1,7 +1,7 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'formatted-metrics/version'
4
+ require 'metrics/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "formatted-metrics"
@@ -1 +1 @@
1
- require 'formatted-metrics/metrics'
1
+ require 'metrics'
File without changes
File without changes
File without changes
@@ -0,0 +1,72 @@
1
+ require 'active_support/concern'
2
+
3
+ module Metrics
4
+ module Instrumentable
5
+ extend ActiveSupport::Concern
6
+
7
+ # Public: See Metrics.instrument.
8
+ #
9
+ # Returns nothing.
10
+ def instrument(*args)
11
+ Metrics.instrument(*args)
12
+ end
13
+
14
+ module ClassMethods
15
+
16
+ # Internal: Used internally to specify the namespace for method
17
+ # instrumentation.
18
+ #
19
+ # Override this in your class if you need it to be something different.
20
+ #
21
+ # Example
22
+ #
23
+ # module Some
24
+ # class Namespace
25
+ # include Metrics::Instrumentable
26
+ #
27
+ # def long_method; end
28
+ # instrument :long_method
29
+ # end
30
+ # end
31
+ #
32
+ # Some::Namespace.metric_namespace
33
+ # # => 'some.namespace'
34
+ #
35
+ # Some::Namespace.new.long_method
36
+ # # => 'source=app measure.some.namespace.long_method=10s'
37
+ #
38
+ # Returns a String namespace for the metric.
39
+ def metric_namespace
40
+ to_s.gsub(/::/, '.').downcase
41
+ end
42
+
43
+ # Public: Wraps the method with a call to instrument the duration of the
44
+ # method. Fancy!
45
+ #
46
+ # Example
47
+ #
48
+ # def some_method
49
+ # do_something_taxing
50
+ # end
51
+ #
52
+ # instrument :some_method
53
+ # # => 'source=app measure.some_method=10s'
54
+ #
55
+ # Returns nothing.
56
+ def instrument(*methods)
57
+ methods.each do |name|
58
+ visibility = %w[public private protected].find { |visibility| send :"#{visibility}_method_defined?", name }
59
+ method = instance_method(name)
60
+ define_method name do |*args, &block|
61
+ method = method.bind(self)
62
+ instrument [self.class.metric_namespace, name].join('.') do
63
+ method.call(*args, &block)
64
+ end
65
+ end
66
+ send visibility, name
67
+ end
68
+ end
69
+
70
+ end
71
+ end
72
+ end
File without changes
@@ -1,3 +1,3 @@
1
1
  module FormattedMetrics
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -1,12 +1,16 @@
1
1
  require 'active_support/notifications'
2
2
  require 'active_support/core_ext/array/extract_options'
3
+ require 'active_support/dependencies/autoload'
3
4
 
4
- require 'formatted-metrics/railtie' if defined?(Rails)
5
+ require 'metrics/railtie' if defined?(Rails)
5
6
 
6
7
  module Metrics
7
- autoload :Configuration, 'formatted-metrics/configuration'
8
- autoload :Handler, 'formatted-metrics/handler'
9
- autoload :Formatter, 'formatted-metrics/formatter'
8
+ extend ActiveSupport::Autoload
9
+
10
+ autoload :Configuration
11
+ autoload :Handler
12
+ autoload :Formatter
13
+ autoload :Instrumentable
10
14
 
11
15
  class << self
12
16
 
@@ -33,11 +37,18 @@ module Metrics
33
37
  # Returns nothing.
34
38
  def instrument(metric, *args, &block)
35
39
  options = args.extract_options!
36
- measure = !args.empty? ? args.first : true
37
- ActiveSupport::Notifications.instrument \
40
+
41
+ measure = if args.empty?
42
+ block_given? ? true : 1
43
+ else
44
+ args.first
45
+ end
46
+
47
+ ActiveSupport::Notifications.instrument(
38
48
  metric,
39
49
  options.merge(measure: measure, source: options[:source]),
40
50
  &block
51
+ )
41
52
  end
42
53
 
43
54
  # Public: Subscribe to all ActiveSupport::Notifications events. Only events
File without changes
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ describe Metrics::Instrumentable do
4
+ let(:klass) do
5
+ Class.new do
6
+ include Metrics::Instrumentable
7
+
8
+ def self.to_s
9
+ 'Some::Module'
10
+ end
11
+
12
+ def initialize
13
+ @foo = 'foo'
14
+ end
15
+
16
+ def long_method
17
+ @foo
18
+ end
19
+
20
+ def protected_method; end
21
+ def private_method; end
22
+
23
+ protected :protected_method
24
+ private :private_method
25
+
26
+ instrument :long_method, :protected_method, :private_method
27
+ end
28
+ end
29
+
30
+ describe '#instrument' do
31
+ it 'instruments the duration of the method' do
32
+ klass.any_instance.should_receive(:instrument).with('some.module.long_method').and_yield
33
+ expect(klass.new.long_method).to eq 'foo'
34
+ end
35
+
36
+ it 'sets the visibility of protected methods' do
37
+ expect(klass.public_method_defined?(:long_method)).to be_true
38
+ end
39
+
40
+ it 'sets the visibility of protected methods' do
41
+ expect(klass.protected_method_defined?(:protected_method)).to be_true
42
+ end
43
+
44
+ it 'sets the visibility of private methods' do
45
+ expect(klass.private_method_defined?(:private_method)).to be_true
46
+ end
47
+ end
48
+ end
@@ -31,5 +31,12 @@ describe Metrics do
31
31
  Metrics.instrument 'rack.request', 10, source: 'foo'
32
32
  end
33
33
  end
34
+
35
+ context 'with empty no block and no measurement' do
36
+ it 'instruments with a measurement of 1' do
37
+ instrumenter.should_receive(:instrument).with('exception', measure: 1, source: nil)
38
+ Metrics.instrument 'exception'
39
+ end
40
+ end
34
41
  end
35
42
  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.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-30 00:00:00.000000000 Z
12
+ date: 2013-07-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -84,21 +84,24 @@ extra_rdoc_files: []
84
84
  files:
85
85
  - .gitignore
86
86
  - .rspec
87
+ - .travis.yml
87
88
  - Gemfile
88
89
  - LICENSE.txt
89
90
  - README.md
90
91
  - Rakefile
91
92
  - formatted-metrics.gemspec
92
93
  - 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
94
+ - lib/metrics.rb
95
+ - lib/metrics/configuration.rb
96
+ - lib/metrics/formatter.rb
97
+ - lib/metrics/handler.rb
98
+ - lib/metrics/instrumentable.rb
99
+ - lib/metrics/railtie.rb
100
+ - lib/metrics/version.rb
101
+ - spec/metrics/formatter_spec.rb
102
+ - spec/metrics/handler_spec.rb
103
+ - spec/metrics/instrumentable_spec.rb
104
+ - spec/metrics_spec.rb
102
105
  - spec/spec_helper.rb
103
106
  homepage: http://github.com/remind101/formatted-metrics
104
107
  licenses:
@@ -115,7 +118,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
115
118
  version: '0'
116
119
  segments:
117
120
  - 0
118
- hash: -4022766637914995296
121
+ hash: -3422730749197559336
119
122
  required_rubygems_version: !ruby/object:Gem::Requirement
120
123
  none: false
121
124
  requirements:
@@ -124,7 +127,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
127
  version: '0'
125
128
  segments:
126
129
  - 0
127
- hash: -4022766637914995296
130
+ hash: -3422730749197559336
128
131
  requirements: []
129
132
  rubyforge_project:
130
133
  rubygems_version: 1.8.23
@@ -132,7 +135,8 @@ signing_key:
132
135
  specification_version: 3
133
136
  summary: Easily output formatted metrics to stdout
134
137
  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/metrics/formatter_spec.rb
139
+ - spec/metrics/handler_spec.rb
140
+ - spec/metrics/instrumentable_spec.rb
141
+ - spec/metrics_spec.rb
138
142
  - spec/spec_helper.rb