Class: Datadog::Tracing::SpanEvent

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/tracing/span_event.rb

Overview

Represents a timestamped annotation on a span. It is analogous to structured log message.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, attributes: nil, time_unix_nano: nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/datadog/tracing/span_event.rb', line 23

def initialize(
  name,
  attributes: nil,
  time_unix_nano: nil
)
  @name = name

  @attributes = attributes.dup || {}
  validate_attributes!(@attributes)
  @attributes.transform_keys!(&:to_s)

  # OpenTelemetry SDK stores span event timestamps in nanoseconds (not seconds).
  # We will do the same here to avoid unnecessary conversions and inconsistencies.
  @time_unix_nano = time_unix_nano || (Core::Utils::Time.now.to_r * 1_000_000_000).to_i
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



16
17
18
# File 'lib/datadog/tracing/span_event.rb', line 16

def attributes
  @attributes
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/datadog/tracing/span_event.rb', line 12

def name
  @name
end

#time_unix_nanoObject (readonly)

Returns the value of attribute time_unix_nano.



20
21
22
# File 'lib/datadog/tracing/span_event.rb', line 20

def time_unix_nano
  @time_unix_nano
end

Instance Method Details

#to_hashObject

Converts the span event into a hash to be used by with the span tag serialization (span.set_tag('events) = [event.to_hash]). This serialization format has the drawback of being limiting span events to the size limit of a span tag. All Datadog agents support this format.



43
44
45
46
47
# File 'lib/datadog/tracing/span_event.rb', line 43

def to_hash
  h = { 'name' => @name, 'time_unix_nano' => @time_unix_nano }
  h['attributes'] = @attributes unless @attributes.empty?
  h
end

#to_native_formatObject

Converts the span event into a hash to be used by the MessagePack serialization as a top-level span field (span.span_events = [event.to_native_format]). This serialization format removes the serialization limitations of the span.set_tag('events) approach, but is only supported by newer version of the Datadog agent.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/datadog/tracing/span_event.rb', line 53

def to_native_format
  h = { 'name' => @name, 'time_unix_nano' => @time_unix_nano }

  attr = {}
  @attributes.each do |key, value|
    attr[key] = if value.is_a?(Array)
                  { type: ARRAY_TYPE, array_value: value.map { |v| serialize_native_attribute(v) } }
                else
                  serialize_native_attribute(value)
                end
  end

  h['attributes'] = attr unless @attributes.empty?

  h
end