Class: ZipkinTracer::RackHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/zipkin-tracer/rack/zipkin-tracer.rb

Overview

This middleware reads Zipkin headers from the request and sets/creates a Trace.id usable by the rest of the app It will also send the trace to the Zipkin service using one of the methods configured.

Defined Under Namespace

Classes: ZipkinEnv

Constant Summary collapse

B3_REQUIRED_HEADERS =
%w[HTTP_X_B3_TRACEID HTTP_X_B3_PARENTSPANID HTTP_X_B3_SPANID HTTP_X_B3_SAMPLED].freeze
B3_OPT_HEADERS =
%w[HTTP_X_B3_FLAGS].freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, config = nil) ⇒ RackHandler

Returns a new instance of RackHandler.



27
28
29
30
31
# File 'lib/zipkin-tracer/rack/zipkin-tracer.rb', line 27

def initialize(app, config = nil)
  @app = app
  @config = Config.new(app, config).freeze
  @tracer = TracerFactory.new.tracer(@config)
end

Instance Method Details

#call(env) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/zipkin-tracer/rack/zipkin-tracer.rb', line 33

def call(env)
  zipkin_env = ZipkinEnv.new(env, @config)
  trace_id = zipkin_env.trace_id
  Trace.with_trace_id(trace_id) do
    if !trace_id.sampled? || !Application.routable_request?(env['PATH_INFO'])
      @app.call(env)
    else
      @tracer.with_new_span(trace_id, zipkin_env.env['REQUEST_METHOD'].to_s.downcase) do |span|
        trace!(span, zipkin_env) { @app.call(env) }
      end
    end
  end
end