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, :'rpc-client', :'rpc-server', :'sidekiq-client', :'sidekiq-worker', :redis, :'resque-client', :'resque-worker' ].freeze
- ENTRY_SPANS =
[ :rack, :'resque-worker', :'rpc-server', :'sidekiq-worker' ].freeze
- EXIT_SPANS =
[ :activerecord, :excon, :'net-http', :'resque-client', :'rpc-client', :'sidekiq-client', :redis ].freeze
- HTTP_SPANS =
[ :rack, :excon, :'net-http' ].freeze
Instance Attribute Summary collapse
-
#baggage ⇒ Object
Returns the value of attribute baggage.
-
#context ⇒ Instana::SpanContext
Retrieve the context of this span.
-
#is_root ⇒ Object
Returns the value of attribute is_root.
-
#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 = ::Instana::Util.now_in_ms) ⇒ Span
Closes out the span.
-
#configure_custom(name) ⇒ Object
Configure this span to be a custom span per the SDK generic span type.
-
#custom? ⇒ Boolean
Indicates whether this span is a custom or registered Span.
-
#duration ⇒ Integer
Get the duration value for this Span.
-
#finish(end_time = ::Instana::Util.now_in_ms) ⇒ 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, parent_ctx: nil, start_time: ::Instana::Util.now_in_ms) ⇒ Span
constructor
A new instance of Span.
- #inspect ⇒ Object
-
#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, parent_ctx: nil, start_time: ::Instana::Util.now_in_ms) ⇒ Span
Returns a new instance of Span.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 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 66 67 68 69 70 71 72 73 |
# File 'lib/instana/tracing/span.rb', line 17 def initialize(name, parent_ctx: nil, start_time: ::Instana::Util.now_in_ms) @data = {} if parent_ctx == nil # No parent specified so we're starting a new Trace - this will be the root span id = ::Instana::Util.generate_id @data[:t] = id # Trace ID @data[:s] = id # Span ID is_root = true else if parent_ctx.is_a?(::Instana::Span) @parent = parent_ctx parent_context = parent_ctx.context elsif parent_ctx.is_a?(::Instana::SpanContext) parent_context = parent_ctx end @data[:t] = parent_context.trace_id # Trace ID @data[:s] = ::Instana::Util.generate_id # Span ID @data[:p] = parent_context.span_id # Parent ID if parent_context.baggage @baggage = parent_ctx.baggage.dup else @baggage = nil end is_root = false end @data[:data] = {} # Entity Source @data[:f] = { :e => ::Instana.agent.report_pid, :h => ::Instana.agent.agent_uuid } # Start time if start_time.is_a?(Time) @data[:ts] = ::Instana::Util.time_to_ms(start_time) else @data[:ts] = start_time end if ::Instana.config[:collect_backtraces] # 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) end # 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.
13 14 15 |
# File 'lib/instana/tracing/span.rb', line 13 def baggage @baggage end |
#context ⇒ Instana::SpanContext
Retrieve the context of this span.
180 181 182 |
# File 'lib/instana/tracing/span.rb', line 180 def context @context end |
#is_root ⇒ Object
Returns the value of attribute is_root.
14 15 16 |
# File 'lib/instana/tracing/span.rb', line 14 def is_root @is_root end |
#parent ⇒ Object
Returns the value of attribute parent.
12 13 14 |
# File 'lib/instana/tracing/span.rb', line 12 def parent @parent end |
Instance Method Details
#[](key) ⇒ Object
Hash accessor to the internal @data hash
244 245 246 |
# File 'lib/instana/tracing/span.rb', line 244 def [](key) @data[key.to_sym] end |
#[]=(key, value) ⇒ Object
Hash setter to the internal @data hash
250 251 252 |
# File 'lib/instana/tracing/span.rb', line 250 def []=(key, value) @data[key.to_sym] = value end |
#add_error(e) ⇒ Object
Log an error into the span
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/instana/tracing/span.rb', line 107 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(:error, Time.now, { :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
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/instana/tracing/span.rb', line 79 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] << { :c => x[0], :n => x[1], :m => x[2] } frame_count = frame_count + 1 if limit end end end |
#close(end_time = ::Instana::Util.now_in_ms) ⇒ 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)
159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/instana/tracing/span.rb', line 159 def close(end_time = ::Instana::Util.now_in_ms) if end_time.is_a?(Time) end_time = ::Instana::Util.time_to_ms(end_time) end @data[:d] = end_time - @data[:ts] # Add this span to the queue for reporting ::Instana.processor.add_span(self) self end |
#configure_custom(name) ⇒ Object
Configure this span to be a custom span per the SDK generic span type.
Default to an intermediate kind span. Can be overridden by setting a span.kind tag.
144 145 146 147 148 149 150 |
# File 'lib/instana/tracing/span.rb', line 144 def configure_custom(name) @data[:n] = :sdk @data[:k] = 3 @data[:data] = { :sdk => { :name => name.to_sym, :type => :intermediate } } @data[:data][:sdk][:custom] = { :tags => {}, :logs => {} } self end |
#custom? ⇒ Boolean
Indicates whether this span is a custom or registered Span
267 268 269 |
# File 'lib/instana/tracing/span.rb', line 267 def custom? @data[:n] == :sdk end |
#duration ⇒ Integer
Get the duration value for this Span
238 239 240 |
# File 'lib/instana/tracing/span.rb', line 238 def duration @data[:d] end |
#finish(end_time = ::Instana::Util.now_in_ms) ⇒ Object
Finish the Instana::Span Spec: OpenTracing API
403 404 405 406 |
# File 'lib/instana/tracing/span.rb', line 403 def finish(end_time = ::Instana::Util.now_in_ms) close(end_time) self end |
#get_baggage_item(key) ⇒ Object
Get a baggage item Spec: OpenTracing API
363 364 365 366 |
# File 'lib/instana/tracing/span.rb', line 363 def get_baggage_item(key) return nil if @baggage.nil? @baggage[key] end |
#id ⇒ Integer
Retrieve the ID for this span
187 188 189 |
# File 'lib/instana/tracing/span.rb', line 187 def id @data[:s] end |
#inspect ⇒ Object
271 272 273 |
# File 'lib/instana/tracing/span.rb', line 271 def inspect @data.inspect end |
#key?(k) ⇒ Boolean
Hash key query to the internal @data hash
256 257 258 |
# File 'lib/instana/tracing/span.rb', line 256 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
386 387 388 389 390 391 392 393 394 395 396 |
# File 'lib/instana/tracing/span.rb', line 386 def log(event = nil, = Time.now, **fields) ts = ::Instana::Util.time_to_ms().to_s if custom? @data[:data][:sdk][:custom][:logs][ts] = fields @data[:data][:sdk][:custom][:logs][ts][:event] = event else (:log => fields) end rescue StandardError => e Instana.logger.debug { "#{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: #{e.}" } end |
#name ⇒ String
Get the name (operation) of this Span
215 216 217 218 219 220 221 |
# File 'lib/instana/tracing/span.rb', line 215 def name if custom? @data[:data][:sdk][:name] else @data[:n] end end |
#name=(n) ⇒ Object
Set the name (operation) for this Span
227 228 229 230 231 232 233 |
# File 'lib/instana/tracing/span.rb', line 227 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
284 285 286 |
# File 'lib/instana/tracing/span.rb', line 284 def operation_name=(name) @data[:n] = name end |
#parent_id ⇒ Integer
Retrieve the parent ID of this span
201 202 203 |
# File 'lib/instana/tracing/span.rb', line 201 def parent_id @data[:p] end |
#parent_id=(id) ⇒ Integer
Set the parent ID of this span
208 209 210 |
# File 'lib/instana/tracing/span.rb', line 208 def parent_id=(id) @data[:p] = id end |
#raw ⇒ Object
Get the raw @data hash that summarizes this span
262 263 264 |
# File 'lib/instana/tracing/span.rb', line 262 def raw @data end |
#set_baggage_item(key, value) ⇒ Object
Set a baggage item on the span Spec: OpenTracing API
344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/instana/tracing/span.rb', line 344 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], 1, @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
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/instana/tracing/span.rb', line 295 def set_tag(key, value) if custom? @data[:data][:sdk][:custom] ||= {} @data[:data][:sdk][:custom][:tags] ||= {} @data[:data][:sdk][:custom][:tags][key] = value if key.to_sym == :'span.kind' case value.to_sym when :server, :consumer @data[:data][:sdk][:type] = :entry @data[:k] = 1 when :client, :producer @data[:data][:sdk][:type] = :exit @data[:k] = 2 else @data[:data][:sdk][:type] = :intermediate @data[:k] = 3 end end 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
331 332 333 334 335 336 337 |
# File 'lib/instana/tracing/span.rb', line 331 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
370 371 372 373 374 375 376 377 |
# File 'lib/instana/tracing/span.rb', line 370 def (key = nil) if custom? = @data[:data][:sdk][:custom][:tags] else = @data[:data][key] end key ? [key] : end |
#trace_id ⇒ Integer
Retrieve the Trace ID for this span
194 195 196 |
# File 'lib/instana/tracing/span.rb', line 194 def trace_id @data[:t] end |