statsd-instrument 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # StatsD client for Ruby apps
2
2
 
3
- ![Built on Travis](https://secure.travis-ci.org/Shopify/statsd-instrument.png?branch=master)
3
+ [![Built on Travis](https://secure.travis-ci.org/Shopify/statsd-instrument.png?branch=master)](https://secure.travis-ci.org/Shopify/statsd-instrument)
4
4
 
5
5
  This is a ruby client for statsd (http://github.com/etsy/statsd). It provides a lightweight way to track and measure metrics in your application.
6
6
 
@@ -12,11 +12,12 @@ end
12
12
  module StatsD
13
13
  class << self
14
14
  attr_accessor :host, :port, :mode, :logger, :enabled, :default_sample_rate,
15
- :prefix
15
+ :prefix, :implementation
16
16
  end
17
17
  self.enabled = true
18
18
  self.default_sample_rate = 1
19
-
19
+ self.implementation = :statsd
20
+
20
21
  TimeoutClass = defined?(::SystemTimer) ? ::SystemTimer : ::Timeout
21
22
 
22
23
  # StatsD.server = 'localhost:1234'
@@ -28,17 +29,17 @@ module StatsD
28
29
  module Instrument
29
30
  def statsd_measure(method, name)
30
31
  add_to_method(method, name, :measure) do |old_method, new_method, metric_name, *args|
31
- define_method(new_method) do |*args|
32
- StatsD.measure(metric_name) { send(old_method, *args) }
32
+ define_method(new_method) do |*args, &block|
33
+ StatsD.measure(metric_name) { send(old_method, *args, &block) }
33
34
  end
34
35
  end
35
36
  end
36
37
 
37
38
  def statsd_count_success(method, name)
38
39
  add_to_method(method, name, :count_success) do |old_method, new_method, metric_name|
39
- define_method(new_method) do |*args|
40
+ define_method(new_method) do |*args, &block|
40
41
  begin
41
- truthiness = result = send(old_method, *args)
42
+ truthiness = result = send(old_method, *args, &block)
42
43
  rescue
43
44
  truthiness = false
44
45
  raise
@@ -54,9 +55,9 @@ module StatsD
54
55
 
55
56
  def statsd_count_if(method, name)
56
57
  add_to_method(method, name, :count_if) do |old_method, new_method, metric_name|
57
- define_method(new_method) do |*args|
58
+ define_method(new_method) do |*args, &block|
58
59
  begin
59
- truthiness = result = send(old_method, *args)
60
+ truthiness = result = send(old_method, *args, &block)
60
61
  rescue
61
62
  truthiness = false
62
63
  raise
@@ -72,9 +73,9 @@ module StatsD
72
73
 
73
74
  def statsd_count(method, name)
74
75
  add_to_method(method, name, :count) do |old_method, new_method, metric_name|
75
- define_method(new_method) do |*args|
76
+ define_method(new_method) do |*args, &block|
76
77
  StatsD.increment(metric_name)
77
- send(old_method, *args)
78
+ send(old_method, *args, &block)
78
79
  end
79
80
  end
80
81
  end
@@ -102,7 +103,7 @@ module StatsD
102
103
  def self.measure(key, milli = nil)
103
104
  result = nil
104
105
  ms = milli || Benchmark.ms do
105
- result = yield
106
+ result = yield
106
107
  end
107
108
 
108
109
  write(key, ms, :ms)
@@ -114,9 +115,10 @@ module StatsD
114
115
  write(key, delta, :incr, sample_rate)
115
116
  end
116
117
 
117
- #gaugor:333|g
118
- def self.gauge(key, value, sample_rate = default_sample_rate)
119
- write(key, value, :g, sample_rate)
118
+ # gaugor:333|g
119
+ # guagor:1234|kv|@1339864935 (statsite)
120
+ def self.gauge(key, value, sample_rate_or_epoch = default_sample_rate)
121
+ write(key, value, :g, sample_rate_or_epoch)
120
122
  end
121
123
 
122
124
  private
@@ -136,10 +138,11 @@ module StatsD
136
138
  when :ms
137
139
  command << '|ms'
138
140
  when :g
139
- command << '|g'
141
+ command << (self.implementation == :statsite ? '|kv' : '|g')
140
142
  end
141
143
 
142
- command << "|@#{sample_rate}" if sample_rate < 1
144
+ command << "|@#{sample_rate}" if sample_rate < 1 || (self.implementation == :statsite && sample_rate > 1)
145
+ command << "\n" if self.implementation == :statsite
143
146
 
144
147
  if mode.to_s == 'production'
145
148
  socket_wrapper { socket.send(command, 0, host, port) }
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "statsd-instrument"
3
- s.version = '1.2.0'
3
+ s.version = '1.3.0'
4
4
  s.authors = ["Jesse Storimer"]
5
5
  s.email = ["jesse@shopify.com"]
6
6
  s.homepage = "http://github.com/shopify/statsd-instrument"
@@ -20,6 +20,10 @@ class ActiveMerchant::Base
20
20
  raise 'Not OK'
21
21
  end
22
22
  end
23
+
24
+ def post_with_block(&block)
25
+ yield if block_given?
26
+ end
23
27
  end
24
28
 
25
29
  class ActiveMerchant::Gateway < ActiveMerchant::Base
@@ -32,7 +36,7 @@ class ActiveMerchant::Gateway < ActiveMerchant::Base
32
36
 
33
37
  def self.sync
34
38
  true
35
- end
39
+ end
36
40
 
37
41
  def self.singleton_class
38
42
  class << self; self; end
@@ -65,6 +69,16 @@ class StatsDTest < Test::Unit::TestCase
65
69
  ActiveMerchant::Gateway.new.purchase(false)
66
70
  end
67
71
 
72
+ def test_statsd_count_if_with_method_receiving_block
73
+ ActiveMerchant::Base.statsd_count_if :post_with_block, 'ActiveMerchant.Base.post_with_block' do |result|
74
+ result[:success]
75
+ end
76
+
77
+ return_value = ActiveMerchant::Base.new.post_with_block {'block called'}
78
+
79
+ assert_equal 'block called', return_value
80
+ end
81
+
68
82
  def test_statsd_count_if_with_block
69
83
  ActiveMerchant::UniqueGateway.statsd_count_if :ssl_post, 'ActiveMerchant.Gateway.block' do |result|
70
84
  result[:success]
@@ -85,6 +99,16 @@ class StatsDTest < Test::Unit::TestCase
85
99
  ActiveMerchant::Gateway.new.purchase(false)
86
100
  end
87
101
 
102
+ def test_statsd_count_success_with_method_receiving_block
103
+ ActiveMerchant::Base.statsd_count_success :post_with_block, 'ActiveMerchant.Base.post_with_block' do |result|
104
+ result[:success]
105
+ end
106
+
107
+ return_value = ActiveMerchant::Base.new.post_with_block {'block called'}
108
+
109
+ assert_equal 'block called', return_value
110
+ end
111
+
88
112
  def test_statsd_count_success_with_block
89
113
  ActiveMerchant::UniqueGateway.statsd_count_success :ssl_post, 'ActiveMerchant.Gateway' do |result|
90
114
  result[:success]
@@ -104,6 +128,14 @@ class StatsDTest < Test::Unit::TestCase
104
128
  ActiveMerchant::Gateway.new.purchase(true)
105
129
  end
106
130
 
131
+ def test_statsd_count_with_method_receiving_block
132
+ ActiveMerchant::Base.statsd_count :post_with_block, 'ActiveMerchant.Base.post_with_block'
133
+
134
+ return_value = ActiveMerchant::Base.new.post_with_block {'block called'}
135
+
136
+ assert_equal 'block called', return_value
137
+ end
138
+
107
139
  def test_statsd_measure
108
140
  ActiveMerchant::UniqueGateway.statsd_measure :ssl_post, 'ActiveMerchant.Gateway.ssl_post'
109
141
 
@@ -111,6 +143,14 @@ class StatsDTest < Test::Unit::TestCase
111
143
  ActiveMerchant::UniqueGateway.new.purchase(true)
112
144
  end
113
145
 
146
+ def test_statsd_measure_with_method_receiving_block
147
+ ActiveMerchant::Base.statsd_measure :post_with_block, 'ActiveMerchant.Base.post_with_block'
148
+
149
+ return_value = ActiveMerchant::Base.new.post_with_block {'block called'}
150
+
151
+ assert_equal 'block called', return_value
152
+ end
153
+
114
154
  def test_instrumenting_class_method
115
155
  ActiveMerchant::Gateway.singleton_class.extend StatsD::Instrument
116
156
  ActiveMerchant::Gateway.singleton_class.statsd_count :sync, 'ActiveMerchant.Gateway.sync'
@@ -159,6 +199,30 @@ class StatsDTest < Test::Unit::TestCase
159
199
  StatsD.gauge('fooy', 42)
160
200
  end
161
201
 
202
+ def test_write_supports_statsite_gauge_syntax
203
+ StatsD.unstub(:gauge)
204
+
205
+ StatsD.mode = :production
206
+ StatsD.server = 'localhost:123'
207
+ StatsD.implementation = :statsite
208
+
209
+ UDPSocket.any_instance.expects(:send).with("fooy:42|kv\n", 0, 'localhost', 123)
210
+
211
+ StatsD.gauge('fooy', 42)
212
+ end
213
+
214
+ def test_write_supports_statsite_gauge_timestamp
215
+ StatsD.unstub(:gauge)
216
+
217
+ StatsD.mode = :production
218
+ StatsD.server = 'localhost:123'
219
+ StatsD.implementation = :statsite
220
+
221
+ UDPSocket.any_instance.expects(:send).with("fooy:42|kv|@123456\n", 0, 'localhost', 123)
222
+
223
+ StatsD.gauge('fooy', 42, 123456)
224
+ end
225
+
162
226
  def test_should_not_write_when_disabled
163
227
  StatsD.enabled = false
164
228
  StatsD.expects(:logger).never
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: statsd-instrument
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-20 00:00:00.000000000 Z
12
+ date: 2012-07-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mocha
16
- requirement: &2178819120 !ruby/object:Gem::Requirement
16
+ requirement: &2173609220 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2178819120
24
+ version_requirements: *2173609220
25
25
  description: A StatsD client for Ruby apps. Provides metaprogramming methods to inject
26
26
  StatsD instrumentation into your code.
27
27
  email: