datadog 2.41.0 → 2.43.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +57 -1
  3. data/README.md +3 -0
  4. data/ext/datadog_profiling_native_extension/NativeExtensionDesign.md +12 -24
  5. data/ext/datadog_profiling_native_extension/collectors_cpu_and_wall_time_worker.c +120 -61
  6. data/ext/datadog_profiling_native_extension/collectors_stack.c +11 -3
  7. data/ext/datadog_profiling_native_extension/collectors_thread_context.c +156 -108
  8. data/ext/datadog_profiling_native_extension/collectors_thread_context.h +7 -3
  9. data/ext/datadog_profiling_native_extension/datadog_ruby_common.h +0 -10
  10. data/ext/datadog_profiling_native_extension/extconf.rb +52 -93
  11. data/ext/datadog_profiling_native_extension/heap_recorder.c +351 -255
  12. data/ext/datadog_profiling_native_extension/heap_recorder.h +50 -31
  13. data/ext/datadog_profiling_native_extension/native_extension_helpers.rb +0 -24
  14. data/ext/datadog_profiling_native_extension/private_vm_api_access.c +518 -442
  15. data/ext/datadog_profiling_native_extension/private_vm_api_access.h +15 -3
  16. data/ext/datadog_profiling_native_extension/profiling.c +2 -0
  17. data/ext/datadog_profiling_native_extension/ruby_helpers.c +1 -79
  18. data/ext/datadog_profiling_native_extension/ruby_helpers.h +0 -7
  19. data/ext/datadog_profiling_native_extension/stack_recorder.c +106 -61
  20. data/ext/datadog_profiling_native_extension/stack_recorder.h +12 -4
  21. data/ext/datadog_profiling_native_extension/time_helpers.h +2 -1
  22. data/ext/libdatadog_api/datadog_ruby_common.h +0 -10
  23. data/ext/libdatadog_api/di.c +10 -0
  24. data/ext/libdatadog_api/extconf.rb +3 -0
  25. data/ext/libdatadog_api/init.c +2 -0
  26. data/ext/libdatadog_api/otel_thread_context.c +247 -0
  27. data/ext/libdatadog_api/otel_thread_context.h +5 -0
  28. data/ext/libdatadog_api/trace_exporter.c +642 -22
  29. data/ext/libdatadog_extconf_helpers.rb +1 -1
  30. data/lib/datadog/appsec/assets/blocked.html +1 -108
  31. data/lib/datadog/core/configuration/components.rb +1 -0
  32. data/lib/datadog/core/crashtracking/component.rb +5 -1
  33. data/lib/datadog/data_streams/pathway_context.rb +20 -22
  34. data/lib/datadog/data_streams/processor.rb +31 -0
  35. data/lib/datadog/di/instrumenter.rb +41 -1
  36. data/lib/datadog/di/logger.rb +2 -2
  37. data/lib/datadog/di/probe.rb +9 -1
  38. data/lib/datadog/di/probe_notification_builder.rb +1 -0
  39. data/lib/datadog/di/remote.rb +3 -3
  40. data/lib/datadog/open_feature/evaluation_engine.rb +29 -3
  41. data/lib/datadog/open_feature/exposures/event.rb +10 -3
  42. data/lib/datadog/open_feature/ext.rb +19 -0
  43. data/lib/datadog/open_feature/flag_evaluation/aggregator.rb +236 -80
  44. data/lib/datadog/open_feature/flag_evaluation/writer.rb +179 -68
  45. data/lib/datadog/open_feature/hooks/flag_eval_evp_hook.rb +24 -21
  46. data/lib/datadog/open_feature/native_evaluator.rb +33 -6
  47. data/lib/datadog/open_feature/noop_evaluator.rb +5 -0
  48. data/lib/datadog/open_feature/provider.rb +11 -2
  49. data/lib/datadog/open_feature/transport.rb +5 -0
  50. data/lib/datadog/opentelemetry/sdk/propagator.rb +1 -1
  51. data/lib/datadog/opentelemetry/trace.rb +3 -0
  52. data/lib/datadog/profiling/collectors/cpu_and_wall_time_worker.rb +3 -0
  53. data/lib/datadog/profiling/collectors/info.rb +2 -6
  54. data/lib/datadog/profiling/collectors/thread_context.rb +0 -4
  55. data/lib/datadog/profiling/component.rb +8 -16
  56. data/lib/datadog/tracing/contrib/active_record/events/sql.rb +1 -0
  57. data/lib/datadog/tracing/contrib/sequel/jdbc_connection_string.rb +140 -0
  58. data/lib/datadog/tracing/contrib/sequel/utils.rb +49 -62
  59. data/lib/datadog/tracing/distributed/baggage.rb +0 -1
  60. data/lib/datadog/tracing/distributed/datadog.rb +3 -3
  61. data/lib/datadog/tracing/distributed/propagation.rb +3 -0
  62. data/lib/datadog/tracing/distributed/trace_context.rb +14 -271
  63. data/lib/datadog/tracing/distributed/trace_state/datadog.rb +233 -0
  64. data/lib/datadog/tracing/distributed/trace_state/ext.rb +44 -0
  65. data/lib/datadog/tracing/distributed/trace_state/open_telemetry.rb +156 -0
  66. data/lib/datadog/tracing/distributed/trace_state.rb +121 -0
  67. data/lib/datadog/tracing/otel_thread_context.rb +34 -0
  68. data/lib/datadog/tracing/remote.rb +195 -27
  69. data/lib/datadog/tracing/sampling/rule_sampler.rb +2 -0
  70. data/lib/datadog/tracing/trace_digest.rb +22 -4
  71. data/lib/datadog/tracing/trace_operation.rb +22 -10
  72. data/lib/datadog/tracing/tracer.rb +5 -5
  73. data/lib/datadog/tracing/transport/native.rb +17 -25
  74. data/lib/datadog/tracing/transport/span_events_negotiation.rb +41 -0
  75. data/lib/datadog/tracing/transport/traces.rb +2 -22
  76. data/lib/datadog/version.rb +1 -1
  77. metadata +16 -8
  78. data/lib/datadog/tracing/distributed/datadog_tags_codec.rb +0 -69
