Skip to content

Publish form count KPI metrics to CloudWatch#2821

Open
theseanything wants to merge 5 commits into
mainfrom
theseanything/publish-form-counts-to-cloudwatch
Open

Publish form count KPI metrics to CloudWatch#2821
theseanything wants to merge 5 commits into
mainfrom
theseanything/publish-form-counts-to-cloudwatch

Conversation

@theseanything

Copy link
Copy Markdown
Contributor

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.

@theseanything theseanything marked this pull request as ready for review June 4, 2026 07:58
@theseanything theseanything requested review from a team and Copilot June 4, 2026 07:59

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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::FormCountService to aggregate form counts by organisation and form state and publish them to CloudWatch.
  • Added a metrics:export_form_counts rake 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.

Comment thread app/services/metrics/form_count_service.rb Outdated
@theseanything theseanything force-pushed the theseanything/publish-form-counts-to-cloudwatch branch 3 times, most recently from 00a4a7a to 12d129c Compare June 4, 2026 17:58
Comment thread app/services/metrics/form_count_service.rb
@theseanything theseanything force-pushed the theseanything/publish-form-counts-to-cloudwatch branch from 12d129c to fa34024 Compare June 8, 2026 07:54
lfdebrux
lfdebrux previously approved these changes Jun 8, 2026
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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 7 changed files in this pull request and generated 3 comments.

Comment on lines +31 to +33
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
Comment on lines +86 to +93
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
Comment on lines +12 to +26
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
@github-actions

Copy link
Copy Markdown

🎉 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
after 5 minutes, there may be something wrong with the ECS task. You will need to go to the integration AWS account
to debug, or otherwise ask an infrastructure person.

For the sign in details and more information, see the review apps wiki page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants