yabeda-rb / yabeda

Extendable framework for collecting and exporting metrics from your Ruby application
MIT License
775 stars 25 forks source link

Testing mode with RSpec matchers #25

Closed Envek closed 3 years ago

Envek commented 3 years ago

Goal: provide tools to make testing that Yabeda metrics are being changed as expected to be convenient and allow users not to rely on Yabeda internals that are going to change

Usage with RSpec

Add the following to your rails_helper.rb (or spec_helper.rb):

require "yabeda/rspec"

Now you can use increment_yabeda_counter, update_yabeda_gauge, and measure_yabeda_histogram matchers:

it "increments counters" do
  expect { subject }.to increment_yabeda_counter(Yabeda.myapp.foo_count).by(3)
end

You can scope metrics by used tags with with_tags:

it "updates gauges" do
  expect { subject }.to \
    update_yabeda_gauge("some_gauge_name").
    with_tags(method: "command", command: "subscribe")
end

Note that tags you specified doesn't need to be exact, but can be a subset of tags used on metric update. In this example updates with following sets of tags { method: "command", command: "subscribe", status: "SUCCESS" } and { method: "command", command: "subscribe", status: "FAILURE" } will make test example to pass.

And check for values with by for counters, to for gauges, and with for gauges and histograms (and you can use other matchers here):

expect { subject }.to \
  measure_yabeda_histogram(Yabeda.something.anything_runtime).
  with(be_between(0.005, 0.05))
dsalahutdinov commented 3 years ago

Great job! Testing yabeda is now a little bit tricky and not standardized:

Having rspec matchers simplifies testing. WDYT if we remove internal yabeda values in the same release?