Module: Datadog::Contrib::Rack::MiddlewareNamePatcher

Includes:
Patcher
Defined in:
lib/ddtrace/contrib/rack/patcher.rb

Overview

Provides instrumentation for Rack middleware names

Class Method Summary collapse

Methods included from Patcher

included

Class Method Details

.get_option(option) ⇒ Object



63
64
65
# File 'lib/ddtrace/contrib/rack/patcher.rb', line 63

def get_option(option)
  Datadog.configuration[:rack].get_option(option)
end

.patchObject



30
31
32
# File 'lib/ddtrace/contrib/rack/patcher.rb', line 30

def patch
  patch_middleware_names
end

.patch_middleware_namesObject



34
35
36
37
38
39
40
41
42
# File 'lib/ddtrace/contrib/rack/patcher.rb', line 34

def patch_middleware_names
  retain_middleware_name(get_option(:application))
rescue => e
  # We can safely ignore these exceptions since they happen only in the
  # context of middleware patching outside a Rails server process (eg. a
  # process that doesn't serve HTTP requests but has Rails environment
  # loaded such as a Resque master process)
  Datadog.logger.debug("Error patching middleware stack: #{e}")
end

.retain_middleware_name(middleware) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ddtrace/contrib/rack/patcher.rb', line 44

def retain_middleware_name(middleware)
  return unless middleware && middleware.respond_to?(:call)

  middleware.singleton_class.class_eval do
    alias_method :__call, :call

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

  following = if middleware.instance_variable_defined?('@app')
                middleware.instance_variable_get('@app')
              end

  retain_middleware_name(following)
end

.target_versionObject



26
27
28
# File 'lib/ddtrace/contrib/rack/patcher.rb', line 26

def target_version
  Integration.version
end