statsd-instrument 3.5.5 → 3.5.6

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: 0b4caa06189952666d3dd979521ec76bcf4bbf4265605c2d9d30198773f74ab8
4
- data.tar.gz: ec65748283c1a32434f6388e24b167133ba53ec36353c4ec5f8ccfe920657b58
3
+ metadata.gz: ebf148c48529e19f207e601d9f31158c7d1fda2d6dc0f368192163b4d01c2f9c
4
+ data.tar.gz: b66c620179390cf51365d4d9f23ffba1cdf02b8036d35a3bc161d7394148b9f0
5
5
  SHA512:
6
- metadata.gz: af4a6f52973f439a4d9246fff0d68c74751173998b1a76c9fdbff87ac001f2718ccc8fe2c5abb6d5bdaa5555f9c82150f196c8fe6f819e913a6828ef269a2e14
7
- data.tar.gz: 79f76342bc46cf2d07fd96f1d72bbe5bc2367251ff93b05b6c4ed938fe96d85bf44c8178ca4f1f4a7186f10921bfbaea456b2ce2d32e64db806ec526b2894d71
6
+ metadata.gz: 25633d9db00b7187fe226ec98b2d4d1944a550af5de739b0a99f87152322f6d0fe764cf0b491308a6bf7165038ae122f9cd13f352f2a0eee17ded614886fa959
7
+ data.tar.gz: 187a4bce514fc40da281fab5ddf6d620a788151663410da44444985e9172ca56764679cdab51323ad4a130ecedb1ebbee5c896caf7604a89e2f64bafe6aa7b57
data/CHANGELOG.md CHANGED
@@ -6,6 +6,11 @@ section below.
6
6
 
7
7
  ## Unreleased changes
8
8
 
9
+ ## Version 3.5.6
10
+
11
+ - Fix issue from 3.5.5 where tests using RSpec matcher for tag assertion would fail, because the matcher as being
12
+ use as an array.
13
+
9
14
  ## Version 3.5.5
10
15
 
11
16
  - Fix issue on 3.5.4, allowing user to specify compound matcher without tags
@@ -49,19 +49,41 @@ module StatsD
49
49
  # @param [Array<String>] metric_names (default: []) The metric names that are not
50
50
  # allowed to happen inside the block. If this is set to `[]`, the assertion
51
51
  # will fail if any metric occurs.
52
+ # @param [Array<StatsD::Instrument::Datagram>] datagrams (default: nil) The datagrams
53
+ # to be inspected for metric emission.
54
+ # @param [StatsD::Instrument::Client] client (default: nil) The client to be used
55
+ # for fetching datagrams (if not provided) and metric prefix. If not provided, the
56
+ # singleton client will attempt to be used.
57
+ # @param [Boolean] no_prefix (default: false) A directive to indicate if the client's
58
+ # prefix should be prepended to the metric names.
52
59
  # @yield A block in which the specified metric should not occur. This block
53
60
  # should not raise any exceptions.
54
61
  # @return [void]
55
62
  # @raise [Minitest::Assertion] If an exception occurs, or if any metric (with the
56
63
  # provided names, or any), occurred during the execution of the provided block.
57
- def assert_no_statsd_calls(*metric_names, datagrams: nil, client: nil, &block)
64
+ def assert_no_statsd_calls(*metric_names, datagrams: nil, client: nil, no_prefix: false, &block)
65
+ client ||= StatsD.singleton_client
66
+
58
67
  if datagrams.nil?
59
68
  raise LocalJumpError, "assert_no_statsd_calls requires a block" unless block_given?
60
69
 
61
70
  datagrams = capture_statsd_datagrams_with_exception_handling(client: client, &block)
62
71
  end
63
72
 
64
- datagrams.select! { |metric| metric_names.include?(metric.name) } unless metric_names.empty?
73
+ formatted_metrics = if no_prefix
74
+ metric_names
75
+ else
76
+ metric_names.map do |metric|
77
+ if StatsD::Instrument::Helpers.prefixed_metric?(metric, client: client)
78
+ warn("`#{__method__}` will prefix metrics by default. `#{metric}` skipped due to existing prefix.")
79
+ metric
80
+ else
81
+ StatsD::Instrument::Helpers.prefix_metric(metric, client: client)
82
+ end
83
+ end
84
+ end
85
+
86
+ datagrams.select! { |metric| formatted_metrics.include?(metric.name) } unless formatted_metrics.empty?
65
87
  assert(datagrams.empty?, "No StatsD calls for metric #{datagrams.map(&:name).join(", ")} expected.")
66
88
  end
67
89
 
@@ -37,9 +37,8 @@ module StatsD
37
37
  def initialize(client: nil, type:, name:, value: nil,
38
38
  sample_rate: nil, tags: nil, no_prefix: false, times: 1)
39
39
 
40
- client ||= StatsD.singleton_client
41
40
  @type = type
