Commit 626ef5d0 authored by Pavel Shutsin's avatar Pavel Shutsin

Add graceful timeout handling for analytics

Analytics endpoint should return graceful error message
when it fails with query timeout
parent 62c2cb5e
# frozen_string_literal: true
module GracefulTimeoutHandling
extend ActiveSupport::Concern
included do
rescue_from ActiveRecord::QueryCanceled do |exception|
raise exception unless request.format.json?
log_exception(exception)
render json: { error: _('There is too much data to calculate. Please change your selection.') }
end
end
end
......@@ -4,6 +4,7 @@ module Projects
module CycleAnalytics
class EventsController < Projects::ApplicationController
include CycleAnalyticsParams
include GracefulTimeoutHandling
before_action :authorize_read_cycle_analytics!
before_action :authorize_read_build!, only: [:test, :staging]
......
......@@ -5,6 +5,7 @@ class Projects::CycleAnalyticsController < Projects::ApplicationController
include ActionView::Helpers::TextHelper
include CycleAnalyticsParams
include Analytics::UniqueVisitsHelper
include GracefulTimeoutHandling
before_action :whitelist_query_limiting, only: [:show]
before_action :authorize_read_cycle_analytics!
......
---
title: Add graceful timeout handling for analytics
merge_request: 36811
author:
type: fixed
......@@ -3,6 +3,7 @@
module Analytics
class ApplicationController < ::ApplicationController
include RoutableActions
include GracefulTimeoutHandling
layout 'analytics'
......
......@@ -28,4 +28,6 @@ RSpec.describe Analytics::AnalyticsController do
end
end
end
include_examples GracefulTimeoutHandling
end
......@@ -23684,6 +23684,9 @@ msgstr ""
msgid "There is no data available. Please change your selection."
msgstr ""
msgid "There is too much data to calculate. Please change your selection."
msgstr ""
msgid "There was a problem communicating with your device."
msgstr ""
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe GracefulTimeoutHandling, type: :controller do
controller(ApplicationController) do
include GracefulTimeoutHandling
skip_before_action :authenticate_user!
def index
raise ActiveRecord::QueryCanceled.new
end
end
context 'for json request' do
subject { get :index, format: :json }
it 'renders graceful error message' do
subject
expect(json_response['error']).to eq(_('There is too much data to calculate. Please change your selection.'))
expect(response.code).to eq '200'
end
it 'logs exception' do
expect(Gitlab::ErrorTracking).to receive(:track_exception).with(kind_of(ActiveRecord::QueryCanceled))
subject
end
end
context 'for html request' do
subject { get :index, format: :html }
it 'has no effect' do
expect do
subject
end.to raise_error(ActiveRecord::QueryCanceled)
end
end
end
......@@ -57,6 +57,8 @@ RSpec.describe Projects::CycleAnalytics::EventsController do
end
end
include_examples GracefulTimeoutHandling
def get_issue(additional_params: {})
params = additional_params.merge(namespace_id: project.namespace, project_id: project)
get(:issue, params: params, format: :json)
......
......@@ -67,4 +67,6 @@ RSpec.describe Projects::CycleAnalyticsController do
end
end
end
include_examples GracefulTimeoutHandling
end
# frozen_string_literal: true
RSpec.shared_examples GracefulTimeoutHandling do
it 'includes GracefulTimeoutHandling' do
expect(controller).to be_a(GracefulTimeoutHandling)
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment