Class: Jaeger::Tracer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reporter:, sampler:, injectors:, extractors:) ⇒ Tracer

Returns a new instance of Tracer.



5
6
7
8
9
10
11
# File 'lib/jaeger/tracer.rb', line 5

def initialize(reporter:, sampler:, injectors:, extractors:)
  @reporter = reporter
  @sampler = sampler
  @injectors = injectors
  @extractors = extractors
  @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.



15
16
17
# File 'lib/jaeger/tracer.rb', line 15

def scope_manager
  @scope_manager
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.



20
21
22
23
# File 'lib/jaeger/tracer.rb', line 20

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



157
158
159
160
161
162
163
164
# File 'lib/jaeger/tracer.rb', line 157

def extract(format, carrier)
  @extractors
    .fetch(format)
    .lazy
    .map { |extractor| extractor.extract(carrier) }
    .reject(&:nil?)
    .first
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`



146
147
148
149
150
# File 'lib/jaeger/tracer.rb', line 146

def inject(span_context, format, carrier)
  @injectors.fetch(format).each do |injector|
    injector.inject(span_context, carrier)
  end
end

#start_active_span(operation_name, child_of: nil, references: nil, start_time: Time.now, tags: {}, ignore_active_scope: false, finish_on_close: true) {|Scope| ... } ⇒ Scope, Object

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: {})

    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_span 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, Object)

    If passed an optional block, start_active_span returns the block’s return value, otherwise it returns the newly-started and activated Scope



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/jaeger/tracer.rb', line 112

def start_active_span(operation_name,
                      child_of: nil,
                      references: nil,
                      start_time: Time.now,
                      tags: {},
                      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
  else
    scope
  end
end

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

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.

  • 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

  • ignore_active_scope (Boolean) (defaults to: false)

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

Yields:

  • (Span)

    If passed an optional block, start_span will yield the newly-created span to the block. The span will be finished automatically after the block is executed.

Returns:

  • (Span, Object)

    If passed an optional block, start_span will return the block’s return value, otherwise it returns the newly-started Span instance, which has not been automatically registered via the ScopeManager



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/jaeger/tracer.rb', line 51

def start_span(operation_name,
               child_of: nil,
               references: nil,
               start_time: Time.now,
               tags: {},
               ignore_active_scope: false,
               **)
  context, sampler_tags = prepare_span_context(
    operation_name: operation_name,
    child_of: child_of,
    references: references,
    ignore_active_scope: ignore_active_scope
  )
  span = Span.new(
    context,
    operation_name,
    @reporter,
    start_time: start_time,
    references: references,
    tags: tags.merge(sampler_tags)
  )

  if block_given?
    begin
      yield(span)
    ensure
      span.finish
    end
  else
    span
  end
end