Class: LightStep::Span

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

Overview

Span represents an OpenTracer Span

See www.opentracing.io for more information.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tracer:, operation_name:, child_of_guid: nil, trace_guid:, start_micros:, tags: nil, max_log_records:) ⇒ Span

Creates a new LightStep::Span

Parameters:

  • tracer (Tracer)

    the tracer that created this span

  • operation_name (String)

    the operation name of this span

  • child_of_guid (String) (defaults to: nil)

    the guid of the span this span is a child of

  • trace_guid (String)

    the guid of this span’s trace

  • start_micros (Numeric)

    start time of the span in microseconds



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lightstep/span.rb', line 23

def initialize(
  tracer:,
  operation_name:,
  child_of_guid: nil,
  trace_guid:,
  start_micros:,
  tags: nil,
  max_log_records:
)
  @tags = Concurrent::Hash.new
  @tags.update(tags) unless tags.nil?
  @baggage = Concurrent::Hash.new
  @log_records = Concurrent::Array.new
  @dropped_logs = Concurrent::AtomicFixnum.new
  @max_log_records = max_log_records

  @tracer = tracer
  @guid = LightStep.guid
  self.operation_name = operation_name
  self.start_micros = start_micros
  self.trace_guid = trace_guid
  set_tag(:parent_span_guid, child_of_guid) if !child_of_guid.nil?
end

Instance Attribute Details

#baggageObject (readonly)

Internal use only



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

def baggage
  @baggage
end

#end_microsObject

Internal use only



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

def end_micros
  @end_micros
end

#guidObject

Internal use only



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

def guid
  @guid
end

#operation_nameObject

Internal use only



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

def operation_name
  @operation_name
end

#start_microsObject

Internal use only



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

def start_micros
  @start_micros
end

#tagsObject (readonly)

Internal use only



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

def tags
  @tags
end

#trace_guidObject

Internal use only



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

def trace_guid
  @trace_guid
end

Instance Method Details

#dropped_logs_countObject

Internal use only



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

def dropped_logs_count
  dropped_logs.value
end

#finish(end_time: Time.now) ⇒ Object

Finish the LightStep::Span

Parameters:

  • end_time (Time) (defaults to: Time.now)

    custom end time, if not now



108
109
110
111
112
113
114
# File 'lib/lightstep/span.rb', line 108

def finish(end_time: Time.now)
  if end_micros.nil?
    self.end_micros = LightStep.micros(end_time)
  end
  tracer.finish_span(self)
  self
end

#get_baggage_item(key) ⇒ Object

Get a baggage item

Parameters:

  • key (String)

    the key of the baggage item

Returns:

  • Value of the baggage item



75
76
77
# File 'lib/lightstep/span.rb', line 75

def get_baggage_item(key)
  baggage[key]
end

#log(event: nil, timestamp: Time.now, **fields) ⇒ Object

Add a log entry to this span

Parameters:

  • event (String) (defaults to: nil)

    event name for the log

  • timestamp (Time) (defaults to: Time.now)

    time of the log

  • fields (Hash)

    Additional information to log



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/lightstep/span.rb', line 83

def log(event: nil, timestamp: Time.now, **fields)
  return unless tracer.enabled?

  record = {
    runtime_guid: tracer.guid,
    timestamp_micros: LightStep.micros(timestamp)
  }
  record[:stable_name] = event.to_s if !event.nil?

  begin
    record[:payload_json] = JSON.generate(fields, max_nesting: 8)
  rescue
    # TODO: failure to encode a payload as JSON should be recorded in the
    # internal library logs, with catioun not flooding the internal logs.
  end

  log_records.push(record)
  if log_records.size > @max_log_records
    log_records.shift
    dropped_logs.increment
  end
end

#logs_countObject

Internal use only



142
143
144
# File 'lib/lightstep/span.rb', line 142

def logs_count
  log_records.size
end

#set_baggage_item(key, value) ⇒ Object

Set a baggage item on the span

Parameters:

  • key (String)

    the key of the baggage item

  • value (String)

    the value of the baggage item



67
68
69
70
# File 'lib/lightstep/span.rb', line 67

def set_baggage_item(key, value)
  baggage[key] = value
  self
end

#set_tag(key, value) ⇒ Object

Set a tag value on this span a String, Numeric, or Boolean it will be encoded with to_s

Parameters:

  • key (String)

    the key of the tag

  • value (String, Numeric, Boolean)

    the value of the tag. If it’s not



51
52
53
54
55
56
57
58
59
# File 'lib/lightstep/span.rb', line 51

def set_tag(key, value)
  case value
  when String, Fixnum, TrueClass, FalseClass
    tags[key] = value
  else
    tags[key] = value.to_s
  end
  self
end

#to_hObject

Hash representation of a span



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/lightstep/span.rb', line 117

def to_h
  {
    runtime_guid: tracer.guid,
    span_guid: guid,
    trace_guid: trace_guid,
    span_name: operation_name,
    attributes: tags.map {|key, value|
      {Key: key.to_s, Value: value}
    },
    oldest_micros: start_micros,
    youngest_micros: end_micros,
    error_flag: false,
    dropped_logs: dropped_logs_count,
    log_records: log_records
  }
end