Skip to content

Commit

Permalink
Merge pull request #62 from Shopify/ignore_tags
Browse files Browse the repository at this point in the history
assertion options[:ignore_tags]
  • Loading branch information
fw42 committed Sep 16, 2015
2 parents ede70f8 + 8b4e955 commit 9bcba32
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/statsd/instrument/assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,23 @@ def assert_statsd_call(metric_type, metric_name, options = {}, &block)

assert_equal options[:sample_rate], metric.sample_rate, "Unexpected value submitted for StatsD metric #{metric_name}" if options[:sample_rate]
assert_equal options[:value], metric.value, "Unexpected StatsD sample rate for metric #{metric_name}" if options[:value]
assert_equal Set.new(StatsD::Instrument::Metric.normalize_tags(options[:tags])), Set.new(metric.tags), "Unexpected StatsD tags for metric #{metric_name}" if options[:tags]

if options[:tags]
expected_tags = Set.new(StatsD::Instrument::Metric.normalize_tags(options[:tags]))
actual_tags = Set.new(metric.tags)

if options[:ignore_tags]
ignored_tags = Set.new(StatsD::Instrument::Metric.normalize_tags(options[:ignore_tags])) - expected_tags
actual_tags -= ignored_tags

if options[:ignore_tags].is_a?(Array)
actual_tags.delete_if{ |key| options[:ignore_tags].include?(key.split(":").first) }
end
end

assert_equal expected_tags, actual_tags,
"Unexpected StatsD tags for metric #{metric_name}. Expected: #{expected_tags.inspect}, actual: #{actual_tags.inspect}"
end

metric
end
Expand Down
42 changes: 42 additions & 0 deletions test/assertions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,48 @@ def test_assert_statsd_call
end
end

assert_assertion_triggered do
@test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: ['a', 'b'], ignore_tags: ['b']) do
StatsD.increment('counter', sample_rate: 0.5, tags: ['a'])
end
end

assert_no_assertion_triggered do
@test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: ['a'], ignore_tags: ['b']) do
StatsD.increment('counter', sample_rate: 0.5, tags: ['a', 'b'])
end
end

assert_no_assertion_triggered do
@test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: ['a'], ignore_tags: ['b']) do
StatsD.increment('counter', sample_rate: 0.5, tags: ['a'])
end
end

assert_no_assertion_triggered do
@test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: { a: 1 }, ignore_tags: { b: 2 }) do
StatsD.increment('counter', sample_rate: 0.5, tags: { a: 1, b: 2 })
end
end

assert_assertion_triggered do
@test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: { a: 1 }, ignore_tags: { b: 2 }) do
StatsD.increment('counter', sample_rate: 0.5, tags: { a: 1, b: 3 })
end
end

assert_assertion_triggered do
@test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: { a: 1, b: 3 }, ignore_tags: ['b']) do
StatsD.increment('counter', sample_rate: 0.5, tags: { a: 1, b: 2 })
end
end

assert_no_assertion_triggered do
@test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: { a: 1 }, ignore_tags: ['b']) do
StatsD.increment('counter', sample_rate: 0.5, tags: { a: 1, b: 2 })
end
end

assert_assertion_triggered do
@test_case.assert_statsd_increment('counter', sample_rate: 0.5, tags: ['a', 'b']) do
StatsD.increment('counter', sample_rate: 0.2, tags: ['c'])
Expand Down

0 comments on commit 9bcba32

Please sign in to comment.