Class: SplunkTracing::Span
- Inherits:
-
Object
- Object
- SplunkTracing::Span
- Defined in:
- lib/splunktracing/span.rb
Overview
Span represents an OpenTracer Span
See www.opentracing.io for more information.
Instance Attribute Summary collapse
-
#context ⇒ Object
(also: #span_context)
readonly
Internal use only.
-
#end_micros ⇒ Object
readonly
Internal use only.
-
#operation_name ⇒ Object
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: nil, references: [], start_micros:, tags: nil, max_log_records:) ⇒ Span
constructor
Creates a new Span.
-
#log(event: nil, timestamp: Time.now, **fields) ⇒ Object
deprecated
Deprecated.
Use #log_kv instead.
- #log_kv(timestamp: Time.now, **fields) ⇒ Object
-
#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: nil, references: [], start_micros:, tags: nil, max_log_records:) ⇒ Span
Creates a new SplunkTracing::Span
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 |
# File 'lib/splunktracing/span.rb', line 34 def initialize( tracer:, operation_name:, child_of: nil, references: [], start_micros:, tags: nil, max_log_records: ) @tags = Concurrent::Hash.new @tags.update(.each { |k, v| [k] = v.to_s }) 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 ref = child_of ? child_of : references ref = ref[0] if (Array === ref) ref = ref.context if (Span === ref) if SpanContext === ref @context = SpanContext.new(id: SplunkTracing.guid, trace_id: ref.trace_id, parent_id: ref.id) set_baggage(ref.baggage) # set_tag(:parent_span_guid, ref.id) else @context = SpanContext.new(id: SplunkTracing.guid, trace_id: SplunkTracing.guid) end end |
Instance Attribute Details
#context ⇒ Object (readonly) Also known as: span_context
Internal use only
14 15 16 |
# File 'lib/splunktracing/span.rb', line 14 def context @context end |
#end_micros ⇒ Object
Internal use only
14 15 16 |
# File 'lib/splunktracing/span.rb', line 14 def end_micros @end_micros end |
#operation_name ⇒ Object
Internal use only
14 15 16 |
# File 'lib/splunktracing/span.rb', line 14 def operation_name @operation_name end |
#start_micros ⇒ Object
Internal use only
14 15 16 |
# File 'lib/splunktracing/span.rb', line 14 def start_micros @start_micros end |
#tags ⇒ Object (readonly)
Internal use only
14 15 16 |
# File 'lib/splunktracing/span.rb', line 14 def @tags end |
Instance Method Details
#dropped_logs_count ⇒ Object
Internal use only
176 177 178 |
# File 'lib/splunktracing/span.rb', line 176 def dropped_logs_count dropped_logs.value end |
#finish(end_time: Time.now) ⇒ Object
Finish the SplunkTracing::Span
145 146 147 148 149 150 151 |
# File 'lib/splunktracing/span.rb', line 145 def finish(end_time: Time.now) if end_micros.nil? self.end_micros = SplunkTracing.micros(end_time) end tracer.finish_span(self) self end |
#get_baggage_item(key) ⇒ Object
Get a baggage item
106 107 108 |
# File 'lib/splunktracing/span.rb', line 106 def get_baggage_item(key) context.baggage[key] end |
#log(event: nil, timestamp: Time.now, **fields) ⇒ Object
Use #log_kv instead.
Add a log entry to this span
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/splunktracing/span.rb', line 115 def log(event: nil, timestamp: Time.now, **fields) warn 'Span#log is deprecated. Please use Span#log_kv instead.' return unless tracer.enabled? fields = {} if fields.nil? unless event.nil? fields[:event] = event.to_s end log_kv(timestamp: , **fields) end |
#log_kv(timestamp: Time.now, **fields) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/splunktracing/span.rb', line 127 def log_kv(timestamp: Time.now, **fields) return unless tracer.enabled? fields = {} if fields.nil? record = { timestamp_micros: SplunkTracing.micros(), fields: fields, } 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
182 183 184 |
# File 'lib/splunktracing/span.rb', line 182 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.
94 95 96 97 98 99 100 101 |
# File 'lib/splunktracing/span.rb', line 94 def set_baggage(baggage = {}) @context = SpanContext.new( id: context.id, trace_id: context.trace_id, parent_id: context.parent_id, baggage: baggage ) end |
#set_baggage_item(key, value) ⇒ Object
Set a baggage item on the span
82 83 84 85 86 87 88 89 90 |
# File 'lib/splunktracing/span.rb', line 82 def set_baggage_item(key, value) @context = SpanContext.new( id: context.id, trace_id: context.trace_id, parent_id: context.parent_id, baggage: 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
71 72 73 74 |
# File 'lib/splunktracing/span.rb', line 71 def set_tag(key, value) [key] = value.to_s self end |
#to_h ⇒ Object
Hash representation of a span
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/splunktracing/span.rb', line 154 def to_h if end_micros.nil? self.end_micros = SplunkTracing.micros(Time.now) end { guid: tracer.guid, span_id: context.id, trace_id: context.trace_id, parent_span_id: context.parent_id, operation_name: operation_name, tags: , timestamp: start_micros/1000000.0, duration: end_micros-start_micros, error_flag: false, dropped_logs: dropped_logs_count, log_records: log_records, baggage: context.baggage } end |