@@ -0,0 +1,156 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../sampling/rate_sampler"
4
+ require_relative "ext"
5
+
6
+ module Datadog
7
+ module Tracing
8
+ module Distributed
9
+ class TraceState
10
+ # Encodes consistent probability sampling in the OpenTelemetry `ot=` tracestate member.
11
+ # @see https://opentelemetry.io/docs/specs/otel/trace/tracestate-probability-sampling/
12
+ class OpenTelemetry
13
+ class << self
14
+ def from_digest(digest, propagate_sampling: true)
15
+ new(
16
+ random_value: (digest.trace_otel_random_value if propagate_sampling),
17
+ threshold: (digest.trace_otel_threshold if propagate_sampling),
18
+ unknown_fields: digest.trace_otel_unknown_fields,
19
+ )
20
+ end
21
+
22
+ # Parses known fields and retains unknown fields for propagation.
23
+ def from_tracestate_member(value)
24
+ return new unless value
25
+
26
+ # @type var random_value: ::String?
27
+ # @type var threshold: ::String?
28
+ random_value = nil
29
+ threshold = nil
30
+ unknown = []
31
+
32
+ value.split(";").each do |pair|
33
+ key, field = pair.split(":", 2)
34
+ case key
35
+ when "rv"
36
+ random_value = field
37
+ when "th"
38
+ threshold = field
39
+ else
40
+ unknown << pair
41
+ end
42
+ end
43
+
44
+ threshold = nil if threshold && !Ext::OpenTelemetry::VALID_THRESHOLD.match?(threshold)
45
+ random_value = nil if random_value && !Ext::OpenTelemetry::VALID_RANDOM_VALUE.match?(random_value)
46
+
47
+ new(
48
+ random_value: random_value,
49
+ threshold: threshold,
50
+ unknown_fields: unknown.empty? ? nil : "#{unknown.join(";")};",
51
+ )
52
+ end
53
+ end
54
+
55
+ attr_reader :random_value, :threshold, :unknown_fields
56
+
57
+ def initialize(random_value: nil, threshold: nil, unknown_fields: nil)
58
+ @random_value = random_value
59
+ @threshold = threshold
60
+ @unknown_fields = unknown_fields
61
+ end
62
+
63
+ # Resolves fields that represent the trace's current sampling decision.
64
+ def outbound(
65
+ trace_id:,
66
+ sampling_priority:,
67
+ decision_maker:,
68
+ applied_rate:,
69
+ rate_limiter_rate:,
70
+ distributed_sampling_priority:
71
+ )
72
+ if Ext::OpenTelemetry::NON_PROBABILITY_DECISIONS.include?(decision_maker)
73
+ return self.class.new(random_value: random_value, unknown_fields: unknown_fields)
74
+ end
75
+
76
+ trace_kept = sampling_priority && sampling_priority >= Sampling::Ext::Priority::AUTO_KEEP
77
+ if rate_limiter_rate && !trace_kept
78
+ return self.class.new(random_value: random_value, unknown_fields: unknown_fields)
79
+ end
80
+
81
+ return self if random_value || threshold
82
+ return self.class.new(unknown_fields: unknown_fields) if distributed_sampling_priority || !applied_rate
83
+
84
+ outbound_threshold = threshold_for(applied_rate)
85
+ outbound_random_value = reconcile_random_value(
86
+ random_value_for(trace_id),
87
+ outbound_threshold,
88
+ !!trace_kept
89
+ )
90
+ self.class.new(
91
+ random_value: format_random_value(outbound_random_value),
92
+ threshold: format_threshold(outbound_threshold),
93
+ unknown_fields: unknown_fields,
94
+ )
95
+ end
96
+
97
+ def to_s
98
+ fields = []
99
+ fields << "rv:#{random_value}" if random_value
100
+ fields << "th:#{threshold}" if threshold
101
+ fields.concat(unknown_fields.chomp(";").split(";")) if unknown_fields
102
+
103
+ value = +""
104
+ fields.each do |field|
105
+ separator = value.empty? ? "" : ";"
106
+ break if value.bytesize + separator.bytesize + field.bytesize > Ext::TRACESTATE_VALUE_SIZE_LIMIT
107
+
108
+ value << separator << field
109
+ end
110
+ value.empty? ? "" : "ot=#{value}"
111
+ end
112
+
113
+ private
114
+
115
+ # Converts Datadog's Knuth hash into OTel's 56-bit random value.
116
+ def random_value_for(trace_id)
117
+ hash = (trace_id * Sampling::RateSampler::KNUTH_FACTOR) % Ext::OpenTelemetry::UINT64_MODULO
118
+ (~hash & Ext::OpenTelemetry::UINT64_MASK) >> 8
119
+ end
120
+
121
+ # Converts a sample rate into a 56-bit rejection threshold.
122
+ def threshold_for(rate)
123
+ threshold = ((1.0 - rate) * Ext::OpenTelemetry::MAX_THRESHOLD).round
124
+ return 0 if threshold < 0
125
+ return Ext::OpenTelemetry::MAX_ENCODABLE_THRESHOLD if threshold > Ext::OpenTelemetry::MAX_ENCODABLE_THRESHOLD
126
+
127
+ threshold
128
+ end
129
+
130
+ # Preserve Datadog's 64-bit decision after expressing it with 56-bit OTel values.
131
+ def reconcile_random_value(random_value, threshold, kept)
132
+ if kept && random_value < threshold
133
+ threshold
134
+ elsif !kept && random_value >= threshold
135
+ [threshold - 1, 0].max
136
+ else
137
+ random_value
138
+ end
139
+ end
140
+
141
+ def format_random_value(random_value)
142
+ format("%014x", random_value)
143
+ end
144
+
145
+ # OTel thresholds omit trailing zero nibbles but retain leading zeroes.
146
+ def format_threshold(threshold)
147
+ return "0" if threshold.zero?
148
+
149
+ trailing_nibbles = ((threshold & -threshold).bit_length - 1) / 4
150
+ (threshold >> (trailing_nibbles * 4)).to_s(16).rjust(14 - trailing_nibbles, "0")
151
+ end
152
+ end
153
+ end
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../core/utils"
4
+ require_relative "trace_state/ext"
5
+ require_relative "trace_state/datadog"
6
+ require_relative "trace_state/open_telemetry"
7
+
8
+ module Datadog
9
+ module Tracing
10
+ module Distributed
11
+ # Parsed vendor state from a W3C `tracestate` header.
12
+ # @api private
13
+ class TraceState
14
+ class << self
15
+ def from_digest(digest, propagate_sampling: true)
16
+ new(
17
+ unknown_vendors: digest.trace_state,
18
+ datadog: Datadog.from_digest(digest),
19
+ open_telemetry: OpenTelemetry.from_digest(digest, propagate_sampling: propagate_sampling),
20
+ )
21
+ end
22
+
23
+ def serialize_digest(digest)
24
+ serialize(
25
+ unknown_vendors: digest.trace_state,
26
+ datadog: Datadog.from_digest(digest),
27
+ open_telemetry: OpenTelemetry.from_digest(digest),
28
+ )
29
+ end
30
+
31
+ # Parses owned members while retaining other vendors unchanged.
32
+ def extract(tracestate)
33
+ vendors = split(tracestate) || []
34
+ datadog_index = vendors.index { |vendor| vendor.start_with?("dd=") }
35
+ datadog_value = vendors.delete_at(datadog_index).delete_prefix("dd=") if datadog_index
36
+ otel_index = vendors.index { |vendor| vendor.start_with?("ot=") }
37
+ otel_value = vendors.delete_at(otel_index).delete_prefix("ot=") if otel_index
38
+
39
+ new(
40
+ unknown_vendors: vendors.empty? ? nil : vendors.join(","),
41
+ datadog: Datadog.from_tracestate_member(datadog_value),
42
+ open_telemetry: OpenTelemetry.from_tracestate_member(otel_value),
43
+ )
44
+ end
45
+
46
+ # Partial members must not be propagated, but complete members within the limits are retained.
47
+ def split(tracestate)
48
+ return if tracestate.nil? || tracestate.empty?
49
+
50
+ remove_last_vendor = false
51
+ if tracestate.bytesize > Ext::TRACESTATE_MAX_SIZE_LIMIT
52
+ remove_last_vendor = tracestate.byteslice(Ext::TRACESTATE_MAX_SIZE_LIMIT, 1) != ","
53
+ tracestate = tracestate.byteslice(0, Ext::TRACESTATE_MAX_SIZE_LIMIT + 1) #: String
54
+ tracestate = tracestate.chop
55
+ end
56
+
57
+ tracestate = ::Datadog::Core::Utils.utf8_encode(tracestate, placeholder: nil)
58
+ return unless tracestate
59
+
60
+ vendors = tracestate.split(",", Ext::TRACESTATE_MAX_LIST_VENDORS + 1)
61
+ vendors.pop if vendors.length > Ext::TRACESTATE_MAX_LIST_VENDORS || remove_last_vendor
62
+ vendors.each(&:strip!)
63
+ vendors.pop while vendors.last == ""
64
+ vendors
65
+ end
66
+
67
+ private
68
+
69
+ def serialize(unknown_vendors:, datadog:, open_telemetry:)
70
+ leading_vendors = select_leading_vendors(datadog, open_telemetry)
71
+ vendors = split(unknown_vendors)
72
+
73
+ if leading_vendors.empty?
74
+ return unless vendors && !vendors.empty?
75
+
76
+ return vendors.join(",")
77
+ end
78
+
79
+ vendors&.reject! { |vendor| vendor.start_with?("dd=", "ot=") }
80
+
81
+ tracestate = leading_vendors.join(",")
82
+ if vendors && !vendors.empty?
83
+ vendors.first(Ext::TRACESTATE_MAX_LIST_VENDORS - leading_vendors.size).each do |vendor|
84
+ break if tracestate.bytesize + vendor.bytesize + 1 > Ext::TRACESTATE_MAX_SIZE_LIMIT
85
+
86
+ tracestate << "," << vendor
87
+ end
88
+ end
89
+
90
+ tracestate
91
+ end
92
+
93
+ def select_leading_vendors(datadog, open_telemetry)
94
+ leading_vendors = []
95
+ add_leading_vendor(leading_vendors, datadog)
96
+ add_leading_vendor(leading_vendors, open_telemetry)
97
+
98
+ leading_vendors
99
+ end
100
+
101
+ def add_leading_vendor(leading_vendors, state)
102
+ vendor = state.to_s
103
+ return if vendor.empty?
104
+
105
+ combined_size = leading_vendors.sum { |current| current.bytesize } +
106
+ leading_vendors.size + vendor.bytesize
107
+ leading_vendors << vendor if combined_size <= Ext::TRACESTATE_MAX_SIZE_LIMIT
108
+ end
109
+ end
110
+
111
+ attr_reader :unknown_vendors, :datadog, :open_telemetry
112
+
113
+ def initialize(unknown_vendors: nil, datadog: nil, open_telemetry: nil)
114
+ @unknown_vendors = unknown_vendors
115
+ @datadog = datadog || Datadog.new
116
+ @open_telemetry = open_telemetry || OpenTelemetry.new
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datadog
4
+ module Tracing
5
+ # Publishes a per-thread OpenTelemetry context record into a thread-local slot,
6
+ # so an out-of-process reader (e.g. the eBPF profiler) can discover it.
7
+ #
8
+ # See the "OTel Thread Context" OTEP:
9
+ # https://github.com/open-telemetry/opentelemetry-specification/blob/main/oteps/profiles/4947-thread-ctx.md
10
+ #
11
+ # Native functions are implemented in ext/libdatadog_api/otel_thread_context.c
12
+ module OTelThreadContext
13
+ class << self
14
+ def supported?
15
+ Datadog::Core::LIBDATADOG_API_FAILURE.nil? && _native_supported?
16
+ end
17
+
18
+ def enable!
19
+ return false unless supported?
20
+
21
+ _native_enable
22
+ end
23
+
24
+ def set(trace_id:, span_id:, local_root_span_id:)
25
+ _native_set(trace_id, span_id, local_root_span_id)
26
+ end
27
+
28
+ def clear
29
+ _native_clear
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -15,22 +15,198 @@ module Datadog
15
15
  1 << 13, # APM_TRACING_LOGS_INJECTION: Dynamic trace logs injection configuration
