Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Enable basic request logging for grape

Oliver Güntherf98e50ccf6bf
diff --git a/Gemfile b/Gemfile
index 09129acf665..bd54cf28450 100644
--- a/Gemfile
+++ b/Gemfile
@@ -295,6 +295,7 @@ gem 'bootsnap', '~> 1.7.0', require: false
# API gems
gem 'grape', '~> 1.5.0'
+gem 'grape_logging', '~> 1.8.4'
gem 'roar', '~> 1.1.0'
# CORS for API
diff --git a/Gemfile.lock b/Gemfile.lock
index 7a780615573..b7e49643f2a 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -511,6 +511,9 @@ GEM
mustermann-grape (~> 1.0.0)
rack (>= 1.3.0)
rack-accept
+ grape_logging (1.8.4)
+ grape
+ rack
gravatar_image_tag (1.2.0)
hashdiff (1.0.1)
hashery (2.1.2)
@@ -1003,6 +1006,7 @@ DEPENDENCIES
fuubar (~> 2.5.0)
gon (~> 6.4.0)
grape (~> 1.5.0)
+ grape_logging (~> 1.8.4)
grids!
html-pipeline (~> 2.14.0)
htmldiff
diff --git a/config/initializers/grape_logging.rb b/config/initializers/grape_logging.rb
new file mode 100644
index 00000000000..24ea3fd324c
--- /dev/null
+++ b/config/initializers/grape_logging.rb
@@ -0,0 +1,17 @@
+OpenProject::Application.configure do
+ config.after_initialize do
+ formatter = ::Lograge::Formatters::KeyValue.new
+
+ ActiveSupport::Notifications.subscribe('openproject_grape_logger') do |_, _, _, _, payload|
+ time = payload.delete :time
+ attributes = {
+ duration: time[:total],
+ db: time[:db],
+ view: time[:view]
+ }.merge(payload)
+
+ string = formatter.call(attributes)
+ Rails.logger.info string
+ end
+ end
+end
diff --git a/lib/api/root_api.rb b/lib/api/root_api.rb
index 1d71cf492a6..340500d8f88 100644
--- a/lib/api/root_api.rb
+++ b/lib/api/root_api.rb
@@ -39,6 +39,10 @@ module API
include OpenProject::Authentication::Scope
extend API::Utilities::GrapeHelper
+ insert_before Grape::Middleware::Error,
+ ::GrapeLogging::Middleware::RequestLogger,
+ { instrumentation_key: 'openproject_grape_logger' }
+
content_type :json, 'application/json; charset=utf-8'
use OpenProject::Authentication::Manager