Publish form count KPI metrics to CloudWatch#2821
Conversation
There was a problem hiding this comment.
Pull request overview
This PR introduces automated KPI publishing for form counts to AWS CloudWatch, enabling ongoing platform growth tracking without manual reporting.
Changes:
- Added
Metrics::FormCountServiceto aggregate form counts by organisation and form state and publish them to CloudWatch. - Added a
metrics:export_form_countsrake task intended for scheduled execution. - Added specs covering the service publishing behaviour and the rake task wiring.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
app/services/metrics/form_count_service.rb |
Implements aggregation + CloudWatch publishing for per-org/per-state form counts. |
lib/tasks/metrics.rake |
Adds scheduled-entry rake task to trigger publishing. |
spec/services/metrics/form_count_service_spec.rb |
Verifies CloudWatch payload structure and error capture/re-raise behaviour. |
spec/lib/tasks/metrics.rake_spec.rb |
Ensures rake task invokes Metrics::FormCountService#publish_form_counts. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
00a4a7a to
12d129c
Compare
12d129c to
fa34024
Compare
Publishes form counts grouped by organisation and state so they can be monitored in CloudWatch.
Provides metrics:export_form_counts for scheduled publishing of form count metrics via FormCountService.
Internal orgs are used for GOV.UK Forms testing and should not appear in CloudWatch form count metrics published for reporting.
Replace direct CloudWatch API calls with the OpenTelemetry Metrics SDK, pushing gauge metrics to the collector sidecar for forwarding. This is due to otel metrics ingestion being significantly cheaper for high cardinality metrics.
f096a0f to
bb4d244
Compare
| organisation_names.each { |org_name| ensure_all_metric_states(totals, org_name) } | ||
| ensure_all_metric_states(totals, UNKNOWN_ORG) if totals.keys.any? { |(org, _), _| org == UNKNOWN_ORG } | ||
| totals |
| def export_metrics! | ||
| return if OpenTelemetry.meter_provider.metric_readers.empty? | ||
|
|
||
| result = OpenTelemetry.meter_provider.force_flush | ||
| return if result == OpenTelemetry::SDK::Metrics::Export::SUCCESS | ||
|
|
||
| raise ExportError, "OpenTelemetry metrics export failed with result code #{result}" | ||
| end |
| before do | ||
| allow(Settings).to receive(:forms_env).and_return(forms_env) | ||
|
|
||
| provider = OpenTelemetry::SDK::Metrics::MeterProvider.new | ||
| periodic_reader = OpenTelemetry::SDK::Metrics::Export::PeriodicMetricReader.new( | ||
| export_interval_millis: 60_000, | ||
| exporter: metric_exporter, | ||
| ) | ||
| provider.add_metric_reader(periodic_reader) | ||
| OpenTelemetry.meter_provider = provider | ||
| end | ||
|
|
||
| after do | ||
| OpenTelemetry.meter_provider.shutdown | ||
| end |
|
🎉 A review copy of this PR has been deployed! You can reach it at: https://pr-2821.admin.review.forms.service.gov.uk/ It may take 5 minutes or so for the application to be fully deployed and working. If it still isn't ready For the sign in details and more information, see the review apps wiki page. |
What problem does this pull request solve?
Trello card: https://trello.com/c/b3cNvzu5/3136-record-kpis-in-cloudwatch
This adds automated form count metrics to CloudWatch so we can track how the platform is growing over time and measure the impact of our work. A new service publishes counts grouped by organisation and state (live, archived, draft), and a metrics:export_form_counts rake task will run on a scheduled cron job to publish those metrics on a regular basis. This lays the foundation for KPI reporting without manual updates.