Add custom matchers for internal events
What does this MR do and why?
Adds a maintainable & usable framework for composing assertions about internal events & metrics. The MR also migrates a more complicated use-case that was previously unable to use the existing 'internal events tracking'
shared example.
Composable how?
- a new metric-based matcher mirrors the behavior of the
.change
matcher, so assertions can be chained together. - a new event-based matcher mirrors the behavior of the
.receive
matcher, but can also be tacked onto the same assertions.
Goals:
- straightforward for engineers to implement specs for events/metrics
- communicate clearly what is being tested by reading the specs
- cover all cases where internal events/metrics are being used
Related MRs:
- Improve error messages for
trigger_internal_events
failures - Migrate existing internal events shared example to use new matchers
Example usage:
RSpec.describe 'Composable assertions for events & metrics' do
it 'increments internal events metrics for compliance dashboard' do
expect { Gitlab::InternalEvents.track_event('g_compliance_dashboard', user: user, namespace: group) }
.to trigger_internal_event('g_compliance_dashboard').with(user: user, namespace: group).exactly(1).time
.and increment_usage_metric('redis_hll_counters.compliance.g_compliance_dashboard_monthly').by(1)
.and not_trigger_internal_event('web_ide_viewed')
end
it 'increments database-backed issue creation metrics' do
expect { Issues::CreateService.new(container: project, current_user: user) }
.to increment_usage_metric('usage_activity_by_stage.plan.issues')
.and increment_usage_metric('usage_activity_by_stage_monthly.plan.issues')
end
end
MR acceptance checklist
Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.
Related to #450452 (closed)
Edited by Sarah Yasonik