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
156 157 158 |
# File 'lib/datadog/tracing/transport/serializable_trace.rb', line 156 def duration_nano(duration) (duration * 1e9).to_i end |
#time_nano(time) ⇒ Integer
Used for serialization 如果启用了时间同步,会自动应用时间偏移校正
140 141 142 143 144 145 146 147 148 |
# File 'lib/datadog/tracing/transport/serializable_trace.rb', line 140 def time_nano(time) local_time_ns = time.to_i * 1000000000 + time.nsec # 应用时间同步偏移校正(如果启用且 TimeSyncWorker 已加载) if defined?(Datadog::Core::Cloudwise::TimeSyncWorker) Datadog::Core::Cloudwise::TimeSyncWorker.(local_time_ns) else local_time_ns end end |
#to_hash ⇒ Object
150 151 152 |
# File 'lib/datadog/tracing/transport/serializable_trace.rb', line 150 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 |