Class: Datadog::Span

Inherits:
Object
  • Object
show all
Defined in:
lib/ddtrace/span.rb

Overview

Represents a logical unit of work in the system. Each trace consists of one or more spans. Each span consists of a start time and a duration. For example, a span can describe the time spent on a distributed call on a separate machine, or the time spent in a small component within a larger operation. Spans can be nested within each other, and in those instances will have a parent-child relationship.

Constant Summary collapse

MAX_ID =

The max value for a Span identifier

2**64 - 1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tracer, name, options = {}) ⇒ Span

Create a new span linked to the given tracer. Call the finish() method once the tracer operation is over or use the finish_at(time) helper to close the span with the given time. Available options are:

  • service: the service name for this span

  • resource: the resource this span refers, or name if it’s missing

  • span_type: the type of the span (such as http, db and so on)

  • parent_id: the identifier of the parent span

  • trace_id: the identifier of the root span for this trace



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ddtrace/span.rb', line 30

def initialize(tracer, name, options = {})
  @tracer = tracer

  @name = name
  @service = options.fetch(:service, nil)
  @resource = options.fetch(:resource, name)
  @span_type = options.fetch(:span_type, nil)

  @span_id = Datadog::Utils.next_id()
  @parent_id = options.fetch(:parent_id, 0)
  @trace_id = options.fetch(:trace_id, @span_id)

  @meta = {}
  @status = 0

  @parent = nil

  @start_time = Time.now.utc
  @end_time = nil
end

Instance Attribute Details

#end_timeObject

Returns the value of attribute end_time.



16
17
18
# File 'lib/ddtrace/span.rb', line 16

def end_time
  @end_time
end

#nameObject

Returns the value of attribute name.



16
17
18
# File 'lib/ddtrace/span.rb', line 16

def name
  @name
end

#parentObject

Returns the value of attribute parent.



16
17
18
# File 'lib/ddtrace/span.rb', line 16

def parent
  @parent
end

#parent_idObject

Returns the value of attribute parent_id.



16
17
18
# File 'lib/ddtrace/span.rb', line 16

def parent_id
  @parent_id
end

#resourceObject

Returns the value of attribute resource.



16
17
18
# File 'lib/ddtrace/span.rb', line 16

def resource
  @resource
end

#serviceObject

Returns the value of attribute service.



16
17
18
# File 'lib/ddtrace/span.rb', line 16

def service
  @service
end

#span_idObject

Returns the value of attribute span_id.



16
17
18
# File 'lib/ddtrace/span.rb', line 16

def span_id
  @span_id
end

#span_typeObject

Returns the value of attribute span_type.



16
17
18
# File 'lib/ddtrace/span.rb', line 16

def span_type
  @span_type
end

#start_timeObject

Returns the value of attribute start_time.



16
17
18
# File 'lib/ddtrace/span.rb', line 16

def start_time
  @start_time
end

#statusObject

Returns the value of attribute status.



16
17
18
# File 'lib/ddtrace/span.rb', line 16

def status
  @status
end

#trace_idObject

Returns the value of attribute trace_id.



16
17
18
# File 'lib/ddtrace/span.rb', line 16

def trace_id
  @trace_id
end

Instance Method Details

#finishObject

Mark the span finished at the current time and submit it.



76
77
78
# File 'lib/ddtrace/span.rb', line 76

def finish
  finish_at(Time.now.utc)
end

#finish_at(end_time) ⇒ Object

Mark the span finished at the given time and submit it.



81
82
83
84
85
86
87
# File 'lib/ddtrace/span.rb', line 81

def finish_at(end_time)
  @end_time = end_time

  @tracer.record(self) unless @tracer.nil?

  self
end

#finished?Boolean

Return whether the span is finished or not.

Returns:

  • (Boolean)


90
91
92
# File 'lib/ddtrace/span.rb', line 90

def finished?
  !@end_time.nil?
end

#get_tag(key) ⇒ Object

Return the tag wth the given key, nil if it doesn’t exist.



62
63
64
# File 'lib/ddtrace/span.rb', line 62

def get_tag(key)
  @meta[key]
end

#pretty_print(q) ⇒ Object

Return a human readable version of the span



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/ddtrace/span.rb', line 137

def pretty_print(q)
  start_time = (@start_time.to_f * 1e9).to_i rescue '-'
  end_time = (@end_time.to_f * 1e9).to_i rescue '-'
  duration = ((@end_time - @start_time) * 1e9).to_i rescue 0
  q.group 0 do
    q.breakable
    q.text "Name: #{@name}\n"
    q.text "Span ID: #{@span_id}\n"
    q.text "Parent ID: #{@parent_id}\n"
    q.text "Trace ID: #{@trace_id}\n"
    q.text "Type: #{@span_type}\n"
    q.text "Service: #{@service}\n"
    q.text "Resource: #{@resource}\n"
    q.text "Error: #{@status}\n"
    q.text "Start: #{start_time}\n"
    q.text "End: #{end_time}\n"
    q.text "Duration: #{duration}\n"
    q.group(2, 'Tags: [', ']') do
      q.breakable
      q.seplist @meta.each do |key, value|
        q.text "#{key} => #{value}"
      end
    end
  end
end

#set_error(e) ⇒ Object

Mark the span with the given error.



67
68
69
70
71
72
73
# File 'lib/ddtrace/span.rb', line 67

def set_error(e)
  return if e.nil?
  @status = 1
  @meta[Datadog::Ext::Errors::MSG] = e.message if e.respond_to?(:message) && e.message
  @meta[Datadog::Ext::Errors::TYPE] = e.class.to_s
  @meta[Datadog::Ext::Errors::STACK] = e.backtrace.join("\n") if e.respond_to?(:backtrace) && e.backtrace
end

#set_parent(parent) ⇒ Object

Set this span’s parent, inheriting any properties not explicitly set. If the parent is nil, set the span zero values.



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ddtrace/span.rb', line 101

def set_parent(parent)
  @parent = parent

  if parent.nil?
    @trace_id = @span_id
    @parent_id = 0
  else
    @trace_id = parent.trace_id
    @parent_id = parent.span_id
    @service ||= parent.service
  end
end

#set_tag(key, value) ⇒ Object

Set the given key / value tag pair on the span. Keys and values must be strings. A valid example is:

span.set_tag('http.method', request.method)


55
56
57
58
59
# File 'lib/ddtrace/span.rb', line 55

def set_tag(key, value)
  @meta[key] = value.to_s
rescue StandardError => e
  Datadog::Tracer.log.error("Unable to set the tag #{key}, ignoring it. Caused by: #{e}")
end

#to_hashObject

Return the hash representation of the current span.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/ddtrace/span.rb', line 115

def to_hash
  h = {
    span_id: @span_id,
    parent_id: @parent_id,
    trace_id: @trace_id,
    name: @name,
    service: @service,
    resource: @resource,
    type: @span_type,
    meta: @meta,
    error: @status
  }

  if !@start_time.nil? && !@end_time.nil?
    h[:start] = (@start_time.to_f * 1e9).to_i
    h[:duration] = ((@end_time - @start_time) * 1e9).to_i
  end

  h
end

#to_sObject

Return a string representation of the span.



95
96
97
# File 'lib/ddtrace/span.rb', line 95

def to_s
  "Span(name:#{@name},sid:#{@span_id},tid:#{@trace_id},pid:#{@parent_id})"
end