Class: Datadog::Transport::SerializableSpan

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

Overview

Adds serialization functions to a Span

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(span) ⇒ SerializableSpan

Returns a new instance of SerializableSpan.



41
42
43
44
# File 'lib/ddtrace/transport/serializable_trace.rb', line 41

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

Instance Attribute Details

#spanObject (readonly)

Returns the value of attribute span.



38
39
40
# File 'lib/ddtrace/transport/serializable_trace.rb', line 38

def span
  @span
end

Instance Method Details

#duration_nano(duration) ⇒ Integer

Used for serialization

Returns:

  • (Integer)

    in nanoseconds since Epoch



117
118
119
# File 'lib/ddtrace/transport/serializable_trace.rb', line 117

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

#time_nano(time) ⇒ Integer

Used for serialization

Returns:

  • (Integer)

    in nanoseconds since Epoch



107
108
109
# File 'lib/ddtrace/transport/serializable_trace.rb', line 107

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

#to_hashObject



111
112
113
# File 'lib/ddtrace/transport/serializable_trace.rb', line 111

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.



101
102
103
# File 'lib/ddtrace/transport/serializable_trace.rb', line 101

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

Parameters:

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

    serialization buffer, can be nil with JRuby



55
56
57
58
59
60
61
62
63
64
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
# File 'lib/ddtrace/transport/serializable_trace.rb', line 55

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

  number_of_elements_to_write = 10

  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

  # 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('error')
  packer.write(span.status)
  packer
end