From d23fcf08060fdf7efeb3ff81d7babcde87410072 Mon Sep 17 00:00:00 2001 From: Richard Towers Date: Wed, 15 Jan 2025 16:36:57 +0000 Subject: [PATCH] Add document_type and schema_name to prom labels 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 https://github.com/alphagov/govuk_app_config/pull/424 which adds the functionality to support setting prometheus labels in this way. --- app/controllers/content_items_controller.rb | 9 +++++++++ test/controllers/content_items_controller_test.rb | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/app/controllers/content_items_controller.rb b/app/controllers/content_items_controller.rb index a10a5e9dd..28092357a 100644 --- a/app/controllers/content_items_controller.rb +++ b/app/controllers/content_items_controller.rb @@ -25,6 +25,7 @@ def show load_content_item set_expiry + set_prometheus_labels if is_service_manual? show_service_manual_page @@ -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? diff --git a/test/controllers/content_items_controller_test.rb b/test/controllers/content_items_controller_test.rb index 692c1a1ff..d7be54947 100644 --- a/test/controllers/content_items_controller_test.rb +++ b/test/controllers/content_items_controller_test.rb @@ -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")