Files
Christophe Bliard 88f9772b23 Make grape logging grokable by our logging instrumentation
Our grok pattern is something like this:
....%{SPACE}\[%{DATA:namespace}\]%{SPACE}method=%{WORD:http_method}...

The parsing works for logs generated by lograge which look like this:

```
I, [2025-09-22T13:31:21.045328 #47]  INFO -- : [51a28da6-d112-45d9-b5fd-ba2f2c86fbba] [trace_id=2f2ce94953e0b1188c47eabfdd4d5b08] [span_id=1aa8769d2ed98fc6] [public] method=GET path=/health_checks/default format=*/* controller=OkComputer::OkComputerController action=show status=200 allocations=860 duration=2.47 view=0.15 db=0.00 user=3 domain= namespace=public shard=
```

But fails to parse for logs generated by grape loggin, which look like this:
```
I, [2025-09-22T12:15:22.139783 #55]  INFO -- : [6878233d-b216-4f6e-a039-6f67f2c99684] [trace_id=af9a3f2a8d129f5603522c9a9e0e7599] [span_id=1f43864c812ab072] [instance_1450094160_7477212_4176] duration=8384.68 db=1008.17 view=7376.51 status=200 method=GET path=/api/v3/queries/3687 params={\"pageSize\" => \"100\"} host=qa.openproject-edge.com request_id=6878233d-b216-4f6e-a039-6f67f2c99684 user=344 domain=qa.openproject-edge.com namespace=instance_1450094160_7477212_4176 shard=
```

The parsing fails because `duration` is the field after `namespace`, and
our pattern expects `method` instead.

This commit reorders the fields in the grape logging logs so that
`method` and `path` appear first, as in lograge logs for controller
actions.
2025-09-22 18:00:01 +02:00

51 lines
2.0 KiB
Ruby

# frozen_string_literal: true
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++
Rails.application.configure do
config.after_initialize do
ActiveSupport::Notifications.subscribe("openproject_grape_logger") do |_, _, _, _, payload|
# Have attributes somewhat in the same order as lograge does with
# processed controller action to ease later grok parsing.
# See `Lograge::LogSubscribers::ActionController#initial_data`
attributes = {
method: payload[:method],
path: payload[:path],
duration: payload[:time][:total],
db: payload[:time][:db],
view: payload[:time][:view],
**payload.except(:method, :path, :time)
}
extended = OpenProject::Logging.extend_payload!(attributes, {})
Rails.logger.info OpenProject::Logging.formatter.call(extended)
end
end
end