statsd-instrument 1.6.2 → 1.7.0

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: 4332f173929c941bac2a915fed3be82bc447dec3
4
- data.tar.gz: 52aa66f9144a09740da7767d59e4846ff38d5ffe
3
+ metadata.gz: a8222e6b4a44fdc424a2f767b69d1871b84f52a6
4
+ data.tar.gz: 092c03b43435642c4f0ae8e098971bc4c412867f
5
5
  SHA512:
6
- metadata.gz: 57f6a5a32129aa3b62d76565e42d941bba4c83602454f4e05fb1383b201b826a1fd6aa120d74da5e1cd10340a528ed51ab0bcfb3e7eedc343ad8f3f2571033b9
7
- data.tar.gz: 8b76f2cdf7d83ce7dd9054b882129797b5d884cabd16c699fb6e29c7e3692723ecb6d361b6df91dca4b53be6e0b9de755735fd0df356fc6c880522f79a88cd18
6
+ metadata.gz: eb67acff19291c3a464c99b564e2bfcc29102acaa43e60f4adbafbc04ce67c296b22f32392c1297e3462032e58cb01a7ceecb1ff0de37ff2be6fa8ee63d9d9bb
7
+ data.tar.gz: 159d8332ccf49f9882caa18e8f0fa7cdc95fec53ab2fe0f946ef3e12cfa6bababd91553bf1fed8ca80a7eb4fd91c5b07f0b3f2a32193b8bcd1cc9589e9daa41f
data/README.md CHANGED
@@ -81,7 +81,7 @@ StatsD.increment('GoogleBase.insert')
81
81
  StatsD.increment('GoogleBase.insert', 10)
82
82
  # you can also specify a sample rate, so only 1/10 of events
83
83
  # actually get to statsd. Useful for very high volume data
84
- StatsD.increment('GoogleBase.insert', 1, 0.1)
84
+ StatsD.increment('GoogleBase.insert', 1, sample_rate: 0.1)
85
85
  ```
86
86
 
87
87
  #### StatsD.gauge
@@ -89,7 +89,7 @@ StatsD.increment('GoogleBase.insert', 1, 0.1)
89
89
  A gauge is a single numerical value value that tells you the state of the system at a point in time. A good example would be the number of messages in a queue.
90
90
 
91
91
  ``` ruby
92
- StatsD.gauge('GoogleBase.queued', 12, 1.0)
92
+ StatsD.gauge('GoogleBase.queued', 12, sample_rate: 1.0)
93
93
  ```
94
94
 
95
95
  Normally, you shouldn't update this value too often, and therefore there is no need to sample this kind metric.
@@ -100,7 +100,7 @@ A set keeps track of the number of unique values that have been seen. This is a
100
100
 
101
101
  ``` ruby
102
102
  # Submit the customer ID to the set. It will only be counted if it hasn't been seen before.
103
- StatsD.set('GoogleBase.customers', "12345", 1.0)
103
+ StatsD.set('GoogleBase.customers', "12345", sample_rate: 1.0)
104
104
  ```
105
105
 
106
106
  Because you are counting unique values, the results of using a sampling value less than 1.0 can lead to unexpected, hard to interpret results.
@@ -174,7 +174,6 @@ end
174
174
  You can instrument class methods, just like instance methods, using the metaprogramming methods. You simply have to configure the instrumentation on the singleton class of the Class you want to instrument.
175
175
 
176
176
  ```ruby
177
- AWS::S3::Base.singleton_class.extend StatsD::Instrument
178
177
  AWS::S3::Base.singleton_class.statsd_measure :request, 'S3.request'
179
178
  ```
180
179
 
@@ -189,6 +188,15 @@ passed.
189
188
  GoogleBase.statsd_count :insert, lamdba{|object, args| object.class.to_s.downcase + "." + args.first.to_s + ".insert" }
190
189
  ```
191
190
 
191
+ ### Tags
192
+
193
+ The Datadog implementation support tags, which you can use to slice and dice metrics in their UI. You can specify a list of tags as an option, either standalone tag (e.g. `"mytag"`), or key value based, separated by a colon: `"env:production"`.
194
+
195
+ ``` ruby
196
+ StatsD.increment('my.counter', tags: ['env:production', 'unicorn'])
197
+ GoogleBase.statsd_count :insert, 'GoogleBase.insert', tags: ['env:production']
198
+ ```
199
+
192
200
  ## Reliance on DNS
