mirror of
https://github.com/opf/openproject.git
synced 2026-06-13 19:20:00 +00:00
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.
This commit is contained in:
@@ -31,12 +31,17 @@
|
||||
Rails.application.configure do
|
||||
config.after_initialize do
|
||||
ActiveSupport::Notifications.subscribe("openproject_grape_logger") do |_, _, _, _, payload|
|
||||
time = payload[:time]
|
||||
# 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 = {
|
||||
duration: time[:total],
|
||||
db: time[:db],
|
||||
view: time[:view]
|
||||
}.merge(payload.except(:time))
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user