Class: Skylight::Middleware Private

Inherits:
Object show all
Includes:
Util::Logging
Defined in:
lib/skylight/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.

Defined Under Namespace

Classes: BodyProxy

Constant Summary collapse

SKYLIGHT_REQUEST_ID =

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.request_id".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Logging

#config_for_logging, #debug, #error, #fmt, #info, #log, #raise_on_error?, #t, #trace, #trace?, #warn

Constructor Details

#initialize(app, opts = {}) ⇒ Middleware

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 a new instance of Middleware.



77
78
79
80
# File 'lib/skylight/middleware.rb', line 77

def initialize(app, opts = {})
  @app = app
  @config = opts[:config]
end

Instance Attribute Details

#configObject (readonly)

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.

For Util::Logging



75
76
77
# File 'lib/skylight/middleware.rb', line 75

def config
  @config
end

Class Method Details

.with_after_close(resp, debug_identifier: "unknown", &block) ⇒ 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.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/skylight/middleware.rb', line 51

def self.with_after_close(resp, debug_identifier: "unknown", &block)
  unless resp.respond_to?(:to_ary)
    if resp.respond_to?(:to_a)
      Skylight.warn(
        "Rack response from \"#{debug_identifier}\" cannot be implicitly converted to an array. " \
          "This is in violation of the Rack SPEC and will raise an error in future versions."
      )
      resp = resp.to_a
    else
      Skylight.error(
        "Rack response from \"#{debug_identifier}\" cannot be converted to an array. This is in " \
          "violation of the Rack SPEC and may cause problems with Skylight operation."
      )
      return resp
    end
  end

  status, headers, body = resp
  [status, headers, BodyProxy.new(body, &block)]
end

Instance Method Details

#call(env) ⇒ 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.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/skylight/middleware.rb', line 82

def call(env)
  set_request_id(env)

  if Skylight.tracing?
    debug "Already instrumenting. Make sure the Skylight Rack Middleware hasn't been added more than once."
    return @app.call(env)
  end

  if env["REQUEST_METHOD"] == "HEAD"
    t { "middleware skipping HEAD" }
    @app.call(env)
  else
    begin
      t { "middleware beginning trace" }
      trace = Skylight.trace(endpoint_name(env), "app.rack.request", nil, meta: endpoint_meta(env), component: :web)
      t { "middleware began trace=#{trace ? trace.uuid : nil}" }

      resp = @app.call(env)

      trace ? Middleware.with_after_close(resp, debug_identifier: "Rack App: #{@app.class}") { trace.submit } : resp
    rescue Exception => e
      t { "middleware exception: #{e}\n#{e.backtrace.join("\n")}" }
      trace&.submit
      raise
    end
  end
end