42
- @name = no_prefix || !client.prefix ? name : "#{client.prefix}.#{name}"
41
+ @name = no_prefix ? name : StatsD::Instrument::Helpers.prefix_metric(name, client: client)
43
42
  @value = normalized_value_for_type(type, value) if value
44
43
  @sample_rate = sample_rate
45
44
  @tags = normalize_tags(tags)
@@ -26,6 +26,18 @@ module StatsD
26
26
 
27
27
  tags
28
28
  end
29
+
30
+ def self.prefix_metric(metric_name, client: nil)
31
+ client ||= StatsD.singleton_client
32
+ client&.prefix ? "#{client.prefix}.#{metric_name}" : metric_name
33
+ end
34
+
35
+ def self.prefixed_metric?(metric_name, client: nil)
36
+ client ||= StatsD.singleton_client
37
+ return false unless client&.prefix
38
+
39
+ metric_name =~ /\A#{Regexp.escape(client.prefix)}\./
40
+ end
29
41
  end
30
42
  end
31
43
  end
@@ -45,8 +45,8 @@ module StatsD
45
45
  metrics = capture_statsd_calls(&block)
46
46
  metrics = metrics.select do |m|
47
47
  metric_tags = m.tags || []
48
- options_tags = options[:tags] || []
49
- tag_matches = options_tags.empty? || metric_tags.all? { |t| options_tags.include?(t) }
48
+ options_tags = options[:tags]
49
+ tag_matches = options_tags.nil? || RSpec::Matchers::BuiltIn::Match.new(options_tags).matches?(metric_tags)
50
50
  m.type == metric_type && m.name == metric_name && tag_matches
51
51
  end
52
52
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module StatsD
4
4
  module Instrument
5
- VERSION = "3.5.5"
5
+ VERSION = "3.5.6"
6
6
  end
7
7
  end
@@ -74,6 +74,66 @@ class AssertionsTest < Minitest::Test
74
74
  assert_equal(assertion.message, "No StatsD calls for metric other, another expected.")
75
75
  end
76
76
 
77
+ def test_assert_no_statsd_calls_with_prefix
78
+ client = StatsD::Instrument::Client.new(prefix: "prefix")
79
+
80
+ @test_case.assert_no_statsd_calls("counter", client: client) do
81
+ client.increment("other")
82
+ end
83
+
84
+ assertion = assert_raises(Minitest::Assertion) do
85
+ @test_case.assert_no_statsd_calls("counter", client: client) do
86
+ client.increment("counter")
87
+ end
88
+ end
89
+ assert_equal(assertion.message, "No StatsD calls for metric prefix.counter expected.")
90
+
91
+ @test_case.expects(:warn).with(
92
+ "`assert_no_statsd_calls` will prefix metrics by default. `prefix.counter` skipped due to existing prefix."
93
+ )
94
+ assertion = assert_raises(Minitest::Assertion) do
95
+ @test_case.assert_no_statsd_calls("prefix.counter", client: client) do
96
+ client.increment("counter")
97
+ end
98
+ end
99
+ assert_equal(assertion.message, "No StatsD calls for metric prefix.counter expected.")
100
+
101
+ @test_case.expects(:warn).never
102
+ assertion = assert_raises(Minitest::Assertion) do
103
+ @test_case.assert_no_statsd_calls("prefix.counter", client: client, no_prefix: true) do
104
+ client.increment("counter")
105
+ end
106
+ end
107
+ assert_equal(assertion.message, "No StatsD calls for metric prefix.counter expected.")
108
+
109
+ assertion = assert_raises(Minitest::Assertion) do
110
+ @test_case.assert_no_statsd_calls("counter", client: client, no_prefix: true) do
111
+ client.increment("counter", no_prefix: true)
112
+ end
113
+ end
114
+ assert_equal(assertion.message, "No StatsD calls for metric counter expected.")
115
+
116
+ StatsD.singleton_client = client
117
+
118
+ @test_case.assert_no_statsd_calls("counter") do
119
+ StatsD.increment("other")
120
+ end
121
+
122
+ assertion = assert_raises(Minitest::Assertion) do
123
+ @test_case.assert_no_statsd_calls("counter") do
124
+ StatsD.increment("counter")
125
+ end
126
+ end
127
+ assert_equal(assertion.message, "No StatsD calls for metric prefix.counter expected.")
128
+
129
+ assertion = assert_raises(Minitest::Assertion) do
130
+ @test_case.assert_no_statsd_calls("counter", no_prefix: true) do
131
+ StatsD.increment("counter", no_prefix: true)
132
+ end
133
+ end
134
+ assert_equal(assertion.message, "No StatsD calls for metric counter expected.")
135
+ end
136
+
77
137
  def test_assert_statsd
78
138
  @test_case.assert_statsd_increment("counter") do
79
139
  StatsD.increment("counter")
data/test/helpers_test.rb CHANGED
@@ -7,6 +7,11 @@ class HelpersTest < Minitest::Test
7
7
  test_class = Class.new(Minitest::Test)
8
8
  test_class.send(:include, StatsD::Instrument::Helpers)
9
9
  @test_case = test_class.new("fake")
10
+ @old_client = StatsD.singleton_client
11
+ end
12
+
13
+ def teardown
14
+ StatsD.singleton_client = @old_client
10
15
  end
11
16
 
12
17
  def test_capture_metrics_inside_block_only
@@ -78,4 +83,58 @@ class HelpersTest < Minitest::Test
78
83
  StatsD::Instrument::Helpers.add_tag(1, :key, 123)
79
84
  end
80
85
  end
86
+
87
+ def test_prefix_metric_returns_metric_if_no_prefix
88
+ metric = "metric"
89
+ client = StatsD::Instrument::Client.new(prefix: nil)
90
+ assert_equal(metric, StatsD::Instrument::Helpers.prefix_metric(metric, client: client))
91
+ end
92
+
93
+ def test_prefix_metric_returns_prefixed_metric
94
+ prefix = "prefix"
95
+ metric = "metric"
96
+ client = StatsD::Instrument::Client.new(prefix: prefix)
97
+ assert_equal("#{prefix}.#{metric}", StatsD::Instrument::Helpers.prefix_metric(metric, client: client))
98
+ end
99
+
100
+ def test_prefix_metric_can_use_singleton_client
101
+ prefix = "prefix"
102
+ metric = "metric"
103
+ StatsD.singleton_client = StatsD::Instrument::Client.new(prefix: prefix)
104
+ assert_equal("#{prefix}.#{metric}", StatsD::Instrument::Helpers.prefix_metric(metric))
105
+ end
106
+
107
+ def test_prefixed_metric_return_true_if_prefix_present
108
+ prefix = "prefix"
109
+ metric = "prefix.metric"
110
+ client = StatsD::Instrument::Client.new(prefix: prefix)
111
+ assert(StatsD::Instrument::Helpers.prefixed_metric?(metric, client: client))
112
+ end
113
+
114
+ def test_prefixed_meric_returns_false_if_prefix_missing
115
+ prefix = "prefix"
116
+ metric = "metric"
117
+ client = StatsD::Instrument::Client.new(prefix: prefix)
118
+ refute(StatsD::Instrument::Helpers.prefixed_metric?(metric, client: client))
119
+ end
120
+
121
+ def test_prefixed_metric_returns_false_if_prefix_not_at_beginning
122
+ prefix = "prefix"
123
+ metric = "metric.prefix"
124
+ client = StatsD::Instrument::Client.new(prefix: prefix)
125
+ refute(StatsD::Instrument::Helpers.prefixed_metric?(metric, client: client))
126
+ end
127
+
128
+ def test_prefixed_metrics_returns_false_if_no_prefix_defined
129
+ metric = "prefix.metric"
130
+ client = StatsD::Instrument::Client.new(prefix: nil)
131
+ refute(StatsD::Instrument::Helpers.prefixed_metric?(metric, client: client))
132
+ end
133
+
134
+ def test_prefixed_metric_can_use_singleton_client
135
+ prefix = "prefix"
136
+ metric = "prefix.metric"
137
+ StatsD.singleton_client = StatsD::Instrument::Client.new(prefix: prefix)
138
+ assert(StatsD::Instrument::Helpers.prefixed_metric?(metric))
139
+ end
81
140
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "test_helper"
4
4
  require "statsd/instrument/matchers"
5
+ require "rspec/mocks/argument_matchers"
5
6
 
6
7
  class MatchersTest < Minitest::Test
7
8
  def test_statsd_increment_matched
@@ -114,6 +115,13 @@ class MatchersTest < Minitest::Test
114
115
  .matches?(lambda { StatsD.increment("counter", tags: ["a", "b"]) }))
115
116
  end
116
117
 
118
+ def test_statsd_increment_with_subset_matcher
119
+ include_matcher = RSpec::Matchers::BuiltIn::Include.new("foo:bar")
120
+ final = RSpec::Matchers::AliasedMatcher.new(include_matcher, :include)
121
+ assert(StatsD::Instrument::Matchers::Increment.new(:c, "counter", tags: final)
122
+ .matches?(lambda { StatsD.increment("counter", tags: ["foo:bar", "bar:baz"]) }))
123
+ end
124
+
117
125
  def test_statsd_increment_with_tags_not_matched
118
126
  refute(StatsD::Instrument::Matchers::Increment.new(:c, "counter", tags: ["a", "b"])
119
127
  .matches?(lambda { StatsD.increment("counter", tags: ["c"]) }))
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.5.5
4
+ version: 3.5.6
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: 2023-03-02 00:00:00.000000000 Z
13
+ date: 2023-03-09 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.