Merge branch 'an/api-route-logger' into 'master'

Add route information to lograge structured logging for API logs

Closes #50993

See merge request gitlab-org/gitlab-ce!21487
This commit is contained in:
Stan Hu 2018-09-05 20:46:22 +00:00
commit 9dd34eac14
3 changed files with 31 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
title: Add route information to lograge structured logging for API logs
merge_request: 21487
author:
type: other

View file

@ -15,6 +15,7 @@ module API
include: [
GrapeLogging::Loggers::FilterParameters.new,
GrapeLogging::Loggers::ClientEnv.new,
Gitlab::GrapeLogging::Loggers::RouteLogger.new,
Gitlab::GrapeLogging::Loggers::UserLogger.new,
Gitlab::GrapeLogging::Loggers::QueueDurationLogger.new,
Gitlab::GrapeLogging::Loggers::PerfLogger.new

View file

@ -0,0 +1,25 @@
# frozen_string_literal: true
# This grape_logging module (https://github.com/aserafin/grape_logging) makes it
# possible to log the details of the action
module Gitlab
module GrapeLogging
module Loggers
class RouteLogger < ::GrapeLogging::Loggers::Base
def parameters(request, _)
endpoint = request.env[Grape::Env::API_ENDPOINT]
route = endpoint&.route&.pattern&.origin
return {} unless route
{ route: route }
rescue
# endpoint.route calls env[Grape::Env::GRAPE_ROUTING_ARGS][:route_info]
# but env[Grape::Env::GRAPE_ROUTING_ARGS] is nil in the case of a 405 response
# so we're rescuing exceptions and bailing out
{}
end
end
end
end
end