From e79fd6f1f48c079f602997bff31fc4de3f5f6541 Mon Sep 17 00:00:00 2001 From: dakotadux Date: Fri, 11 Jul 2025 19:34:52 -0600 Subject: [PATCH] Update ObservabilityController to use presenter This updates the ObservabilityController to use the presenter to generate the JWT tokens for the Observability service and pass them to the view. --- .../groups/observability_controller.rb | 23 +++-------------- app/views/groups/observability/show.html.haml | 8 ++++-- locale/gitlab.pot | 3 +++ .../groups/observability_controller_spec.rb | 25 ++++++++++++++----- 4 files changed, 32 insertions(+), 27 deletions(-) diff --git a/app/controllers/groups/observability_controller.rb b/app/controllers/groups/observability_controller.rb index 1253e86d386367..5de4d563ca6a2f 100644 --- a/app/controllers/groups/observability_controller.rb +++ b/app/controllers/groups/observability_controller.rb @@ -17,28 +17,13 @@ class ObservabilityController < Groups::ApplicationController p.frame_src(*frame_src_values) end - VALID_PATHS = %w[ - services - traces-explorer - logs/logs-explorer - metrics-explorer/summary - infrastructure-monitoring/hosts - dashboard - messaging-queues - api-monitoring/explorer - alerts - exceptions - service-map - settings - ].freeze + VALID_PATHS = Observability::ObservabilityPresenter::PATHS.keys.freeze def show - @o11y_url = group.observability_group_o11y_setting&.o11y_service_url - - @path = permitted_params[:id] - - return render_404 unless VALID_PATHS.include?(@path) + path = permitted_params[:id] + return render_404 unless VALID_PATHS.include?(path) + @data = Observability::ObservabilityPresenter.new(group, path) render end diff --git a/app/views/groups/observability/show.html.haml b/app/views/groups/observability/show.html.haml index 7be6e7400584ed..793e9a4aafead3 100644 --- a/app/views/groups/observability/show.html.haml +++ b/app/views/groups/observability/show.html.haml @@ -3,6 +3,10 @@ .observability-container.gl-flex.gl-flex-col .gl-display-flex.gl-items-center.gl-mt-3.gl-mb-5 - %h1.gl-heading-1= _("Observability") + %h1.gl-heading-1= @data.title - #js-observability{ data: { o11y_url: @o11y_url, path: @path } } + - if @data&.auth_tokens.blank? + = render Pajamas::AlertComponent.new(dismissible: false, variant: :danger, title: _('Unable to authenticate with the observability service. Please check your configuration and try again.')) + + - else + #js-observability{ data: @data.to_h } diff --git a/locale/gitlab.pot b/locale/gitlab.pot index 575f6e0b6da082..9bfef7c0463194 100644 --- a/locale/gitlab.pot +++ b/locale/gitlab.pot @@ -66468,6 +66468,9 @@ msgstr "" msgid "Unable to apply suggestions to a deleted line." msgstr "" +msgid "Unable to authenticate with the observability service. Please check your configuration and try again." +msgstr "" + msgid "Unable to build Slack link." msgstr "" diff --git a/spec/requests/groups/observability_controller_spec.rb b/spec/requests/groups/observability_controller_spec.rb index 86667126269cbc..e3794f01370700 100644 --- a/spec/requests/groups/observability_controller_spec.rb +++ b/spec/requests/groups/observability_controller_spec.rb @@ -27,9 +27,10 @@ context 'when feature flag is enabled' do before do stub_feature_flags(observability_sass_features: group) + allow(Observability::O11yToken).to receive(:generate_tokens).and_return({ 'testToken' => 'value' }) end - context 'with incorrect permissons' do + context 'with incorrect permissions' do let(:user) { create(:user) } before do @@ -52,8 +53,9 @@ it 'sets the o11y url from group settings' do services_page expect(response).to render_template(:show) - expect(assigns(:path)).to eq('services') - expect(assigns(:o11y_url)).to eq('https://observability.example.com') + expect(assigns(:data)).to be_a(Observability::ObservabilityPresenter) + expect(assigns(:data).to_h[:path]).to eq('services') + expect(assigns(:data).to_h[:o11y_url]).to eq('https://observability.example.com') end end @@ -63,12 +65,17 @@ it 'sets the o11y url to nil' do services_page expect(response).to render_template(:show) - expect(assigns(:path)).to eq('services') - expect(assigns(:o11y_url)).to be_nil + expect(assigns(:data)).to be_a(Observability::ObservabilityPresenter) + expect(assigns(:data).to_h[:path]).to eq('services') + expect(assigns(:data).to_h[:o11y_url]).to be_nil end end context 'with a valid path parameter' do + let!(:observability_setting) do + create(:observability_group_o11y_setting, group: group, o11y_service_url: 'https://o11y.gitlab.com') + end + Groups::ObservabilityController::VALID_PATHS.each do |path| context "with path=#{path}" do subject(:observability_page) { get group_observability_path(group, path) } @@ -77,7 +84,12 @@ observability_page expect(response).to have_gitlab_http_status(:ok) - expect(assigns(:path)).to eq(path) + expect(assigns(:data)).to be_a(Observability::ObservabilityPresenter) + expect(assigns(:data).to_h[:path]).to eq(path) + expect(assigns(:data).to_h[:o11y_url]).to eq('https://o11y.gitlab.com') + expect(assigns(:data).title).to eq(Observability::ObservabilityPresenter::PATHS.fetch(path, + 'Observability')) + expect(assigns(:data).auth_tokens).to eq({ 'test_token' => 'value' }) end end end @@ -154,6 +166,7 @@ group_with_different_o11y.add_maintainer(user) stub_feature_flags(observability_sass_features: group_with_different_o11y) create(:observability_group_o11y_setting, group: group_with_different_o11y, o11y_service_url: o11y_url_2) + allow(Observability::O11yToken).to receive(:generate_tokens).and_return({ 'testToken' => 'value' }) get group_observability_path(group_with_different_o11y, 'services') end -- GitLab