Placeholder variables are not working (newrelic)

Created on 30 April 2024, 7 months ago
Updated 3 May 2024, 7 months ago

After updating to Monolog 3 got same problem with newrelic as in Placeholder variables are not working (new relic) 🐛 Placeholder variables are not working (new relic) Fixed .
Solution to use https://github.com/newrelic/newrelic-monolog-logenricher-php no longer works because it does not support Monolog 3 and is deprecated because Newrelic now has option to capture Monolog logs out of the box.

Newrelic settings:

newrelic.application_logging.enabled=true
newrelic.application_logging.forwarding.enabled=true
newrelic.application_logging.forwarding.context_data.enabled=true
newrelic.application_logging.local_decorating.enabled=false

The newrelic.application_logging.forwarding.context_data.enabled captures $context array and placeholder values appears in the Newrelic logs, but they are not replaced inside log message, also context item has 255 length limit. So if in Drupal there is log message like:
\Drupal::logger('system')->info('Some message with @log_placeholder', ['@long_placeholder' => 'String longer than 255 chars']);
in Newrelic logs it gets recorded as:

{
  context.@long_placeholder:[string sipped to 255 chars],
  level:"INFO",
  message:"Some message with @log_placeholder",
  ...
}

IMHO issue is on Newrerlic side, but as I couldn't find how to fix this on Newrelic side had to find a fix/workaround.

After debugging found out that Newrerlic (PHP agent v10.19.0.9) intercepts log messages when \Monolog\Logger::addRecord() method is called and just gathers passed arguments. All processors, that could alter the message, are executed inside that method after Newrerlic captures it.

At the end came up with a dirty solution to execute monolog.logger.processors on a log message before it is passed to the \Monolog\Logger::addRecord() (patch attached). Custom monolog_newrelic.services.yml that is included into $settings['container_yamls'] have these settings:

parameters:
  monolog.processors:
    - 'current_user'
    - 'request_uri'
    - 'ip'
    - 'referer'
    - 'filter_backtrace'
  monolog.logger.processors:
    - 'message_placeholder'
🐛 Bug report
Status

Active

Version

3.0

Component

Code

Created by

🇱🇹Lithuania k-l

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @k-l
  • 🇬🇧United Kingdom bloke_zero

    Thanks for the patch! I tested it and it works.

    Could we encourage NR to look at the issue? I'm happy to open a support request if you could give me a summary of the issue for them to look at?

Production build 0.71.5 2024