Skip to content

Commit 70c1c91

Browse files
committed
Record submission counts with OpenTelemetry counter
Replace the periodic database aggregation rake task with an incrementing counter at submission creation time, and export metrics via PeriodicMetricReader.
1 parent 76eb56d commit 70c1c91

9 files changed

Lines changed: 152 additions & 227 deletions

File tree

app/services/form_submission_service.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ def create_submission_record
104104

105105
submission.deliveries.create!(delivery_schedule: :immediate)
106106

107+
Metrics::SubmissionCounter.record(form_id: form.id, form_name: form.name, mode:)
108+
107109
submission
108110
end
109111

app/services/metrics/submission_count_service.rb

Lines changed: 0 additions & 86 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
module Metrics
2+
class SubmissionCounter
3+
METRIC_NAME = "SubmissionCount".freeze
4+
METER_NAME = "forms-runner".freeze
5+
METER_VERSION = "1.0".freeze
6+
7+
class << self
8+
def record(form_id:, form_name:, mode:, meter_provider: OpenTelemetry.meter_provider)
9+
return if mode.preview?
10+
11+
counter(meter_provider).add(
12+
1,
13+
attributes: metric_attributes(form_id:, form_name:),
14+
)
15+
end
16+
17+
private
18+
19+
def counter(meter_provider)
20+
counters[meter_provider] ||= meter(meter_provider).create_counter(
21+
METRIC_NAME,
22+
unit: "1",
23+
description: "Number of form submissions",
24+
)
25+
end
26+
27+
def counters
28+
@counters ||= {}
29+
end
30+
31+
def meter(meter_provider)
32+
meter_provider.meter(METER_NAME, version: METER_VERSION)
33+
end
34+
35+
def metric_attributes(form_id:, form_name:)
36+
{
37+
"Environment" => Settings.forms_env.downcase,
38+
"FormId" => form_id.to_s,
39+
"FormName" => form_name.to_s,
40+
}
41+
end
42+
end
43+
end
44+
end

config/initializers/opentelemetry.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
return unless ENV["ENABLE_OTEL"] == "true"
77

8-
ENV["OTEL_METRICS_EXPORTER"] ||= "otlp"
9-
108
OpenTelemetry::SDK.configure do |c|
119
instrumentation_config = { "OpenTelemetry::Instrumentation::Rack" => { untraced_endpoints: ["/up"] } }
1210
c.use_all(instrumentation_config)
@@ -16,6 +14,14 @@
1614
c.id_generator = OpenTelemetry::Propagator::XRay::IDGenerator
1715
end
1816

17+
unless ENV.fetch("OTEL_METRICS_EXPORTER", "otlp") == "none"
18+
c.add_metric_reader(
19+
OpenTelemetry::SDK::Metrics::Export::PeriodicMetricReader.new(
20+
exporter: OpenTelemetry::Exporter::OTLP::Metrics::MetricsExporter.new,
21+
),
22+
)
23+
end
24+
1925
# Disable logging for Rake tasks to avoid cluttering output
2026
c.logger = Logger.new(File::NULL) if Rails.const_defined?(:Rake) && Rake.application.top_level_tasks.any?
2127
end

lib/tasks/metrics.rake

Lines changed: 0 additions & 6 deletions
This file was deleted.

spec/lib/tasks/metrics.rake_spec.rb

Lines changed: 0 additions & 17 deletions
This file was deleted.

spec/services/form_submission_service_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@
102102
expect(log_line["submission_reference"]).to eq(reference)
103103
end
104104

105+
it "records a submission count metric" do
106+
expect(Metrics::SubmissionCounter).to receive(:record).with(
107+
form_id: form.id,
108+
form_name: form.name,
109+
mode:,
110+
)
111+
112+
service.submit
113+
end
114+
105115
shared_examples "logging" do
106116
it "logs submission" do
107117
allow(LogEventService).to receive(:log_submit).once

spec/services/metrics/submission_count_service_spec.rb

Lines changed: 0 additions & 116 deletions
This file was deleted.

0 commit comments

Comments
 (0)