-
Notifications
You must be signed in to change notification settings - Fork 0
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 #3 from alphagov/publishing-latency-sli
Publishing latency SLI
- Loading branch information
Showing
23 changed files
with
1,720 additions
and
0 deletions.
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
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 @@ | ||
--require spec_helper |
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
inherit_gem: | ||
rubocop-govuk: | ||
- config/default.yml | ||
- config/rspec.yml | ||
|
||
inherit_mode: | ||
merge: | ||
|
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
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
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
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,10 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require "bundler" | ||
Bundler.setup(:default) | ||
|
||
$LOAD_PATH << "./lib" | ||
|
||
require "govuk_sli_collector" | ||
|
||
GovukSliCollector.call |
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,7 @@ | ||
require "govuk_sli_collector/publishing_latency_sli" | ||
|
||
module GovukSliCollector | ||
def self.call | ||
GovukSliCollector::PublishingLatencySli.new.call | ||
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,64 @@ | ||
require "prometheus/client" | ||
require "prometheus/client/push" | ||
|
||
require "govuk_sli_collector/publishing_latency_sli/content_store_events" | ||
require "govuk_sli_collector/publishing_latency_sli/logit_search" | ||
require "govuk_sli_collector/publishing_latency_sli/record_metrics" | ||
require "govuk_sli_collector/publishing_latency_sli/whitehall_events" | ||
|
||
module GovukSliCollector | ||
class PublishingLatencySli | ||
def initialize | ||
@logit_search = LogitSearch.new( | ||
host: ENV.fetch("LOGIT_OPENSEARCH_HOST"), | ||
basic_auth: ENV.fetch("LOGIT_OPENSEARCH_BASIC_AUTH"), | ||
) | ||
@prometheus_pushgateway_url = ENV.fetch("PROMETHEUS_PUSHGATEWAY_URL") | ||
@to_time = minutes_ago(Integer(ENV.fetch("OFFSET_MINUTES"))) | ||
@from_time = @to_time - minutes(Integer(ENV.fetch("INTERVAL_MINUTES"))) | ||
end | ||
|
||
def call | ||
whitehall_events = WhitehallEvents.new(logit_search:).call( | ||
from_time:, | ||
to_time:, | ||
) | ||
|
||
return if whitehall_events.empty? | ||
|
||
content_store_events = ContentStoreEvents.new(logit_search:).call( | ||
from_time:, | ||
matching: whitehall_events, | ||
) | ||
|
||
return if content_store_events.empty? | ||
|
||
prometheus_registry = Prometheus::Client.registry | ||
|
||
RecordMetrics.new(prometheus_registry:).call( | ||
whitehall_events:, | ||
content_store_events:, | ||
) | ||
|
||
Prometheus::Client::Push.new( | ||
job: "govuk_sli_collector_publishing_latency_sli", | ||
gateway: prometheus_pushgateway_url, | ||
).add(prometheus_registry) | ||
end | ||
|
||
private | ||
|
||
attr_reader :logit_search, | ||
:prometheus_pushgateway_url, | ||
:from_time, | ||
:to_time | ||
|
||
def minutes(number_of) | ||
number_of * 60 | ||
end | ||
|
||
def minutes_ago(number_of) | ||
Time.now.utc - minutes(number_of) | ||
end | ||
end | ||
end |
33 changes: 33 additions & 0 deletions
33
lib/govuk_sli_collector/publishing_latency_sli/content_store_events.rb
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,33 @@ | ||
require "govuk_sli_collector/publishing_latency_sli/log_event" | ||
|
||
module GovukSliCollector | ||
class PublishingLatencySli | ||
class ContentStoreEvents | ||
def initialize(logit_search:) | ||
@logit_search = logit_search | ||
end | ||
|
||
def call(matching:, from_time:) | ||
govuk_request_ids = matching.map(&:govuk_request_id) | ||
|
||
log_event_hashes = logit_search.call( | ||
app_name: "content-store-mongo-main", | ||
govuk_request_ids:, | ||
route: "content_items#update", | ||
from_time:, | ||
) | ||
|
||
log_event_hashes.map do |event_data| | ||
LogEvent.new( | ||
govuk_request_id: event_data["govuk_request_id"].first, | ||
time: Time.new(event_data["@timestamp"].first).floor, | ||
) | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :logit_search | ||
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,5 @@ | ||
module GovukSliCollector | ||
class PublishingLatencySli | ||
LogEvent = Struct.new(:govuk_request_id, :time) | ||
end | ||
end |
67 changes: 67 additions & 0 deletions
67
lib/govuk_sli_collector/publishing_latency_sli/logit_search.rb
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,67 @@ | ||
require "json" | ||
require "rest-client" | ||
|
||
module GovukSliCollector | ||
class PublishingLatencySli | ||
class LogitSearch | ||
class Error < StandardError; end | ||
|
||
def initialize(host:, basic_auth:) | ||
@url = URI.join(host, "_search").to_s | ||
@basic_auth = basic_auth | ||
end | ||
|
||
def call(app_name:, route:, from_time:, to_time: nil, govuk_request_ids: []) | ||
response = RestClient::Request.execute( | ||
method: :get, | ||
url:, | ||
headers: { | ||
"Content-Type": "application/json", | ||
"Authorization": "Basic #{basic_auth}", | ||
}, | ||
payload: { | ||
query: { | ||
bool: { | ||
filter: [ | ||
{ term: { "kubernetes.deployment.name": app_name } }, | ||
{ term: { "kubernetes.container.name": "app" } }, | ||
( | ||
if route.is_a?(Array) | ||
{ terms: { route: } } | ||
else | ||
{ term: { route: } } | ||
end | ||
), | ||
{ | ||
range: { | ||
"@timestamp": { | ||
gte: from_time.iso8601, | ||
lt: (to_time.iso8601 unless to_time.nil?), | ||
}.compact, | ||
}, | ||
}, | ||
( | ||
unless govuk_request_ids.empty? | ||
{ terms: { govuk_request_id: govuk_request_ids } } | ||
end | ||
), | ||
].compact, | ||
}, | ||
}, | ||
fields: ["duration", "govuk_request_id", "@timestamp"], | ||
_source: false, | ||
}.to_json, | ||
) | ||
|
||
payload = JSON.parse(response.body) | ||
payload["hits"]["hits"].map { |event_data| event_data["fields"] } | ||
rescue StandardError => e | ||
raise Error, e.inspect | ||
end | ||
|
||
private | ||
|
||
attr_reader :url, :basic_auth | ||
end | ||
end | ||
end |
43 changes: 43 additions & 0 deletions
43
lib/govuk_sli_collector/publishing_latency_sli/record_metrics.rb
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,43 @@ | ||
module GovukSliCollector | ||
class PublishingLatencySli | ||
class RecordMetrics | ||
def initialize(prometheus_registry:) | ||
@prometheus_registry = prometheus_registry | ||
end | ||
|
||
def call(whitehall_events:, content_store_events:) | ||
return [] if whitehall_events.empty? || content_store_events.empty? | ||
|
||
first_content = prometheus_registry.histogram( | ||
:publishing_latency_first_content_s, | ||
docstring: "Publishing latency for a single content item, in seconds", | ||
) | ||
all_content = prometheus_registry.histogram( | ||
:publishing_latency_all_content_s, | ||
docstring: "Publishing latency for all affected content items, in seconds", | ||
) | ||
|
||
content_store_events_by_id = content_store_events.group_by(&:govuk_request_id) | ||
|
||
whitehall_events.each do |whitehall_event| | ||
matching_events = content_store_events_by_id[whitehall_event.govuk_request_id] | ||
|
||
next if matching_events.nil? || matching_events.empty? | ||
|
||
first_content_store_time, last_content_store_time = matching_events.map(&:time).minmax | ||
|
||
(first_content_store_time - whitehall_event.time).tap do |latency| | ||
first_content.observe(latency) unless latency.negative? | ||
end | ||
(last_content_store_time - whitehall_event.time).tap do |latency| | ||
all_content.observe(latency) unless latency.negative? | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :prometheus_registry | ||
end | ||
end | ||
end |
Oops, something went wrong.