Skip to content

Commit 0836e11

Browse files
committed
Add meta_struct support on traces and spans
1 parent cefebdf commit 0836e11

File tree

16 files changed

+88
-13
lines changed

16 files changed

+88
-13
lines changed

lib/datadog/tracing/metadata.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require_relative 'metadata/analytics'
44
require_relative 'metadata/tagging'
5+
require_relative 'metadata/meta_struct'
56
require_relative 'metadata/errors'
67

78
module Datadog
@@ -11,6 +12,7 @@ module Metadata
1112
def self.included(base)
1213
base.include(Metadata::Tagging)
1314
base.include(Metadata::Errors)
15+
base.include(Metadata::MetaStruct)
1416

1517
# Additional extensions
1618
base.prepend(Metadata::Analytics)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
module Datadog
4+
module Tracing
5+
module Metadata
6+
# Adds complex structures tagging behavior through meta_struct
7+
# @public_api
8+
module MetaStruct
9+
def set_meta_struct(meta_struct)
10+
self.meta_struct.merge!(meta_struct)
11+
end
12+
13+
protected
14+
15+
def meta_struct
16+
@meta_struct ||= {}
17+
end
18+
end
19+
end
20+
end
21+
end

lib/datadog/tracing/span.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Span
2121
:end_time,
2222
:id,
2323
:meta,
24+
:meta_struct,
2425
:metrics,
2526
:name,
2627
:parent_id,
@@ -53,6 +54,7 @@ def initialize(
5354
end_time: nil,
5455
id: nil,
5556
meta: nil,
57+
meta_struct: nil,
5658
metrics: nil,
5759
parent_id: 0,
5860
resource: name,
@@ -75,6 +77,7 @@ def initialize(
7577
@trace_id = trace_id || Tracing::Utils.next_id
7678

7779
@meta = meta || {}
80+
@meta_struct = meta_struct || {}
7881
@metrics = metrics || {}
7982
@status = status || 0
8083

@@ -144,6 +147,7 @@ def to_hash
144147
error: @status,
145148
meta: @meta,
146149
metrics: @metrics,
150+
meta_struct: @meta_struct,
147151
name: @name,
148152
parent_id: @parent_id,
149153
resource: @resource,
@@ -185,12 +189,16 @@ def pretty_print(q)
185189
q.text "#{key} => #{value}"
186190
end
187191
end
188-
q.group(2, 'Metrics: [', ']') do
192+
q.group(2, 'Metrics: [', "]\n") do
189193
q.breakable
190194
q.seplist @metrics.each do |key, value|
191195
q.text "#{key} => #{value}"
192196
end
193197
end
198+
q.group(2, 'Meta-Struct: [', ']') do
199+
q.breakable
200+
q.pp meta_struct
201+
end
194202
end
195203
end
196204

lib/datadog/tracing/span_operation.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ def to_hash
289289
id: @id,
290290
meta: meta,
291291
metrics: metrics,
292+
meta_struct: meta_struct,
292293
name: @name,
293294
parent_id: @parent_id,
294295
resource: @resource,
@@ -328,12 +329,16 @@ def pretty_print(q)
328329
q.text "#{key} => #{value}"
329330
end
330331
end
331-
q.group(2, 'Metrics: [', ']') do
332+
q.group(2, 'Metrics: [', "]\n") do
332333
q.breakable
333334
q.seplist metrics.each do |key, value|
334335
q.text "#{key} => #{value}"
335336
end
336337
end
338+
q.group(2, 'Meta-Struct: [', ']') do
339+
q.breakable
340+
q.pp meta_struct
341+
end
337342
end
338343
end
339344

@@ -456,6 +461,7 @@ def build_span
456461
id: @id,
457462
meta: Core::Utils::SafeDup.frozen_or_dup(meta),
458463
metrics: Core::Utils::SafeDup.frozen_or_dup(metrics),
464+
meta_struct: Core::Utils::SafeDup.frozen_or_dup(meta_struct),
459465
parent_id: @parent_id,
460466
resource: @resource,
461467
service: @service,

lib/datadog/tracing/trace_operation.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
require_relative '../core/utils'
55
require_relative 'tracer'
66
require_relative 'event'
7-
require_relative 'metadata/tagging'
7+
require_relative 'metadata'
88
require_relative 'sampling/ext'
99
require_relative 'span_operation'
1010
require_relative 'trace_digest'
@@ -25,7 +25,7 @@ module Tracing
2525
#
2626
# @public_api
2727
class TraceOperation
28-
include Metadata::Tagging
28+
include Metadata
2929

3030
DEFAULT_MAX_LENGTH = 100_000
3131

@@ -73,6 +73,7 @@ def initialize(
7373
profiling_enabled: nil,
7474
tags: nil,
7575
metrics: nil,
76+
meta_struct: nil,
7677
trace_state: nil,
7778
trace_state_unknown_fields: nil,
7879
remote_parent: false,
@@ -105,6 +106,7 @@ def initialize(
105106
# Generic tags
106107
set_tags(tags) if tags
107108
set_tags(metrics) if metrics
109+
set_meta_struct(meta_struct) if meta_struct
108110

109111
# State
110112
@root_span = nil
@@ -369,6 +371,7 @@ def fork_clone
369371
trace_state_unknown_fields: (@trace_state_unknown_fields && @trace_state_unknown_fields.dup),
370372
tags: meta.dup,
371373
metrics: metrics.dup,
374+
meta_struct: meta_struct.dup,
372375
remote_parent: @remote_parent
373376
)
374377
end
@@ -508,6 +511,7 @@ def build_trace(spans, partial = false)
508511
service: service,
509512
tags: meta,
510513
metrics: metrics,
514+
meta_struct: meta_struct,
511515
root_span_id: !partial ? root_span && root_span.id : nil,
512516
profiling_enabled: @profiling_enabled,
513517
)

lib/datadog/tracing/trace_segment.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def initialize(
5858
service: nil,
5959
tags: nil,
6060
metrics: nil,
61+
meta_struct: nil,
6162
profiling_enabled: nil
6263
)
6364
@id = id
@@ -68,6 +69,7 @@ def initialize(
6869
# The caller is expected to have done that
6970
@meta = (tags && tags.dup) || {}
7071
@metrics = (metrics && metrics.dup) || {}
72+
@meta_struct = (meta_struct && meta_struct.dup) || {}
7173

7274
# Set well-known tags, defaulting to getting the values from tags
7375
@agent_sample_rate = agent_sample_rate || agent_sample_rate_tag
@@ -146,7 +148,8 @@ def high_order_tid
146148
attr_reader \
147149
:root_span_id,
148150
:meta,
149-
:metrics
151+
:metrics,
152+
:meta_struct
150153

151154
private
152155

lib/datadog/tracing/transport/serializable_trace.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def initialize(span, native_events_supported)
6565
def to_msgpack(packer = nil)
6666
packer ||= MessagePack::Packer.new
6767

68-
number_of_elements_to_write = 11
68+
number_of_elements_to_write = 12
6969

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

@@ -113,6 +113,9 @@ def to_msgpack(packer = nil)
113113
packer.write(span.meta)
114114
packer.write('metrics')
115115
packer.write(span.metrics)
116+
packer.write('meta_struct')
117+
# We encapsulate the resulting msgpack in a binary msgpack
118+
packer.write(span.meta_struct.transform_values(&:to_msgpack))
116119
packer.write('span_links')
117120
packer.write(span.links.map(&:to_hash))
118121
packer.write('error')

lib/datadog/tracing/transport/trace_formatter.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def format!
4343
# Apply generic trace tags. Any more specific value will be overridden
4444
# by the subsequent calls below.
4545
set_trace_tags!
46+
set_meta_struct!
4647

4748
set_resource!
4849

@@ -89,6 +90,12 @@ def set_trace_tags!
8990
root_span.set_tags(trace.send(:metrics))
9091
end
9192

93+
def set_meta_struct!
94+
return if partial?
95+
96+
root_span.set_meta_struct(trace.send(:meta_struct))
97+
end
98+
9299
def tag_agent_sample_rate!
93100
return unless trace.agent_sample_rate
94101

lib/datadog/tracing/transport/traces.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ def initialize(apis, default_api)
125125
end
126126

127127
def send_traces(traces)
128+
# object that extends Datadog::Core::Encoding::Encoder (MsgpackEncoder or JSONEncoder)
128129
encoder = current_api.encoder
129130
chunker = Datadog::Tracing::Transport::Traces::Chunker.new(encoder)
130131

sig/datadog/tracing/metadata.rbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Datadog
33
module Metadata
44
include Metadata::Tagging
55
include Metadata::Errors
6+
include Metadata::MetaStruct
67
prepend Metadata::Analytics
78
end
89
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Datadog
2+
module Tracing
3+
module Metadata
4+
module MetaStruct
5+
def set_meta_struct: (Hash[String, untyped] meta_struct) -> void
6+
7+
def meta_struct: () -> Hash[String, untyped]
8+
end
9+
end
10+
end
11+
end

sig/datadog/tracing/span.rbs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ module Datadog
66
attr_accessor end_time: (Time | nil)
77
attr_accessor id: Integer
88
attr_accessor meta: Hash[String, String]
9+
attr_accessor meta_struct: Hash[String, untyped]
910
attr_accessor metrics: Hash[String, Float]
1011
attr_accessor name: String
1112
attr_accessor parent_id: Integer
1213
attr_accessor resource: String
1314
attr_accessor service: (String | nil)
14-
attr_accessor links: Array[untyped]
15-
attr_accessor events: Array[untyped]
15+
attr_accessor links: Array[Datadog::Tracing::SpanLink]
16+
attr_accessor events: Array[Datadog::Tracing::SpanEvent]
1617
attr_accessor type: (String | nil)
1718
attr_accessor start_time: (Time | nil)
1819
attr_accessor status: Integer
@@ -25,6 +26,7 @@ module Datadog
2526
?end_time: (Time | nil),
2627
?id: (Integer | nil),
2728
?meta: (Hash[String, String] | nil),
29+
?meta_struct: (Hash[String, untyped] | nil),
2830
?metrics: (Hash[String, Float] | nil),
2931
?parent_id: Integer,
3032
?resource: String,
@@ -34,12 +36,15 @@ module Datadog
3436
?type: (String | nil),
3537
?trace_id: (Integer | nil),
3638
?service_entry: (bool | nil),
37-
?links: (Array[untyped] | nil),
38-
?events: (Array[untyped] | nil)
39+
?links: (Array[Datadog::Tracing::SpanLink] | nil),
40+
?events: (Array[Datadog::Tracing::SpanEvent] | nil)
3941
) -> void
4042

4143
def started?: -> bool
4244
def stopped?: -> bool
45+
46+
alias finished? stopped?
47+
4348
def duration: -> (Float | nil)
4449
def set_error: (Exception e) -> void
4550
def ==: (Span other) -> bool

sig/datadog/tracing/trace_operation.rbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Datadog
22
module Tracing
33
class TraceOperation
4-
include Metadata::Tagging
4+
include Metadata
55

66
DEFAULT_MAX_LENGTH: ::Integer
77

@@ -23,7 +23,7 @@ module Datadog
2323
attr_writer sampled: untyped
2424
attr_writer service: untyped
2525

26-
def initialize: (?agent_sample_rate: untyped?, ?events: untyped?, ?hostname: untyped?, ?id: untyped?, ?max_length: untyped, ?name: untyped?, ?origin: untyped?, ?parent_span_id: untyped?, ?rate_limiter_rate: untyped?, ?resource: untyped?, ?rule_sample_rate: untyped?, ?sample_rate: untyped?, ?sampled: untyped?, ?sampling_priority: untyped?, ?service: untyped?, ?tags: untyped?, ?metrics: untyped?, ?remote_parent: untyped?) -> void
26+
def initialize: (?agent_sample_rate: untyped?, ?events: untyped?, ?hostname: untyped?, ?id: untyped?, ?max_length: untyped, ?name: untyped?, ?origin: untyped?, ?parent_span_id: untyped?, ?rate_limiter_rate: untyped?, ?resource: untyped?, ?rule_sample_rate: untyped?, ?sample_rate: untyped?, ?sampled: untyped?, ?sampling_priority: untyped?, ?service: untyped?, ?tags: untyped?, ?meta_struct: untyped?, ?metrics: untyped?, ?remote_parent: untyped?) -> void
2727
def full?: () -> untyped
2828
def finished_span_count: () -> untyped
2929
def finished?: () -> untyped

sig/datadog/tracing/trace_segment.rbs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module Datadog
2222
attr_reader sampling_priority: untyped
2323
attr_reader service: untyped
2424

25-
def initialize: (untyped spans, ?agent_sample_rate: untyped?, ?hostname: untyped?, ?id: untyped?, ?lang: untyped?, ?name: untyped?, ?origin: untyped?, ?process_id: untyped?, ?rate_limiter_rate: untyped?, ?resource: untyped?, ?root_span_id: untyped?, ?rule_sample_rate: untyped?, ?runtime_id: untyped?, ?sample_rate: untyped?, ?sampling_priority: untyped?, ?service: untyped?, ?tags: untyped?, ?metrics: untyped?) -> void
25+
def initialize: (untyped spans, ?agent_sample_rate: untyped?, ?hostname: untyped?, ?id: untyped?, ?lang: untyped?, ?name: untyped?, ?origin: untyped?, ?process_id: untyped?, ?rate_limiter_rate: untyped?, ?resource: untyped?, ?root_span_id: untyped?, ?rule_sample_rate: untyped?, ?runtime_id: untyped?, ?sample_rate: untyped?, ?sampling_priority: untyped?, ?service: untyped?, ?tags: untyped?, ?metrics: untyped?, ?meta_struct: untyped?) -> void
2626
def any?: () -> untyped
2727
def count: () -> untyped
2828
def empty?: () -> untyped
@@ -36,6 +36,7 @@ module Datadog
3636
attr_reader root_span_id: untyped
3737
attr_reader meta: untyped
3838
attr_reader metrics: untyped
39+
attr_reader meta_struct: untyped
3940

4041
private
4142

spec/datadog/tracing/span_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@
243243
resource: 'my.span',
244244
type: nil,
245245
meta: {},
246+
meta_struct: {},
246247
metrics: {},
247248
span_links: [],
248249
error: 0

spec/datadog/tracing/transport/serializable_trace_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
'resource',
4040
'type',
4141
'meta',
42+
'meta_struct',
4243
'metrics',
4344
'span_links',
4445
'error',

0 commit comments

Comments
 (0)