statsd-instrument 3.4.0 → 3.5.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: bcbaac2cd4178c61bfcb484c45931bb387d81dfa632fbd114bba126c393beb75
4
- data.tar.gz: d1101fbc534b6612ff76a282ab45d37aad3a0c185b3ccedf6dcf2ef78ce85bb1
3
+ metadata.gz: 45499b5743c8202fb33807dfbea271ce432b9e28cf4cb360ecde628b4aacee1d
4
+ data.tar.gz: 9faea13d1feb7b2d26d1f26334a306e881c87d12df52fece199047af0ecf62b9
5
5
  SHA512:
6
- metadata.gz: 4cd62e31fe1dc59ae49345e598de791ba1295a18d527d88f840de7349d31e8d325cf00543e59eddf02c3fed56a58f9020ace88ca7554de2340d193e8e62cb1d4
7
- data.tar.gz: 98fcae4a9b924ab432d745ad2c02f4ac5ed6d0e94ad1f002e80afc0f4f741f01412dbf605151813432dc828ad57f9c1747e26a7127c32950df9ffddeb90e9523
6
+ metadata.gz: 9d75637c2b6761835723a8dd343a7238c29137c5ca082784e859a6c79303432639d2be9f55cc98a3188fb45b2cd163c9b82086c256d8d4f673e3bae918807a40
7
+ data.tar.gz: 986d8661bdd02ba809ea03a56bde2fefee5d98e4c73ef823c62fc5dff4efc616ba0f881e6f1b9be8a295da3649e48a2cc74b159f8c9380db2b0ae658b72e385b
data/CHANGELOG.md CHANGED
@@ -4,7 +4,17 @@ 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.1
10
+
11
+ - Fix bug when passing a lambda function to dynamically set the tags in the strict mode.
12
+
13
+ ## Version 3.5.0
14
+
15
+ - Allow user to provide a lambda function to dynamically set metric tags
16
+
17
+ ## Version 3.4.0
8
18
 
9
19
  - UDP Batching has been largely refactored again. The `STATSD_FLUSH_INTERVAL` environment variable
10
20
  is deprecated. It still disable batching if set to `0`, but other than that is has no effect.
@@ -516,123 +526,123 @@ average metric considerably.
516
526
 
