statsd-instrument 2.7.0 → 2.7.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3929918b255f6be82e1fa81b2f996dbef0f51cabc3fa374c0386bb37cd53f58f
4
- data.tar.gz: 322b93e4298c2a5bb2a32934d2c8d6ffb580248a229d5956053ea02aafa0934e
3
+ metadata.gz: 94982ff8dbf8c0e2b32a7ffdadbf683253b621c1acb40c1a87c3f02d1fa9f516
4
+ data.tar.gz: b13215159f2f939e5afa426a9fecaf9bdd03227d670bca6299140cfb3102f780
5
5
  SHA512:
6
- metadata.gz: b7a72b5b53f07719d207a91d37b30eb78d13f6c06f26566f8450c4e14b50bc9095498c19c12cb440484292f31e9a890d97ab478c4fced766ff55f0c2b32b8ea8
7
- data.tar.gz: fb2d2484b3dd9c6533fd6c445ab7871400d3fa5e06545303ef373f3d566ad0dcf1638a40228f84fb321b0a52c60e687a119e13b08664f385aaa03695604f339e
6
+ metadata.gz: 2a699f95247928c560a4cdf4af97eee07053bff488f969ccf1b269c1fcdb3973221127f116a5c97901bf9c2ac7f46d3f2fddd02dd2db3fdc1aa8ccaafe0cf3cd
7
+ data.tar.gz: 9c536d216d5a016971744490e4f92c8bcb563a53c0efbd01cae08201a20c15b744cee9097a7d34d4d64c6aea0af9dc8653843ba54af9fa9af96837399f159dea
data/CHANGELOG.md CHANGED
@@ -8,6 +8,17 @@ section below.
8
8
 
9
9
  _Nothing yet!_
10
10
 
11
+ ## Version 2.7.1
12
+
13
+ This release has some small fixes related to the new client only:
14
+
15
+ - Bugfix: Fix the metaprogramming methods so they work with a new client when
16
+ strict mode is _not_ enabled.
17
+ - Make it easier to instantiate new clients by specifying an implementation
18
+ (e.g. `datadog`) rather than a DatagramBuilder class.
19
+ - Change `NullSink#sample?` to always return `true`, so it's easier to verify
20
+ business rules by using a different DatabagramBuilder in test suites.
21
+
11
22
  ## Version 2.7.0
12
23
 
13
24
  This release allows you to switch the StatsD singleton to use the new, more
@@ -26,7 +37,7 @@ and fix deprecations in your code base.
26
37
  calls on the `StatsD` singleton will be delegated to this legacy client.
27
38
  - By setting `STATSD_USE_NEW_CLIENT` as environment variable, these method
28
39
  calls will be delegated to an instance of the new client instead. This
29
- client is configured using the xisting `STATD_*` environment variables,
40
+ client is configured using the existing `STATD_*` environment variables,
30
41
  like `STATSD_ADDR` and `STATSD_IMPLEMENTATION`.
31
42
  - You can also assign a custom client to `StatsD.singleton_client`.
32
43
 
@@ -57,6 +68,11 @@ datagrams coming from a legacy client, or from a new client.
57
68
  This makes it easy to run multiple assertions on the set of metrics that
58
69
  was emitted without having to nest calls.
59
70
 
71
+ - **⚠️ DEPRECATION** The `assert_statsd_*`-family of methods now use keyword
72
+ arguments, rather than option hashes. This means that calls that use
73
+ unsupported arguments will raise an `ArgumentError` now, rather than silently
74
+ ignoring unknown keys.
75
+
60
76
  - **⚠️ DEPRECATION**: The following methods to configure the legacy client
61
77
  are deprecated:
62
78
 
@@ -201,7 +217,7 @@ s
201
217
 
202
218
  See #193, #184, and #166 for more information.
203
219
 
204
- ## Verison 2.5.1
220
+ ## Version 2.5.1
205
221
 
206
222
  - **Bugfix:** when using metaprogramming methods, changes to `StatsD.prefix` after
207
223
  the metaprogramming method was evaluated would not be respected. This
@@ -104,8 +104,10 @@ module StatsD
104
104
  end
105
105
 
106
106
  # @private
107
- def self.generate_metric_name(metric_name, callee, *args)
108
- metric_name.respond_to?(:call) ? metric_name.call(callee, args).gsub('::', '.') : metric_name.gsub('::', '.')
107
+ def self.generate_metric_name(prefix, key, callee, *args)
108
+ name = key.respond_to?(:call) ? key.call(callee, args).gsub('::', '.') : key.gsub('::', '.')
109
+ name = "#{prefix}.#{name}" if prefix
110
+ name
109
111
  end
110
112
 
111
113
  # Even though this method is considered private, and is no longer used internally,
@@ -140,13 +142,16 @@ module StatsD
140
142
  def statsd_measure(method, name, deprecated_sample_rate_arg = nil, deprecated_tags_arg = nil, as_dist: false,
141
143
  sample_rate: deprecated_sample_rate_arg, tags: deprecated_tags_arg, prefix: nil, no_prefix: false)
142
144
 
145
+ if as_dist
146
+ return statsd_distribution(method, name, # rubocop:disable StatsD/MetricPrefixArgument
147
+ sample_rate: sample_rate, tags: tags, prefix: prefix, no_prefix: no_prefix)
148
+ end
149
+
143
150
  add_to_method(method, name, :measure) do
144
151
  define_method(method) do |*args, &block|
145
- key = StatsD::Instrument.generate_metric_name(name, self, *args)
146
- prefix ||= StatsD.prefix
147
- StatsD.measure( # rubocop:disable StatsD/MeasureAsDistArgument, StatsD/MetricPrefixArgument
148
- key, sample_rate: sample_rate, tags: tags, prefix: prefix, no_prefix: no_prefix, as_dist: as_dist
149
- ) do
152
+ prefix ||= StatsD.prefix unless no_prefix
153
+ key = StatsD::Instrument.generate_metric_name(prefix, name, self, *args)
154
+ StatsD.measure(key, sample_rate: sample_rate, tags: tags, no_prefix: true) do
150
155
  super(*args, &block)
151
156
  end
152
157
  end
@@ -166,11 +171,9 @@ module StatsD
166
171
 
167
172
  add_to_method(method, name, :distribution) do
168
173
  define_method(method) do |*args, &block|
169
- key = StatsD::Instrument.generate_metric_name(name, self, *args)
170
- prefix ||= StatsD.prefix
171
- StatsD.distribution( # rubocop:disable StatsD/MetricPrefixArgument
172
- key, sample_rate: sample_rate, tags: tags, prefix: prefix, no_prefix: no_prefix
173
- ) do
174
+ prefix ||= StatsD.prefix unless no_prefix
175
+ key = StatsD::Instrument.generate_metric_name(prefix, name, self, *args)
176
+ StatsD.distribution(key, sample_rate: sample_rate, tags: tags, no_prefix: true) do
174
177
  super(*args, &block)
175
178
  end
176
179
  end
@@ -213,10 +216,9 @@ module StatsD
213
216
  result
214
217
  ensure
215
218
  suffix = truthiness == false ? 'failure' : 'success'
216
- key = "#{StatsD::Instrument.generate_metric_name(name, self, *args)}.#{suffix}"
217
- prefix ||= StatsD.prefix
218
- StatsD.increment(key, prefix: prefix, # rubocop:disable StatsD/MetricPrefixArgument
219
- sample_rate: sample_rate, tags: tags, no_prefix: no_prefix)
219
+ prefix ||= StatsD.prefix unless no_prefix
220
+ key = StatsD::Instrument.generate_metric_name(prefix, name, self, *args)
221
+ StatsD.increment("#{key}.#{suffix}", sample_rate: sample_rate, tags: tags, no_prefix: true)
220
222
  end
221
223
  end
222
224
  end
@@ -255,10 +257,9 @@ module StatsD
255
257
  result
256
258
  ensure
257
259
  if truthiness
258
- key = StatsD::Instrument.generate_metric_name(name, self, *args)
259
- prefix ||= StatsD.prefix
260
- StatsD.increment(key, prefix: prefix, # rubocop:disable StatsD/MetricPrefixArgument
261
- sample_rate: sample_rate, tags: tags, no_prefix: no_prefix)
260
+ prefix ||= StatsD.prefix unless no_prefix
261
+ key = StatsD::Instrument.generate_metric_name(prefix, name, self, *args)
262
+ StatsD.increment(key, sample_rate: sample_rate, tags: tags, no_prefix: true)
262
263
  end
263
264
  end
264
265
  end
@@ -279,10 +280,9 @@ module StatsD
279
280
 
280
281
  add_to_method(method, name, :count) do
281
282
  define_method(method) do |*args, &block|
282
- key = StatsD::Instrument.generate_metric_name(name, self, *args)
283
- prefix ||= StatsD.prefix
284
- StatsD.increment(key, prefix: prefix, # rubocop:disable StatsD/MetricPrefixArgument
285
- sample_rate: sample_rate, tags: tags, no_prefix: no_prefix)
283
+ prefix ||= StatsD.prefix unless no_prefix
284
+ key = StatsD::Instrument.generate_metric_name(prefix, name, self, *args)
285
+ StatsD.increment(key, sample_rate: sample_rate, tags: tags, no_prefix: true)
286
286
  super(*args, &block)
287
287
  end
288
288
  end
@@ -22,7 +22,8 @@ class StatsD::Instrument::Client
22
22
  prefix: nil,