16
16
  1 << 14, # APM_TRACING_HTTP_HEADER_TAGS: Dynamic trace HTTP header tags configuration
17
17
  1 << 29, # APM_TRACING_SAMPLE_RULES: Dynamic trace sampling rules configuration
18
+ 1 << 45, # APM_TRACING_MULTICONFIG: merge multiple org/env-level APM_TRACING configs
18
19
  # APM_TRACING_ENABLE_DYNAMIC_INSTRUMENTATION (bit 38) is declared in
19
20
  # DI::Remote.capabilities, not here, so it is registered only when DI
20
21
  # is not explicitly disabled and the runtime supports DI.
21
22
  ].freeze
22
23
 
24
+ # Diagnostic scope label per specificity priority.
25
+ SCOPE_LABELS = {
26
+ 5 => "service+env",
27
+ 4 => "service",
28
+ 3 => "env",
29
+ 2 => "cluster",
30
+ 1 => "org",
31
+ }.freeze
32
+
33
+ # @return [Array[String]] the remote config products this module handles
23
34
  def products
24
35
  [PRODUCT]
25
36
  end
26
37
 
38
+ # @return [Array[Integer]] the remote config capability bits advertised
27
39
  def capabilities
28
40
  CAPABILITIES
29
41
  end
30
42
 
