statsd-instrument 2.3.2 → 2.3.4

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
  SHA256:
3
- metadata.gz: 03ab2f74c3b653690d20eea1102515c64d901f739599816a0813cb9ab748d950
4
- data.tar.gz: 528dee5c361d7080a16a6a4da64057cc81596bbd9906eeb220fc731b30feb4f2
3
+ metadata.gz: a8076b4d86af6ed043e664abf288c4d33a89d324017eeb1a8ae230266352cb9c
4
+ data.tar.gz: a56269d70cbca5d0788838f96e9515f92cf93b1330d3ef826620c1af5ae0ba72
5
5
  SHA512:
6
- metadata.gz: 3c4ecdac1ee080fb1f92ca316f24669cccdc803fbb205ce577fbe45a58cb313cae98e9e7e04fc579ab230a99d1ae1353cfafe0d8d7c056ff7a98a3af35d1ea9e
7
- data.tar.gz: d9f9161e46aa12696eeaabf9d975cac6a64118e608d2b16978a3528b50459014dbf1f50623a54e8021fba7b504067c6c8a942e4e36907830fa939338588cb60f
6
+ metadata.gz: 9e922ee94f9bcbf22390cc9ac2e740a539fb63b162d77529d4a32bb058de5e047de8519808457d86f744519fccd9ae639a6c20d022d67f6845add5769af10356
7
+ data.tar.gz: 50f3c7a81c69d5b26196f38d4716d03b24dbc52147c6c2894edf41dd4e7d7efa7fdc725d4f669213327121eca9d0e771469d3aa4d6b20bb86252d30b5a5b57e9
data/CHANGELOG.md CHANGED
@@ -5,6 +5,34 @@ please at an entry to the "unreleased changes" section below.
5
5
 
6
6
  ### Unreleased changes
7
7
 