193
201
 
194
202
  Out of the box StatsD is set up to be unidirectional fire-and-forget over UDP. Configuring the StatsD host to be a non-ip will trigger a DNS lookup (ie synchronous round trip network call) for each metric sent. This can be particularly problematic in clouds that have a shared DNS infrastructure such as AWS.
@@ -4,42 +4,21 @@ require 'benchmark'
4
4
  require 'statsd/instrument/version'
5
5
 
6
6
  module StatsD
7
- class << self
8
- attr_accessor :host, :port, :mode, :logger, :enabled, :default_sample_rate,
9
- :prefix, :implementation
10
- end
11
-
12
- def self.server=(conn)
13
- self.host, port = conn.split(':')
14
- self.port = port.to_i
15
- invalidate_socket
16
- end
17
-
18
- def self.host=(host)
19
- @host = host
20
- invalidate_socket
21
- end
22
-
23
- def self.port=(port)
24
- @port = port
25
- invalidate_socket
26
- end
27
-
28
7
  module Instrument
29
8
 
30
9
  def self.generate_metric_name(metric_name, callee, *args)
31
10
  metric_name.respond_to?(:call) ? metric_name.call(callee, args).gsub('::', '.') : metric_name.gsub('::', '.')
32
11
  end
33
12
 
34
- def statsd_measure(method, name, sample_rate = StatsD.default_sample_rate)
13
+ def statsd_measure(method, name, *metric_options)
35
14
  add_to_method(method, name, :measure) do |old_method, new_method, metric_name, *args|
36
15
  define_method(new_method) do |*args, &block|
37
- StatsD.measure(StatsD::Instrument.generate_metric_name(metric_name, self, *args), nil, sample_rate) { send(old_method, *args, &block) }
16
+ StatsD.measure(StatsD::Instrument.generate_metric_name(metric_name, self, *args), nil, *metric_options) { send(old_method, *args, &block) }
38
17
  end
39
18
  end
40
19
  end
41
20
 
42
- def statsd_count_success(method, name, sample_rate = StatsD.default_sample_rate)
21
+ def statsd_count_success(method, name, *metric_options)
43
22
  add_to_method(method, name, :count_success) do |old_method, new_method, metric_name|
44
23
  define_method(new_method) do |*args, &block|
45
24
  begin
@@ -52,13 +31,13 @@ module StatsD
52
31
  result
53
32
  ensure
54
33
  suffix = truthiness == false ? 'failure' : 'success'
55
- StatsD.increment("#{StatsD::Instrument.generate_metric_name(metric_name, self, *args)}.#{suffix}", 1, sample_rate)
34
+ StatsD.increment("#{StatsD::Instrument.generate_metric_name(metric_name, self, *args)}.#{suffix}", 1, *metric_options)
56
35
  end
57
36
  end
58
37
  end
59
38
  end
60
39
 
61
- def statsd_count_if(method, name, sample_rate = StatsD.default_sample_rate)
40
+ def statsd_count_if(method, name, *metric_options)
62
41
  add_to_method(method, name, :count_if) do |old_method, new_method, metric_name|
63
42
  define_method(new_method) do |*args, &block|
64
43
  begin
@@ -70,16 +49,16 @@ module StatsD
70
49
  truthiness = (yield(result) rescue false) if block_given?
71
50
  result
72
51
  ensure
73
- StatsD.increment(StatsD::Instrument.generate_metric_name(metric_name, self, *args), sample_rate) if truthiness
52
+ StatsD.increment(StatsD::Instrument.generate_metric_name(metric_name, self, *args), *metric_options) if truthiness
74
53
  end
75
54
  end
76
55
  end
77
56
  end
78
57
 
79
- def statsd_count(method, name, sample_rate = StatsD.default_sample_rate)
58
+ def statsd_count(method, name, *metric_options)
80
59
  add_to_method(method, name, :count) do |old_method, new_method, metric_name|
81
60
  define_method(new_method) do |*args, &block|
82
- StatsD.increment(StatsD::Instrument.generate_metric_name(metric_name, self, *args), 1, sample_rate)
61
+ StatsD.increment(StatsD::Instrument.generate_metric_name(metric_name, self, *args), 1, *metric_options)
83
62
  send(old_method, *args, &block)
