diff options
author | Stan Hu <stanhu@gmail.com> | 2018-09-05 23:46:22 +0300 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2018-09-05 23:46:22 +0300 |
commit | 9dd34eac14a94e58999c335a175e06067f1092fa (patch) | |
tree | bcf98684fb3b2091c1cbaeb1552ff3b04637d9de /lib | |
parent | c903fcd56441c4c3e8e487f1fb20bb4fe60bf1a3 (diff) | |
parent | 189b063ee15d3751fb7b03a0794edfdb60893eb8 (diff) |
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
Diffstat (limited to 'lib')
-rw-r--r-- | lib/api/api.rb | 1 | ||||
-rw-r--r-- | lib/gitlab/grape_logging/loggers/route_logger.rb | 25 |
2 files changed, 26 insertions, 0 deletions
diff --git a/lib/api/api.rb b/lib/api/api.rb index 850cef26449..843f75d3096 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -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 diff --git a/lib/gitlab/grape_logging/loggers/route_logger.rb b/lib/gitlab/grape_logging/loggers/route_logger.rb new file mode 100644 index 00000000000..f3146b4dfd9 --- /dev/null +++ b/lib/gitlab/grape_logging/loggers/route_logger.rb @@ -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 |