Commit cf941ceb authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Support API requests in Performance Bar

This adds Peek support for Grape API endpoints
parent bdc9ae90
---
title: Support adding of API requests to the performance bar
merge_request: 39057
author:
type: added
......@@ -14,3 +14,9 @@ Peek.into Peek::Views::Rugged
Peek.into Peek::Views::BulletDetailed if defined?(Bullet)
Peek.into Peek::Views::Tracing if Labkit::Tracing.tracing_url_enabled?
ActiveSupport::Notifications.subscribe('endpoint_run.grape') do |_name, _start, _finish, _id, payload|
if request_id = payload[:env]['action_dispatch.request_id']
Peek.adapter.save(request_id)
end
end
......@@ -56,6 +56,10 @@ module API
)
end
before do
set_peek_enabled_for_current_request
end
# The locale is set to the current user's locale when `current_user` is loaded
after { Gitlab::I18n.use_default_locale }
......@@ -116,6 +120,7 @@ module API
# Ensure the namespace is right, otherwise we might load Grape::API::Helpers
helpers ::API::Helpers
helpers ::API::Helpers::CommonHelpers
helpers ::API::Helpers::PerformanceBarHelpers
namespace do
after do
......
......@@ -7,6 +7,7 @@ require 'rack/oauth2'
module API
module APIGuard
extend ActiveSupport::Concern
include Gitlab::Utils::StrongMemoize
included do |base|
# OAuth2 Resource Server Authentication
......@@ -64,10 +65,12 @@ module API
end
def find_user_from_sources
deploy_token_from_request ||
find_user_from_bearer_token ||
find_user_from_job_token ||
find_user_from_warden
strong_memoize(:find_user_from_sources) do
deploy_token_from_request ||
find_user_from_bearer_token ||
find_user_from_job_token ||
find_user_from_warden
end
end
private
......
# frozen_string_literal: true
module API
module Helpers
module PerformanceBarHelpers
def set_peek_enabled_for_current_request
Gitlab::SafeRequestStore.fetch(:peek_enabled) { perf_bar_cookie_enabled? && perf_bar_enabled_for_user? }
end
def perf_bar_cookie_enabled?
cookies[:perf_bar_enabled] == 'true'
end
def perf_bar_enabled_for_user?
# We cannot use `current_user` here because that method raises an exception when the user
# is unauthorized and some API endpoints require that `current_user` is not called.
Gitlab::PerformanceBar.enabled_for_user?(find_user_from_sources)
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Performance Bar for API requests', :request_store, :clean_gitlab_redis_cache do
context 'with user that has access to the performance bar' do
let_it_be(:admin) { create(:admin) }
context 'when cookie is set to true' do
before do
cookies[:perf_bar_enabled] = 'true'
end
it 'stores performance data' do
get api("/users/#{admin.id}", admin)
expect(Peek.adapter.get(headers['X-Request-Id'])).not_to be_empty
end
end
context 'when cookie is missing' do
it 'does not store performance data' do
get api("/users/#{admin.id}", admin)
expect(Peek.adapter.get(headers['X-Request-Id'])).to be_nil
end
end
end
context 'with user that does not have access to the performance bar' do
let(:user) { create(:user) }
it 'does not store performance data' do
cookies[:perf_bar_enabled] = 'true'
get api("/users/#{user.id}", user)
expect(Peek.adapter.get(headers['X-Request-Id'])).to be_nil
end
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