Shopify / statsd-instrument

A StatsD client for Ruby apps. Provides metaprogramming methods to inject StatsD instrumentation into your code.
http://shopify.github.io/statsd-instrument
MIT License
570 stars 94 forks source link

Allow user to assert different tags using RSpec #331

Closed rafaelzimmermann closed 1 year ago

rafaelzimmermann commented 1 year ago

Previously the matcher filtered out metrics based on type and name to test against expectation. So, when the user tried to assert different different tags, the matcher would fail. With this change, the matcher will filter out metrics also based on tags too, allowing the user to use RSpec and expectation.

For instance this test would fail before:

RSpec.describe 'Matchers' do
  context 'trigger_statsd_increment' do
    it 'will pass if every statsD call matches its calls' do
      expect do
        StatsD.increment('counter', tags: ['variation:a'])
        StatsD.increment('counter', tags: ['variation:b'])
      end.to trigger_statsd_increment('counter', times: 1, tags: ["variation:a"]).and trigger_statsd_increment('counter', times: 1, tags: ["variation:b"])
    end
  end
end

fixes: https://github.com/Shopify/statsd-instrument/issues/300

ChanChar commented 1 year ago

@rafaelzimmermann this may break existing tests that didn't specify tags before.

def test_statsd_increment_compound_without_explicit_tags_using_and_matched
  matcher_1 = StatsD::Instrument::Matchers::Increment.new(:c, "counter", times: 1)
  matcher_2 = StatsD::Instrument::Matchers::Increment.new(:c, "counter", times: 1)

  assert(matcher_1.and(matcher_2).matches?(lambda {
    StatsD.increment("counter", tags: ["a"])
    StatsD.increment("counter", tags: ["b"])
  }))
end