Class: ZipkinTracer::RackHandler

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

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(app, config = nil) ⇒ RackHandler

Returns a new instance of RackHandler.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/zipkin-tracer.rb', line 40

def initialize(app, config=nil)
  @app = app
  @lock = Mutex.new

  config ||= app.config.zipkin_tracer # if not specified, try on app (e.g. Rails 3+)
  @service_name = config[:service_name]
  @service_port = config[:service_port]

  if config[:scribe_server] && defined?(::Scribe)
    scribe = config[:scribe_server] ? Scribe.new(config[:scribe_server]) : Scribe.new()
    careless_scribe = CarelessScribe.new(scribe)
    scribe_max_buffer = config[:scribe_max_buffer] ? config[:scribe_max_buffer] : 10
    ::Trace.tracer = ::Trace::ZipkinTracer.new(careless_scribe, scribe_max_buffer)
  elsif config[:zookeeper] && RUBY_PLATFORM == 'java' && defined?(::Hermann)
    kafkaTracer = ::Trace::ZipkinKafkaTracer.new
    kafkaTracer.connect(config[:zookeeper])
    ::Trace.tracer = kafkaTracer
  end

  @sample_rate = config[:sample_rate] ? config[:sample_rate] : 0.1

  @annotate_plugin = config[:annotate_plugin]     # call for trace annotation
  @filter_plugin = config[:filter_plugin]         # skip tracing if returns false
  @whitelist_plugin = config[:whitelist_plugin]   # force sampling if returns true
end

Instance Method Details

#call(env) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/zipkin-tracer.rb', line 66

def call(env)
  # skip certain requests
  return @app.call(env) if filtered?(env)

  ::Trace.default_endpoint = ::Trace.default_endpoint.with_service_name(@service_name).with_port(@service_port)
  ::Trace.sample_rate=(@sample_rate)
  whitelisted = force_sample?(env)
  id = get_or_create_trace_id(env, whitelisted) # note that this depends on the sample rate being set
  tracing_filter(id, env, whitelisted) { @app.call(env) }
end