dd-trace-rb icon indicating copy to clipboard operation
dd-trace-rb copied to clipboard

Lograge in JSON mode in Rails causes double addition of log correlation information and breaks log parsing

Open chall8908 opened this issue 2 years ago • 19 comments

The current implementation of the Rails integration causes log correlation information to be added twice to the logger when using Lograge. This breaks log parsing in Datadog if Lograge is using the JSON formatter.

I'm currently attempting to work around this by setting Rails.configuration.log_tags = [] in initializers/datadog.rb.

As far as I can tell, this is likely going to be a problem in any Rails project that doesn't override the default logger when using Lograge (which doesn't modify the default logger) and using the JSON formatter.

I'm not sure if it's possible to simply delay modifying Rails.configuration.log_tags until after_initialize and still have everything function correctly. If nothing else, a mention in the README about this potential issue would save other people the head-scratching.

chall8908 avatar May 11 '22 01:05 chall8908

👋 @chall8908, thanks for this issue report. Could you provide the relevant Rails log configuration you use (Lograge configuration, any /config\..*log.*/ related Rails configuration) and the Rails version and Lograge you are running? One possibility is that the problem is a mix of specific versions of Rails, Lograge and ddtrace combined.

marcotc avatar May 19 '22 00:05 marcotc

Hi @marcotc 👋🏻 I'm noticing this as well. With Datadog v1.1.0 (we upgraded recently and directly from v0.54.2) and our Lograge integration, our logs have prepended the Datadog log text before the json format which does include the Datadog tracing info.

[dd.env=production dd.service=my-app dd.version=123 dd.trace_id=123 dd.span_id=123] {"method":"POST","path":"/","format":"html","controller":"SiteController","action":"index","status":200,"duration":90.02,"view":18.85,"db":25.61,"dd":{"trace_id":"123","span_id":"123","env":"production","service":"my-app","version":"123"},"ddsource":"ruby","time":728707.869930716,"params":{},"request_id":"123"}

Before replacing the info in the log shared above, the trace and span ids were a match in both text and json formats.

We are using lograge v0.12.0 (latest), ddtrace v1.1.0 (latest) and rails v6.1.6.

javierjulio avatar Jun 03 '22 16:06 javierjulio

@marcotc we are actually overriding the default logger whereas with what @chall8908 wrote it seems they aren't but that is still resulting in the same output issue.

For Lograge config:

Rails.application.configure do
  config.lograge.enabled = true

  # Set the default log formatter but only if we have Lograge
  # enabled since we are using the Lograge JSON formatter.
  if config.lograge.enabled
    config.log_formatter = Lograge::Formatters::Json.new
    config.lograge.formatter = config.log_formatter
  end

  # Add our own custom data and Lograge will take care of merging this with
  # a base set. Do not merge `event.payload` (only merge explicit values)
  # as that contains a large object from ActionDispatch.
  config.lograge.custom_options = ->(event) do
    {
      time: event.time,
      params: event.payload[:params].except("controller", "action", "format")
    }.merge(event.payload[:custom_payload] || {})
  end

  # A hook to access controller methods so we can log common request info.
  config.lograge.custom_payload do |controller|
    {
      request_id: controller.request.uuid,
      user_id: controller.current_user.try(:id)
    }.compact
  end
end

For Rails logging:

  # Include generic and useful information about system operation, but avoid logging too much
  # information to avoid inadvertent exposure of personally identifiable information (PII).
  config.log_level = ENVied.LOG_LEVEL

  # Prepend all log lines with the following tags.
  # config.log_tags = [ :request_id ]

  # Log disallowed deprecations.
  config.active_support.disallowed_deprecation = :log

  # Tell Active Support which deprecation messages to disallow.
  config.active_support.disallowed_deprecation_warnings = []

  # Use default logging formatter so that PID and timestamp are not suppressed.
  # config.log_formatter = ::Logger::Formatter.new

  # Use a different logger for distributed setups.
  # require "syslog/logger"
  # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name')

  if ENVied.RAILS_LOG_TO_STDOUT
    config.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT))
  end

  # Log CSRF failures
  config.action_controller.log_warning_on_csrf_failure = true

javierjulio avatar Jun 03 '22 17:06 javierjulio

@marcotc sorry for leaving this for so long. We ended up pivoting off Lograge entirely and I got pulled away to other projects.

@javierjulio has the right of it, though. We were using Lograge more-or-less out-of-the-box. That particular project is on Rails 6 when I was testing it. Before deciding to ditch Lograge entirely, I'd pared down our configuration to just enabling Lograge and setting the JSON formatter. No other configuration.

Since Lograge doesn't change the Rails.logger by default, it usually remains as an ActiveSupport::TaggedLogging instance which triggers dd-trace to set log_tags which causes the problem.

I think swapping the add_logger call from before_initialize to after_initialize and actually checking if Lograge is enabled should fix the problem, but I'm not sure if that would cause downstream problems.

chall8908 avatar Jul 07 '22 23:07 chall8908

Hey :wave:

I was getting the same error as @javierjulio in my logs using the same stack. I'm not sure about the side effects but I've got a fix by disabling the log injection before rails initialization. I've added Datadog.configuration.tracing.log_injection = false in my config/environment.rb before Rails.application.initialize!, and the datadog tracing prefix was removed from my JSON logs.

