Class: Skylight::Core::Probes::Middleware::Probe Private

Inherits:
Object
  • Object
show all
Defined in:
lib/skylight/core/probes/middleware.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary collapse

DISABLED_KEY =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

:__skylight_middleware_disabled

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_instrumentation(middleware, default_name: "Anonymous Middleware", category: "rack.middleware") ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


19
20
21
22
23
24
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
# File 'lib/skylight/core/probes/middleware.rb', line 19

def self.add_instrumentation(middleware, default_name: "Anonymous Middleware", category: "rack.middleware")
  middleware.instance_eval <<-RUBY, __FILE__, __LINE__ + 1
    alias call_without_sk call
    def call(*args, &block)
      return call_without_sk(*args, &block) if Skylight::Core::Probes::Middleware::Probe.disabled?

      traces = Skylight::Core::Fanout.each_trace.to_a
      return call_without_sk(*args, &block) if traces.empty?

      begin
        name = self.class.name || "#{default_name}"

        traces.each{ |t| t.endpoint = name }

        spans = Skylight::Core::Fanout.instrument(title: name, category: "#{category}")
        resp = call_without_sk(*args, &block)

        proxied_response = Skylight::Core::Middleware.with_after_close(resp, debug_identifier: "Middleware: #{name}") do
          Skylight::Core::Fanout.done(spans)
        end
      rescue Exception => err
        # FIXME: Log this?
        Skylight::Core::Fanout.done(spans, exception_object: err)
        raise
      ensure
        unless err || proxied_response
          # If we've gotten to this point, the most likely scenario is that
          # a throw/catch has bypassed a portion of the callstack. Since these spans would not otherwise
          # be closed, mark them deferred to indicate that they should be implicitly closed.
          # See Core::Trace#deferred_spans or Core::Trace#stop for more information.
          Skylight::Core::Fanout.done(spans, defer: true)
        end
      end
    end
  RUBY
end

.disable!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


7
8
9
# File 'lib/skylight/core/probes/middleware.rb', line 7

def self.disable!
  @disabled = true
end

.disabled?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

15
16
17
# File 'lib/skylight/core/probes/middleware.rb', line 15

def self.disabled?
  !!@disabled
end

.enable!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


11
12
13
# File 'lib/skylight/core/probes/middleware.rb', line 11

def self.enable!
  @disabled = false
end

Instance Method Details

#installObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/skylight/core/probes/middleware.rb', line 56

def install
  return if defined?(::ActionDispatch::MiddlewareStack::InstrumentationProxy)

  ::ActionDispatch::MiddlewareStack::Middleware.class_eval do
    alias_method :build_without_sk, :build
    def build(*args)
      sk_instrument_middleware(build_without_sk(*args))
    end

    def sk_instrument_middleware(middleware)
      return middleware if middleware.is_a?(Skylight::Core::Middleware)

      # Not sure how this would actually happen
      return middleware if middleware.respond_to?(:call_without_sk)

      # On Rails 3, ActionDispatch::Session::CookieStore is frozen, for one
      return middleware if middleware.frozen?

      Skylight::Core::Probes::Middleware::Probe.add_instrumentation(middleware)

      middleware
    end
  end
end