Class: OpenTelemetry::Instrumentation::ActionPack::Handlers::ActionController

Inherits:
Object
  • Object
show all
Defined in:
lib/opentelemetry/instrumentation/action_pack/handlers/action_controller.rb

Overview

Action Controller handler to handle the notification from Active Support

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ ActionController



14
15
16
17
# File 'lib/opentelemetry/instrumentation/action_pack/handlers/action_controller.rb', line 14

def initialize(config)
  @config = config
  @span_naming = config.fetch(:span_naming)
end

Instance Method Details

#finish(_name, _id, payload) ⇒ Hash

Invoked by ActiveSupport::Notifications at the end of the instrumentation block



65
66
67
68
69
70
# File 'lib/opentelemetry/instrumentation/action_pack/handlers/action_controller.rb', line 65

def finish(_name, _id, payload)
  span = OpenTelemetry::Instrumentation::Rack.current_span
  span.record_exception(payload[:exception_object]) if payload[:exception_object]
rescue StandardError => e
  OpenTelemetry.handle_error(exception: e)
end

#start(_name, _id, payload) ⇒ Hash

Invoked by ActiveSupport::Notifications at the start of the instrumentation block



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/opentelemetry/instrumentation/action_pack/handlers/action_controller.rb', line 25

def start(_name, _id, payload)
  span = OpenTelemetry::Instrumentation::Rack.current_span
  return unless span.recording?

  request = payload[:request]
  # It seems that there are cases in Rails functional tests where it bypasses the routing system and the `action_dispatch.route_uri_pattern` header not being set.
  # Our Test suite executes the routing system so we are unable to recreate this error case.
  # https://github.com/rails/rails/blob/747f85f200e7bb2c1a31b4e26e5a5655e2dc0cdc/actionpack/lib/action_dispatch/http/request.rb#L160
  http_route = request.route_uri_pattern&.chomp('(.:format)') if request.respond_to?(:route_uri_pattern)

  attributes = {
    OpenTelemetry::SemanticConventions::Trace::CODE_NAMESPACE => String(payload[:controller]),
    OpenTelemetry::SemanticConventions::Trace::CODE_FUNCTION => String(payload[:action])
  }
  attributes[OpenTelemetry::SemanticConventions::Trace::HTTP_ROUTE] = http_route if http_route
  attributes[OpenTelemetry::SemanticConventions::Trace::HTTP_TARGET] = request.filtered_path if request.filtered_path != request.fullpath

  if @span_naming == :semconv
    span.name = if http_route
                  "#{request.method} #{http_route}"
                else
                  "#{request.method} /#{payload.dig(:params, :controller)}/#{payload.dig(:params, :action)}"
                end
  # If there is an exception we want to keep the original span name
  # so it is easier to see where the request was routed to.
  elsif !request.env['action_dispatch.exception']
    span.name = "#{payload[:controller]}##{payload[:action]}"
  end

  span.add_attributes(attributes)
rescue StandardError => e
  OpenTelemetry.handle_error(exception: e)
end