Module: Datadog::Contrib::ActionPack::ActionController::Instrumentation

Defined in:
lib/ddtrace/contrib/action_pack/action_controller/instrumentation.rb

Overview

Instrumentation for ActionController components

Defined Under Namespace

Modules: Metal

Class Method Summary collapse

Class Method Details

.exception_controller?(payload) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ddtrace/contrib/action_pack/action_controller/instrumentation.rb', line 76

def exception_controller?(payload)
  exception_controller_class = Datadog.configuration[:action_pack][:exception_controller]
  controller = payload.fetch(:controller)
  headers = payload.fetch(:headers)

  # If no exception controller class has been set,
  # guess whether this is an exception controller from the headers.
  if exception_controller_class.nil?
    !headers[:request_exception].nil?
  # If an exception controller class has been specified,
  # check if the controller is a kind of the exception controller class.
  elsif exception_controller_class.is_a?(Class) || exception_controller_class.is_a?(Module)
    controller <= exception_controller_class
  # Otherwise if the exception controller class is some other value (like false)
  # assume that this controller doesn't handle exceptions.
  else
    false
  end
end

.finish_processing(payload) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ddtrace/contrib/action_pack/action_controller/instrumentation.rb', line 38

def finish_processing(payload)
  # retrieve the tracing context and the latest active span
  tracing_context = payload.fetch(:tracing_context)
  span = tracing_context[:dd_request_span]
  return unless span && !span.finished?

  begin
    # We repeat this in both start and at finish because the resource may have changed during the request
    try_setting_rack_request_resource(payload, span.resource)

    # Set analytics sample rate
    Utils.set_analytics_sample_rate(span)

    # Measure service stats
    Contrib::Analytics.set_measured(span)

    # Associate with runtime metrics
    Datadog.runtime_metrics.associate_with_span(span)

    span.set_tag(Ext::TAG_ROUTE_ACTION, payload.fetch(:action))
    span.set_tag(Ext::TAG_ROUTE_CONTROLLER, payload.fetch(:controller))

    exception = payload[:exception_object]
    if exception.nil?
      # [christian] in some cases :status is not defined,
      # rather than firing an error, simply acknowledge we don't know it.
      status = payload.fetch(:status, '?').to_s
      span.status = 1 if status.start_with?('5')
    elsif Utils.exception_is_error?(exception)
      span.set_error(exception)
    end
  ensure
    span.finish
  end
rescue StandardError => e
  Datadog.logger.error(e.message)
end

.start_processing(payload) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ddtrace/contrib/action_pack/action_controller/instrumentation.rb', line 17

def start_processing(payload)
  # trace the execution
  tracer = Datadog.configuration[:action_pack][:tracer]
  service = Datadog.configuration[:action_pack][:controller_service]
  type = Datadog::Ext::HTTP::TYPE_INBOUND
  span = tracer.trace(
    Ext::SPAN_ACTION_CONTROLLER,
    service: service,
    span_type: type,
    resource: "#{payload.fetch(:controller)}##{payload.fetch(:action)}",
  )

  # attach the current span to the tracing context
  tracing_context = payload.fetch(:tracing_context)
  tracing_context[:dd_request_span] = span

  try_setting_rack_request_resource(payload, span.resource)
rescue StandardError => e
  Datadog.logger.error(e.message)
end

.try_setting_rack_request_resource(payload, resource) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/ddtrace/contrib/action_pack/action_controller/instrumentation.rb', line 96

def try_setting_rack_request_resource(payload, resource)
  # Set the resource name of the Rack request span unless this is an exception controller.
  unless payload.fetch(:exception_controller?)
    rack_request_span = payload.fetch(:env)[Datadog::Contrib::Rack::TraceMiddleware::RACK_REQUEST_SPAN]
    rack_request_span.resource = resource if rack_request_span
  end
end