84
63
  end
85
64
  end
@@ -129,103 +108,138 @@ module StatsD
129
108
  end
130
109
  end
131
110
 
132
- # glork:320|ms
133
- def self.measure(key, milli = nil, sample_rate = default_sample_rate, tags = nil)
134
- result = nil
135
- ms = milli || 1000 * Benchmark.realtime do
136
- result = yield
111
+ class << self
112
+ attr_accessor :host, :port, :mode, :logger, :enabled, :default_sample_rate, :prefix, :implementation
113
+
114
+ def server=(conn)
115
+ self.host, port = conn.split(':')
116
+ self.port = port.to_i
117
+ invalidate_socket
137
118
  end
138
119
 
139
- collect(key, ms, :ms, sample_rate, tags)
140
- result
141
- end
120
+ def host=(host)
121
+ @host = host
122
+ invalidate_socket
123
+ end
142
124
 
143
- # gorets:1|c
144
- def self.increment(key, delta = 1, sample_rate = default_sample_rate, tags = nil)
145
- collect(key, delta, :incr, sample_rate, tags)
146
- end
125
+ def port=(port)
126
+ @port = port
127
+ invalidate_socket
128
+ end
147
129
 
148
- # gaugor:333|g
149
- # guagor:1234|kv|@1339864935 (statsite)
150
- def self.gauge(key, value, sample_rate_or_epoch = default_sample_rate, tags = nil)
151
- collect(key, value, :g, sample_rate_or_epoch, tags)
152
- end
130
+ def invalidate_socket
131
+ @socket = nil
132
+ end
153
133
 
154
- # histogram:123.45|h
155
- def self.histogram(key, value, sample_rate_or_epoch = default_sample_rate, tags = nil)
156
- collect(key, value, :h, sample_rate_or_epoch, tags)
157
- end
134
+ # glork:320|ms
135
+ def measure(key, value = nil, *metric_options)
136
+ if value.is_a?(Hash) && metric_options.empty?
137
+ metric_options = [value]
138
+ value = nil
139
+ end
158
140
 
159
- # uniques:765|s
160
- def self.set(key, value, sample_rate_or_epoch = default_sample_rate, tags = nil)
161
- collect(key, value, :s, sample_rate_or_epoch, tags)
162
- end
141
+ result = nil
142
+ ms = value || 1000 * Benchmark.realtime do
143
+ result = yield
144
+ end
163
145
 
164
- private
146
+ collect(:ms, key, ms, hash_argument(metric_options))
147
+ result
148
+ end
165
149
 
166
- def self.invalidate_socket
167
- @socket = nil
168
- end
150
+ # gorets:1|c
151
+ def increment(key, value = 1, *metric_options)
152
+ if value.is_a?(Hash) && metric_options.empty?
153
+ metric_options = [value]
154
+ value = 1
155
+ end
169
156
 
170
- def self.socket
171
- if @socket.nil?
172
- @socket = UDPSocket.new
173
- @socket.connect(host, port)
157
+ collect(:c, key, value, hash_argument(metric_options))
174
158
  end
175
- @socket
176
- end
177
159
 
178
- def self.collect(k, v, op, sample_rate = default_sample_rate, tags = nil)
179
- return unless enabled
180
- return if sample_rate < 1 && rand > sample_rate
160
+ # gaugor:333|g
161
+ # guagor:1234|kv|@1339864935 (statsite)
162
+ def gauge(key, value, *metric_options)
163
+ collect(:g, key, value, hash_argument(metric_options))
164
+ end
181
165
 
