Module: Datadog::Tracing::Contrib::Rails::Patcher

Includes:
Patcher
Defined in:
lib/datadog/tracing/contrib/rails/patcher.rb

Overview

Patcher enables patching of ‘rails’ module.

Constant Summary collapse

BEFORE_INITIALIZE_ONLY_ONCE_PER_APP =
Hash.new { |h, key| h[key] = Core::Utils::OnlyOnce.new }
AFTER_INITIALIZE_ONLY_ONCE_PER_APP =
Hash.new { |h, key| h[key] = Core::Utils::OnlyOnce.new }

Class Method Summary collapse

Methods included from Patcher

included

Class Method Details

.add_middleware(app) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/datadog/tracing/contrib/rails/patcher.rb', line 51

def add_middleware(app)
  # Add trace middleware at the top of the middleware stack,
  # to ensure we capture the complete execution time.
  app.middleware.insert_before(0, Contrib::Rack::TraceMiddleware)

  # Some Rails middleware can swallow an application error, preventing
  # the error propagation to the encompassing Rack span.
  #
  # We insert our own middleware right before these Rails middleware
  # have a chance to swallow the error.
  #
  # Note: because the middleware stack is push/pop, "before" and "after" are reversed
  # for our use case: we insert ourselves with "after" a middleware to ensure we are
  # able to pop the request "before" it.
  app.middleware.insert_after(::ActionDispatch::DebugExceptions, Contrib::Rails::ExceptionMiddleware)
end

.after_initialize(app) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/datadog/tracing/contrib/rails/patcher.rb', line 74

def after_initialize(app)
  AFTER_INITIALIZE_ONLY_ONCE_PER_APP[app].run do
    # Finish configuring the tracer after the application is initialized.
    # We need to wait for some things, like application name, middleware stack, etc.
    setup_tracer
  end
end

.before_initialize(app) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/datadog/tracing/contrib/rails/patcher.rb', line 41

def before_initialize(app)
  BEFORE_INITIALIZE_ONLY_ONCE_PER_APP[app].run do
    # Middleware must be added before the application is initialized.
    # Otherwise the middleware stack will be frozen.
    add_middleware(app) if Datadog.configuration.tracing[:rails][:middleware]

    Rails::LogInjection.configure_log_tags(app.config)
  end
end

.patchObject



29
30
31
32
33
# File 'lib/datadog/tracing/contrib/rails/patcher.rb', line 29

def patch
  patch_before_initialize
  patch_after_initialize
  patch_rails_runner
end

.patch_after_initializeObject



68
69
70
71
72
# File 'lib/datadog/tracing/contrib/rails/patcher.rb', line 68

def patch_after_initialize
  ::ActiveSupport.on_load(:after_initialize) do
    Contrib::Rails::Patcher.after_initialize(self)
  end
end

.patch_before_initializeObject



35
36
37
38
39
# File 'lib/datadog/tracing/contrib/rails/patcher.rb', line 35

def patch_before_initialize
  ::ActiveSupport.on_load(:before_initialize) do
    Contrib::Rails::Patcher.before_initialize(self)
  end
end

.patch_rails_runnerObject

Instruments the ‘bin/rails runner` command.



88
89
90
# File 'lib/datadog/tracing/contrib/rails/patcher.rb', line 88

def patch_rails_runner
  ::Rails::Command.singleton_class.prepend(Command) if defined?(::Rails::Command)
end

.setup_tracerObject

Configure Rails tracing with settings



83
84
85
# File 'lib/datadog/tracing/contrib/rails/patcher.rb', line 83

def setup_tracer
  Contrib::Rails::Framework.setup
end

.target_versionObject



25
26
27
# File 'lib/datadog/tracing/contrib/rails/patcher.rb', line 25

def target_version
  Integration.version
end