517
527
  - Add mutex around UDP socket invalidation (#147)
518
528
 
519
- ### Version 2.3.0
529
+ ## Version 2.3.0
520
530
 
521
531
  - No changes from `beta6`, distributions are GA at DataDog so making the distribution changes GA in gem
522
532
 
523
- ### Version 2.3.0.beta6
533
+ ## Version 2.3.0.beta6
524
534
 
525
535
  - Fix invalidate socket on connectivity issues in UDP (#135)
526
536
 
527
- ### Version 2.3.0.beta5
537
+ ## Version 2.3.0.beta5
528
538
 
529
539
  - Fixes bug in return value for blocks used in distributions (#132)
530
540
 
531
- ### Version 2.3.0.beta4
541
+ ## Version 2.3.0.beta4
532
542
 
533
543
  - Add support for distribution to accept a block
534
544
  - Add class method for defining and removing a distribution from a method (same as a measure)
535
545
  - Refactor most instrument methods to reduce code duplication
536
546
 
537
- ### Version 2.3.0.beta3
547
+ ## Version 2.3.0.beta3
538
548
 
539
549
  - fix for `:as_dist` parameter in the `statsd_measure` class method
540
550
 
541
- ### Version 2.3.0.beta2
551
+ ## Version 2.3.0.beta2
542
552
 
543
553
  - Add support for specifying a measure to emit as a distribution using `:as_dist` parameter
544
554
 
545
- ### Version 2.3.0.beta
555
+ ## Version 2.3.0.beta
546
556
 
547
557
  - Add support for beta, Datadog specific distribution metrics
548
558
  - Invalidate socket on connectivity issues
549
559
 
550
- ### Version 2.2.1
560
+ ## Version 2.2.1
551
561
 
552
562
  - Fix performance regression from v2.2.0
553
563
 
554
- ### Version 2.2.0
564
+ ## Version 2.2.0
555
565
 
556
566
  - Add support for two new Datadog specific metric types: events and service checks.
557
567
 
558
- ### Version 2.1.3
568
+ ## Version 2.1.3
559
569
 
560
570
  - The `assert_statsd_calls` test helper will now raise an exception whenever a block isn't passed.
561
571
  - Sending stats inside an exit handler will no longer cause programs to exit abruptly.
562
572
 
563
- ### Version 2.1.2
573
+ ## Version 2.1.2
564
574
 
565
575
  - Use `prepend` instead of rewriting classes for metaprogramming methods.
566
576
  - RSpec: make matchers more flexible.
567
577
  - Bugfix: Only ask Rails for the environment when it's actually loaded.
568
578
 
569
- ### Version 2.1.1
579
+ ## Version 2.1.1
570
580
 
571
581
  - 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
582
 
573
- ### Version 2.1.0
583
+ ## Version 2.1.0
574
584
 
575
585
  - Fix rspec-rails compatibility
576
586
  - Add `value` keyword argument to all metric types.
577
587
 
578
- ### Version 2.0.12
588
+ ## Version 2.0.12
579
589
 
580
590
  - Make StatsD client thread-safe
581
591
  - Assertions: Ensure sample rates have proper values.
582
592
  - Assertions: Make tag assertions work more intuitively
583
593
  - RSpec: Add backwards compatibility for RSpec 2
584
594
 
585
- ### Version 2.0.11
595
+ ## Version 2.0.11
586
596
 
587
597
  - Don't change method visibility when adding instrumentation to methods using metaprogramming
588
598
  - RSpec: add support for Compound expectations
589
599
 
590
- ### Version 2.0.10
600
+ ## Version 2.0.10
591
601
 
592
602
  - Assertions: allow ignoring certain tags when asserting for other tags to be present.
593
603
 
594
- ### Version 2.0.9
604
+ ## Version 2.0.9
595
605
 
596
606
  - Better error message for `assert_no_statsd_calls`
597
607
 
598
- ### Version 2.0.8
608
+ ## Version 2.0.8
599
609
 
600
610
  - More tag handling performance improvements.
601
611
  - RSpec matchers documentation improvements
602
612
 
603
- ### Version 2.0.7
613
+ ## Version 2.0.7
604
614
 
605
615
  - Tag handling performance improvements.
606
616
  - Test against Ruby 2.2.
607
617
  - Drop support for Ruby 1.9.3.
608
618
 
609
- ### Version 2.0.6
619
+ ## Version 2.0.6
610
620
 
611
621
  - Fix some loading order issues in Rails environments.
612
622
  - Default behavior: in a **staging** environment, the defaults are now the same as in a **production environment**.
613
623
  - Documentation overhaul
614
624
 
615
- ### Version 2.0.5
625
+ ## Version 2.0.5
616
626
 
617
627
  - Allow for nested assertions using the `assert_statsd_*` assertion methods.
618
628
 
619
- ### Version 2.0.4
629
+ ## Version 2.0.4
620
630
 
621
631
  - Add a Railtie to fix some initialization issues.
622
632
 
623
- ### Version 2.0.3
633
+ ## Version 2.0.3
624
634
 
625
635
  - Assertion method bugfixes
626
636
 
627
- ### Version 2.0.2
637
+ ## Version 2.0.2
628
638
 
629
639
  - Documentation fixes
630
640
 
631
- ### Version 2.0.1
641
+ ## Version 2.0.1
632
642
 
633
643
  - Add assertion methods `assert_statsd_histogram`, `assert_statsd_set`, and `assert_statsd_key_value`.
634
644
 
635
- ### Version 2.0.0
645
+ ## Version 2.0.0
636
646
 
637
647
  - Complete rewrite using pluggable backends.
638
648
  - 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
@@ -97,8 +97,8 @@ module StatsD
97
97
  unless sample_rate.nil? || sample_rate.is_a?(Numeric)
98
98
  raise ArgumentError, "The sample_rate argument should be a number, got #{sample_rate}"
99
99
  end
100
- unless tags.nil? || tags.is_a?(Hash) || tags.is_a?(Array)
101
- raise ArgumentError, "The tags argument should be a hash or an array, got #{tags.inspect}"
100
+ unless tags.nil? || tags.is_a?(Hash) || tags.is_a?(Array) || tags.is_a?(Proc)
101
+ raise ArgumentError, "The tags argument should be a hash, a proc or an array, got #{tags.inspect}"
102
102
  end
103
103
  end
104
104
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module StatsD
4
4
  module Instrument
5
- VERSION = "3.4.0"
5
+ VERSION = "3.5.1"
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.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: 2022-08-29 00:00:00.000000000 Z
13
+ date: 2023-01-03 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