Skip to content

Commit

Permalink
Add document_type and schema_name to prom labels
Browse files Browse the repository at this point in the history
This results in metrics like:

    http_requests_total{action="show",controller="content_items",document_type="answer",schema_name="answer",status="200"} 1

This will allow us to investigate the performance of different content
types, for example how long does it take to load a manual vs. an answer
or a case_study. Currently we can't distinguish these in the metrics for
government-frontend, because it uses the same controller action for all
content types.

If there are some content types which take a much longer time to load,
maybe there are performance issues we should address.

This follows on from alphagov/govuk_app_config#424
which adds the functionality to support setting prometheus labels in this way.
  • Loading branch information
richardTowers committed Jan 15, 2025
1 parent 997e932 commit d23fcf0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/controllers/content_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def show
load_content_item

set_expiry
set_prometheus_labels

if is_service_manual?
show_service_manual_page
Expand Down Expand Up @@ -234,6 +235,14 @@ def set_expiry
)
end

def set_prometheus_labels
prometheus_labels = request.env.fetch("govuk.prometheus_labels", {})
request.env["govuk.prometheus_labels"] = prometheus_labels.merge(
document_type: @content_item.document_type,
schema_name: @content_item.schema_name,
)
end

def service_url(original_url)
ga_param = params[:_ga]
return original_url if ga_param.nil?
Expand Down
8 changes: 8 additions & 0 deletions test/controllers/content_items_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ class ContentItemsControllerTest < ActionController::TestCase
assert_equal content_item["title"], assigns[:content_item].title
end

test "sets prometheus labels on the rack env" do
content_item = content_store_has_schema_example("case_study", "case_study")

get :show, params: { path: path_for(content_item) }
assert_response :success
assert_equal @request.env["govuk.prometheus_labels"], { document_type: "case_study", schema_name: "case_study" }
end

test "gets item from content store and keeps existing ordered_related_items when links already exist" do
content_item = content_store_has_schema_example("guide", "guide")

Expand Down

0 comments on commit d23fcf0

Please sign in to comment.