Class: Instana::Span
- Inherits:
-
Object
- Object
- Instana::Span
- Defined in:
- lib/instana/tracing/span.rb
Constant Summary collapse
- REGISTERED_SPANS =
[ :actioncontroller, :actionview, :activerecord, :excon, :memcache, :'net-http', :rack, :render ].freeze
- ENTRY_SPANS =
[ :rack ].freeze
- EXIT_SPANS =
[ :'net-http', :excon, :activerecord ].freeze
- HTTP_SPANS =
ENTRY_SPANS + EXIT_SPANS
Instance Attribute Summary collapse
-
#baggage ⇒ Object
Returns the value of attribute baggage.
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Hash accessor to the internal @data hash.
-
#[]=(key, value) ⇒ Object
Hash setter to the internal @data hash.
-
#add_error(e) ⇒ Object
Log an error into the span.
-
#add_stack(limit: nil, stack: Kernel.caller) ⇒ Object
Adds a backtrace to this span.
-
#close(end_time = Time.now) ⇒ Span
Closes out the span.
-
#configure_custom(name) ⇒ Object
Configure this span to be a custom span per the SDK generic span type.
-
#context ⇒ Instana::SpanContext
Retrieve the context of this span.
-
#custom? ⇒ Boolean
Indicates whether this span is a custom or registered Span.
-
#duration ⇒ Integer
Get the duration value for this Span.
-
#finish(end_time = Time.now) ⇒ Object
Finish the Span Spec: OpenTracing API.
-
#get_baggage_item(key) ⇒ Object
Get a baggage item Spec: OpenTracing API.
-
#id ⇒ Integer
Retrieve the ID for this span.
-
#initialize(name, trace_id, parent_id: nil, start_time: Time.now) ⇒ Span
constructor
A new instance of Span.
-
#is_root? ⇒ Boolean
Indicates whether this span in the root span in the Trace.
-
#key?(k) ⇒ Boolean
Hash key query to the internal @data hash.
-
#log(event = nil, _timestamp = Time.now, **fields) ⇒ Object
Add a log entry to this span Spec: OpenTracing API.
-
#name ⇒ String
Get the name (operation) of this Span.
-
#name=(n) ⇒ Object
Set the name (operation) for this Span.
-
#operation_name=(name) ⇒ Object
Set the name of the operation Spec: OpenTracing API.
-
#parent_id ⇒ Integer
Retrieve the parent ID of this span.
-
#parent_id=(id) ⇒ Integer
Set the parent ID of this span.
-
#raw ⇒ Object
Get the raw @data hash that summarizes this span.
-
#set_baggage_item(key, value) ⇒ Object
Set a baggage item on the span Spec: OpenTracing API.
-
#set_tag(key, value) ⇒ Object
Set a tag value on this span Spec: OpenTracing API.
-
#set_tags(tags) ⇒ Span
Helper method to add multiple tags to this span.
-
#tags(key = nil) ⇒ Object
Retrieve the hash of tags for this span.
-
#trace_id ⇒ Integer
Retrieve the Trace ID for this span.
Constructor Details
#initialize(name, trace_id, parent_id: nil, start_time: Time.now) ⇒ Span
Returns a new instance of Span.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/instana/tracing/span.rb', line 11 def initialize(name, trace_id, parent_id: nil, start_time: Time.now) @data = {} @data[:t] = trace_id # Trace ID @data[:s] = ::Instana::Util.generate_id # Span ID @data[:p] = parent_id if parent_id # Parent ID @data[:ta] = :ruby # Agent @data[:data] = {} # Entity Source @data[:f] = { :e => ::Instana.agent.report_pid, :h => ::Instana.agent.agent_uuid } # Start time @data[:ts] = ::Instana::Util.time_to_ms(start_time) @baggage = {} # For entry spans, add a backtrace fingerprint add_stack(limit: 2) if ENTRY_SPANS.include?(name) # Attach a backtrace to all exit spans add_stack if EXIT_SPANS.include?(name) # Check for custom tracing if REGISTERED_SPANS.include?(name.to_sym) @data[:n] = name.to_sym else configure_custom(name) end end |
Instance Attribute Details
#baggage ⇒ Object
Returns the value of attribute baggage.
9 10 11 |
# File 'lib/instana/tracing/span.rb', line 9 def baggage @baggage end |
#parent ⇒ Object
Returns the value of attribute parent.
8 9 10 |
# File 'lib/instana/tracing/span.rb', line 8 def parent @parent end |
Instance Method Details
#[](key) ⇒ Object
Hash accessor to the internal @data hash
223 224 225 |
# File 'lib/instana/tracing/span.rb', line 223 def [](key) @data[key.to_sym] end |
#[]=(key, value) ⇒ Object
Hash setter to the internal @data hash
229 230 231 |
# File 'lib/instana/tracing/span.rb', line 229 def []=(key, value) @data[key.to_sym] = value end |
#add_error(e) ⇒ Object
Log an error into the span
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/instana/tracing/span.rb', line 73 def add_error(e) @data[:error] = true if @data.key?(:ec) @data[:ec] = @data[:ec] + 1 else @data[:ec] = 1 end # If a valid exception has been passed in, log the information about it # In case of just logging an error for things such as HTTP client 5xx # responses, an exception/backtrace may not exist. if e if e.backtrace.is_a?(Array) add_stack(stack: e.backtrace) end if HTTP_SPANS.include?(@data[:n]) (:http => { :error => "#{e.class}: #{e.}" }) else (:log => { :message => e., :parameters => e.class.to_s }) end e.instance_variable_set(:@instana_logged, true) end self end |
#add_stack(limit: nil, stack: Kernel.caller) ⇒ Object
Adds a backtrace to this span
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/instana/tracing/span.rb', line 45 def add_stack(limit: nil, stack: Kernel.caller) frame_count = 0 @data[:stack] = [] stack.each do |i| # If the stack has the full instana gem version in it's path # then don't include that frame. Also don't exclude the Rack module. if !i.match(/instana\/instrumentation\/rack.rb/).nil? || (i.match(::Instana::VERSION_FULL).nil? && i.match('lib/instana/').nil?) break if limit && frame_count >= limit x = i.split(':') @data[:stack] << { :f => x[0], :n => x[1], :m => x[2] } frame_count = frame_count + 1 if limit end end end |
#close(end_time = Time.now) ⇒ Span
Closes out the span. This difference between this and the finish method tells us how the tracing is being performed (with OpenTracing or Instana default)
133 134 135 136 137 138 139 140 |
# File 'lib/instana/tracing/span.rb', line 133 def close(end_time = Time.now) unless end_time.is_a?(Time) ::Instana.logger.debug "span.close: Passed #{end_time.class} instead of Time class" end @data[:d] = (::Instana::Util.time_to_ms(end_time) - @data[:ts]) self end |
#configure_custom(name) ⇒ Object
Configure this span to be a custom span per the SDK generic span type.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/instana/tracing/span.rb', line 107 def configure_custom(name) @data[:n] = :sdk @data[:data] = { :sdk => { :name => name.to_sym } } #if kvs.is_a?(Hash) # @data[:data][:sdk][:type] = kvs.key?(:type) ? kvs[:type] : :local # # if kvs.key?(:arguments) # @data[:data][:sdk][:arguments] = kvs[:arguments] # end # # if kvs.key?(:return) # @data[:data][:sdk][:return] = kvs[:return] # end # @data[:data][:sdk][:custom] = kvs unless kvs.empty? # end self end |
#context ⇒ Instana::SpanContext
Retrieve the context of this span.
150 151 152 |
# File 'lib/instana/tracing/span.rb', line 150 def context @context ||= ::Instana::SpanContext.new(@data[:t], @data[:s], @baggage) end |
#custom? ⇒ Boolean
Indicates whether this span is a custom or registered Span
246 247 248 |
# File 'lib/instana/tracing/span.rb', line 246 def custom? @data[:n] == :sdk end |
#duration ⇒ Integer
Get the duration value for this Span
208 209 210 |
# File 'lib/instana/tracing/span.rb', line 208 def duration @data[:d] end |
#finish(end_time = Time.now) ⇒ Object
Finish the Instana::Span Spec: OpenTracing API
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/instana/tracing/span.rb', line 354 def finish(end_time = Time.now) unless end_time.is_a?(Time) ::Instana.logger.debug "span.finish: Passed #{end_time.class} instead of Time class" end if ::Instana.tracer.current_span.id != id ::Instana.logger.tracing "Closing a span that isn't active. This will result in a broken trace: #{self.inspect}" end if is_root? # This is the root span for the trace. Call log_end to close # out and queue the trace ::Instana.tracer.log_end(name, {}, end_time) else ::Instana.tracer.current_trace.end_span({}, end_time) end self end |
#get_baggage_item(key) ⇒ Object
Get a baggage item Spec: OpenTracing API
323 324 325 |
# File 'lib/instana/tracing/span.rb', line 323 def get_baggage_item(key) @baggage[key] end |
#id ⇒ Integer
Retrieve the ID for this span
157 158 159 |
# File 'lib/instana/tracing/span.rb', line 157 def id @data[:s] end |
#is_root? ⇒ Boolean
Indicates whether this span in the root span in the Trace
217 218 219 |
# File 'lib/instana/tracing/span.rb', line 217 def is_root? @data[:s] == @data[:t] end |
#key?(k) ⇒ Boolean
Hash key query to the internal @data hash
235 236 237 |
# File 'lib/instana/tracing/span.rb', line 235 def key?(k) @data.key?(k.to_sym) end |
#log(event = nil, _timestamp = Time.now, **fields) ⇒ Object
Add a log entry to this span Spec: OpenTracing API
345 346 347 |
# File 'lib/instana/tracing/span.rb', line 345 def log(event = nil, = Time.now, **fields) (:log => { :message => event, :parameters => fields }) end |
#name ⇒ String
Get the name (operation) of this Span
185 186 187 188 189 190 191 |
# File 'lib/instana/tracing/span.rb', line 185 def name if custom? @data[:data][:sdk][:name] else @data[:n] end end |
#name=(n) ⇒ Object
Set the name (operation) for this Span
197 198 199 200 201 202 203 |
# File 'lib/instana/tracing/span.rb', line 197 def name=(n) if custom? @data[:data][:sdk][:name] = n else @data[:n] = n end end |
#operation_name=(name) ⇒ Object
Set the name of the operation Spec: OpenTracing API
259 260 261 |
# File 'lib/instana/tracing/span.rb', line 259 def operation_name=(name) @data[:n] = name end |
#parent_id ⇒ Integer
Retrieve the parent ID of this span
171 172 173 |
# File 'lib/instana/tracing/span.rb', line 171 def parent_id @data[:p] end |
#parent_id=(id) ⇒ Integer
Set the parent ID of this span
178 179 180 |
# File 'lib/instana/tracing/span.rb', line 178 def parent_id=(id) @data[:p] = id end |
#raw ⇒ Object
Get the raw @data hash that summarizes this span
241 242 243 |
# File 'lib/instana/tracing/span.rb', line 241 def raw @data end |
#set_baggage_item(key, value) ⇒ Object
Set a baggage item on the span Spec: OpenTracing API
304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/instana/tracing/span.rb', line 304 def set_baggage_item(key, value) @baggage ||= {} @baggage[key] = value # Init/Update the SpanContext item if @context @context.baggage = @baggage else @context ||= ::Instana::SpanContext.new(@data[:t], @data[:s], @baggage) end self end |
#set_tag(key, value) ⇒ Object
Set a tag value on this span Spec: OpenTracing API
a String, Numeric, or Boolean it will be encoded with to_s
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/instana/tracing/span.rb', line 270 def set_tag(key, value) if custom? @data[:data][:sdk][:custom] ||= {} @data[:data][:sdk][:custom][key] = value else if !@data[:data].key?(key) @data[:data][key] = value elsif value.is_a?(Hash) && self[:data][key].is_a?(Hash) @data[:data][key].merge!(value) else @data[:data][key] = value end end self end |
#set_tags(tags) ⇒ Span
Helper method to add multiple tags to this span
291 292 293 294 295 296 297 |
# File 'lib/instana/tracing/span.rb', line 291 def () return unless .is_a?(Hash) .each do |k,v| set_tag(k, v) end self end |
#tags(key = nil) ⇒ Object
Retrieve the hash of tags for this span
329 330 331 332 333 334 335 336 |
# File 'lib/instana/tracing/span.rb', line 329 def (key = nil) if custom? = @data[:data][:sdk][:custom] else = @data[:data][key] end key ? [key] : end |
#trace_id ⇒ Integer
Retrieve the Trace ID for this span
164 165 166 |
# File 'lib/instana/tracing/span.rb', line 164 def trace_id @data[:t] end |