182
- command = generate_packet(k, v, op, sample_rate, tags)
183
- write_packet(command)
184
- end
166
+ # histogram:123.45|h
167
+ def histogram(key, value, *metric_options)
168
+ raise NotImplementedError, "StatsD.histogram only supported on :datadog implementation." unless self.implementation == :datadog
169
+ collect(:h, key, value, hash_argument(metric_options))
170
+ end
171
+
172
+ def key_value(key, value, *metric_options)
173
+ raise NotImplementedError, "StatsD.key_value only supported on :statsite implementation." unless self.implementation == :statsite
174
+ collect(:kv, key, value, hash_argument(metric_options))
175
+ end
176
+
177
+ # uniques:765|s
178
+ def set(key, value, *metric_options)
179
+ collect(:s, key, value, hash_argument(metric_options))
180
+ end
181
+
182
+ private
183
+
184
+ def hash_argument(args)
185
+ return {} if args.length == 0
186
+ return args.first if args.length == 1 && args.first.is_a?(Hash)
187
+
188
+ order = [:sample_rate, :tags]
189
+ hash = {}
190
+ args.each_with_index do |value, index|
191
+ hash[order[index]] = value
192
+ end
193
+
194
+ return hash
195
+ end
196
+
197
+ def socket
198
+ if @socket.nil?
199
+ @socket = UDPSocket.new
200
+ @socket.connect(host, port)
201
+ end
202
+ @socket
203
+ end
204
+
205
+ def collect(type, k, v, options = {})
206
+ return unless enabled
207
+ sample_rate = options[:sample_rate] || StatsD.default_sample_rate
208
+ return if sample_rate < 1 && rand > sample_rate
185
209
 
186
- def self.write_packet(command)
187
- if mode.to_s == 'production'
188
- begin
210
+ packet = generate_packet(type, k, v, sample_rate, options[:tags])
211
+ write_packet(packet)
212
+ end
213
+
214
+ def write_packet(command)
215
+ if mode.to_s == 'production'
189
216
  socket.send(command, 0)
190
- rescue SocketError, IOError, SystemCallError => e
191
- logger.error e
192
- end
193
- else
194
- logger.info "[StatsD] #{command}"
217
+ else
218
+ logger.info "[StatsD] #{command}"
219
+ end
220
+ rescue SocketError, IOError, SystemCallError => e
221
+ logger.error e
195
222
  end
196
- end
197
223
 
198
- def self.clean_tags(tags)
199
- tags.map do |tag|
200
- components = tag.split(':', 2)
201
- components.map { |c| c.gsub(/[^\w\.-]+/, '_') }.join(':')
224
+ def clean_tags(tags)
225
+ tags.map do |tag|
226
+ components = tag.split(':', 2)
227
+ components.map { |c| c.gsub(/[^\w\.-]+/, '_') }.join(':')
228
+ end
202
229
  end
203
- end
204
230
 
205
- def self.generate_packet(k, v, op, sample_rate = default_sample_rate, tags = nil)
206
- command = "#{self.prefix + '.' if self.prefix}#{k}:#{v}"
207
- case op
208
- when :incr
209
- command << '|c'
210
- when :ms
211
- command << '|ms'
212
- when :g
213
- command << (self.implementation == :statsite ? '|kv' : '|g')
214
- when :h
215
- raise NotImplementedError, "Histograms only supported on DataDog implementation." unless self.implementation == :datadog
216
- command << '|h'
217
- when :s
218
- command << '|s'
219
- end
220
-
221
- command << "|@#{sample_rate}" if sample_rate < 1 || (self.implementation == :statsite && sample_rate > 1)
222
- if tags
223
- raise ArgumentError, "Tags are only supported on Datadog" unless self.implementation == :datadog
224
- command << "|##{clean_tags(tags).join(',')}"
225
- end
226
-
227
- command << "\n" if self.implementation == :statsite
228
- return command
231
+ def generate_packet(type, k, v, sample_rate = default_sample_rate, tags = nil)
232
+ command = self.prefix ? self.prefix + '.' : ''
233
+ command << "#{k}:#{v}|#{type}"
234
+ command << "|@#{sample_rate}" if sample_rate < 1 || (self.implementation == :statsite && sample_rate > 1)
235
+ if tags
236
+ raise ArgumentError, "Tags are only supported on :datadog implementation" unless self.implementation == :datadog
237
+ command << "|##{clean_tags(tags).join(',')}"
238
+ end
239
+
240
+ command << "\n" if self.implementation == :statsite
241
+ command
242
+ end
229
243
  end
230
244
  end
231
245
 
@@ -1,5 +1,5 @@
1
1
  module StatsD
2
2
  module Instrument
3
- VERSION = "1.6.2"
3
+ VERSION = "1.7.0"
4
4
  end
5
5
  end
@@ -51,7 +51,7 @@ class StatsDInstrumentationTest < Test::Unit::TestCase
51
51
  def test_statsd_count_if
