Module: Datadog::CI::Contrib::ActiveSupport::Patcher

Includes:
Patcher
Defined in:
lib/datadog/ci/contrib/activesupport/patcher.rb

Overview

Patcher enables patching of activesupport module

Class Method Summary collapse

Methods included from Patcher

included

Class Method Details

.datadog_logs_componentObject



43
44
45
# File 'lib/datadog/ci/contrib/activesupport/patcher.rb', line 43

def datadog_logs_component
  Datadog.send(:components).agentless_logs_submission
end

.patchObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/datadog/ci/contrib/activesupport/patcher.rb', line 16

def patch
  unless datadog_logs_component.enabled
    Datadog.logger.debug("Datadog logs submission is disabled, skipping activesupport patching")
    return
  end

  unless ::Rails.logger.formatter.is_a?(::ActiveSupport::TaggedLogging::Formatter)
    Datadog.logger.debug {
      "Rails logger formatter is not an instance of ActiveSupport::TaggedLogging::Formatter, skipping activesupport patching. " \
      "Formatter: #{::Rails.logger.formatter.class}"
    }
    return
  end

  # Before Ruby 3.0, prepending to a module did not change existing instances where this module was included
  # It means that for Ruby 2.7 we have to patch formatter's class directly
  #
  # Context:
  # - https://bugs.ruby-lang.org/issues/9573
  # - https://rubyreferences.github.io/rubychanges/3.0.html#include-and-prepend-now-affects-modules-including-the-receiver
  if RUBY_VERSION.start_with?("2.7")
    Rails.logger.formatter.class.prepend(LogsFormatter)
  else
    ::ActiveSupport::TaggedLogging::Formatter.prepend(LogsFormatter)
  end
end