diff --git a/examples/testing/app.rb b/examples/testing/app.rb new file mode 100644 index 0000000..d6a82c5 --- /dev/null +++ b/examples/testing/app.rb @@ -0,0 +1,16 @@ +require 'metrics/provider' + +class MyApplication + def work + # ... + end + + Metrics::Provider(self) do + WORK_METRIC = Metrics.metric('my_application.work.count', :counter, description: 'Work counter') + + def work + WORK_METRIC.emit(1) + super + end + end +end diff --git a/examples/testing/test.rb b/examples/testing/test.rb new file mode 100755 index 0000000..b94a0ec --- /dev/null +++ b/examples/testing/test.rb @@ -0,0 +1,12 @@ +#!/usr/bin/env sus + +ENV['METRICS_BACKEND'] ||= 'metrics/backend/test' + +require_relative 'app' + +describe MyApplication do + it 'should emit metrics' do + expect(MyApplication::WORK_METRIC).to receive(:emit).with(1) + MyApplication.new.work + end +end diff --git a/guides/links.yaml b/guides/links.yaml index 8d9310c..599e569 100644 --- a/guides/links.yaml +++ b/guides/links.yaml @@ -1,4 +1,6 @@ getting-started: order: 1 capture: - order: 2 \ No newline at end of file + order: 2 +testing: + order: 3 diff --git a/guides/testing/readme.md b/guides/testing/readme.md new file mode 100644 index 0000000..c38b819 --- /dev/null +++ b/guides/testing/readme.md @@ -0,0 +1,43 @@ +# Testing + +This guide explains how to write assertions in your test suite to validate `metrics` are being emitted correctly. + +## Application Code + +In your application code, you should emit metrics, e.g. + +```ruby +require 'metrics/provider' + +class MyApplication + def work + # ... + end + + Metrics::Provider(self) do + WORK_METRIC = Metrics.metric('my_application.work.count', :counter, description: 'Work counter') + + def work + WORK_METRIC.emit(1) + super + end + end +end +``` + +## Test Code + +In your test code, you should assert that the metrics are being emitted correctly, e.g. + +```ruby +ENV['METRICS_BACKEND'] ||= 'metrics/backend/test' + +require_relative 'app' + +describe MyApplication do + it 'should emit metrics' do + expect(MyApplication::WORK_METRIC).to receive(:emit).with(1) + MyApplication.new.work + end +end +```