52
52
  ActiveMerchant::Gateway.statsd_count_if :ssl_post, 'ActiveMerchant.Gateway.if'
53
53
 
54
- StatsD.expects(:increment).with('ActiveMerchant.Gateway.if', 1).once
54
+ StatsD.expects(:increment).with('ActiveMerchant.Gateway.if').once
55
55
  ActiveMerchant::Gateway.new.purchase(true)
56
56
  ActiveMerchant::Gateway.new.purchase(false)
57
57
 
@@ -63,7 +63,7 @@ class StatsDInstrumentationTest < Test::Unit::TestCase
63
63
  result == 'true'
64
64
  end
65
65
 
66
- StatsD.expects(:collect).with('ActiveMerchant.Base.post_with_block', 1, :incr, 1.0, nil).once
66
+ StatsD.expects(:collect).with(:c, 'ActiveMerchant.Base.post_with_block', 1, {}).once
67
67
  assert_equal 'true', ActiveMerchant::Base.new.post_with_block { 'true' }
68
68
  assert_equal 'false', ActiveMerchant::Base.new.post_with_block { 'false' }
69
69
 
@@ -75,7 +75,7 @@ class StatsDInstrumentationTest < Test::Unit::TestCase
75
75
  result[:success]
76
76
  end
77
77
 
78
- StatsD.expects(:increment).with('ActiveMerchant.Gateway.block', 1).once
78
+ StatsD.expects(:increment).with('ActiveMerchant.Gateway.block').once
79
79
  ActiveMerchant::UniqueGateway.new.purchase(true)
80
80
  ActiveMerchant::UniqueGateway.new.purchase(false)
81
81
 
@@ -99,8 +99,8 @@ class StatsDInstrumentationTest < Test::Unit::TestCase
99
99
  result == 'successful'
100
100
  end
101
101
 
102
- StatsD.expects(:collect).with('ActiveMerchant.Base.post_with_block.success', 1, :incr, 1.0, nil).once
103
- StatsD.expects(:collect).with('ActiveMerchant.Base.post_with_block.failure', 1, :incr, 1.0, nil).once
102
+ StatsD.expects(:collect).with(:c, 'ActiveMerchant.Base.post_with_block.success', 1, {}).once
103
+ StatsD.expects(:collect).with(:c, 'ActiveMerchant.Base.post_with_block.failure', 1, {}).once
104
104
 
105
105
  assert_equal 'successful', ActiveMerchant::Base.new.post_with_block { 'successful' }
106
106
  assert_equal 'not so successful', ActiveMerchant::Base.new.post_with_block { 'not so successful' }
@@ -113,10 +113,10 @@ class StatsDInstrumentationTest < Test::Unit::TestCase
113
113
  result[:success]
114
114
  end
115
115
 
116
- StatsD.expects(:increment).with('ActiveMerchant.Gateway.success', 1, StatsD.default_sample_rate)
116
+ StatsD.expects(:increment).with('ActiveMerchant.Gateway.success', 1)
117
117
  ActiveMerchant::UniqueGateway.new.purchase(true)
118
118
 
119
- StatsD.expects(:increment).with('ActiveMerchant.Gateway.failure', 1, StatsD.default_sample_rate)
119
+ StatsD.expects(:increment).with('ActiveMerchant.Gateway.failure', 1)
120
120
  ActiveMerchant::UniqueGateway.new.purchase(false)
121
121
 
122
122
  ActiveMerchant::UniqueGateway.statsd_remove_count_success :ssl_post, 'ActiveMerchant.Gateway'
@@ -125,7 +125,7 @@ class StatsDInstrumentationTest < Test::Unit::TestCase
125
125
  def test_statsd_count
126
126
  ActiveMerchant::Gateway.statsd_count :ssl_post, 'ActiveMerchant.Gateway.ssl_post'
127
127
 
128
- StatsD.expects(:increment).with('ActiveMerchant.Gateway.ssl_post', 1, StatsD.default_sample_rate)
128
+ StatsD.expects(:increment).with('ActiveMerchant.Gateway.ssl_post', 1)
129
129
  ActiveMerchant::Gateway.new.purchase(true)
130
130
 
131
131
  ActiveMerchant::Gateway.statsd_remove_count :ssl_post, 'ActiveMerchant.Gateway.ssl_post'
