Class: Zipkin::Tracer

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

Constant Summary collapse

DEFAULT_FLUSH_INTERVAL =
10

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collector, sender, logger: Logger.new(STDOUT)) ⇒ Tracer

Returns a new instance of Tracer.



28
29
30
31
32
# File 'lib/zipkin/tracer.rb', line 28

def initialize(collector, sender, logger: Logger.new(STDOUT))
  @collector = collector
  @sender = sender
  @logger = logger
end

Class Method Details

.build(url:, service_name:, flush_interval: DEFAULT_FLUSH_INTERVAL, logger: Logger.new(STDOUT)) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/zipkin/tracer.rb', line 16

def self.build(url:, service_name:, flush_interval: DEFAULT_FLUSH_INTERVAL, logger: Logger.new(STDOUT))
  collector = Collector.new(Endpoint.local_endpoint(service_name))
  sender = JsonClient.new(
    url: url,
    collector: collector,
    flush_interval: flush_interval,
    logger: logger
  )
  sender.start
  new(collector, sender, logger: logger)
end

Instance Method Details

#extract(format, carrier) ⇒ SpanContext

Extract a SpanContext in the given format from the given carrier.

Parameters:

  • format (OpenTracing::FORMAT_TEXT_MAP, OpenTracing::FORMAT_BINARY, OpenTracing::FORMAT_RACK)
  • carrier (Carrier)

    A carrier object of the type dictated by the specified ‘format`

Returns:

  • (SpanContext)

    the extracted SpanContext or nil if none could be found



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/zipkin/tracer.rb', line 92

def extract(format, carrier)
  case format
  when OpenTracing::FORMAT_TEXT_MAP
    trace_id = carrier['x-b3-traceid']
    parent_id = carrier['x-b3-parentspanid']
    span_id = carrier['x-b3-spanid']
    sampled = carrier['x-b3-sampled'] == '1'

    create_span_context(trace_id, span_id, parent_id, sampled)
  when OpenTracing::FORMAT_RACK
    trace_id = carrier['HTTP_X_B3_TRACEID']
    parent_id = carrier['HTTP_X_B3_PARENTSPANID']
    span_id = carrier['HTTP_X_B3_SPANID']
    sampled = carrier['HTTP_X_B3_SAMPLED'] == '1'

    create_span_context(trace_id, span_id, parent_id, sampled)
  else
    @logger.error "Logasm::Tracer with format #{format} is not supported yet"
    nil
  end
end

#inject(span_context, format, carrier) ⇒ Object

Inject a SpanContext into the given carrier

Parameters:

  • span_context (SpanContext)
  • format (OpenTracing::FORMAT_TEXT_MAP, OpenTracing::FORMAT_BINARY, OpenTracing::FORMAT_RACK)
  • carrier (Carrier)

    A carrier object of the type dictated by the specified ‘format`



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/zipkin/tracer.rb', line 70

def inject(span_context, format, carrier)
  case format
  when OpenTracing::FORMAT_TEXT_MAP
    carrier['x-b3-traceid'] = span_context.trace_id
    carrier['x-b3-parentspanid'] = span_context.parent_id
    carrier['x-b3-spanid'] = span_context.span_id
    carrier['x-b3-sampled'] = span_context.sampled? ? '1' : '0'
  when OpenTracing::FORMAT_RACK
    carrier['X-B3-TraceId'] = span_context.trace_id
    carrier['X-B3-ParentSpanId'] = span_context.parent_id
    carrier['X-B3-SpanId'] = span_context.span_id
    carrier['X-B3-Sampled'] = span_context.sampled? ? '1' : '0'
  else
    @logger.error "Logasm::Tracer with format #{format} is not supported yet"
  end
end

#start_span(operation_name, child_of: nil, start_time: Time.now, tags: {}) ⇒ Span

Starts a new span.

Parameters:

  • operation_name (String)

    The operation name for the Span

  • child_of (SpanContext, Span) (defaults to: nil)

    SpanContext that acts as a parent to the newly-started Span. If a Span instance is provided, its context is automatically substituted.

  • start_time (Time) (defaults to: Time.now)

    When the Span started, if not now

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

    Tags to assign to the Span at start time

Returns:

  • (Span)

    The newly-started Span



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/zipkin/tracer.rb', line 48

def start_span(operation_name, child_of: nil, start_time: Time.now, tags: {}, **)
  context =
    if child_of
      parent_context = child_of.respond_to?(:context) ? child_of.context : child_of
      SpanContext.create_from_parent_context(parent_context)
    else
      SpanContext.create_parent_context
    end
  Span.new(
    context,
    operation_name,
    @collector,
    start_time: start_time,
    tags: tags
  )
end

#stopObject



34
35
36
# File 'lib/zipkin/tracer.rb', line 34

def stop
  @sender.stop
end