statsd-instrument 3.4.0 → 3.5.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
  SHA256:
3
- metadata.gz: bcbaac2cd4178c61bfcb484c45931bb387d81dfa632fbd114bba126c393beb75
4
- data.tar.gz: d1101fbc534b6612ff76a282ab45d37aad3a0c185b3ccedf6dcf2ef78ce85bb1
3
+ metadata.gz: 7f4884abfa414d10454edd851f0a15cf51b4333396cf4cd2da209f6f7e543366
4
+ data.tar.gz: 2e3f6b906fd8ab8a651d683475dbb660ea37a5207de95722efdf5c467cda2c94
5
5
  SHA512:
6
- metadata.gz: 4cd62e31fe1dc59ae49345e598de791ba1295a18d527d88f840de7349d31e8d325cf00543e59eddf02c3fed56a58f9020ace88ca7554de2340d193e8e62cb1d4
7
- data.tar.gz: 98fcae4a9b924ab432d745ad2c02f4ac5ed6d0e94ad1f002e80afc0f4f741f01412dbf605151813432dc828ad57f9c1747e26a7127c32950df9ffddeb90e9523
6
+ metadata.gz: d771c8e13ef08ef146170b41024e09abbabaca4ebbfeb9c34781f04a2fdaf287776e7c79d93fe49d5ffb9ac18da82a1759d37621697d18cf9e5e423577110aae
7
+ data.tar.gz: 573003d398eba6f81fb13385bf93c0ee4254c5074ba935471cd2dee659c1c2016f9c9bb44f9c71383a30b5b91129c12753c949118ebc103b6cb1595609e534e6
data/CHANGELOG.md CHANGED
@@ -4,7 +4,13 @@ This file documents the changes between releases of this library. When
4
4
  creating a pull request, please add an entry to the "unreleased changes"
5
5
  section below.
6
6
 
7
- ### Unreleased changes
7
+ ## Unreleased changes
8
+
9
+ ## Version 3.5.0
10
+
11
+ - Allow user to provide a lambda function to dynamically set metric tags
12
+
13
+ ## Version 3.4.0
8
14
 
9
15
  - UDP Batching has been largely refactored again. The `STATSD_FLUSH_INTERVAL` environment variable
10
16
  is deprecated. It still disable batching if set to `0`, but other than that is has no effect.
@@ -516,123 +522,123 @@ average metric considerably.
516
522
 