@@ -135,7 +135,7 @@ class StatsDInstrumentationTest < Test::Unit::TestCase
135
135
  metric_namer = lambda { |object, args| object.class.to_s.downcase + ".insert." + args.first.to_s }
136
136
  ActiveMerchant::Gateway.statsd_count(:ssl_post, metric_namer)
137
137
 
138
- StatsD.expects(:increment).with('gatewaysubclass.insert.true', 1, StatsD.default_sample_rate)
138
+ StatsD.expects(:increment).with('gatewaysubclass.insert.true', 1)
139
139
  GatewaySubClass.new.purchase(true)
140
140
 
141
141
  ActiveMerchant::Gateway.statsd_remove_count(:ssl_post, metric_namer)
@@ -144,7 +144,7 @@ class StatsDInstrumentationTest < Test::Unit::TestCase
144
144
  def test_statsd_count_with_method_receiving_block
145
145
  ActiveMerchant::Base.statsd_count :post_with_block, 'ActiveMerchant.Base.post_with_block'
146
146
 
147
- StatsD.expects(:collect).with('ActiveMerchant.Base.post_with_block', 1, :incr, 1.0, nil)
147
+ StatsD.expects(:collect).with(:c, 'ActiveMerchant.Base.post_with_block', 1, {})
148
148
  assert_equal 'block called', ActiveMerchant::Base.new.post_with_block { 'block called' }
149
149
 
150
150
  ActiveMerchant::Base.statsd_remove_count :post_with_block, 'ActiveMerchant.Base.post_with_block'
@@ -153,7 +153,7 @@ class StatsDInstrumentationTest < Test::Unit::TestCase
153
153
  def test_statsd_measure_with_nested_modules
154
154
  ActiveMerchant::UniqueGateway.statsd_measure :ssl_post, 'ActiveMerchant::Gateway.ssl_post'
155
155
 
156
- StatsD.expects(:measure).with('ActiveMerchant.Gateway.ssl_post', nil, StatsD.default_sample_rate)
156
+ StatsD.expects(:measure).with('ActiveMerchant.Gateway.ssl_post', nil)
157
157
  ActiveMerchant::UniqueGateway.new.purchase(true)
158
158
 
159
159
  ActiveMerchant::UniqueGateway.statsd_remove_measure :ssl_post, 'ActiveMerchant::Gateway.ssl_post'
@@ -171,7 +171,7 @@ class StatsDInstrumentationTest < Test::Unit::TestCase
171
171
  def test_statsd_measure_with_method_receiving_block
172
172
  ActiveMerchant::Base.statsd_measure :post_with_block, 'ActiveMerchant.Base.post_with_block'
173
173
 
174
- StatsD.expects(:collect).with('ActiveMerchant.Base.post_with_block', is_a(Float), :ms, 1.0, nil)
174
+ StatsD.expects(:collect).with(:ms, 'ActiveMerchant.Base.post_with_block', is_a(Float), {})
175
175
  assert_equal 'block called', ActiveMerchant::Base.new.post_with_block { 'block called' }
176
176
 
177
177
  ActiveMerchant::Base.statsd_remove_measure :post_with_block, 'ActiveMerchant.Base.post_with_block'
@@ -181,7 +181,17 @@ class StatsDInstrumentationTest < Test::Unit::TestCase
181
181
  ActiveMerchant::Gateway.singleton_class.extend StatsD::Instrument
182
182
  ActiveMerchant::Gateway.singleton_class.statsd_count :sync, 'ActiveMerchant.Gateway.sync'
183
183
 
184
- StatsD.expects(:increment).with('ActiveMerchant.Gateway.sync', 1, StatsD.default_sample_rate)
184
+ StatsD.expects(:increment).with('ActiveMerchant.Gateway.sync', 1)
185
+ ActiveMerchant::Gateway.sync
186
+
187
+ ActiveMerchant::Gateway.singleton_class.statsd_remove_count :sync, 'ActiveMerchant.Gateway.sync'
188
+ end
189
+
190
+ def test_statsd_count_with_tags
191
+ ActiveMerchant::Gateway.singleton_class.extend StatsD::Instrument
192
+ ActiveMerchant::Gateway.singleton_class.statsd_count :sync, 'ActiveMerchant.Gateway.sync', :tags => ['t']
193
+
194
+ StatsD.expects(:increment).with('ActiveMerchant.Gateway.sync', 1, :tags => ['t'])
185
195
  ActiveMerchant::Gateway.sync
