Commit 6539978d authored by Bob Van Landuyt's avatar Bob Van Landuyt

Add request method to the endpoint_id for Grape

This adds the request method to the endpoint_id and caller_id
metadata for Grape requests. This allows us to differentiate routes if
they have the same path but different methods.
parent 172b3826
...@@ -59,7 +59,7 @@ module API ...@@ -59,7 +59,7 @@ module API
project: -> { @project }, project: -> { @project },
namespace: -> { @group }, namespace: -> { @group },
runner: -> { @current_runner || @runner }, runner: -> { @current_runner || @runner },
caller_id: route.origin, caller_id: api_endpoint.endpoint_id,
remote_ip: request.ip, remote_ip: request.ip,
feature_category: feature_category feature_category: feature_category
) )
......
...@@ -32,6 +32,10 @@ module API ...@@ -32,6 +32,10 @@ module API
end end
end.compact.to_set end.compact.to_set
end end
def endpoint_id
"#{request.request_method} #{route.origin}"
end
end end
end end
end end
......
...@@ -15,7 +15,7 @@ module API ...@@ -15,7 +15,7 @@ module API
Gitlab::ApplicationContext.push( Gitlab::ApplicationContext.push(
user: -> { actor&.user }, user: -> { actor&.user },
project: -> { project }, project: -> { project },
caller_id: route.origin, caller_id: api_endpoint.endpoint_id,
remote_ip: request.ip, remote_ip: request.ip,
feature_category: feature_category feature_category: feature_category
) )
......
...@@ -107,7 +107,7 @@ RSpec.describe API::API do ...@@ -107,7 +107,7 @@ RSpec.describe API::API do
allow_any_instance_of(Gitlab::GrapeLogging::Loggers::ContextLogger).to receive(:parameters) do allow_any_instance_of(Gitlab::GrapeLogging::Loggers::ContextLogger).to receive(:parameters) do
Gitlab::ApplicationContext.current.tap do |log_context| Gitlab::ApplicationContext.current.tap do |log_context|
expect(log_context).to match('correlation_id' => an_instance_of(String), expect(log_context).to match('correlation_id' => an_instance_of(String),
'meta.caller_id' => '/api/:version/projects/:id/issues', 'meta.caller_id' => 'GET /api/:version/projects/:id/issues',
'meta.remote_ip' => an_instance_of(String), 'meta.remote_ip' => an_instance_of(String),
'meta.project' => project.full_path, 'meta.project' => project.full_path,
'meta.root_namespace' => project.namespace.full_path, 'meta.root_namespace' => project.namespace.full_path,
...@@ -124,7 +124,7 @@ RSpec.describe API::API do ...@@ -124,7 +124,7 @@ RSpec.describe API::API do
allow_any_instance_of(Gitlab::GrapeLogging::Loggers::ContextLogger).to receive(:parameters) do allow_any_instance_of(Gitlab::GrapeLogging::Loggers::ContextLogger).to receive(:parameters) do
Gitlab::ApplicationContext.current.tap do |log_context| Gitlab::ApplicationContext.current.tap do |log_context|
expect(log_context).to match('correlation_id' => an_instance_of(String), expect(log_context).to match('correlation_id' => an_instance_of(String),
'meta.caller_id' => '/api/:version/users', 'meta.caller_id' => 'GET /api/:version/users',
'meta.remote_ip' => an_instance_of(String), 'meta.remote_ip' => an_instance_of(String),
'meta.client_id' => an_instance_of(String), 'meta.client_id' => an_instance_of(String),
'meta.feature_category' => 'users') 'meta.feature_category' => 'users')
...@@ -141,7 +141,7 @@ RSpec.describe API::API do ...@@ -141,7 +141,7 @@ RSpec.describe API::API do
let(:component_map) do let(:component_map) do
{ {
"application" => "test", "application" => "test",
"endpoint_id" => "/api/:version/users/:id" "endpoint_id" => "GET /api/:version/users/:id"
} }
end end
......
...@@ -127,7 +127,7 @@ RSpec.describe API::Ci::Runner, :clean_gitlab_redis_shared_state do ...@@ -127,7 +127,7 @@ RSpec.describe API::Ci::Runner, :clean_gitlab_redis_shared_state do
authorize_artifacts_with_token_in_params authorize_artifacts_with_token_in_params
end end
it_behaves_like 'API::CI::Runner application context metadata', '/api/:version/jobs/:id/artifacts/authorize' do it_behaves_like 'API::CI::Runner application context metadata', 'POST /api/:version/jobs/:id/artifacts/authorize' do
let(:send_request) { subject } let(:send_request) { subject }
end end
...@@ -262,7 +262,7 @@ RSpec.describe API::Ci::Runner, :clean_gitlab_redis_shared_state do ...@@ -262,7 +262,7 @@ RSpec.describe API::Ci::Runner, :clean_gitlab_redis_shared_state do
end end
describe 'POST /api/v4/jobs/:id/artifacts' do describe 'POST /api/v4/jobs/:id/artifacts' do
it_behaves_like 'API::CI::Runner application context metadata', '/api/:version/jobs/:id/artifacts' do it_behaves_like 'API::CI::Runner application context metadata', 'POST /api/:version/jobs/:id/artifacts' do
let(:send_request) do let(:send_request) do
upload_artifacts(file_upload, headers_with_token) upload_artifacts(file_upload, headers_with_token)
end end
...@@ -784,7 +784,7 @@ RSpec.describe API::Ci::Runner, :clean_gitlab_redis_shared_state do ...@@ -784,7 +784,7 @@ RSpec.describe API::Ci::Runner, :clean_gitlab_redis_shared_state do
describe 'GET /api/v4/jobs/:id/artifacts' do describe 'GET /api/v4/jobs/:id/artifacts' do
let(:token) { job.token } let(:token) { job.token }
it_behaves_like 'API::CI::Runner application context metadata', '/api/:version/jobs/:id/artifacts' do it_behaves_like 'API::CI::Runner application context metadata', 'GET /api/:version/jobs/:id/artifacts' do
let(:send_request) { download_artifact } let(:send_request) { download_artifact }
end end
......
...@@ -36,7 +36,7 @@ RSpec.describe API::Ci::Runner, :clean_gitlab_redis_shared_state do ...@@ -36,7 +36,7 @@ RSpec.describe API::Ci::Runner, :clean_gitlab_redis_shared_state do
job.run! job.run!
end end
it_behaves_like 'API::CI::Runner application context metadata', '/api/:version/jobs/:id' do it_behaves_like 'API::CI::Runner application context metadata', 'PUT /api/:version/jobs/:id' do
let(:send_request) { update_job(state: 'success') } let(:send_request) { update_job(state: 'success') }
end end
......
...@@ -41,7 +41,7 @@ RSpec.describe API::Ci::Runner, :clean_gitlab_redis_shared_state do ...@@ -41,7 +41,7 @@ RSpec.describe API::Ci::Runner, :clean_gitlab_redis_shared_state do
initial_patch_the_trace initial_patch_the_trace
end end
it_behaves_like 'API::CI::Runner application context metadata', '/api/:version/jobs/:id/trace' do it_behaves_like 'API::CI::Runner application context metadata', 'PATCH /api/:version/jobs/:id/trace' do
let(:send_request) { patch_the_trace } let(:send_request) { patch_the_trace }
end end
......
...@@ -644,7 +644,7 @@ RSpec.describe API::Internal::Base do ...@@ -644,7 +644,7 @@ RSpec.describe API::Internal::Base do
context 'with Project' do context 'with Project' do
it_behaves_like 'storing arguments in the application context' do it_behaves_like 'storing arguments in the application context' do
let(:expected_params) { { user: key.user.username, project: project.full_path } } let(:expected_params) { { user: key.user.username, project: project.full_path, caller_id: "POST /api/:version/internal/allowed" } }
subject { push(key, project) } subject { push(key, project) }
end end
...@@ -652,7 +652,7 @@ RSpec.describe API::Internal::Base do ...@@ -652,7 +652,7 @@ RSpec.describe API::Internal::Base do
context 'with PersonalSnippet' do context 'with PersonalSnippet' do
it_behaves_like 'storing arguments in the application context' do it_behaves_like 'storing arguments in the application context' do
let(:expected_params) { { user: key.user.username } } let(:expected_params) { { user: key.user.username, caller_id: "POST /api/:version/internal/allowed" } }
subject { push(key, personal_snippet) } subject { push(key, personal_snippet) }
end end
...@@ -660,7 +660,7 @@ RSpec.describe API::Internal::Base do ...@@ -660,7 +660,7 @@ RSpec.describe API::Internal::Base do
context 'with ProjectSnippet' do context 'with ProjectSnippet' do
it_behaves_like 'storing arguments in the application context' do it_behaves_like 'storing arguments in the application context' do
let(:expected_params) { { user: key.user.username, project: project_snippet.project.full_path } } let(:expected_params) { { user: key.user.username, project: project_snippet.project.full_path, caller_id: "POST /api/:version/internal/allowed" } }
subject { push(key, project_snippet) } subject { push(key, project_snippet) }
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