Module: Datadog::RailsActionPatcher

Includes:
Patcher
Defined in:
lib/ddtrace/contrib/rails/core_extensions.rb

Overview

RailsActionPatcher contains functions to patch Rails action controller instrumentation

Class Method Summary collapse

Methods included from Patcher

included

Methods included from Patcher::CommonMethods

#do_once, #without_warnings

Class Method Details

.patch_action_controllerObject



152
153
154
155
156
# File 'lib/ddtrace/contrib/rails/core_extensions.rb', line 152

def patch_action_controller
  do_once(:patch_action_controller) do
    patch_process_action
  end
end

.patch_process_actionObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/ddtrace/contrib/rails/core_extensions.rb', line 158

def patch_process_action
  do_once(:patch_process_action) do
    ::ActionController::Instrumentation.class_eval do
      def process_action_with_datadog(*args)
        # mutable payload with a tracing context that is used in two different
        # signals; it propagates the request span so that it can be finished
        # no matter what
        payload = {
          controller: self.class,
          action: action_name,
          headers: {
            # The exception this controller was given in the request,
            # which is typical if the controller is configured to handle exceptions.
            request_exception: request.headers['action_dispatch.exception']
          },
          tracing_context: {}
        }

        begin
          # process and catch request exceptions
          Datadog::Contrib::Rails::ActionController.start_processing(payload)
          result = process_action_without_datadog(*args)
          payload[:status] = response.status
          result
        rescue Exception => e
          payload[:exception] = [e.class.name, e.message]
          payload[:exception_object] = e
          raise e
        end
      ensure
        Datadog::Contrib::Rails::ActionController.finish_processing(payload)
      end

      alias_method :process_action_without_datadog, :process_action
      alias_method :process_action, :process_action_with_datadog
    end
  end
end