Class: Instana::Span

Inherits:
Object
  • Object
show all
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].freeze
ENTRY_SPANS =
[ :rack, :'rpc-server', :'sidekiq-worker' ].freeze
EXIT_SPANS =
[ :activerecord, :excon, :'net-http', :'rpc-client', :'sidekiq-client', :redis ].freeze
HTTP_SPANS =
[ :rack, :excon, :'net-http' ].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, trace_id, parent_id: nil, start_time: ::Instana::Util.now_in_ms) ⇒ Span

Returns a new instance of Span.



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
40
41
42
43
44
45
46
47
# File 'lib/instana/tracing/span.rb', line 13

def initialize(name, trace_id, parent_id: nil, start_time: ::Instana::Util.now_in_ms)
  @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
  if start_time.is_a?(Time)
    @data[:ts] = ::Instana::Util.time_to_ms(start_time)
  else
    @data[:ts] = start_time
  end

  @baggage = {}

  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

#baggageObject

Returns the value of attribute baggage.



11
12
13
# File 'lib/instana/tracing/span.rb', line 11

def baggage
  @baggage
end

#parentObject

Returns the value of attribute parent.



10
11
12
# File 'lib/instana/tracing/span.rb', line 10

def parent
  @parent
end

Instance Method Details

#[](key) ⇒ Object

Hash accessor to the internal @data hash



219
220
221
# File 'lib/instana/tracing/span.rb', line 219

def [](key)
  @data[key.to_sym]
end

#[]=(key, value) ⇒ Object

Hash setter to the internal @data hash



225
226
227
# File 'lib/instana/tracing/span.rb', line 225

def []=(key, value)
  @data[key.to_sym] = value
end

#add_error(e) ⇒ Object

Log an error into the span

Parameters:

  • e (Exception)

    The exception to be logged



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

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])
      set_tags(:http => { :error => "#{e.class}: #{e.message}" })
    else
      set_tags(:log => { :message => e.message, :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

Parameters:

  • limit (Integer) (defaults to: nil)

    Limit the backtrace to the top <limit> frames



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/instana/tracing/span.rb', line 53

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 = ::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)

Parameters:

  • end_time (Time) (defaults to: ::Instana::Util.now_in_ms)

    custom end time, if not now

Returns:



128
129
130
131
132
133
134
135
136
# File 'lib/instana/tracing/span.rb', line 128

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]
  self
end

#configure_custom(name) ⇒ Object

Configure this span to be a custom span per the SDK generic span type.

Parameters:

  • name (String)

    name of the span

  • kvs (Hash)

    list of key values to be reported in the span



115
116
117
118
119
# File 'lib/instana/tracing/span.rb', line 115

def configure_custom(name)
  @data[:n] = :sdk
  @data[:data] = { :sdk => { :name => name.to_sym } }
  self
end

#contextInstana::SpanContext

Retrieve the context of this span.



146
147
148
# File 'lib/instana/tracing/span.rb', line 146

def context
  @context ||= ::Instana::SpanContext.new(@data[:t], @data[:s], @baggage)
end

#custom?Boolean

Indicates whether this span is a custom or registered Span

Returns:

  • (Boolean)


242
243
244
# File 'lib/instana/tracing/span.rb', line 242

def custom?
  @data[:n] == :sdk
end

#durationInteger

Get the duration value for this Span

Returns:

  • (Integer)

    the duration in milliseconds



204
205
206
# File 'lib/instana/tracing/span.rb', line 204

def duration
  @data[:d]
end

#finish(end_time = ::Instana::Util.now_in_ms) ⇒ Object

Finish the Instana::Span Spec: OpenTracing API

Parameters:

  • end_time (Time) (defaults to: ::Instana::Util.now_in_ms)

    custom end time, if not now



359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/instana/tracing/span.rb', line 359

def finish(end_time = ::Instana::Util.now_in_ms)
  if ::Instana.tracer.current_span.id != id
    ::Instana.logger.debug "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

Parameters:

  • key (String)

    the key of the baggage item

