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

Includes:
Patcher
Defined in:
lib/datadog/tracing/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



62
63
64
# File 'lib/datadog/tracing/contrib/rack/patcher.rb', line 62

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

.patchObject



31
32
33
# File 'lib/datadog/tracing/contrib/rack/patcher.rb', line 31

def patch
  patch_middleware_names
end

.patch_middleware_namesObject



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

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



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

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 = (middleware.instance_variable_get('@app') if middleware.instance_variable_defined?('@app'))

  retain_middleware_name(following)
end

.target_versionObject



27
28
29
# File 'lib/datadog/tracing/contrib/rack/patcher.rb', line 27

def target_version
  Integration.version
end