Class: Datadog::Tracing::Transport::SerializableSpan
- Inherits:
-
Object
- Object
- Datadog::Tracing::Transport::SerializableSpan
- Defined in:
- lib/datadog/tracing/transport/serializable_trace.rb
Overview
Adds serialization functions to a Span
Instance Attribute Summary collapse
-
#span ⇒ Object
readonly
Returns the value of attribute span.
Instance Method Summary collapse
-
#duration_nano(duration) ⇒ Integer
Used for serialization.
-
#initialize(span, native_events_supported:) ⇒ SerializableSpan
constructor
A new instance of SerializableSpan.
-
#time_nano(time) ⇒ Integer
Used for serialization.
- #to_hash ⇒ Object
-
#to_json(*args) ⇒ Object
JSON serializer interface.
-
#to_msgpack(packer = nil) ⇒ Object
MessagePack serializer interface.
Constructor Details
#initialize(span, native_events_supported:) ⇒ SerializableSpan
Returns a new instance of SerializableSpan.
53 54 55 56 57 |
# File 'lib/datadog/tracing/transport/serializable_trace.rb', line 53 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
#span ⇒ Object (readonly)
Returns the value of attribute span.
48 49 50 |
# File 'lib/datadog/tracing/transport/serializable_trace.rb', line 48 def span @span end |
Instance Method Details
#duration_nano(duration) ⇒ Integer
Used for serialization
149 150 151 |
# File 'lib/datadog/tracing/transport/serializable_trace.rb', line 149 def duration_nano(duration) (duration * 1e9).to_i end |
#time_nano(time) ⇒ Integer
Used for serialization
139 140 141 |
# File 'lib/datadog/tracing/transport/serializable_trace.rb', line 139 def time_nano(time) time.to_i * 1000000000 + time.nsec end |
#to_hash ⇒ Object
143 144 145 |
# File 'lib/datadog/tracing/transport/serializable_trace.rb', line 143 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.
133 134 135 |
# File 'lib/datadog/tracing/transport/serializable_trace.rb', line 133 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
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 122 123 124 125 126 127 |
# File 'lib/datadog/tracing/transport/serializable_trace.rb', line 69 def to_msgpack(packer = nil) packer ||= MessagePack::Packer.new number_of_elements_to_write = 12 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.) packer.write('metrics') packer.write(span.metrics) packer.write('meta_struct') packer.write(span.) packer.write('span_links') packer.write(span.links.map(&:to_hash)) packer.write('error') packer.write(span.status) packer end |