class LightStep::Tracer

Constants

DEFAULT_MAX_LOG_RECORDS
DEFAULT_MAX_SPAN_RECORDS
MIN_MAX_LOG_RECORDS
MIN_MAX_SPAN_RECORDS

Attributes

access_token[R]
guid[R]

Public Class Methods

new(component_name:, access_token: nil, transport: nil, tags: {}, propagator: :lightstep) click to toggle source

Initialize a new tracer. Either an access_token or a transport must be provided. A component_name is always required. @param component_name [String] Component name to use for the tracer @param access_token [String] The project access token when pushing to LightStep @param transport [LightStep::Transport::Base] How the data should be transported @param tags [Hash] Tracer-level tags @param propagator [Propagator] Symbol one of :lightstep, :b3 indicating the propagator

to use

@return LightStep::Tracer @raise LightStep::ConfigurationError if the group name or access token is not a valid string.

# File lib/lightstep/tracer.rb, line 35
def initialize(component_name:,
               access_token: nil,
               transport: nil,
               tags: {},
               propagator: :lightstep)
  configure(component_name: component_name,
            access_token: access_token,
            transport: transport,
            tags: tags,
            propagator: propagator)
end

Public Instance Methods

active_span() click to toggle source

Returns the span from the active scope, if any.

@return [Span, nil] the active span. This is a shorthand for

`scope_manager.active.span`, and nil will be returned if
Scope#active is nil.
# File lib/lightstep/tracer.rb, line 144
def active_span
  scope = scope_manager.active
  scope.span if scope
end
disable(discard: true) click to toggle source

Disables the tracer @param discard [Boolean] whether to discard queued data

# File lib/lightstep/tracer.rb, line 226
def disable(discard: true)
  @enabled = false
  @reporter.clear if discard
  @reporter.flush
end
enable() click to toggle source

Enables the tracer

# File lib/lightstep/tracer.rb, line 220
def enable
  @enabled = true
end
enabled?() click to toggle source

@return true if the tracer is enabled

# File lib/lightstep/tracer.rb, line 214
def enabled?
  return @enabled if defined?(@enabled)
  @enabled = true
end
extract(format, carrier) click to toggle source

Extract a SpanContext from a carrier @param format [OpenTracing::FORMAT_TEXT_MAP, OpenTracing::FORMAT_BINARY, OpenTracing::FORMAT_RACK] @param carrier [Carrier] A carrier object of the type dictated by the specified `format` @return [SpanContext] the extracted SpanContext or nil if none could be found

# File lib/lightstep/tracer.rb, line 209
def extract(format, carrier)
  @propagator.extract(format, carrier)
end
finish_span(span) click to toggle source

Internal use only. @private

# File lib/lightstep/tracer.rb, line 240
def finish_span(span)
  return unless enabled?
  @reporter.add_span(span)
end
flush() click to toggle source

Flush to the Transport

# File lib/lightstep/tracer.rb, line 233
def flush
  return unless enabled?
  @reporter.flush
end
inject(span_context, format, carrier) click to toggle source

Inject a SpanContext into the given carrier

@param spancontext [SpanContext] @param format [OpenTracing::FORMAT_TEXT_MAP, OpenTracing::FORMAT_BINARY] @param carrier [Carrier] A carrier object of the type dictated by the specified `format`

# File lib/lightstep/tracer.rb, line 201
def inject(span_context, format, carrier)
  @propagator.inject(span_context, format, carrier)
end
max_log_records() click to toggle source
# File lib/lightstep/tracer.rb, line 47
def max_log_records
  @max_log_records ||= DEFAULT_MAX_LOG_RECORDS
end
max_log_records=(max) click to toggle source
# File lib/lightstep/tracer.rb, line 51
def max_log_records=(max)
  @max_log_records = [MIN_MAX_LOG_RECORDS, max].max
end
max_span_records() click to toggle source
# File lib/lightstep/tracer.rb, line 55
def max_span_records
  @max_span_records ||= DEFAULT_MAX_SPAN_RECORDS
end
max_span_records=(max) click to toggle source
# File lib/lightstep/tracer.rb, line 59
def max_span_records=(max)
  @max_span_records = [MIN_MAX_SPAN_RECORDS, max].max
  @reporter.max_span_records = @max_span_records
