-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #275 from alphagov/freshness-metrics
Freshness metrics
- Loading branch information
Showing
4 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
require "govuk_app_config/govuk_prometheus_exporter" | ||
GovukPrometheusExporter.configure | ||
require "collectors/global_prometheus_collector" | ||
|
||
GovukPrometheusExporter.configure(collectors: [Collectors::GlobalPrometheusCollector]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require "prometheus_exporter" | ||
require "prometheus_exporter/server" | ||
|
||
module Collectors | ||
class GlobalPrometheusCollector < PrometheusExporter::Server::TypeCollector | ||
SECONDS_PER_DAY = 86_400 | ||
|
||
def type | ||
"locations_api_global" | ||
end | ||
|
||
def metrics | ||
oldest_os_places_postcode = PrometheusExporter::Metric::Gauge.new("locations_api_oldest_os_places_postcode_age_days", "Days since oldest postcode was last updated via OS Places API (Expected in prod: ~7)") | ||
oldest_os_places_postcode.observe(get_oldest_postcode / SECONDS_PER_DAY) | ||
|
||
[oldest_os_places_postcode] | ||
end | ||
|
||
private | ||
|
||
def get_oldest_postcode | ||
# Cache metric to prevent needless expensive calls to the database | ||
Rails.cache.fetch("metrics:oldest_postcode", expires_in: 1.hour) do | ||
(Time.zone.now.to_i - Postcode.os_places.order(updated_at: :asc).first.updated_at.to_i) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require "spec_helper" | ||
|
||
RSpec.describe Collectors::GlobalPrometheusCollector do | ||
before do | ||
@collector = Collectors::GlobalPrometheusCollector.new | ||
Rails.cache.delete("metrics:oldest_postcode") | ||
end | ||
|
||
describe "#type" do | ||
it "has the correct value" do | ||
expect(@collector.type).to eq("locations_api_global") | ||
end | ||
end | ||
|
||
describe "#metrics" do | ||
before do | ||
@postcode = Postcode.create!(postcode: "E18QS", updated_at: Time.zone.now - 2.days) | ||
end | ||
|
||
it "records the time in days since the oldest postcode's update timestamp" do | ||
metrics = @collector.metrics | ||
|
||
expect(metrics.first.data.first.last).to eq(2) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters