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) ⇒ SerializableSpan

Returns a new instance of SerializableSpan.



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

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.



41
42
43
# File 'lib/datadog/tracing/transport/serializable_trace.rb', line 41

def span
  @span
end

Instance Method Details

#duration_nano(duration) ⇒ Integer

Used for serialization

Returns:

  • (Integer)

    in nanoseconds since Epoch



120
121
122
# File 'lib/datadog/tracing/transport/serializable_trace.rb', line 120

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

#time_nano(time) ⇒ Integer

Used for serialization

Returns:

  • (Integer)

    in nanoseconds since Epoch



110
111
112
# File 'lib/datadog/tracing/transport/serializable_trace.rb', line 110

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

#to_hashObject



114
115
116
# File 'lib/datadog/tracing/transport/serializable_trace.rb', line 114

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.



104
105
106
# File 'lib/datadog/tracing/transport/serializable_trace.rb', line 104

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



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
97
98
99
# File 'lib/datadog/tracing/transport/serializable_trace.rb', line 58

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