end
report_period_seconds=(seconds) click to toggle source

Set the report flushing period. If set to 0, no flushing will be done, you must manually call flush.

# File lib/lightstep/tracer.rb, line 66
def report_period_seconds=(seconds)
  @reporter.period = seconds
end
scope_manager() click to toggle source

Creates a scope manager or returns the already-created one.

@return [ScopeManager] the current ScopeManager, which may be a no-op but

may not be nil.
# File lib/lightstep/tracer.rb, line 76
def scope_manager
  @scope_manager ||= LightStep::ScopeManager.new
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| ... } click to toggle source

Returns a newly started and activated Scope.

If 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_span is invoked.

@param operation_name [String] The operation name for the Span @param child_of [SpanContext, Span] 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.

@param references [Array<Reference>] An array of reference

objects that identify one or more parent SpanContexts.

@param start_time [Time] When the Span started, if not now @param tags [Hash] Tags to assign to the Span at start time @param ignore_active_scope [Boolean] whether to create an implicit

References#CHILD_OF reference to the ScopeManager#active.

@param finish_on_close [Boolean] whether span should automatically be

finished when Scope#close is called

@yield [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.

@return [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
# File lib/lightstep/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)
  if child_of.nil? && references.nil? && !ignore_active_scope
    child_of = active_span
  end

  span = start_span(
    operation_name,
    child_of: child_of,
    references: references,
    start_time: start_time,
    tags: tags,
    ignore_active_scope: ignore_active_scope
  )

  scope_manager.activate(span: span, finish_on_close: finish_on_close).tap do |scope|
    if block_given?
      begin
        return yield scope
      ensure
        scope.close
      end
    end
  end
end
start_span(operation_name, child_of: nil, references: nil, start_time: nil, tags: nil, ignore_active_scope: false) { |span| ... } click to toggle source

Starts a new span.

@param operation_name [String] The operation name for the Span @param child_of [SpanContext] SpanContext that acts as a parent to

the newly-started Span. If a Span instance is provided, its
.span_context is automatically substituted.

@param references [Array<SpanContext>] An array of SpanContexts that

identify any parent SpanContexts of newly-started Span. If Spans
are provided, their .span_context is automatically substituted.

@param start_time [Time] When the Span started, if not now @param tags [Hash] Tags to assign to the Span at start time @param ignore_active_scope [Boolean] whether to create an implicit

References#CHILD_OF reference to the ScopeManager#active.

@yield [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.

@return [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
# File lib/lightstep/tracer.rb, line 169
def start_span(operation_name, child_of: nil, references: nil, start_time: nil, tags: nil, ignore_active_scope: false)
  if child_of.nil? && references.nil? && !ignore_active_scope
    child_of = active_span
  end

  span_options = {
    tracer: self,
    operation_name: operation_name,
    child_of: child_of,
    references: references,
    start_micros: start_time.nil? ? LightStep.micros(Time.now) : LightStep.micros(start_time),
    tags: tags,
    max_log_records: max_log_records,
  }

  Span.new(**span_options).tap do |span|
    if block_given?
      begin
        return yield span
      ensure
        span.finish
      end
    end
  end
end

Protected Instance Methods

configure(component_name:, access_token: nil, transport: nil, tags: {}, propagator: :lightstep) click to toggle source
# File lib/lightstep/tracer.rb, line 247
def configure(component_name:,
              access_token: nil,
              transport: nil, tags: {},
              propagator: :lightstep)

  raise ConfigurationError, "component_name must be a string" unless component_name.is_a?(String)
  raise ConfigurationError, "component_name cannot be blank"  if component_name.empty?

  if transport.nil? and !access_token.nil?
    transport = Transport::HTTPJSON.new(access_token: access_token)
  end

  raise ConfigurationError, "you must provide an access token or a transport" if transport.nil?
  raise ConfigurationError, "#{transport} is not a LightStep transport class" if !(LightStep::Transport::Base === transport)

  @propagator = Propagation[propagator]

  @guid = LightStep.guid

  @reporter = LightStep::Reporter.new(
    max_span_records: max_span_records,
    transport: transport,
    guid: guid,
    component_name: component_name,
    tags: tags
  )
end