Class: LightStep::Tracer

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

Direct Known Subclasses

GlobalTracer

Defined Under Namespace

Classes: ConfigurationError, Error

Constant Summary collapse

FORMAT_TEXT_MAP =
1
FORMAT_BINARY =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(component_name:, access_token: nil, transport: nil) ⇒ Object

Initialize a new tracer. Either an access_token or a transport must be provided. A component_name is always required.

Parameters:

  • component_name (String)

    Component name to use for the tracer

  • access_token (String) (defaults to: nil)

    The project access token when pushing to LightStep

  • transport (LightStep::Transport) (defaults to: nil)

    How the data should be transported

Raises:

  • LightStep::ConfigurationError if the group name or access token is not a valid string.



27
28
29
# File 'lib/lightstep/tracer.rb', line 27

def initialize(component_name:, access_token: nil, transport: nil)
  configure(component_name: component_name, access_token: access_token, transport: transport)
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



18
19
20
# File 'lib/lightstep/tracer.rb', line 18

def access_token
  @access_token
end

#guidObject (readonly)

Returns the value of attribute guid.



18
19
20
# File 'lib/lightstep/tracer.rb', line 18

def guid
  @guid
end

Instance Method Details

#disable(discard: true) ⇒ Object

Disables the tracer

Parameters:

  • discard (Boolean) (defaults to: true)

    whether to discard queued data



129
130
131
132
133
# File 'lib/lightstep/tracer.rb', line 129

def disable(discard: true)
  @enabled = false
  @reporter.clear if discard
  @reporter.flush
end

#enableObject

Enables the tracer



123
124
125
# File 'lib/lightstep/tracer.rb', line 123

def enable
  @enabled = true
end

#enabled?Boolean

Returns true if the tracer is enabled.

Returns:

  • (Boolean)

    true if the tracer is enabled



117
118
119
120
# File 'lib/lightstep/tracer.rb', line 117

def enabled?
  return @enabled if defined?(@enabled)
  @enabled = true
end

#extract(operation_name, format, carrier) ⇒ Span

Extract a span from a carrier

Parameters:

Returns:



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/lightstep/tracer.rb', line 103

def extract(operation_name, format, carrier)
  case format
  when LightStep::Tracer::FORMAT_TEXT_MAP
    extract_from_text_map(operation_name, carrier)
  when LightStep::Tracer::FORMAT_BINARY
    warn 'Binary join format not yet implemented'
    nil
  else
    warn 'Unknown join format'
    nil
  end
end

#finish_span(span) ⇒ Object

Internal use only.



143
144
145
146
# File 'lib/lightstep/tracer.rb', line 143

def finish_span(span)
  return unless enabled?
  @reporter.add_span(span)
end

#flushObject

Flush to the Transport



136
137
138
139
# File 'lib/lightstep/tracer.rb', line 136

def flush
  return unless enabled?
  @reporter.flush
end

#inject(span, format, carrier) ⇒ Object

Inject a span into the given carrier

Parameters:



87
88
89
90
91
92
93
94
95
96
# File 'lib/lightstep/tracer.rb', line 87

def inject(span, format, carrier)
  case format
  when LightStep::Tracer::FORMAT_TEXT_MAP
    inject_to_text_map(span, carrier)
  when LightStep::Tracer::FORMAT_BINARY
    warn 'Binary inject format not yet implemented'
  else
    warn 'Unknown inject format'
  end
end

#max_log_recordsObject



31
32
33
# File 'lib/lightstep/tracer.rb', line 31

def max_log_records
  @max_log_records ||= DEFAULT_MAX_LOG_RECORDS
end

#max_log_records=(max) ⇒ Object



35
36
37
# File 'lib/lightstep/tracer.rb', line 35

def max_log_records=(max)
  @max_log_records = [MIN_MAX_LOG_RECORDS, max].max
end

#max_span_recordsObject



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

def max_span_records
  @max_span_records ||= DEFAULT_MAX_SPAN_RECORDS
end

#max_span_records=(max) ⇒ Object



43
44
45
46
# File 'lib/lightstep/tracer.rb', line 43

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) ⇒ Object

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



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

def report_period_seconds=(seconds)
  @reporter.period = seconds
end

#start_span(operation_name, child_of: nil, start_time: nil, tags: nil) ⇒ Span

Starts a new span.

Parameters:

  • operation_name (String)

    the operation name for the Span

  • child_of (Span) (defaults to: nil)

    Span to inherit from

  • start_time (Time) (defaults to: nil)

    When the Span started, if not now

  • tags (Hash) (defaults to: nil)

    tags for the span

Returns:



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

def start_span(operation_name, child_of: nil, start_time: nil, tags: nil)
  child_of_guid = nil
  trace_guid = nil
  if Span === child_of
    child_of_guid = child_of.guid
    trace_guid = child_of.trace_guid
  else
    trace_guid = LightStep.guid
  end

  Span.new(
    tracer: self,
    operation_name: operation_name,
    child_of_guid: child_of_guid,
    trace_guid: trace_guid,
    start_micros: start_time.nil? ? LightStep.micros(Time.now) : LightStep.micros(start_time),
    tags: tags,
    max_log_records: max_log_records
  )
end