Class: Aws::Xray::Faraday

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/aws/xray/faraday.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, name = nil) ⇒ Faraday

Returns a new instance of Faraday.

Parameters:

  • app (Object)

    Faraday app.

  • name (String) (defaults to: nil)

    Logical service name for downstream API.

Raises:

  • (ArgumentError)


9
10
11
12
13
# File 'lib/aws/xray/faraday.rb', line 9

def initialize(app, name = nil)
  raise ArgumentError.new("name must be a String but given a #{name.class}") if name && !name.is_a?(String)
  super(app)
  @name = name
end

Instance Method Details

#call(req_env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/aws/xray/faraday.rb', line 15

def call(req_env)
  return @app.call(req_env) unless Context.started?

  name = @name || req_env.request_headers['Host'] || "unknown-request-from-#{Context.current.name}"

  Context.current.start_subsegment(remote: true, name: name) do |sub|
    propagate_trace = sub.generate_trace
    req_env.request_headers[TRACE_HEADER] = propagate_trace.to_header_value
    sub.set_http_request(Request.build_from_faraday_env(req_env))

    res = Context.current.disable_trace(:net_http) { @app.call(req_env) }
    res.on_complete do |res_env|
      sub.set_http_response_with_error(res_env.status, res_env.response_headers['Content-Length'], remote: true)
      sub.(CallerBuilder.call) if Aws::Xray.config.record_caller_of_http_requests
    end
  end
end