31
- def process_config(config, content, repository = nil)
32
- lib_config = config["lib_config"]
43
+ # Merges every active APM_TRACING config in the repository and applies the
44
+ # result once. Org/env-level (multi-config) remote enablement delivers
45
+ # several APM_TRACING configs in parallel (a (service, env)-specific one,
46
+ # an env-wide one, and/or an org-wide "*" one); for each lib_config field
47
+ # the value from the most-specific matching config wins. The RC repository
48
+ # prunes deleted configs, so this recomputes the merge from the repository's
49
+ # current contents on each dispatch.
50
+ #
51
+ # @param repository [Core::Remote::Configuration::Repository] the current RC repository
52
+ # @return [nil]
53
+ def merge_and_apply_configs(repository)
54
+ # Resolve the service identity the same way the RC client registers
55
+ # it with the backend (Core::Remote::Client#service_name =
56
+ # remote.service || service). When a customer sets remote.service,
57
+ # the backend delivers service_target.service values targeting that
58
+ # override; matching against the local service instead would drop
59
+ # every service-scoped config and let a less-specific org config win.
60
+ service = Datadog.configuration.remote.service || Datadog.configuration.service
61
+ env = Datadog.configuration.env
62
+
63
+ # @type var parsed: Array[[::Datadog::Core::Remote::Configuration::Content, ::Hash[::String, untyped]]]
64
+ parsed = []
65
+ repository.contents.each do |content|
66
+ next unless content.path.product == PRODUCT
67
+
68
+ begin
69
+ config = parse_content(content)
70
+ unless config.is_a?(Hash)
71
+ # A syntactically valid but non-object payload (null, [], 42)
72
+ # would later make config_matches? raise outside this per-content
73
+ # rescue and abort the whole merge, erroring valid configs too.
74
+ raise TypeError, "APM_TRACING remote config must be a JSON object, got #{config.class}"
75
+ end
76
+ # A present but non-object lib_config (e.g. {"lib_config": []}) is a
77
+ # schema violation the customer can fix. Acknowledging it as applied
78
+ # would tell the backend the config was successfully applied when it
79
+ # was not, and an empty merge would revert active tracing overrides
80
+ # to their non-RC values. Reject it per content so the malformed
81
+ # payload is reported errored instead. A null or absent lib_config
82
+ # carries no overrides and is left as a no-op.
83
+ lib_config = config["lib_config"]
84
+ if !lib_config.nil? && !lib_config.is_a?(Hash)
85
+ raise TypeError, "APM_TRACING lib_config must be a JSON object, got #{lib_config.class}"
86
+ end
87
+ parsed << [content, config]
88
+ rescue => e
89
+ Datadog.logger.debug { "APM_TRACING RC: skipping unparseable config: #{e.class}: #{e.message}" }
90
+ Datadog.send(:components, allow_initialization: false)&.telemetry&.report(
91
+ e,
92
+ description: "Failed to parse APM_TRACING remote config",
93
+ )
94
+ content.errored("#{e.class}: #{e.message}: #{Array(e.backtrace).join("\n")}")
95
+ end
96
+ end
97
+
98
+ Datadog.logger.debug { "APM_TRACING RC: received #{parsed.length} config(s)" }
99
+
100
+ applicable = parsed.select do |content, config|
101
+ if config_matches?(config, service, env)
102
+ Datadog.logger.debug do
103
+ "APM_TRACING RC: config #{content.path.config_id} " \
104
+ "scope=#{SCOPE_LABELS[config_priority(config)]} priority=#{config_priority(config)}"
105
+ end
106
+ true
107
+ else
108
+ Datadog.logger.debug do
109
+ "APM_TRACING RC: dropped config #{content.path.config_id} " \
110
+ "(service_target=#{config["service_target"].inspect}, self=#{service}/#{env})"
111
+ end
112
+ false
113
+ end
114
+ end
115
+
116
+ # Most-specific first; ties broken by config id (ascending) for a
117
+ # deterministic merge.
118
+ ordered = applicable.sort_by { |content, config| [-config_priority(config), content.path.config_id] }
119
+ merged = merge_lib_configs(ordered.map { |_content, config| config })
120
+
121
+ # Applied even when the set is empty: an emptied repository (last config
122
+ # removed) reverts the tracing overrides to their non-RC values.
123
+ apply_lib_config(merged, repository)
124
+
125
+ parsed.each { |content, _config| content.applied }
126
+ nil
127
+ rescue => e
128
+ Datadog.logger.debug { "APM_TRACING RC: failed to apply configs: #{e.class}: #{e.message}" }
129
+ Datadog.send(:components, allow_initialization: false)&.telemetry&.report(
130
+ e,
131
+ description: "Failed to apply APM_TRACING remote configs",
132
+ )
133
+ parsed&.each do |content, _config|
134
+ content.errored("#{e.class}: #{e.message}: #{Array(e.backtrace).join("\n")}")
135
+ end
136
+ nil
137
+ end
33
138
 
