Class: Aws::Xray::Rack

Inherits:
Object
  • Object
show all
Defined in:
lib/aws/xray/rack.rb

Constant Summary collapse

TRACE_ENV =
'HTTP_X_AMZN_TRACE_ID'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, client_options: {}) ⇒ Rack

TODO: excluded_paths, included_paths

Parameters:

  • client_options (Hash) (defaults to: {})

    For xray-agent client.

    • host: e.g. ‘127.0.0.1’

    • port: e.g. 2000

    • sock: test purpose.



17
18
19
20
21
# File 'lib/aws/xray/rack.rb', line 17

def initialize(app, client_options: {})
  @app = app
  @name = Aws::Xray.config.name || raise(MissingNameError)
  @client = Client.new(Aws::Xray.config.client_options.merge(client_options))
end

Instance Method Details

#call(env) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/aws/xray/rack.rb', line 23

def call(env)
  header_value = env[TRACE_ENV]
  trace = if header_value
                   Trace.build_from_header_value(header_value)
                 else
                   Trace.generate
                 end

  Context.with_new_context(@name, @client, trace) do
    Context.current.base_trace do |seg|
      seg.set_http_request(Request.build_from_rack_env(env))
      status, headers, body = @app.call(env)
      seg.set_http_response(status, headers['Content-Length'])
      headers[TRACE_HEADER] = trace.to_header_value
      [status, headers, body]
    end
  end
end