186
196
 
187
197
  ActiveMerchant::Gateway.singleton_class.statsd_remove_count :sync, 'ActiveMerchant.Gateway.sync'
data/test/statsd_test.rb CHANGED
@@ -16,38 +16,61 @@ class StatsDTest < Test::Unit::TestCase
16
16
  end
17
17
 
18
18
  def test_statsd_measure_with_explicit_value
19
- StatsD.expects(:collect).with('values.foobar', 42, :ms, 1.0, nil)
19
+ StatsD.expects(:collect).with(:ms, 'values.foobar', 42, {})
20
20
  StatsD.measure('values.foobar', 42)
21
21
  end
22
22
 
23
23
  def test_statsd_measure_with_explicit_value_and_sample_rate
24
- StatsD.expects(:collect).with('values.foobar', 42, :ms, 0.1, nil)
25
- StatsD.measure('values.foobar', 42, 0.1)
24
+ StatsD.expects(:collect).with(:ms, 'values.foobar', 42, :sample_rate => 0.1)
25
+ StatsD.measure('values.foobar', 42, :sample_rate => 0.1)
26
26
  end
27
27
 
28
28
  def test_statsd_measure_with_benchmarked_value
29
29
  Benchmark.stubs(:realtime).returns(1.12)
30
- StatsD.expects(:collect).with('values.foobar', 1120.0, :ms, 1.0, nil)
31
- StatsD.measure('values.foobar', nil) do
30
+ StatsD.expects(:collect).with(:ms, 'values.foobar', 1120.0, {})
31
+ StatsD.measure('values.foobar') do
32
32
  #noop
33
33
  end
34
34
  end
35
35
 
36
+ def test_statsd_measure_with_benchmarked_value_and_options
37
+ Benchmark.stubs(:realtime).returns(1.12)
38
+ StatsD.expects(:collect).with(:ms, 'values.foobar', 1120.0, :sample_rate => 1.0)
39
+ StatsD.measure('values.foobar', :sample_rate => 1.0) do
40
+ #noop
41
+ end
42
+ end
43
+
44
+ def test_statsd_increment_with_hash_argument
45
+ StatsD.expects(:collect).with(:c, 'values.foobar', 1, :tags => ['test'])
46
+ StatsD.increment('values.foobar', :tags => ['test'])
47
+ end
48
+
49
+ def test_statsd_increment_with_multiple_arguments
50
+ StatsD.expects(:collect).with(:c, 'values.foobar', 12, :sample_rate => nil, :tags => ['test'])
51
+ StatsD.increment('values.foobar', 12, nil, ['test'])
52
+ end
53
+
36
54
  def test_statsd_gauge
37
- StatsD.expects(:collect).with('values.foobar', 12, :g, 1.0, nil)
55
+ StatsD.expects(:collect).with(:g, 'values.foobar', 12, {})
38
56
  StatsD.gauge('values.foobar', 12)
39
57
  end
40
58
 
41
59
  def test_statsd_set
42
- StatsD.expects(:collect).with('values.foobar', 12, :s, 1.0, nil)
60
+ StatsD.expects(:collect).with(:s, 'values.foobar', 12, {})
43
61
  StatsD.set('values.foobar', 12)
44
62
  end
45
63
 
46
64
  def test_statsd_histogram_on_datadog
47
65
  StatsD.stubs(:implementation).returns(:datadog)
48
- StatsD.expects(:collect).with('values.hg', 12.33, :h, 0.2, ['tag_123', 'key-name:value123'])
49
- StatsD.histogram('values.hg', 12.33, 0.2, ['tag_123', 'key-name:value123'])
50
- end
66
+ StatsD.expects(:collect).with(:h, 'values.hg', 12.33, :sample_rate => 0.2, :tags => ['tag_123', 'key-name:value123'])
67
+ StatsD.histogram('values.hg', 12.33, :sample_rate => 0.2, :tags => ['tag_123', 'key-name:value123'])
68
+ end
69
+
70
+ def test_raise_when_using_histograms_and_not_on_datadog
71
+ StatsD.stubs(:implementation).returns(:other)
72
+ assert_raises(NotImplementedError) { StatsD.histogram('foohg', 3.33) }
73
+ end
51
74
 
