Class: Skylight::Core::Middleware Private

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

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, #log_env_prefix, #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/core/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/core/middleware.rb', line 75

def config
  @config
end

Class Method Details

.log_targetObject

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.



68
69
70
# File 'lib/skylight/core/middleware.rb', line 68

def self.log_target
  @log_target ||= (Skylight::Core::Fanout.registered.first&.config || Logger.new(STDERR))
end

.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.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/skylight/core/middleware.rb', line 47

def self.with_after_close(resp, debug_identifier: "unknown", &block)
  # Responses should be arrays but in some situations they aren't
  #   e.g. https://github.com/ruby-grape/grape/issues/1041
  # See also https://github.com/rack/rack/issues/1239

  unless resp.respond_to?(:to_ary)
    if resp.respond_to?(:to_a)
      log_target.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
      log_target.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
109
110
111
# File 'lib/skylight/core/middleware.rb', line 82

def call(env)
  set_request_id(env)

  if instrumentable.tracing?
    error "Already instrumenting. Make sure the Skylight Rack Middleware hasn't been added more than once."
  end

  if env["REQUEST_METHOD"] == "HEAD"
    t { "middleware skipping HEAD" }
    @app.call(env)
  else
    begin
      t { "middleware beginning trace" }
      trace = instrumentable.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)

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