Sha256: 4a98e11befd995fda03a8386aba010f2bccdaa63ddca4c689096da68e73ea08f

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

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

require 'opentelemetry'

module OpenTelemetry
  module Adapters
    module Rack
      # The Adapter class contains logic to detect and install the Rack
      # instrumentation adapter
      class Adapter < OpenTelemetry::Instrumentation::Adapter
        install do |config|
          require_dependencies

          retain_middleware_names if config[:retain_middleware_names]
        end

        present do
          defined?(::Rack)
        end

        private

        def require_dependencies
          require_relative 'middlewares/tracer_middleware'
        end

        MissingApplicationError = Class.new(StandardError)

        # intercept all middleware-compatible calls, retain class name
        def retain_middleware_names
          next_middleware = config[:application]
          raise MissingApplicationError unless next_middleware

          while next_middleware
            if next_middleware.respond_to?(:call)
              next_middleware.singleton_class.class_eval do
                alias_method :__call, :call

                def call(env)
                  env['RESPONSE_MIDDLEWARE'] = self.class.to_s
                  __call(env)
                end
              end
            end

            next_middleware = next_middleware.instance_variable_defined?('@app') &&
                              next_middleware.instance_variable_get('@app')
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
opentelemetry-adapters-rack-0.4.0 lib/opentelemetry/adapters/rack/adapter.rb
opentelemetry-adapters-rack-0.3.0 lib/opentelemetry/adapters/rack/adapter.rb