Class: LightStep::Span
- Inherits:
-
Object
- Object
- LightStep::Span
- Defined in:
- lib/lightstep/span.rb
Overview
Span represents an OpenTracer Span
See www.opentracing.io for more information.
Instance Attribute Summary collapse
-
#end_micros ⇒ Object
readonly
Internal use only.
-
#operation_name ⇒ Object
Internal use only.
-
#span_context ⇒ Object
readonly
Internal use only.
-
#start_micros ⇒ Object
readonly
Internal use only.
-
#tags ⇒ Object
readonly
Internal use only.
Instance Method Summary collapse
-
#dropped_logs_count ⇒ Object
Internal use only.
-
#finish(end_time: Time.now) ⇒ Object
Finish the Span.
-
#get_baggage_item(key) ⇒ Object
Get a baggage item.
-
#initialize(tracer:, operation_name:, child_of_id: nil, trace_id:, start_micros:, tags: nil, max_log_records:) ⇒ Span
constructor
Creates a new Span.
-
#log(event: nil, timestamp: Time.now, **fields) ⇒ Object
Add a log entry to this span.
-
#logs_count ⇒ Object
Internal use only.
-
#set_baggage(baggage = {}) ⇒ Object
Set all baggage at once.
-
#set_baggage_item(key, value) ⇒ Object
Set a baggage item on the span.
-
#set_tag(key, value) ⇒ Object
Set a tag value on this span it will be encoded with to_s.
-
#to_h ⇒ Object
Hash representation of a span.
Constructor Details
#initialize(tracer:, operation_name:, child_of_id: nil, trace_id:, start_micros:, tags: nil, max_log_records:) ⇒ Span
Creates a new LightStep::Span
not a String it will be encoded with to_s.
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 25 def initialize( tracer:, operation_name:, child_of_id: nil, trace_id:, start_micros:, tags: nil, max_log_records: ) @tags = Concurrent::Hash.new @tags.update() unless .nil? @log_records = Concurrent::Array.new @dropped_logs = Concurrent::AtomicFixnum.new @max_log_records = max_log_records @tracer = tracer self.operation_name = operation_name.to_s self.start_micros = start_micros @span_context = SpanContext.new(id: LightStep.guid, trace_id: trace_id) set_tag(:parent_span_guid, child_of_id) if !child_of_id.nil? end |
Instance Attribute Details
#end_micros ⇒ Object
Internal use only
14 15 16 |
# File 'lib/lightstep/span.rb', line 14 def end_micros @end_micros end |
#operation_name ⇒ Object
Internal use only
14 15 16 |
# File 'lib/lightstep/span.rb', line 14 def operation_name @operation_name end |
#span_context ⇒ Object (readonly)
Internal use only
14 15 16 |
# File 'lib/lightstep/span.rb', line 14 def span_context @span_context end |
#start_micros ⇒ Object
Internal use only
14 15 16 |
# File 'lib/lightstep/span.rb', line 14 def start_micros @start_micros end |
#tags ⇒ Object (readonly)
Internal use only
14 15 16 |
# File 'lib/lightstep/span.rb', line 14 def @tags end |
Instance Method Details
#dropped_logs_count ⇒ Object
Internal use only
145 146 147 |
# File 'lib/lightstep/span.rb', line 145 def dropped_logs_count dropped_logs.value end |
#finish(end_time: Time.now) ⇒ Object
Finish the LightStep::Span
117 118 119 120 121 122 123 |
# File 'lib/lightstep/span.rb', line 117 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
84 85 86 |
# File 'lib/lightstep/span.rb', line 84 def get_baggage_item(key) span_context.baggage[key] end |
#log(event: nil, timestamp: Time.now, **fields) ⇒ Object
Add a log entry to this span
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/lightstep/span.rb', line 92 def log(event: nil, timestamp: Time.now, **fields) return unless tracer.enabled? record = { runtime_guid: tracer.guid, timestamp_micros: LightStep.micros() } 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, being careful not to flood them. end log_records.push(record) if log_records.size > @max_log_records log_records.shift dropped_logs.increment end end |
#logs_count ⇒ Object
Internal use only
151 152 153 |
# File 'lib/lightstep/span.rb', line 151 def logs_count log_records.size end |
#set_baggage(baggage = {}) ⇒ Object
Set all baggage at once. This will reset the baggage to the given param.
73 74 75 76 77 78 79 |
# File 'lib/lightstep/span.rb', line 73 def set_baggage(baggage = {}) @span_context = SpanContext.new( id: span_context.id, trace_id: span_context.trace_id, baggage: baggage ) end |
#set_baggage_item(key, value) ⇒ Object
Set a baggage item on the span
62 63 64 65 66 67 68 69 |
# File 'lib/lightstep/span.rb', line 62 def set_baggage_item(key, value) @span_context = SpanContext.new( id: span_context.id, trace_id: span_context.trace_id, baggage: span_context.baggage.merge({key => value}) ) self end |
#set_tag(key, value) ⇒ Object
Set a tag value on this span it will be encoded with to_s
51 52 53 54 |
# File 'lib/lightstep/span.rb', line 51 def set_tag(key, value) [key] = value.to_s self end |
#to_h ⇒ Object
Hash representation of a span
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/lightstep/span.rb', line 126 def to_h { runtime_guid: tracer.guid, span_guid: span_context.id, trace_guid: span_context.trace_id, span_name: operation_name, attributes: .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 |