139
+ # Whether a config targets this tracer. A concrete (non-"*") service or
140
+ # env that differs from ours excludes the config; "*" and an absent
141
+ # service_target match anything.
142
+ #
143
+ # @param config [Hash[String, untyped]] a parsed config
144
+ # @param service [String, nil] this tracer's service
145
+ # @param env [String, nil] this tracer's env
146
+ # @return [bool] true when the config applies to this tracer
147
+ def config_matches?(config, service, env)
148
+ target = config["service_target"]
149
+ return true unless target.is_a?(Hash)
150
+
151
+ target_service = target["service"]
152
+ target_env = target["env"]
153
+ return false if target_service && target_service != "*" && target_service != service
154
+ return false if target_env && target_env != "*" && target_env != env
155
+
156
+ true
157
+ end
158
+
159
+ # Specificity of a config, higher meaning more specific: service+env (5),
160
+ # service (4), env (3), cluster (2), org (1). A target counts as concrete
161
+ # only when present and not "*".
162
+ #
163
+ # @param config [Hash[String, untyped]] a parsed config
164
+ # @return [Integer] the specificity rank, 1 through 5
165
+ def config_priority(config)
166
+ target = config["service_target"]
167
+ service = target.is_a?(Hash) ? target["service"] : nil
168
+ env = target.is_a?(Hash) ? target["env"] : nil
169
+ single_service = !service.nil? && service != "*"
170
+ single_env = !env.nil? && env != "*"
171
+
172
+ return 5 if single_service && single_env
173
+ return 4 if single_service
174
+ return 3 if single_env
175
+ return 2 unless config["k8s_target_v2"].nil?
176
+
177
+ 1
178
+ end
179
+
180
+ # Merges lib_configs from configs ordered most-specific first: for each
181
+ # field, the first (most-specific) non-nil value wins. Fields are
182
+ # independent, so a lower-priority config can supply a field the
183
+ # higher-priority one omits.
184
+ #
185
+ # @param configs_most_specific_first [Array[Hash[String, untyped]]] configs, most-specific first
186
+ # @return [Hash[String, untyped]] the merged lib_config
187
+ def merge_lib_configs(configs_most_specific_first)
188
+ merged = {}
189
+ configs_most_specific_first.each do |config|
190
+ lib_config = config["lib_config"]
191
+ next unless lib_config.is_a?(Hash)
192
+
193
+ lib_config.each do |key, value|
194
+ next if value.nil?
195
+
196
+ merged[key] = value unless merged.key?(key)
197
+ end
198
+ end
199
+ merged
200
+ end
201
+
202
+ # Applies one lib_config: maps the dynamic OPTIONS to telemetry, drives DI
203
+ # enablement from "dynamic_instrumentation_enabled", and reports the
204
+ # configuration change to telemetry.
205
+ #
206
+ # @param lib_config [Hash[String, untyped]] the lib_config to apply
207
+ # @param repository [Core::Remote::Configuration::Repository, nil] forwarded to DI enablement
208
+ # @return [nil]
209
+ def apply_lib_config(lib_config, repository)
34
210
  env_vars = Datadog::Tracing::Configuration::Dynamic::OPTIONS.map do |name, env_var, option|
