Sha256: 89f50253b12a156777cc547b0e192276602c057c8fb0ec860ae1258a7a9244b6

Contents?: true

Size: 1.83 KB

Versions: 2

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

module OpenTelemetry
  module Instrumentation
    module ActionPack
      module Patches
        module ActionController
          # Module to prepend to ActionController::Metal for instrumentation
          module Metal
            def dispatch(name, request, response)
              rack_span = OpenTelemetry::Instrumentation::Rack.current_span
              if rack_span.recording?
                unless request.env['action_dispatch.exception']
                  rack_span.name = case instrumentation_config[:span_naming]
                                   when :controller_action then "#{self.class.name}##{name}"
                                   else "#{request.method} #{rails_route(request)}"
                                   end
                end

                attributes_to_append = {}
                attributes_to_append['http.route'] = rails_route(request) if instrumentation_config[:enable_recognize_route]
                attributes_to_append['http.target'] = request.filtered_path if request.filtered_path != request.fullpath
                rack_span.add_attributes(attributes_to_append) unless attributes_to_append.empty?
              end

              super(name, request, response)
            end

            private

            def rails_route(request)
              @rails_route ||= ::Rails.application.routes.router.recognize(request) do |route, _params|
                return route.path.spec.to_s
                # Rails will match on the first route - see https://guides.rubyonrails.org/routing.html#crud-verbs-and-actions
              end
            end

            def instrumentation_config
              ActionPack::Instrumentation.instance.config
            end
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
opentelemetry-instrumentation-action_pack-0.3.1 lib/opentelemetry/instrumentation/action_pack/patches/action_controller/metal.rb
opentelemetry-instrumentation-action_pack-0.3.0 lib/opentelemetry/instrumentation/action_pack/patches/action_controller/metal.rb