517
523
  - Add mutex around UDP socket invalidation (#147)
518
524
 
519
- ### Version 2.3.0
525
+ ## Version 2.3.0
520
526
 
521
527
  - No changes from `beta6`, distributions are GA at DataDog so making the distribution changes GA in gem
522
528
 
523
- ### Version 2.3.0.beta6
529
+ ## Version 2.3.0.beta6
524
530
 
525
531
  - Fix invalidate socket on connectivity issues in UDP (#135)
526
532
 
527
- ### Version 2.3.0.beta5
533
+ ## Version 2.3.0.beta5
528
534
 
529
535
  - Fixes bug in return value for blocks used in distributions (#132)
530
536
 
531
- ### Version 2.3.0.beta4
537
+ ## Version 2.3.0.beta4
532
538
 
533
539
  - Add support for distribution to accept a block
534
540
  - Add class method for defining and removing a distribution from a method (same as a measure)
535
541
  - Refactor most instrument methods to reduce code duplication
536
542
 
537
- ### Version 2.3.0.beta3
543
+ ## Version 2.3.0.beta3
538
544
 
539
545
  - fix for `:as_dist` parameter in the `statsd_measure` class method
540
546
 
541
- ### Version 2.3.0.beta2
547
+ ## Version 2.3.0.beta2
542
548
 
543
549
  - Add support for specifying a measure to emit as a distribution using `:as_dist` parameter
544
550
 
545
- ### Version 2.3.0.beta
551
+ ## Version 2.3.0.beta
546
552
 
547
553
  - Add support for beta, Datadog specific distribution metrics
548
554
  - Invalidate socket on connectivity issues
549
555
 
550
- ### Version 2.2.1
556
+ ## Version 2.2.1
551
557
 
552
558
  - Fix performance regression from v2.2.0
553
559
 
554
- ### Version 2.2.0
560
+ ## Version 2.2.0
555
561
 
556
562
  - Add support for two new Datadog specific metric types: events and service checks.
557
563
 
558
- ### Version 2.1.3
564
+ ## Version 2.1.3
559
565
 
560
566
  - The `assert_statsd_calls` test helper will now raise an exception whenever a block isn't passed.
561
567
  - Sending stats inside an exit handler will no longer cause programs to exit abruptly.
562
568
 
563
- ### Version 2.1.2
569
+ ## Version 2.1.2
564
570
 
565
571
  - Use `prepend` instead of rewriting classes for metaprogramming methods.
566
572
  - RSpec: make matchers more flexible.
567
573
  - Bugfix: Only ask Rails for the environment when it's actually loaded.
568
574
 
569
- ### Version 2.1.1
575
+ ## Version 2.1.1
570
576
 
571
577
  - Add `assert_statsd_calls` to from validating cases where one has multiple metrics with the same name and type being recorded, but with different options.
572
578
 
573
- ### Version 2.1.0
579
+ ## Version 2.1.0
574
580
 
575
581
  - Fix rspec-rails compatibility
576
582
  - Add `value` keyword argument to all metric types.
577
583
 
578
- ### Version 2.0.12
584
+ ## Version 2.0.12
579
585
 
580
586
  - Make StatsD client thread-safe
581
587
  - Assertions: Ensure sample rates have proper values.
582
588
  - Assertions: Make tag assertions work more intuitively
583
589
  - RSpec: Add backwards compatibility for RSpec 2
584
590
 
585
- ### Version 2.0.11
591
+ ## Version 2.0.11
586
592
 
587
593
  - Don't change method visibility when adding instrumentation to methods using metaprogramming
588
594
  - RSpec: add support for Compound expectations
589
595
 
590
- ### Version 2.0.10
596
+ ## Version 2.0.10
591
597
 
592
598
  - Assertions: allow ignoring certain tags when asserting for other tags to be present.
593
599
 
594
- ### Version 2.0.9
600
+ ## Version 2.0.9
595
601
 
596
602
  - Better error message for `assert_no_statsd_calls`
597
603
 
598
- ### Version 2.0.8
604
+ ## Version 2.0.8
599
605
 
600
606
  - More tag handling performance improvements.
601
607
  - RSpec matchers documentation improvements
602
608
 
603
- ### Version 2.0.7
609
+ ## Version 2.0.7
604
610
 
605
611
  - Tag handling performance improvements.
606
612
  - Test against Ruby 2.2.
607
613
  - Drop support for Ruby 1.9.3.
608
614
 
609
- ### Version 2.0.6
615
+ ## Version 2.0.6
610
616
 
611
617
  - Fix some loading order issues in Rails environments.
612
618
  - Default behavior: in a **staging** environment, the defaults are now the same as in a **production environment**.
613
619
  - Documentation overhaul
614
620
 
615
- ### Version 2.0.5
621
+ ## Version 2.0.5
616
622
 
617
623
  - Allow for nested assertions using the `assert_statsd_*` assertion methods.
618
624
 
619
- ### Version 2.0.4
625
+ ## Version 2.0.4
620
626
 
621
627
  - Add a Railtie to fix some initialization issues.
622
628
 
623
- ### Version 2.0.3
629
+ ## Version 2.0.3
624
630
 
625
631
  - Assertion method bugfixes
626
632
 
627
- ### Version 2.0.2
633
+ ## Version 2.0.2
628
634
 
629
635
  - Documentation fixes
630
636
 
631
- ### Version 2.0.1
637
+ ## Version 2.0.1
632
638
 
633
639
  - Add assertion methods `assert_statsd_histogram`, `assert_statsd_set`, and `assert_statsd_key_value`.
634
640
 
635
- ### Version 2.0.0
641
+ ## Version 2.0.0
636
642
 
637
643
  - Complete rewrite using pluggable backends.
638
644
  - Add assertion methods in `StatsD::Instrument::Assertions` to make testing easier and less brittle.
data/README.md CHANGED
@@ -21,7 +21,7 @@ The following environment variables are supported:
21
21
  - `STATSD_ADDR`: (default `localhost:8125`) The address to send the StatsD UDP
22
22
  datagrams to.
23
23
  - `STATSD_IMPLEMENTATION`: (default: `datadog`). The StatsD implementation you
24
- are using. `statsd`, `statsite` and `datadog` are supported. Some features
24
+ are using. `statsd` and `datadog` are supported. Some features
25
25
  are only available on certain implementations,
26
26
  - `STATSD_ENV`: The environment StatsD will run in. If this is not set
27
27
  explicitly, this will be determined based on other environment variables,
@@ -255,6 +255,16 @@ GoogleBase.statsd_count :insert, 'GoogleBase.insert', tags: ['env:production']
255
255
  If implementation is not set to `:datadog`, tags will not be included in the UDP packets, and a
256
256
  warning is logged to `StatsD.logger`.
257
257
 
258
+ You can use lambda function that instead of a list of tags to set the metric tags.
259
+ Like the dynamic metric name, the lambda function must accept two arguments:
260
+ the object the function is being called on and the array of arguments
261
+ passed.
262
+
263
+ ``` ruby
264
+ metric_tagger = lambda { |object, args| { "key": args.first } }
265
+ GoogleBase.statsd_count(:insert, 'GoogleBase.insert', tags: metric_tagger)
266
+ ```
267
+
258
268
  ## Testing
259
269
 
260
270
  This library comes with a module called `StatsD::Instrument::Assertions` and `StatsD::Instrument::Matchers` to help you write tests
@@ -2,6 +2,6 @@
2
2
 
3
3
  module StatsD
4
4
  module Instrument
5
- VERSION = "3.4.0"
5
+ VERSION = "3.5.0"
6
6
  end
7
7
  end
@@ -34,6 +34,15 @@ module StatsD
34
34
  name.respond_to?(:call) ? name.call(callee, args).gsub("::", ".") : name.gsub("::", ".")
35
35
  end
36
36
 
37
+ # Generates the tags for an instrumented method.
38
+ # @private
39
+ # @return [Array[String]]
40
+ def self.generate_tags(tags, callee, *args)
41
+ return if tags.nil?
42
+
43
+ tags.respond_to?(:call) ? tags.call(callee, args) : tags
44
+ end
45
+
37
46
  # Even though this method is considered private, and is no longer used internally,
38
47
  # applications in the wild rely on it. As a result, we cannot remove this method
39
48
  # until the next major version.
@@ -61,6 +70,8 @@ module StatsD
61
70
  # @param method [Symbol] The name of the method to instrument.
62
71
  # @param name [String, #call] The name of the metric to use. You can also pass in a
63
72
  # callable to dynamically generate a metric name
73
+ # @param tags [Hash, #call] The tags to be associated with the metric. You can also
74
+ # pass in a callable to dynamically generate the tags key and values
64
75
  # @param metric_options (see StatsD#measure)
65
76
  # @return [void]
66
77
  def statsd_measure(method, name, sample_rate: nil, tags: nil, no_prefix: false, client: nil)
@@ -68,6 +79,7 @@ module StatsD
68
79
  define_method(method) do |*args, &block|
69
80
  client ||= StatsD.singleton_client
70
81
  key = StatsD::Instrument.generate_metric_name(name, self, *args)
82
+ tags = StatsD::Instrument.generate_tags(tags, self, *args)
71
83
  client.measure(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix) do
72
84
  super(*args, &block)
73
85
  end
@@ -88,6 +100,7 @@ module StatsD
88
100
  define_method(method) do |*args, &block|
89
101
  client ||= StatsD.singleton_client
90
102
  key = StatsD::Instrument.generate_metric_name(name, self, *args)
103
+ tags = StatsD::Instrument.generate_tags(tags, self, *args)
91
104
  client.distribution(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix) do
92
105
  super(*args, &block)
93
106
  end
@@ -132,7 +145,7 @@ module StatsD
132
145
  client ||= StatsD.singleton_client
133
146
  suffix = truthiness == false ? "failure" : "success"
134
147
  key = StatsD::Instrument.generate_metric_name(name, self, *args)
135
-
148
+ tags = StatsD::Instrument.generate_tags(tags, self, *args)
136
149
  tags = Helpers.add_tag(tags, :error_class, error.class.name) if tag_error_class && error
137
150
 
138
151
  client.increment("#{key}.#{suffix}", sample_rate: sample_rate, tags: tags, no_prefix: no_prefix)
@@ -172,6 +185,7 @@ module StatsD
172
185
  if truthiness
173
186
  client ||= StatsD.singleton_client
174
187
  key = StatsD::Instrument.generate_metric_name(name, self, *args)
188
+ tags = StatsD::Instrument.generate_tags(tags, self, *args)
175
189
  client.increment(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix)
176
190
  end
177
191
  end
@@ -192,6 +206,7 @@ module StatsD
192
206
  define_method(method) do |*args, &block|
193
207
  client ||= StatsD.singleton_client
194
208
  key = StatsD::Instrument.generate_metric_name(name, self, *args)
209
+ tags = StatsD::Instrument.generate_tags(tags, self, *args)
195
210
  client.increment(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix)
196
211
  super(*args, &block)
197
212
  end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ class ChangelogTest < Minitest::Test
6
+ def test_has_heading_for_current_version
7
+ assert_includes(changelog_headings, current_version_heading)
8
+ end
9
+
10
+ def test_headings_are_consistent
11
+ invalid_headings = changelog_headings.reject do |heading|
12
+ next true if heading == "# Changelog"
13
+ next true if heading == "## Unreleased changes"
14
+
15
+ # Remaining headings are <h2> if and only if they are version headings, and must be formatted correctly.
16
+ if heading.start_with?("## ") || heading.include?("Version")
17
+ next heading.match?(/^## Version \d+\.\d+\.\d+/)
18
+ end
19
+
20
+ next true if heading.start_with?("###") # <h3> and lower are permitted
21
+
22
+ false # All other headings are forbidden
23
+ end
24
+
25
+ assert_empty(invalid_headings, "Headings must follow formatting conventions")
26
+ end
27
+
28
+ private
29
+
30
+ def current_version_heading
31
+ "## Version #{StatsD::Instrument::VERSION}"
32
+ end
33
+
34
+ def changelog_headings
35
+ File.read("CHANGELOG.md").each_line.grep(/^#/).map(&:strip)
36
+ end
37
+ end
@@ -7,7 +7,7 @@ class StatsDInstrumentationTest < Minitest::Test
7
7
  class Base
8
8
  extend StatsD::Instrument
9
9
 
10
- def ssl_post(arg)
10
+ def ssl_post(arg, async: false)
11
11
  if arg
12
12
  "OK"
13
13
  else
@@ -21,8 +21,8 @@ class StatsDInstrumentationTest < Minitest::Test
21
21
  end
22
22
 
23
23
  class Gateway < Base
24
- def purchase(arg)
25
- ssl_post(arg)
24
+ def purchase(arg, async: false)
25
+ ssl_post(arg, async: async)
26
26
  true
27
27
  rescue
28
28
  false
@@ -34,12 +34,12 @@ class StatsDInstrumentationTest < Minitest::Test
34
34
  end
35
35
 
36
36
  class UniqueGateway < Base
37
- def ssl_post(arg)
37
+ def ssl_post(arg, async: false)
38
38
  { success: arg }
39
39
  end
40
40
 
41
- def purchase(arg)
42
- ssl_post(arg)
41
+ def purchase(arg, async: false)
42
+ ssl_post(arg, async: async)
43
43
  end
44
44
  end
45
45
  end
@@ -223,6 +223,30 @@ class StatsDInstrumentationTest < Minitest::Test
223
223
  ActiveMerchant::Gateway.statsd_remove_count(:ssl_post, metric_namer)
224
224
  end
225
225
 
226
+ def test_statsd_count_with_tags_as_lambda
227
+ metric_namer = lambda { |object, args| "#{object.metric_name}.#{args.first}" }
228
+ metric_tagger = lambda { |_object, args| { "key": args.first } }
229
+ ActiveMerchant::Gateway.statsd_count(:ssl_post, metric_namer, tags: metric_tagger)
230
+
231
+ assert_statsd_increment("subgateway.foo", tags: { "key": "foo" }) do
232
+ GatewaySubClass.new.purchase("foo")
233
+ end
234
+ ensure
235
+ ActiveMerchant::Gateway.statsd_remove_count(:ssl_post, metric_namer)
236
+ end
237
+
238
+ def test_statsd_count_with_tags_as_proc
239
+ metric_namer = proc { |object, args| "#{object.metric_name}.#{args.first}" }
240
+ metric_tagger = proc { |_object, args| { "key": args.first } }
241
+ ActiveMerchant::Gateway.statsd_count(:ssl_post, metric_namer, tags: metric_tagger)
242
+
243
+ assert_statsd_increment("subgateway.foo", tags: { "key": "foo" }) do
244
+ GatewaySubClass.new.purchase("foo")
245
+ end
246
+ ensure
247
+ ActiveMerchant::Gateway.statsd_remove_count(:ssl_post, metric_namer)
248
+ end
249
+
226
250
  def test_statsd_count_with_method_receiving_block
227
251
  ActiveMerchant::Base.statsd_count(:post_with_block, "ActiveMerchant.Base.post_with_block")
228
252
 
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: 3.4.0
4
+ version: 3.5.0
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: 2022-08-29 00:00:00.000000000 Z
13
+ date: 2022-10-04 00:00:00.000000000 Z
14
14
  dependencies: []
15
15
  description: A StatsD client for Ruby apps. Provides metaprogramming methods to inject
16
16
  StatsD instrumentation into your code.
@@ -77,6 +77,7 @@ files:
77
77
  - test/benchmark/metrics.rb
78
78
  - test/benchmark/tags.rb
79
79
  - test/capture_sink_test.rb
80
+ - test/changelog_test.rb
80
81
  - test/client_test.rb
81
82
  - test/datagram_builder_test.rb
82
83
  - test/datagram_test.rb
@@ -131,6 +132,7 @@ test_files:
131
132
  - test/benchmark/metrics.rb
132
133
  - test/benchmark/tags.rb
133
134
  - test/capture_sink_test.rb
135
+ - test/changelog_test.rb
134
136
  - test/client_test.rb
135
137
  - test/datagram_builder_test.rb
136
138
  - test/datagram_test.rb