23
23
  default_sample_rate: 1,
24
24
  default_tags: nil,
25
- datagram_builder_class: StatsD::Instrument::StatsDDatagramBuilder
25
+ implementation: 'datadog',
26
+ datagram_builder_class: datagram_builder_class_from_implementation(implementation)
26
27
  )
27
28
  @sink = sink
28
29
  @datagram_builder_class = datagram_builder_class
@@ -34,6 +35,17 @@ class StatsD::Instrument::Client
34
35
  @datagram_builder = { false => nil, true => nil }
35
36
  end
36
37
 
38
+ def datagram_builder_class_from_implementation(implementation)
39
+ case implementation.to_s
40
+ when 'statsd'
41
+ StatsD::Instrument::StatsDDatagramBuilder
42
+ when 'datadog', 'dogstatsd'
43
+ StatsD::Instrument::DogStatsDDatagramBuilder
44
+ else
45
+ raise NotImplementedError, "No implementation for #{statsd_implementation}"
46
+ end
47
+ end
48
+
37
49
  # @!group Metric Methods
38
50
 
39
51
  # Emits a counter metric.
@@ -96,7 +108,7 @@ class StatsD::Instrument::Client
96
108
  #
97
109
  # You should use a gauge if you are reporting the current value of
98
110
  # something that can only have one value at the time. E.g., the
99
- # speed of your car. A newly reported value will repla e the previously
111
+ # speed of your car. A newly reported value will replace the previously
100
112
  # reported value.
101
113
  #
102
114
  #
@@ -271,7 +283,7 @@ class StatsD::Instrument::Client
271
283
  def capture_sink
272
284
  StatsD::Instrument::CaptureSink.new(
273
285
  parent: @sink,
274
- datagram_class: datagram_builder(no_prefix: false).datagram_class,
286
+ datagram_class: datagram_builder_class.datagram_class,
275
287
  )
276
288
  end
277
289
 
@@ -23,6 +23,10 @@ class StatsD::Instrument::DatagramBuilder
23
23
  end
24
24
  end
25
25
 
26
+ def self.datagram_class
27
+ StatsD::Instrument::Datagram
28
+ end
29
+
26
30
  def initialize(prefix: nil, default_tags: nil)
27
31
  @prefix = prefix.nil? ? "" : "#{normalize_name(prefix)}."
28
32
  @default_tags = normalize_tags(default_tags)
@@ -56,10 +60,6 @@ class StatsD::Instrument::DatagramBuilder
56
60
  generate_generic_datagram(name, value, 'kv', sample_rate, tags)
57
61
  end
58
62
 
59
- def datagram_class
60
- StatsD::Instrument::Datagram
61
- end
62
-
63
63
  def latency_metric_type
64
64
  :ms
65
65
  end
@@ -106,24 +106,13 @@ class StatsD::Instrument::Environment
106
106
  def default_client
107
107
  @default_client ||= StatsD::Instrument::Client.new(
108
108
  sink: default_sink_for_environment,
109
- datagram_builder_class: default_datagram_builder_class_for_implementation,
109
+ implementation: statsd_implementation,
110
110
  default_sample_rate: statsd_sample_rate,
111
111
  prefix: statsd_prefix,
112
112
  default_tags: statsd_default_tags,
113
113
  )
114
114
  end
115
115
 
116
- def default_datagram_builder_class_for_implementation
117
- case statsd_implementation
118
- when 'statsd'
119
- StatsD::Instrument::StatsDDatagramBuilder
120
- when 'datadog', 'dogstatsd'
121
- StatsD::Instrument::DogStatsDDatagramBuilder
122
- else
123
- raise NotImplementedError, "No implementation for #{statsd_implementation}"
124
- end
125
- end
126
-
127
116
  def default_sink_for_environment
128
117
  case environment
129
118
  when 'production', 'staging'
@@ -4,7 +4,7 @@
4
4
  # to become the new default in the next major release of this library.
5
5
  class StatsD::Instrument::NullSink
6
6
  def sample?(_sample_rate)
7
- false
7
+ true
8
8
  end
9
9
 
10
10
  def <<(_datagram)
@@ -114,7 +114,7 @@ module StatsD
114
114
  # Stats.measure call to not use the `as_dist` and `prefix` arguments.
115
115
  add_to_method(method, name, :measure) do
116
116
  define_method(method) do |*args, &block|
117
- key = StatsD::Instrument.generate_metric_name(name, self, *args)
117
+ key = StatsD::Instrument.generate_metric_name(nil, name, self, *args)
118
118
  StatsD.measure(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix) do
119
119
  super(*args, &block)
120
120
  end
@@ -130,7 +130,7 @@ module StatsD
130
130
 
131
131
  add_to_method(method, name, :distribution) do
132
132
  define_method(method) do |*args, &block|