Returns:

  • Value of the baggage item



328
329
330
# File 'lib/instana/tracing/span.rb', line 328

def get_baggage_item(key)
  @baggage[key]
end

#idInteger

Retrieve the ID for this span

Returns:

  • (Integer)

    the span ID



153
154
155
# File 'lib/instana/tracing/span.rb', line 153

def id
  @data[:s]
end

#is_root?Boolean

Indicates whether this span in the root span in the Trace

Returns:

  • (Boolean)


213
214
215
# File 'lib/instana/tracing/span.rb', line 213

def is_root?
  @data[:s] == @data[:t]
end

#key?(k) ⇒ Boolean

Hash key query to the internal @data hash

Returns:

  • (Boolean)


231
232
233
# File 'lib/instana/tracing/span.rb', line 231

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

Parameters:

  • event (String) (defaults to: nil)

    event name for the log

  • timestamp (Time)

    time of the log

  • fields (Hash)

    Additional information to log



350
351
352
# File 'lib/instana/tracing/span.rb', line 350

def log(event = nil, _timestamp = Time.now, **fields)
  set_tags(:log => { :message => event, :parameters => fields })
end

#nameString

Get the name (operation) of this Span

Returns:

  • (String)

    or [Symbol] representing the span name



181
182
183
184
185
186
187
# File 'lib/instana/tracing/span.rb', line 181

def name
  if custom?
    @data[:data][:sdk][:name]
  else
    @data[:n]
  end
end

#name=(n) ⇒ Object

Set the name (operation) for this Span



193
194
195
196
197
198
199
# File 'lib/instana/tracing/span.rb', line 193

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



255
256
257
# File 'lib/instana/tracing/span.rb', line 255

def operation_name=(name)
  @data[:n] = name
end

#parent_idInteger

Retrieve the parent ID of this span

Returns:

  • (Integer)

    parent span ID



167
168
169
# File 'lib/instana/tracing/span.rb', line 167

def parent_id
  @data[:p]
end

#parent_id=(id) ⇒ Integer

Set the parent ID of this span

Returns:

  • (Integer)

    parent span ID



174
175
176
# File 'lib/instana/tracing/span.rb', line 174

def parent_id=(id)
  @data[:p] = id
end

#rawObject

Get the raw @data hash that summarizes this span



237
238
239
# File 'lib/instana/tracing/span.rb', line 237

def raw
  @data
end

#set_baggage_item(key, value) ⇒ Object

Set a baggage item on the span Spec: OpenTracing API

Parameters:

  • key (String)

    the key of the baggage item

  • value (String)

    the value of the baggage item



309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/instana/tracing/span.rb', line 309

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

Parameters:

  • key (String)

    the key of the tag

  • value (String, Numeric, Boolean)

    the value of the tag. If it’s not



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/instana/tracing/span.rb', line 266

def set_tag(key, value)
  if custom?
    @data[:data][:sdk][:custom] ||= {}
    @data[:data][:sdk][:custom][key] = value

    if key.to_sym == :'span.kind'
      case value.to_sym
      when :server || :consumer
        @data[:data][:sdk][:type] = :entry
      when :client || :producer
        @data[:data][:sdk][:type] = :exit
      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

Returns:



296
297
298
299
300
301
302
# File 'lib/instana/tracing/span.rb', line 296

def set_tags(tags)
  return unless tags.is_a?(Hash)
  tags.each do |k,v|
    set_tag(k, v)
  end
  self
end

#tags(key = nil) ⇒ Object

Retrieve the hash of tags for this span



334
335
336
337
338
339
340
341
# File 'lib/instana/tracing/span.rb', line 334

def tags(key = nil)
  if custom?
    tags = @data[:data][:sdk][:custom]
  else
    tags = @data[:data][key]
  end
  key ? tags[key] : tags
end

#trace_idInteger

Retrieve the Trace ID for this span

Returns:

  • (Integer)

    the Trace ID



160
161
162
# File 'lib/instana/tracing/span.rb', line 160

def trace_id
  @data[:t]
end