Class: Zipkin::Tracer

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

Constant Summary collapse

DEFAULT_FLUSH_INTERVAL =
10

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Tracer.



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

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

Instance Attribute Details

#scope_managerScopeManager (readonly)

Returns the current ScopeManager, which may be a no-op but may not be nil.

Returns:

  • (ScopeManager)

    the current ScopeManager, which may be a no-op but may not be nil.



45
46
47
# File 'lib/zipkin/tracer.rb', line 45

def scope_manager
  @scope_manager
end

Class Method Details

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



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/zipkin/tracer.rb', line 20

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

#active_spanSpan?

Returns the active span. This is a shorthand for ‘scope_manager.active.span`, and nil will be returned if Scope#active is nil.

Returns:

  • (Span, nil)

    the active span. This is a shorthand for ‘scope_manager.active.span`, and nil will be returned if Scope#active is nil.



50
51
52
53
# File 'lib/zipkin/tracer.rb', line 50

def active_span
  scope = scope_manager.active
  scope.span if scope
end

#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



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/zipkin/tracer.rb', line 179

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`



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/zipkin/tracer.rb', line 157

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_active_span(operation_name, child_of: nil, references: nil, start_time: Time.now, tags: nil, ignore_active_scope: false, finish_on_close: true) {|Scope| ... } ⇒ Scope

Creates a newly started and activated Scope

If the Tracer’s ScopeManager#active is not nil, no explicit references are provided, and ‘ignore_active_scope` is false, then an inferred References#CHILD_OF reference is created to the ScopeManager#active’s SpanContext when start_active is invoked.

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. See [Reference] for more information.

    If specified, the ‘references` parameter must be omitted.

  • references (Array<Reference>) (defaults to: nil)

    An array of reference objects that identify one or more parent SpanContexts.

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

    When the Span started, if not now

  • tags (Hash) (defaults to: nil)

    Tags to assign to the Span at start time

  • ignore_active_scope (Boolean) (defaults to: false)

    whether to create an implicit References#CHILD_OF reference to the ScopeManager#active.

  • finish_on_close (Boolean) (defaults to: true)

    whether span should automatically be finished when Scope#close is called

Yields:

  • (Scope)

    If an optional block is passed to start_active it will yield the newly-started Scope. If ‘finish_on_close` is true then the Span will be finished automatically after the block is executed.

Returns:

  • (Scope)

    The newly-started and activated Scope



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/zipkin/tracer.rb', line 123

def start_active_span(operation_name,
                      child_of: nil,
                      references: nil,
                      start_time: Time.now,
                      tags: nil,
                      ignore_active_scope: false,
                      finish_on_close: true,
                      **)
  span = start_span(
    operation_name,
    child_of: child_of,
    references: references,
    start_time: start_time,
    tags: tags,
    ignore_active_scope: ignore_active_scope
  )
  scope = @scope_manager.activate(span, finish_on_close: finish_on_close)

  if block_given?
    begin
      yield scope
    ensure
      scope.close
    end
  end

  scope
end

#start_span(operation_name, child_of: nil, start_time: Time.now, tags: nil, references: nil, ignore_active_scope: false) ⇒ Span

Starts a new span

This is similar to #start_active_span, but the returned Span will not be registered via the ScopeManager.

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. See [Reference] for more information.

    If specified, the ‘references` parameter must be omitted.

  • references (Array<Reference>) (defaults to: nil)

    An array of reference objects that identify one or more parent SpanContexts.<Paste>

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

    When the Span started, if not now

  • tags (Hash) (defaults to: nil)

    Tags to assign to the Span at start time

  • ignore_active_scope (Boolean) (defaults to: false)

    whether to create an implicit References#CHILD_OF reference to the ScopeManager#active.

Returns:

  • (Span)

    The newly-started Span



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/zipkin/tracer.rb', line 75

def start_span(operation_name,
               child_of: nil,
               start_time: Time.now,
               tags: nil,
               references: nil,
               ignore_active_scope: false,
               **)
  context = prepare_span_context(
    child_of: child_of,
    references: references,
    ignore_active_scope: ignore_active_scope
  )
  Span.new(
    context,
    operation_name,
    @collector,
    start_time: start_time,
    references: references,
    tags: tags || {}
  )
end

#stopObject



39
40
41
# File 'lib/zipkin/tracer.rb', line 39

def stop
  @sender.stop
end