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



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

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



71
72
73
74
75
76
77
# File 'lib/datadog/tracing/contrib/rails/patcher.rb', line 71

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



37
38
39
40
41
42
43
44
45
46
# File 'lib/datadog/tracing/contrib/rails/patcher.rb', line 37

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.
    # Sometimes we don't want to activate middleware e.g. OpenTracing, etc.
    add_middleware(app) if Datadog.configuration.tracing[:rails][:middleware]

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

.patchObject



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

def patch
  patch_before_initialize
  patch_after_initialize
end

.patch_after_initializeObject



65
66
67
68
69
# File 'lib/datadog/tracing/contrib/rails/patcher.rb', line 65

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

.patch_before_initializeObject



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

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

.setup_tracerObject

Configure Rails tracing with settings



80
81
82
# File 'lib/datadog/tracing/contrib/rails/patcher.rb', line 80

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

.target_versionObject



22
23
24
# File 'lib/datadog/tracing/contrib/rails/patcher.rb', line 22

def target_version
  Integration.version
end