35
211
  value = lib_config[name]
36
212
 
@@ -55,11 +231,11 @@ module Datadog
55
231
  if di_enabled
56
232
  components&.symbol_database&.resume_pending_upload
57
233
  # Advertise the DI products only if the component actually started.
58
- # handle_rc_enablement above no-ops when DI cannot run — the
59
- # component is nil on an unsupported runtime, or the enable signal
60
- # is blocked by DD_DYNAMIC_INSTRUMENTATION_ENABLED=false. Advertising
61
- # then would report DI as in use when it is not and invite probe
62
- # configs the tracer must refuse; withdraw the products otherwise.
234
+ # handle_rc_enablement above no-ops when DI cannot run: the component
235
+ # is nil on an unsupported runtime, or the enable signal is blocked
236
+ # by DD_DYNAMIC_INSTRUMENTATION_ENABLED=false. Advertising then would
237
+ # report DI as in use when it is not and invite probe configs the
238
+ # tracer must refuse; withdraw the products otherwise.
63
239
  if components&.dynamic_instrumentation&.started?
64
240
  components&.remote&.add_products(*di_products)
65
241
  else
@@ -69,33 +245,25 @@ module Datadog
69
245
  components&.symbol_database&.stop_for_di_disable
70
246
  components&.remote&.remove_products(*di_products)
