Class: Datadog::Tracing::Transport::SerializableSpan

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

Overview

Adds serialization functions to a Span

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(span, native_events_supported) ⇒ SerializableSpan

Returns a new instance of SerializableSpan.

Parameters:

  • span (Datadog::Span)

    the span to serialize

  • native_events_supported (Boolean)

    whether the agent supports span events as a top-level field



49
50
51
52
53
# File 'lib/datadog/tracing/transport/serializable_trace.rb', line 49

def initialize(span, native_events_supported)
  @span = span
  @trace_id = Tracing::Utils::TraceId.to_low_order(span.trace_id)
  @native_events_supported = native_events_supported
end

Instance Attribute Details

#spanObject (readonly)

Returns the value of attribute span.



44
45
46
# File 'lib/datadog/tracing/transport/serializable_trace.rb', line 44

def span
  @span
end

Instance Method Details

#duration_nano(duration) ⇒ Integer

Used for serialization

Returns:

  • (Integer)

    in nanoseconds since Epoch



143
144
145
# File 'lib/datadog/tracing/transport/serializable_trace.rb', line 143

def duration_nano(duration)
  (duration * 1e9).to_i
end

#time_nano(time) ⇒ Integer

Used for serialization

Returns:

  • (Integer)

    in nanoseconds since Epoch



133
134
135
# File 'lib/datadog/tracing/transport/serializable_trace.rb', line 133

def time_nano(time)
  time.to_i * 1000000000 + time.nsec
end

#to_hashObject



137
138
139
# File 'lib/datadog/tracing/transport/serializable_trace.rb', line 137

def to_hash
  span.to_hash.merge(trace_id: @trace_id)
end

#to_json(*args) ⇒ Object

JSON serializer interface. Used by older version of the transport.



127
128
129
# File 'lib/datadog/tracing/transport/serializable_trace.rb', line 127

def to_json(*args)
  to_hash.to_json(*args)
end

#to_msgpack(packer = nil) ⇒ Object

MessagePack serializer interface. Making this object respond to ‘#to_msgpack` allows it to be automatically serialized by MessagePack.

This is more efficient than doing MessagePack.pack(span.to_hash) as we don’t have to create an intermediate Hash.

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength

Parameters:

  • packer (MessagePack::Packer) (defaults to: nil)

    serialization buffer, can be nil with JRuby



65
66
67
68
69
70
71
72
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/datadog/tracing/transport/serializable_trace.rb', line 65

def to_msgpack(packer = nil)
  packer ||= MessagePack::Packer.new

  number_of_elements_to_write = 11

  number_of_elements_to_write += 1 if span.events.any? && @native_events_supported

  if span.stopped?
    packer.write_map_header(number_of_elements_to_write + 2) # Set header with how many elements in the map

    packer.write('start')
    packer.write(time_nano(span.start_time))

    packer.write('duration')
    packer.write(duration_nano(span.duration))
  else
    packer.write_map_header(number_of_elements_to_write) # Set header with how many elements in the map
  end

  if span.events.any?
    if @native_events_supported
      # Use top-level field for native events
      packer.write('span_events')
      packer.write(span.events.map(&:to_native_format))
    else
      # Serialize span events as meta tags
      span.set_tag('events', span.events.map(&:to_hash).to_json)
    end
  end

  # DEV: We use strings as keys here, instead of symbols, as
  # DEV: MessagePack will ultimately convert them to strings.
  # DEV: By providing strings directly, we skip this indirection operation.
  packer.write('span_id')
  packer.write(span.id)
  packer.write('parent_id')
  packer.write(span.parent_id)
  packer.write('trace_id')
  packer.write(@trace_id)
  packer.write('name')
  packer.write(span.name)
  packer.write('service')
  packer.write(span.service)
  packer.write('resource')
  packer.write(span.resource)
  packer.write('type')
  packer.write(span.type)
  packer.write('meta')
  packer.write(span.meta)
  packer.write('metrics')
  packer.write(span.metrics)
  packer.write('span_links')
  packer.write(span.links.map(&:to_hash))
  packer.write('error')
  packer.write(span.status)
  packer
end