133
- key = StatsD::Instrument.generate_metric_name(name, self, *args)
133
+ key = StatsD::Instrument.generate_metric_name(nil, name, self, *args)
134
134
  StatsD.distribution(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix) do
135
135
  super(*args, &block)
136
136
  end
@@ -162,7 +162,7 @@ module StatsD
162
162
  result
163
163
  ensure
164
164
  suffix = truthiness == false ? 'failure' : 'success'
165
- key = "#{StatsD::Instrument.generate_metric_name(name, self, *args)}.#{suffix}"
165
+ key = "#{StatsD::Instrument.generate_metric_name(nil, name, self, *args)}.#{suffix}"
166
166
  StatsD.increment(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix)
167
167
  end
168
168
  end
@@ -193,7 +193,7 @@ module StatsD
193
193
  result
194
194
  ensure
195
195
  if truthiness
196
- key = StatsD::Instrument.generate_metric_name(name, self, *args)
196
+ key = StatsD::Instrument.generate_metric_name(nil, name, self, *args)
197
197
  StatsD.increment(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix)
198
198
  end
199
199
  end
@@ -209,7 +209,7 @@ module StatsD
209
209
 
210
210
  add_to_method(method, name, :count) do
211
211
  define_method(method) do |*args, &block|
212
- key = StatsD::Instrument.generate_metric_name(name, self, *args)
212
+ key = StatsD::Instrument.generate_metric_name(nil, name, self, *args)
213
213
  StatsD.increment(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix)
214
214
  super(*args, &block)
215
215
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module StatsD
4
4
  module Instrument
5
- VERSION = "2.7.0"
5
+ VERSION = "2.7.1"
6
6
  end
7
7
  end
data/test/client_test.rb CHANGED
@@ -5,9 +5,7 @@ require 'test_helper'
5
5
  class ClientTest < Minitest::Test
6
6
  def setup
7
7
  @client = StatsD::Instrument::Client.new(datagram_builder_class: StatsD::Instrument::StatsDDatagramBuilder)
8
- @dogstatsd_client = StatsD::Instrument::Client.new(
9
- datagram_builder_class: StatsD::Instrument::DogStatsDDatagramBuilder,
10
- )
8
+ @dogstatsd_client = StatsD::Instrument::Client.new(implementation: 'datadog')
11
9
  end
12
10
 
13
11
  def test_capture
@@ -8,4 +8,10 @@ class NullSinktest < Minitest::Test
8
8
  null_sink << 'foo:1|c' << 'bar:1|c'
9
9
  pass # We don't have anything to assert, except that no exception was raised
10
10
  end
11
+
12
+ def test_null_sink_sample
13
+ null_sink = StatsD::Instrument::NullSink.new
14
+ assert null_sink.sample?(0), "The null sink should always sample"
15
+ assert null_sink.sample?(1), "The null sink should always sample"
16
+ end
11
17
  end
@@ -417,6 +417,32 @@ class StatsDInstrumentationTest < Minitest::Test
417
417
  end
418
418
  end
419
419
 
420
+ def test_statsd_measure_with_new_client
421
+ old_client = StatsD.singleton_client
422
+ StatsD.singleton_client = StatsD::Instrument::Client.new
423
+
424
+ ActiveMerchant::UniqueGateway.statsd_measure :ssl_post, 'ActiveMerchant.Gateway.ssl_post', sample_rate: 0.3
425
+ assert_statsd_measure('ActiveMerchant.Gateway.ssl_post', sample_rate: 0.3) do
426
+ ActiveMerchant::UniqueGateway.new.purchase(true)
427
+ end
428
+ ensure
429
+ ActiveMerchant::UniqueGateway.statsd_remove_measure :ssl_post, 'ActiveMerchant.Gateway.ssl_post'
430
+ StatsD.singleton_client = old_client
431
+ end
432
+
433
+ def test_statsd_count_with_new_client
434
+ old_client = StatsD.singleton_client
435
+ StatsD.singleton_client = StatsD::Instrument::Client.new
436
+
437
+ ActiveMerchant::Gateway.statsd_count :ssl_post, 'ActiveMerchant.Gateway.ssl_post'
438
+ assert_statsd_increment('ActiveMerchant.Gateway.ssl_post') do
439
+ ActiveMerchant::Gateway.new.purchase(true)
440
+ end
441
+ ensure
442
+ ActiveMerchant::Gateway.statsd_remove_count :ssl_post, 'ActiveMerchant.Gateway.ssl_post'
443
+ StatsD.singleton_client = old_client
444
+ end
445
+
420
446
  private
421
447
 
422
448
  def assert_scope(klass, method, expected_scope)
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.7.0
4
+ version: 2.7.1
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: 2019-10-09 00:00:00.000000000 Z
13
+ date: 2019-10-17 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake