Commit 5949f62a authored by Sean McGivern's avatar Sean McGivern Committed by Bob Van Landuyt

Expose API feature categories in logs and metrics

parent 9f252f39
......@@ -48,11 +48,17 @@ module API
before do
coerce_nil_params_to_array!
api_endpoint = env['api.endpoint']
feature_category = api_endpoint.options[:for].try(:feature_category_for_app, api_endpoint).to_s
header[Gitlab::Metrics::RequestsRackMiddleware::FEATURE_CATEGORY_HEADER] = feature_category
Gitlab::ApplicationContext.push(
user: -> { @current_user },
project: -> { @project },
namespace: -> { @group },
caller_id: route.origin
caller_id: route.origin,
feature_category: feature_category
)
end
......
......@@ -7,14 +7,16 @@ module API
before { authenticate_by_gitlab_shell_token! }
before do
route_path = route.origin
route_class = route.app.options[:for]
api_endpoint = env['api.endpoint']
feature_category = api_endpoint.options[:for].try(:feature_category_for_app, api_endpoint).to_s
header[Gitlab::Metrics::RequestsRackMiddleware::FEATURE_CATEGORY_HEADER] = feature_category
Gitlab::ApplicationContext.push(
user: -> { actor&.user },
project: -> { project },
caller_id: route_path,
feature_category: route_class.try(:feature_category_for_action, route_path).to_s
caller_id: route.origin,
feature_category: feature_category
)
end
......
......@@ -92,4 +92,36 @@ RSpec.describe API::API do
end
end
end
context 'application context' do
let_it_be(:project) { create(:project) }
let_it_be(:user) { project.owner }
it 'logs all application context fields' do
allow_any_instance_of(Gitlab::GrapeLogging::Loggers::ContextLogger).to receive(:parameters) do
Labkit::Context.current.to_h.tap do |log_context|
expect(log_context).to match('correlation_id' => an_instance_of(String),
'meta.caller_id' => '/api/:version/projects/:id/issues',
'meta.project' => project.full_path,
'meta.root_namespace' => project.namespace.full_path,
'meta.user' => user.username,
'meta.feature_category' => 'issue_tracking')
end
end
get(api("/projects/#{project.id}/issues", user))
end
it 'skips fields that do not apply' do
allow_any_instance_of(Gitlab::GrapeLogging::Loggers::ContextLogger).to receive(:parameters) do
Labkit::Context.current.to_h.tap do |log_context|
expect(log_context).to match('correlation_id' => an_instance_of(String),
'meta.caller_id' => '/api/:version/users',
'meta.feature_category' => 'users')
end
end
get(api('/users'))
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