71
247
  end
72
- end
73
248
 
74
- content.applied
249
+ Datadog.logger.debug { "APM_TRACING RC: merged dynamic_instrumentation_enabled=#{di_enabled}" }
250
+ end
75
251
 
76
- # allow_initialization: false because process_config runs on the
77
- # remote-config worker thread. If components haven't been built yet
78
- # (e.g. during a teardown/reset window), the default `true` would
79
- # synchronously build the entire component tree from this thread.
80
- # The &. chain matches the pattern used by DI::Remote.handle_rc_enablement
81
- # in the same dispatch path.
252
+ # allow_initialization: false because this runs on the remote-config
253
+ # worker thread. If components haven't been built yet (e.g. during a
254
+ # teardown/reset window), the default value would synchronously build
255
+ # the entire component tree from this thread. The &. chain matches the
256
+ # pattern used by DI::Remote.handle_rc_enablement in the same dispatch
257
+ # path.
82
258
  Datadog.send(:components, allow_initialization: false)&.telemetry&.client_configuration_change!(env_vars)
83
- rescue => e
84
- content.errored("#{e.class}: #{e.message}: #{Array(e.backtrace).join("\n")}")
259
+ nil
85
260
  end
86
261
 
262
+ # @param _telemetry [Core::Telemetry::Component] unused; kept for the receiver contract
263
+ # @return [Array[Core::Remote::Dispatcher::Receiver]] the APM_TRACING receiver
87
264
  def receivers(_telemetry)
88
265
  receiver do |repository, _changes|
89
- # DEV: Filter our by product. Given it will be very common
90
- # DEV: we can filter this out before we receive the data in this method.
91
- # DEV: Apply this refactor to AppSec as well if implemented.
92
- repository.contents.map do |content|
93
- case content.path.product
94
- when PRODUCT
95
- config = parse_content(content)
96
- process_config(config, content, repository)
97
- end
98
- end
266
+ merge_and_apply_configs(repository)
99
267
  end
