Class: Bigcommerce::Lightstep::Tracer

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/bigcommerce/lightstep/tracer.rb

Overview

Global tracer

Instance Method Summary collapse

Instance Method Details

#active_span::LightStep::Span|NilClass

Return the active span

Returns:

  • (::LightStep::Span|NilClass)


88
89
90
# File 'lib/bigcommerce/lightstep/tracer.rb', line 88

def active_span
  Thread.current[:lightstep_active_span]
end

#clear_active_span!Object

Clear the active span



95
96
97
# File 'lib/bigcommerce/lightstep/tracer.rb', line 95

def clear_active_span!
  Thread.current[:lightstep_active_span] = nil
end

#enabled?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/bigcommerce/lightstep/tracer.rb', line 109

def enabled?
  Bigcommerce::Lightstep.enabled
end

#reporter_initialized?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/bigcommerce/lightstep/tracer.rb', line 102

def reporter_initialized?
  tracer.instance_variable_defined?(:@reporter) && !tracer.instance_variable_get(:@reporter).nil?
end

#start_span(name, context: nil, start_time: nil, tags: nil) ⇒ Object

Start a new span

Parameters:

  • name (String)

    The operation name for the Span

  • context (Hash|::LightStep::SpanContext) (defaults to: nil)

    (Optional)

  • start_time (Time) (defaults to: nil)

    (Optional)

  • tags (Hash) (defaults to: nil)

    (Optional)



34
35
36
37
38
39
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/bigcommerce/lightstep/tracer.rb', line 34

def start_span(name, context: nil, start_time: nil, tags: nil)
  if enabled?
    # enable the tracer (for fork support)
    tracer.enable
  elsif tracer&.enabled?
    # We are not enabled and the tracer is currently on
    # https://github.com/lightstep/lightstep-tracer-ruby/blob/master/lib/lightstep/tracer.rb#L129-L130
    # we have to set this through instance_variable_set because of a bug in the core lightstep gem which
    # assumes the presence of a reporter, which happens in the initializer, which may not be called
    # because the reporter attempts to flush spans on initialization (which is bad if lightstep isn't
    # present)
    tracer.instance_variable_set(:@enabled, false)
  end

  # find the currently active span
  last_active_span = active_span

  # determine who is the actual parent span
  current_parent = determine_parent(context: context)

  # create new span
  span = ::LightStep.start_span(name, child_of: current_parent, start_time: start_time, tags: tags)

  mark_root_span(span) if active_span.nil?

  # set it as the active span
  self.active_span = span

  # run the process
  result = nil
  begin
    build_context.intercept(span) do |inner_span|
      result = yield inner_span
    end
  rescue StandardError
    span.set_tag('error', true) unless span.tags.key?('error')
    raise
  ensure
    # finish this span if the reporter is initialized
    span.finish if enabled? && reporter_initialized?

    # now set back the parent as the active span
    self.active_span = last_active_span
  end

  # return result
  result
end