Class: ZipkinTracer::FaradayHandler
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- ZipkinTracer::FaradayHandler
- Defined in:
- lib/zipkin-tracer/faraday/zipkin-tracer.rb
Constant Summary collapse
- B3_HEADERS =
{ :trace_id => "X-B3-TraceId", :parent_id => "X-B3-ParentSpanId", :span_id => "X-B3-SpanId", :sampled => "X-B3-Sampled", :flags => "X-B3-Flags" }.freeze
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, service_name = nil) ⇒ FaradayHandler
constructor
A new instance of FaradayHandler.
Constructor Details
#initialize(app, service_name = nil) ⇒ FaradayHandler
Returns a new instance of FaradayHandler.
16 17 18 19 |
# File 'lib/zipkin-tracer/faraday/zipkin-tracer.rb', line 16 def initialize(app, service_name=nil) @app = app @service_name = service_name end |
Instance Method Details
#call(env) ⇒ Object
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 |
# File 'lib/zipkin-tracer/faraday/zipkin-tracer.rb', line 21 def call(env) # handle either a URI object (passed by Faraday v0.8.x in testing), or something string-izable url = env[:url].respond_to?(:host) ? env[:url] : URI.parse(env[:url].to_s) service_name = @service_name || url.host.split('.').first # default to url-derived service name endpoint = ::Trace::Endpoint.new(host_ip_for(url.host), url.port, service_name) ::Trace.unwind do trace_id = ::Trace.id ::Trace.push(trace_id.next_id) B3_HEADERS.each do |method, header| env[:request_headers][header] = ::Trace.id.send(method).to_s end # annotate with method (GET/POST/etc.) and uri path ::Trace.set_rpc_name(env[:method].to_s.upcase) record(::Trace::BinaryAnnotation.new("http.uri", url.path, "STRING", endpoint)) record(::Trace::Annotation.new(::Trace::Annotation::CLIENT_SEND, endpoint)) result = @app.call(env).on_complete do |renv| # record HTTP status code on response record(::Trace::BinaryAnnotation.new("http.status", [renv[:status]].pack('n'), "I16", endpoint)) end record(::Trace::Annotation.new(::Trace::Annotation::CLIENT_RECV, endpoint)) result end end |