100
268
  end
101
269
 
@@ -192,6 +192,8 @@ module Datadog
192
192
 
193
193
  def reconsider_matching_rule!(trace, rule)
194
194
  trace.agent_sample_rate = nil
195
+ # Avoid mistaking a reconsidered probabilistic drop for a limiter drop.
196
+ trace.rate_limiter_rate = nil
195
197
  trace.clear_tag(Tracing::Metadata::Ext::Distributed::TAG_DECISION_MAKER)
196
198
  apply_rule!(trace, rule)
197
199
  end
@@ -72,10 +72,7 @@ module Datadog
72
72
  # @return [Integer]
73
73
  # @see https://www.w3.org/TR/trace-context/#trace-flags
74
74
  # @!attribute [r] trace_state
75
- # The W3C "tracestate" extracted from a distributed context.
76
- # This field is a string representing vendor-specific distribution data.
77
- # The `dd=` entry is removed from `trace_state` as its value is dynamically calculated
78
- # on every propagation injection.
75
+ # W3C "tracestate" members other than `dd=` and `ot=`, which are stored in parsed fields.
79
76
  # @return [String]
80
77
  # @see https://www.w3.org/TR/trace-context/#tracestate-header
81
78
  # @!attribute [r] trace_state_unknown_fields
@@ -83,6 +80,15 @@ module Datadog
83
80
  # This allows later propagation to include those unknown fields, as they can represent future versions of the spec
84
81
  # sending data through this service. This value ends in a trailing `;` to facilitate serialization.
85
82
  # @return [String]
83
+ # @!attribute [r] trace_otel_random_value
84
+ # OpenTelemetry consistent probability sampling random value.
85
+ # @return [String]
86
+ # @!attribute [r] trace_otel_threshold
87
+ # OpenTelemetry consistent probability sampling threshold.
88
+ # @return [String]
89
+ # @!attribute [r] trace_otel_unknown_fields
90
+ # Unrecognized `ot=` fields, retained with a trailing `;` for propagation.
91
+ # @return [String]
86
92
  # @!attribute [r] baggage
87
93
  # The W3C "baggage" extracted from a distributed context. This field is a hash of key/value pairs.
88
94
  # @return [Hash<String,String>]
@@ -109,6 +115,9 @@ module Datadog
109
115
  :trace_flags,
110
116
  :trace_state,
111
117
  :trace_state_unknown_fields,
118
+ :trace_otel_random_value,
119
+ :trace_otel_threshold,
120
+ :trace_otel_unknown_fields,
112
121
  :span_remote,
113
122
  :baggage
114
123
 
@@ -133,6 +142,9 @@ module Datadog
133
142
  trace_flags: nil,
134
143
  trace_state: nil,
135
144
  trace_state_unknown_fields: nil,
145
+ trace_otel_random_value: nil,
146
+ trace_otel_threshold: nil,
147
+ trace_otel_unknown_fields: nil,
136
148
  span_remote: true,
137
149
  baggage: nil
138
150
  )
@@ -156,6 +168,9 @@ module Datadog
156
168
  @trace_flags = trace_flags
157
169
  @trace_state = trace_state && trace_state.dup.freeze
158
170
  @trace_state_unknown_fields = trace_state_unknown_fields && trace_state_unknown_fields.dup.freeze
171
+ @trace_otel_random_value = trace_otel_random_value && trace_otel_random_value.dup.freeze
172
+ @trace_otel_threshold = trace_otel_threshold && trace_otel_threshold.dup.freeze
173
+ @trace_otel_unknown_fields = trace_otel_unknown_fields && trace_otel_unknown_fields.dup.freeze
159
174
  @span_remote = span_remote
160
175
  @baggage = baggage && baggage.dup.freeze
161
176
  freeze
@@ -188,6 +203,9 @@ module Datadog
188
203
  trace_flags: trace_flags,
189
204
  trace_state: trace_state,
190
205
  trace_state_unknown_fields: trace_state_unknown_fields,
206
+ trace_otel_random_value: trace_otel_random_value,
207
+ trace_otel_threshold: trace_otel_threshold,
208
+ trace_otel_unknown_fields: trace_otel_unknown_fields,
191
209
  span_remote: span_remote,
192
210
  baggage: baggage, **field_value_pairs
193
211
  )