8
+ ## Version 2.3.4
9
+
10
+ - Improve performance of `Metric#to_s` (#152)
11
+ - Fix bug in escaping newlines for events with Datadog Backend (#153)
12
+
13
+ ## Version 2.3.3
14
+
15
+ - Capture measure and distribution metrics on exception and early return (#134)
16
+
17
+ NOTE: Now that exceptions are measured statistics may behave differently. An exception example:
18
+ ```
19
+ StatsD.measure('myhttpcall') do
20
+ my_http_object.invoke
21
+ end
22
+ ```
23
+ Version 2.3.2 and below did not track metrics whenever a HTTP Timeout exception was raised.
24
+ 2.3.3 and above will include those metrics which may increase the values included.
25
+
26
+ A return example:
27
+ ```
28
+ StatsD.measure('myexpensivecalculation') do
29
+ return if expensive_calculation_disabled?
30
+ expensive_calculation
31
+ end
32
+ ```
33
+ If `expensive_calculation_disabled?` is true 50% of the time version 2.3.2 will drop the
34
+ average metric considerably.
35
+
8
36
  ## Version 2.3.2
9
37
 
10
38
  - Add option to override global prefix for metrics (#148)
data/CONTRIBUTING.md CHANGED
@@ -11,7 +11,7 @@ This project is MIT licensed.
11
11
 
12
12
  Report issues using the [Github issues tracker](https://github.com/Shopify/statsd-instrument/issues/new).
13
13
 
14
- When reporting issues, please incldue the following information:
14
+ When reporting issues, please include the following information:
15
15
 
16
16
  - Your Ruby interpreter version.
17
17
  - The statsd-instrument version. **Note:** only the latest version is supported.
@@ -58,17 +58,13 @@ module StatsD
58
58
 
59
59
  if Process.respond_to?(:clock_gettime)
60
60
  # @private
61
- def self.duration
62
- start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
63
- yield
64
- Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
61
+ def self.current_timestamp
62
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
65
63
  end
66
64
  else
67
65
  # @private
68
- def self.duration
69
- start = Time.now
70
- yield
71
- Time.now - start
66
+ def self.current_timestamp
67
+ Time.now
72
68
  end
73
69
  end
74
70
 
@@ -310,11 +306,16 @@ module StatsD
310
306
  value, metric_options = parse_options(value, metric_options)
311
307
  type = (!metric_options.empty? && metric_options.first[:as_dist] ? :d : :ms)
312
308
 
313
- result = nil
314
- value = 1000 * StatsD::Instrument.duration { result = block.call } if block_given?
315
- metric = collect_metric(type, key, value, metric_options)
316
- result = metric unless block_given?
317
- result
309
+ return collect_metric(type, key, value, metric_options) unless block_given?
310
+
311
+ start = StatsD::Instrument.current_timestamp
312
+ begin
313
+ block.call
314
+ ensure
315
+ # Ensure catches both a raised exception and a return in the invoked block
316
+ value = 1000 * (StatsD::Instrument.current_timestamp - start)
317
+ collect_metric(type, key, value, metric_options)
318
+ end
318
319
  end
319
320
 
320
321
  # Emits a counter metric.
@@ -373,11 +374,16 @@ module StatsD
373
374
  # end
374
375
  def distribution(key, value=nil, *metric_options, &block)
375
376
  value, metric_options = parse_options(value, metric_options)
376
- result = nil
377
- value = 1000 * StatsD::Instrument.duration { result = block.call } if block_given?
378
- metric = collect_metric(:d, key, value, metric_options)
379
- result = metric unless block_given?
380
- result
377
+
378
+ return collect_metric(:d, key, value, metric_options) unless block_given?
379
+
380
+ start = StatsD::Instrument.current_timestamp
381
+ begin
382
+ block.call
383
+ ensure
384
+ value = 1000 * (StatsD::Instrument.current_timestamp - start)
385
+ collect_metric(:d, key, value, metric_options)
386
+ end
381
387
  end
382
388
 
383
389
  # Emits a key/value metric.
@@ -27,8 +27,8 @@ module StatsD::Instrument::Backends
27
27
  packet = ""
28
28
 
29
29
  if metric.type == :_e
30
- escaped_title = metric.name.tr('\n', '\\n')
31
- escaped_text = metric.value.tr('\n', '\\n')
30
+ escaped_title = metric.name.gsub("\n", "\\n")
31
+ escaped_text = metric.value.gsub("\n", "\\n")
32
32
 
33
33
  packet << "_e{#{escaped_title.size},#{escaped_text.size}}:#{escaped_title}|#{escaped_text}"
34
34
  packet << generate_metadata(metric, EVENT_OPTIONS)
@@ -83,7 +83,7 @@ class StatsD::Instrument::Metric
83
83
  def to_s
84
84
  str = "#{TYPES[type]} #{name}:#{value}"
85
85
  str << " @#{sample_rate}" if sample_rate != 1.0
86
- str << " " << tags.map { |t| "##{t}"}.join(' ') if tags
86
+ tags.each { |tag| str << " ##{tag}" } if tags
87
87
  str
88
88
  end
89
89
 
@@ -1,5 +1,5 @@
1
1
  module StatsD
2
2
  module Instrument
3
- VERSION = "2.3.2"
3
+ VERSION = "2.3.4"
4
4
  end
5
5
  end
data/test/statsd_test.rb CHANGED
@@ -46,7 +46,7 @@ class StatsDTest < Minitest::Test
46
46
  end
47
47
 
48
48
  def test_statsd_measure_with_benchmarked_block_duration
49
- StatsD::Instrument.stubs(:duration).returns(1.12)
49
+ StatsD::Instrument.stubs(:current_timestamp).returns(5.0, 5.0 + 1.12)
50
50
  metric = capture_statsd_call do
51
51
  StatsD.measure('values.foobar') { 'foo' }
52
52
  end
@@ -70,6 +70,40 @@ class StatsDTest < Minitest::Test
70
70
  assert_nil return_value
71
71
  end
72
72
 
73
+ def test_statsd_measure_with_return_in_block_still_captures
74
+ StatsD::Instrument.stubs(:current_timestamp).returns(5.0, 6.12)
75
+ result = nil
76
+ metric = capture_statsd_call do
77
+ lambda = -> do
78
+ StatsD.measure('values.foobar') { return 'from lambda'}
79
+ end
80
+
81
+ result = lambda.call
82
+ end
83
+
84
+ assert_equal 'from lambda', result
85
+ assert_equal 1120.0, metric.value
86
+ end
87
+
88
+ def test_statsd_measure_with_exception_in_block_still_captures
89
+ StatsD::Instrument.stubs(:current_timestamp).returns(5.0, 6.12)
90
+ result = nil
91
+ metric = capture_statsd_call do
92
+ lambda = -> do
93
+ StatsD.measure('values.foobar') { raise 'from lambda'}
94
+ end
95
+
96
+ begin
97
+ result = lambda.call
98
+ rescue
99
+ end
100
+
101
+ end
102
+
103
+ assert_nil result
104
+ assert_equal 1120.0, metric.value
105
+ end
106
+
73
107
  def test_statsd_increment
74
108
  result = nil
75
109
  metric = capture_statsd_call { result = StatsD.increment('values.foobar', 3) }
@@ -149,7 +183,7 @@ class StatsDTest < Minitest::Test
149
183
  end
150
184
 
151
185
  def test_statsd_distribution_with_benchmarked_block_duration
152
- StatsD::Instrument.stubs(:duration).returns(1.12)
186
+ StatsD::Instrument.stubs(:current_timestamp).returns(5.0, 5.0 + 1.12)
153
187
  metric = capture_statsd_call do
154
188
  StatsD.distribution('values.foobar') { 'foo' }
155
189
  end
@@ -157,8 +191,44 @@ class StatsDTest < Minitest::Test
157
191
  assert_equal 1120.0, metric.value
158
192
  end
159
193
 
194
+ def test_statsd_distribution_with_return_in_block_still_captures
195
+ StatsD::Instrument.stubs(:current_timestamp).returns(5.0, 5.0 + 1.12)
196
+ result = nil
197
+ metric = capture_statsd_call do
198
+ lambda = -> do
199
+ StatsD.distribution('values.foobar') { return 'from lambda'}
200
+ end
201
+
202
+ result = lambda.call
203
+ end
204
+
205
+ assert_equal 'from lambda', result
206
+ assert_equal :d, metric.type
207
+ assert_equal 1120.0, metric.value
208
+ end
209
+
210
+ def test_statsd_distribution_with_exception_in_block_still_captures
211
+ StatsD::Instrument.stubs(:current_timestamp).returns(5.0, 5.0 + 1.12)
212
+ result = nil
213
+ metric = capture_statsd_call do
214
+ lambda = -> do
215
+ StatsD.distribution('values.foobar') { raise 'from lambda'}
216
+ end
217
+
218
+ begin
219
+ result = lambda.call
220
+ rescue
221
+ end
222
+
223
+ end
224
+
225
+ assert_nil result
226
+ assert_equal :d, metric.type
227
+ assert_equal 1120.0, metric.value
228
+ end
229
+
160
230
  def test_statsd_distribution_with_block_and_options
161
- StatsD::Instrument.stubs(:duration).returns(1.12)
231
+ StatsD::Instrument.stubs(:current_timestamp).returns(5.0, 5.0 + 1.12)
162
232
  metric = capture_statsd_call do
163
233
  StatsD.distribution('values.foobar', :tags => ['test'], :sample_rate => 0.9) { 'foo' }
164
234
  end
@@ -90,8 +90,8 @@ class UDPBackendTest < Minitest::Test
90
90
 
91
91
  def test_event_on_datadog_escapes_newlines
92
92
  @backend.implementation = :datadog
93
- @backend.expects(:write_packet).with('_e{8,5}:fooh\\n\\n|baz\\n')
94
- StatsD.event('fooh\n\n', 'baz\n')
93
+ @backend.expects(:write_packet).with("_e{8,5}:fooh\\n\\n|baz\\n")
94
+ StatsD.event("fooh\n\n", "baz\n")
95
95
  end
96
96
 
97
97
  def test_event_on_datadog_ignores_invalid_metadata
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: 2.3.2
4
+ version: 2.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jesse Storimer
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2018-11-28 00:00:00.000000000 Z
13
+ date: 2019-09-06 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
@@ -163,8 +163,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
163
163
  - !ruby/object:Gem::Version
164
164
  version: '0'
165
165
  requirements: []
166
- rubyforge_project:
167
- rubygems_version: 2.7.6
166
+ rubygems_version: 3.0.3
168
167
  signing_key:
169
168
  specification_version: 4
170
169
  summary: A StatsD client for Ruby apps