Class: OpenTracingTestTracer

Inherits:
Object
  • Object
show all
Defined in:
lib/opentracing_test_tracer.rb,
lib/opentracing_test_tracer/span.rb,
lib/opentracing_test_tracer/scope.rb,
lib/opentracing_test_tracer/trace_id.rb,
lib/opentracing_test_tracer/span_context.rb,
lib/opentracing_test_tracer/scope_manager.rb,
lib/opentracing_test_tracer/scope_manager/scope_stack.rb,
lib/opentracing_test_tracer/scope_manager/scope_identifier.rb

Defined Under Namespace

Modules: TraceId Classes: Scope, ScopeManager, Span, SpanContext

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger:) ⇒ OpenTracingTestTracer

Returns a new instance of OpenTracingTestTracer.



17
18
19
20
21
22
# File 'lib/opentracing_test_tracer.rb', line 17

def initialize(logger:)
  @logger = logger
  @scope_manager = ScopeManager.new
  @spans = []
  @finished_spans = []
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.



29
30
31
# File 'lib/opentracing_test_tracer.rb', line 29

def scope_manager
  @scope_manager
end

#spansObject (readonly)

NOTE: This is not OT compliant. This is meant only for testing.



25
26
27
# File 'lib/opentracing_test_tracer.rb', line 25

def spans
  @spans
end

Class Method Details

.build(logger: Logger.new(STDOUT)) ⇒ Object



13
14
15
# File 'lib/opentracing_test_tracer.rb', line 13

def self.build(logger: Logger.new(STDOUT))
  new(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.



34
35
36
37
# File 'lib/opentracing_test_tracer.rb', line 34

def active_span
  scope = scope_manager.active
  scope&.span
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



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/opentracing_test_tracer.rb', line 158

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

    create_span_context(trace_id, span_id, parent_id)
  when OpenTracing::FORMAT_RACK
    trace_id = carrier['HTTP_TEST_TRACEID']
    parent_id = carrier['HTTP_TEST_PARENTSPANID']
    span_id = carrier['HTTP_TEST_SPANID']

    create_span_context(trace_id, span_id, parent_id)
  else
    @logger.error "#{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



142
143
144
145
146
147
148
149
150
151
# File 'lib/opentracing_test_tracer.rb', line 142

def inject(span_context, format, carrier)
  case format
  when OpenTracing::FORMAT_TEXT_MAP, OpenTracing::FORMAT_RACK
    carrier['test-traceid'] = span_context.trace_id
    carrier['test-parentspanid'] = span_context.parent_id
    carrier['test-spanid'] = span_context.span_id
  else
    @logger.error "#{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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/opentracing_test_tracer.rb', line 108

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.

  • 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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/opentracing_test_tracer.rb', line 59

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 = Span.new(
    context: context,
    operation_name: operation_name,
    start_time: start_time,
    references: references,
    tags: tags
  )
  @spans << span
  span
end