52
75
  def test_collect_respects_enabled
53
76
  StatsD.stubs(:enabled).returns(false)
@@ -119,26 +142,31 @@ class StatsDTest < Test::Unit::TestCase
119
142
  def test_rewrite_shitty_tags
120
143
  StatsD.stubs(:implementation).returns(:datadog)
121
144
 
122
- assert_equal ['igno_red'], StatsD.clean_tags(['igno,red'])
123
- assert_equal ['igno_red'], StatsD.clean_tags(['igno red'])
124
- assert_equal ['test:test_test'], StatsD.clean_tags(['test:test:test'])
145
+ assert_equal ['igno_red'], StatsD.send(:clean_tags, ['igno,red'])
146
+ assert_equal ['igno_red'], StatsD.send(:clean_tags, ['igno red'])
147
+ assert_equal ['test:test_test'], StatsD.send(:clean_tags, ['test:test:test'])
125
148
 
126
149
  StatsD.expects(:write_packet).with("fooc:3|c|#topic:foo_foo,bar_")
127
150
  StatsD.increment('fooc', 3, 1.0, ['topic:foo : foo', 'bar '])
128
151
  end
129
152
 
130
- def test_supports_gauge_syntax_on_statsite
153
+ def test_supports_key_value_syntax_on_statsite
131
154
  StatsD.stubs(:implementation).returns(:statsite)
132
155
 
133
156
  StatsD.expects(:write_packet).with("fooy:42|kv\n")
134
- StatsD.gauge('fooy', 42)
157
+ StatsD.key_value('fooy', 42)
135
158
  end
136
159
 
137
- def test_supports_gauge_timestamp_on_statsite
160
+ def test_supports_key_value_with_timestamp_on_statsite
138
161
  StatsD.stubs(:implementation).returns(:statsite)
139
162
 
140
163
  StatsD.expects(:write_packet).with("fooy:42|kv|@123456\n")
141
- StatsD.gauge('fooy', 42, 123456)
164
+ StatsD.key_value('fooy', 42, 123456)
165
+ end
166
+
167
+ def test_raise_when_using_key_value_and_not_on_statsite
168
+ StatsD.stubs(:implementation).returns(:other)
169
+ assert_raises(NotImplementedError) { StatsD.key_value('fookv', 3.33) }
142
170
  end
143
171
 
144
172
  def test_support_key_prefix
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.6.2
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jesse Storimer
@@ -9,34 +9,34 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-06 00:00:00.000000000 Z
12
+ date: 2014-02-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - '>='
18
+ - - ">="
19
19
  - !ruby/object:Gem::Version
20
20
  version: '0'
21
21
  type: :development
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - '>='
25
+ - - ">="
26
26
  - !ruby/object:Gem::Version
27
27
  version: '0'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: mocha
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - '>='
32
+ - - ">="
33
33
  - !ruby/object:Gem::Version
34
34
  version: '0'
35
35
  type: :development
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - '>='
39
+ - - ">="
40
40
  - !ruby/object:Gem::Version
41
41
  version: '0'
42
42
  description: A StatsD client for Ruby appspec. Provides metaprogramming methods to
@@ -47,8 +47,8 @@ executables: []
47
47
  extensions: []
48
48
  extra_rdoc_files: []
49
49
  files:
50
- - .gitignore
51
- - .travis.yml
50
+ - ".gitignore"
51
+ - ".travis.yml"
52
52
  - Gemfile
53
53
  - LICENSE
54
54
  - README.md
@@ -70,17 +70,17 @@ require_paths:
70
70
  - lib
71
71
  required_ruby_version: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - '>='
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  required_rubygems_version: !ruby/object:Gem::Requirement
77
77
  requirements:
78
- - - '>='
78
+ - - ">="
79
79
  - !ruby/object:Gem::Version
80
80
  version: '0'
81
81
  requirements: []
82
82
  rubyforge_project:
83
- rubygems_version: 2.0.3
83
+ rubygems_version: 2.2.0
84
84
  signing_key:
85
85
  specification_version: 4
86
86
  summary: A StatsD client for Ruby apps