Be advised: I'm sure about the side-effects of this solution :smile:

EDIT: thank you @chall8908 for pointing out a way

eduardohertz avatar Jul 27 '22 15:07 eduardohertz

I can confirm the solution used by @eduardohertz, in our case we put the configuration in the config/environment/*.rb files, which are loaded at the right time.

Before that we were tried to remove the DD tagger manually with #Rails.configuration.log_tags.reject! { _1.source_location.first.match? 'lib/datadog/tracing' } but it didn't look pretty.

elia avatar Aug 09 '22 09:08 elia

@chall8908 since you mentioned Lograge was removed, may I ask what you replaced it with and is that new thing still doing JSON formatting for logs? I'd be open to changing ours but I'm not familiar with another alternative.

@marcotc we haven't been able to upgrade from 0.54.2 due to this bug where the logs include both JSON and text output because of ddtrace. Is there any chance of it being resolved?

javierjulio avatar Feb 03 '23 15:02 javierjulio

@javierjulio we switched to Semantic Logger which does still do JSON logging (among other things).

chall8908 avatar Feb 09 '23 18:02 chall8908

+1 to this issue. I have several teams that are running into this or will hit it in the near future and the docs don't offer a workaround.

too-gee avatar May 26 '23 15:05 too-gee

👋 @too-gee Thanks for reporting, I will try to reproduce it and address it.

TonyCTHsu avatar May 26 '23 21:05 TonyCTHsu

@TonyCTHsu thank you!

javierjulio avatar May 26 '23 22:05 javierjulio

@chall8908 @javierjulio 👋 I want to give you guys a quick update from my side.

I was able to reproduce with lograge, I want to try out with semantic_logger later.

Question: Are you using auto instrument? require: 'ddtrace/auto_instrument' in Gemfile ?

https://github.com/DataDog/dd-trace-rb/blob/master/docs/GettingStarted.md#rails-or-hanami-applications

TonyCTHsu avatar May 30 '23 12:05 TonyCTHsu

@TonyCTHsu thanks! No, we are not using auto instrument.

javierjulio avatar May 30 '23 14:05 javierjulio

@TonyCTHsu our projects are using auto instrumentation.

I'd have to double check, but I recall that semantic_logger didn't have this issue because it overrides the Rails logger with its own logger, bypassing the problem code.

chall8908 avatar May 30 '23 21:05 chall8908

@TonyCTHsu We see the same issue with lograge, we use auto_instrument in our Rails projects.

artrybalko avatar Jul 17 '23 11:07 artrybalko

@chall8908 @artrybalko @javierjulio , I realized that auto instrumentation with a Rails app that would enable log injection for both lograge and Rails' ActiveSupport::TaggedLogging, which is causing the double insertion mentioned above.

Currently, the injection for ActiveSupport::TaggedLogging is only controlled by config.log_injection, hence it is a bit cumbersome to only activate Lograge without activate ActiveSupport::TaggedLogging injection.

Workaround: Configure your application explicitly with a logger what is not a ActiveSupport::TaggedLogging

Replace

config.logger = ActiveSupport::TaggedLogging.new(logger)

with

config.logger = logger

Noted that Rails is using ActiveSupport::TaggedLogging by default without any configuration

This is similar to the reason that semantic logger does not observed double insertion because semantic logger replaces the logger instance. In the future, we will be introducing a configuration option explicitly for ActiveSupport::TaggedLogging.

TonyCTHsu avatar Sep 11 '23 09:09 TonyCTHsu

@TonyCTHsu Thanks for the feedback! We are switching to semantic logger as a trial. This issue wasn't the main consideration but it pushed it over the line.

artrybalko avatar Sep 12 '23 13:09 artrybalko

Sorry for the wait everyone.

I did some investigation on this issue this week and found out that lograge is not compatible with ActiveSupport::TaggedLogging when config.log_tags are set. This is especially problematic when JSON output format is configured for lograge.

There are a couple open issues in the lograge repository about this issue: https://github.com/roidrage/lograge/issues/233#issuecomment-404336057, https://github.com/roidrage/lograge/issues/255

There is no good immediate solution for this issue:

  1. Use semantic_logger instead of lograge. Semantic Logger integrates with Rails named tags natively. There's no loss in observability for this solution.
  2. Replace ActiveSupport::TaggedLogging with ActiveSupport::Logger:
    config.logger = ActiveSupport::Logger.new(STDOUT)
    config.active_job.logger = ActiveSupport::Logger.new(STDOUT)
    
    All lograge log lines will have the correct JSON format. The downside is that log lines that are not handled by lograge, for example simple Rails.logger.* calls, will not have trace correlation information.

Looking at internals of both semantic_logger and lograge, it looks at me that semantic_logger it's better equipped to handle modern versions of Rails. I would recommend using semantic_logger instead of lograge if the native ActiveSupport::TaggedLogging is not sufficient.

marcotc avatar Apr 04 '24 23:04 marcotc

@marcotc thank you. We removed tagged logging as it's not something we use. It's actually better for us to provide our logs in JSON format for easy filtering. As you noted the Datadog trace correlation information is not included. Are we able to include that manually in a logger call in a safe manner?

javierjulio avatar Apr 09 '24 14:04 javierjulio