datadog 2.12.2 → 2.13.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 (113) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +36 -1
  3. data/ext/datadog_profiling_native_extension/collectors_thread_context.c +14 -13
  4. data/lib/datadog/appsec/actions_handler/serializable_backtrace.rb +89 -0
  5. data/lib/datadog/appsec/actions_handler.rb +22 -1
  6. data/lib/datadog/appsec/anonymizer.rb +16 -0
  7. data/lib/datadog/appsec/configuration/settings.rb +62 -10
  8. data/lib/datadog/appsec/contrib/auto_instrument.rb +1 -1
  9. data/lib/datadog/appsec/contrib/devise/configuration.rb +7 -31
  10. data/lib/datadog/appsec/contrib/devise/data_extractor.rb +79 -0
  11. data/lib/datadog/appsec/contrib/devise/ext.rb +21 -0
  12. data/lib/datadog/appsec/contrib/devise/integration.rb +0 -1
  13. data/lib/datadog/appsec/contrib/devise/patcher.rb +36 -23
  14. data/lib/datadog/appsec/contrib/devise/patches/signin_tracking_patch.rb +102 -0
  15. data/lib/datadog/appsec/contrib/devise/patches/signup_tracking_patch.rb +69 -0
  16. data/lib/datadog/appsec/contrib/devise/{patcher/rememberable_patch.rb → patches/skip_signin_tracking_patch.rb} +2 -2
  17. data/lib/datadog/appsec/contrib/devise/tracking_middleware.rb +93 -0
  18. data/lib/datadog/appsec/contrib/rack/ext.rb +14 -0
  19. data/lib/datadog/appsec/contrib/rack/gateway/watcher.rb +10 -3
  20. data/lib/datadog/appsec/contrib/rack/request_middleware.rb +0 -2
  21. data/lib/datadog/appsec/event.rb +1 -1
  22. data/lib/datadog/appsec/ext.rb +4 -2
  23. data/lib/datadog/appsec/instrumentation/gateway/argument.rb +4 -2
  24. data/lib/datadog/appsec/monitor/gateway/watcher.rb +8 -3
  25. data/lib/datadog/appsec/security_engine/runner.rb +2 -2
  26. data/lib/datadog/appsec/utils.rb +0 -2
  27. data/lib/datadog/core/configuration/components.rb +2 -1
  28. data/lib/datadog/core/configuration/ext.rb +4 -0
  29. data/lib/datadog/core/configuration/options.rb +2 -2
  30. data/lib/datadog/core/configuration/settings.rb +53 -30
  31. data/lib/datadog/core/environment/agent_info.rb +4 -3
  32. data/lib/datadog/core/remote/component.rb +3 -6
  33. data/lib/datadog/core/remote/configuration/repository.rb +2 -1
  34. data/lib/datadog/core/remote/negotiation.rb +9 -9
  35. data/lib/datadog/core/remote/transport/config.rb +4 -3
  36. data/lib/datadog/core/remote/transport/http/client.rb +4 -3
  37. data/lib/datadog/core/remote/transport/http/config.rb +6 -32
  38. data/lib/datadog/core/remote/transport/http/negotiation.rb +6 -32
  39. data/lib/datadog/core/remote/transport/http.rb +22 -57
  40. data/lib/datadog/core/remote/transport/negotiation.rb +4 -3
  41. data/lib/datadog/core/runtime/metrics.rb +8 -1
  42. data/lib/datadog/core/telemetry/http/adapters/net.rb +1 -1
  43. data/lib/datadog/core/transport/http/api/instance.rb +17 -0
  44. data/lib/datadog/core/transport/http/api/spec.rb +17 -0
  45. data/lib/datadog/core/transport/http/builder.rb +5 -3
  46. data/lib/datadog/core/transport/http.rb +39 -2
  47. data/lib/datadog/di/component.rb +0 -2
  48. data/lib/datadog/di/probe_notifier_worker.rb +16 -16
  49. data/lib/datadog/di/transport/diagnostics.rb +4 -3
  50. data/lib/datadog/di/transport/http/api.rb +2 -12
  51. data/lib/datadog/di/transport/http/client.rb +4 -3
  52. data/lib/datadog/di/transport/http/diagnostics.rb +7 -33
  53. data/lib/datadog/di/transport/http/input.rb +7 -33
  54. data/lib/datadog/di/transport/http.rb +14 -56
  55. data/lib/datadog/di/transport/input.rb +4 -3
  56. data/lib/datadog/di/utils.rb +5 -0
  57. data/lib/datadog/kit/appsec/events.rb +9 -0
  58. data/lib/datadog/kit/identity.rb +5 -1
  59. data/lib/datadog/opentelemetry/api/baggage.rb +90 -0
  60. data/lib/datadog/opentelemetry/api/baggage.rbs +26 -0
  61. data/lib/datadog/opentelemetry/api/context.rb +16 -2
  62. data/lib/datadog/opentelemetry/sdk/trace/span.rb +1 -1
  63. data/lib/datadog/opentelemetry.rb +2 -1
  64. data/lib/datadog/profiling/collectors/thread_context.rb +1 -1
  65. data/lib/datadog/profiling.rb +5 -2
  66. data/lib/datadog/tracing/component.rb +15 -12
  67. data/lib/datadog/tracing/configuration/ext.rb +7 -1
  68. data/lib/datadog/tracing/configuration/settings.rb +18 -2
  69. data/lib/datadog/tracing/context_provider.rb +1 -1
  70. data/lib/datadog/tracing/contrib/configuration/settings.rb +1 -1
  71. data/lib/datadog/tracing/contrib/ethon/easy_patch.rb +4 -5
  72. data/lib/datadog/tracing/contrib/excon/middleware.rb +5 -3
  73. data/lib/datadog/tracing/contrib/faraday/middleware.rb +5 -3
  74. data/lib/datadog/tracing/contrib/grpc/datadog_interceptor/client.rb +7 -1
  75. data/lib/datadog/tracing/contrib/grpc/distributed/propagation.rb +3 -0
  76. data/lib/datadog/tracing/contrib/http/circuit_breaker.rb +0 -15
  77. data/lib/datadog/tracing/contrib/http/distributed/propagation.rb +4 -1
  78. data/lib/datadog/tracing/contrib/http/instrumentation.rb +5 -5
  79. data/lib/datadog/tracing/contrib/httpclient/instrumentation.rb +5 -11
  80. data/lib/datadog/tracing/contrib/httprb/instrumentation.rb +6 -10
  81. data/lib/datadog/tracing/contrib/rest_client/request_patch.rb +5 -3
  82. data/lib/datadog/tracing/contrib/sidekiq/client_tracer.rb +6 -1
  83. data/lib/datadog/tracing/contrib/sidekiq/distributed/propagation.rb +3 -0
  84. data/lib/datadog/tracing/correlation.rb +9 -2
  85. data/lib/datadog/tracing/distributed/baggage.rb +131 -0
  86. data/lib/datadog/tracing/distributed/datadog.rb +2 -0
  87. data/lib/datadog/tracing/distributed/propagation.rb +25 -4
  88. data/lib/datadog/tracing/distributed/propagation_policy.rb +42 -0
  89. data/lib/datadog/tracing/metadata/ext.rb +5 -0
  90. data/lib/datadog/tracing/sampling/span/rule.rb +0 -1
  91. data/lib/datadog/tracing/span_operation.rb +2 -1
  92. data/lib/datadog/tracing/sync_writer.rb +1 -2
  93. data/lib/datadog/tracing/trace_digest.rb +9 -2
  94. data/lib/datadog/tracing/trace_operation.rb +29 -17
  95. data/lib/datadog/tracing/trace_segment.rb +6 -4
  96. data/lib/datadog/tracing/tracer.rb +38 -2
  97. data/lib/datadog/tracing/transport/http/api.rb +2 -10
  98. data/lib/datadog/tracing/transport/http/client.rb +5 -4
  99. data/lib/datadog/tracing/transport/http/traces.rb +13 -41
  100. data/lib/datadog/tracing/transport/http.rb +11 -44
  101. data/lib/datadog/tracing/transport/trace_formatter.rb +7 -0
  102. data/lib/datadog/tracing/transport/traces.rb +21 -9
  103. data/lib/datadog/tracing/workers/trace_writer.rb +2 -6
  104. data/lib/datadog/tracing/writer.rb +2 -6
  105. data/lib/datadog/tracing.rb +16 -3
  106. data/lib/datadog/version.rb +2 -2
  107. metadata +17 -13
  108. data/lib/datadog/appsec/contrib/devise/event.rb +0 -54
  109. data/lib/datadog/appsec/contrib/devise/patcher/authenticatable_patch.rb +0 -72
  110. data/lib/datadog/appsec/contrib/devise/patcher/registration_controller_patch.rb +0 -47
  111. data/lib/datadog/appsec/contrib/devise/resource.rb +0 -35
  112. data/lib/datadog/appsec/contrib/devise/tracking.rb +0 -57
  113. data/lib/datadog/appsec/utils/trace_operation.rb +0 -15
@@ -42,7 +42,7 @@ module Datadog
42
42
  #
43
43
  # The tracer will try to find distributed headers in the order they are present in the list provided to this option.
44
44
  # The first format to have valid data present will be used.
45
- #
45
+ # Baggage style is a special case, as it will always be extracted in addition if present.
46
46
  # @default `DD_TRACE_PROPAGATION_STYLE_EXTRACT` environment variable (comma-separated list),
47
47
  # otherwise `['datadog','b3multi','b3']`.
48
48
  # @return [Array<String>]
@@ -53,6 +53,7 @@ module Datadog
53
53
  [
54
54
  Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_DATADOG,
55
55
  Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_TRACE_CONTEXT,
56
+ Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_BAGGAGE,
56
57
  ]
57
58
  )
58
59
  o.after_set do |styles|
@@ -74,6 +75,7 @@ module Datadog
74
75
  o.default [
75
76
  Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_DATADOG,
76
77
  Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_TRACE_CONTEXT,
78
+ Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_BAGGAGE,
77
79
  ]
78
80
  o.after_set do |styles|
79
81
  # Make values case-insensitive
@@ -186,7 +188,7 @@ module Datadog
186
188
  # It is not supported by our backend yet. Do not enable it.
187
189
  option :trace_id_128_bit_logging_enabled do |o|
188
190
  o.env Tracing::Configuration::Ext::Correlation::ENV_TRACE_ID_128_BIT_LOGGING_ENABLED
189
- o.default false
191
+ o.default true
190
192
  o.type :bool
191
193
  end
192
194
 
@@ -249,6 +251,20 @@ module Datadog
249
251
  o.type :bool
250
252
  end
251
253
 
254
+ # Forces the tracer to always send span events with the native span events format
255
+ # regardless of the agent support. This is useful in agent-less setups.
256
+ #
257
+ # When set to `nil`, the default, the agent will be queried for
258
+ # native span events support.
259
+ #
260
+ # @default `DD_TRACE_NATIVE_SPAN_EVENTS` environment variable, otherwise `false`
261
+ # @return [Boolean,nil]
262
+ option :native_span_events do |o|
263
+ o.env Tracing::Configuration::Ext::ENV_NATIVE_SPAN_EVENTS
264
+ o.default nil
265
+ o.type :bool, nilable: true
266
+ end
267
+
252
268
  # A custom sampler instance.
253
269
  # The object must respect the {Datadog::Tracing::Sampling::Sampler} interface.
254
270
  # @default `nil`
@@ -49,7 +49,7 @@ module Datadog
49
49
  # To support multiple tracers simultaneously, each {Datadog::Tracing::FiberLocalContext}
50
50
  # instance has its own fiber-local variable.
51
51
  def initialize
52
- @key = "datadog_context_#{FiberLocalContext.next_instance_id}".to_sym
52
+ @key = :"datadog_context_#{FiberLocalContext.next_instance_id}"
53
53
 
54
54
  self.local = Context.new
55
55
  end
@@ -22,7 +22,7 @@ module Datadog
22
22
  end
23
23
 
24
24
  def configure(options = {})
25
- self.class.options.each do |name, _value|
25
+ self.class.options.each_key do |name|
26
26
  self[name] = options[name] if options.key?(name)
27
27
  end
28
28
 
@@ -110,11 +110,10 @@ module Datadog
110
110
 
111
111
  datadog_tag_request
112
112
 
113
- if Datadog::AppSec::Utils::TraceOperation.appsec_standalone_reject?(datadog_trace)
114
- datadog_trace.sampling_priority = Tracing::Sampling::Ext::Priority::AUTO_REJECT
115
- end
116
-
117
- if datadog_configuration[:distributed_tracing]
113
+ if Tracing::Distributed::PropagationPolicy.enabled?(
114
+ global_config: datadog_configuration,
115
+ trace: datadog_trace
116
+ )
118
117
  @datadog_original_headers ||= {}
119
118
  Contrib::HTTP.inject(datadog_trace, @datadog_original_headers)
120
119
  self.headers = @datadog_original_headers
@@ -30,10 +30,12 @@ module Datadog
30
30
  trace = Tracing.active_trace
31
31
  datum[:datadog_span] = span
32
32
  annotate!(span, datum)
33
- if Datadog::AppSec::Utils::TraceOperation.appsec_standalone_reject?(trace)
34
- trace.sampling_priority = Tracing::Sampling::Ext::Priority::AUTO_REJECT
33
+ if Tracing::Distributed::PropagationPolicy.enabled?(
34
+ global_config: @options,
35
+ trace: trace
36
+ )
37
+ propagate!(trace, span, datum)
35
38
  end
36
- propagate!(trace, span, datum) if distributed_tracing?
37
39
 
38
40
  span
39
41
  end
@@ -29,10 +29,12 @@ module Datadog
29
29
 
30
30
  Tracing.trace(Ext::SPAN_REQUEST, on_error: request_options[:on_error]) do |span, trace|
31
31
  annotate!(span, env, request_options)
32
- if Datadog::AppSec::Utils::TraceOperation.appsec_standalone_reject?(trace)
33
- trace.sampling_priority = Tracing::Sampling::Ext::Priority::AUTO_REJECT
32
+ if Tracing::Distributed::PropagationPolicy.enabled?(
33
+ global_config: request_options,
34
+ trace: trace
35
+ )
36
+ propagate!(trace, span, env)
34
37
  end
35
- propagate!(trace, span, env) if request_options[:distributed_tracing] && Tracing.enabled?
36
38
  app.call(env).on_complete { |resp| handle_response(span, resp, request_options) }
37
39
  end
38
40
  end
@@ -81,7 +81,13 @@ module Datadog
81
81
  # Set analytics sample rate
82
82
  Contrib::Analytics.set_sample_rate(span, analytics_sample_rate) if analytics_enabled?
83
83
 
84
- GRPC.inject(trace, metadata) if distributed_tracing?
84
+ if Tracing::Distributed::PropagationPolicy.enabled?(
85
+ pin_config: Datadog.configuration_for(self),
86
+ global_config: datadog_configuration,
87
+ trace: trace
88
+ )
89
+ GRPC.inject(trace, metadata)
90
+ end
85
91
  Contrib::SpanAttributeSchema.set_peer_service!(span, Ext::PEER_SERVICE_SOURCES)
86
92
  rescue StandardError => e
87
93
  Datadog.logger.debug("GRPC client trace failed: #{e}")
@@ -3,6 +3,7 @@
3
3
  require_relative 'fetcher'
4
4
  require_relative '../../../distributed/b3_multi'
5
5
  require_relative '../../../distributed/b3_single'
6
+ require_relative '../../../distributed/propagation_policy'
6
7
  require_relative '../../../distributed/datadog'
7
8
  require_relative '../../../distributed/none'
8
9
  require_relative '../../../distributed/propagation'
@@ -31,6 +32,8 @@ module Datadog
31
32
  Tracing::Distributed::Datadog.new(fetcher: Fetcher),
32
33
  Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_TRACE_CONTEXT =>
33
34
  Tracing::Distributed::TraceContext.new(fetcher: Fetcher),
35
+ Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_BAGGAGE =>
36
+ Tracing::Distributed::Baggage.new(fetcher: Fetcher),
34
37
  Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_NONE => Tracing::Distributed::None.new
35
38
  },
36
39
  propagation_style_inject: propagation_style_inject,
@@ -27,21 +27,6 @@ module Datadog
27
27
  !!(request[Datadog::Core::Transport::Ext::HTTP::HEADER_META_TRACER_VERSION] ||
28
28
  request[Datadog::Core::Transport::Ext::HTTP::HEADER_DD_INTERNAL_UNTRACED_REQUEST])
29
29
  end
30
-
31
- def should_skip_distributed_tracing?(client_config)
32
- if Datadog.configuration.appsec.standalone.enabled
33
- # Skip distributed tracing so that we don't bill distributed traces in case of absence of
34
- # upstream ASM event (_dd.p.appsec:1) and no local security event (which sets _dd.p.appsec:1 locally).
35
- # If there is an ASM event, we still have to check if distributed tracing is enabled or not
36
- return true unless Tracing.active_trace
37
-
38
- return true if Tracing.active_trace.get_tag(Datadog::AppSec::Ext::TAG_DISTRIBUTED_APPSEC_EVENT) != '1'
39
- end
40
-
41
- return !client_config[:distributed_tracing] if client_config && client_config.key?(:distributed_tracing)
42
-
43
- !Datadog.configuration.tracing[:http][:distributed_tracing]
44
- end
45
30
  end
46
31
  end
47
32
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative 'fetcher'
4
4
  require_relative '../../../distributed/propagation'
5
+ require_relative '../../../distributed/propagation_policy'
5
6
  require_relative '../../../distributed/b3_multi'
6
7
  require_relative '../../../distributed/b3_single'
7
8
  require_relative '../../../distributed/datadog'
@@ -30,7 +31,9 @@ module Datadog
30
31
  Tracing::Distributed::Datadog.new(fetcher: Fetcher),
31
32
  Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_TRACE_CONTEXT =>
32
33
  Tracing::Distributed::TraceContext.new(fetcher: Fetcher),
33
- Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_NONE => Tracing::Distributed::None.new
34
+ Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_BAGGAGE =>
35
+ Tracing::Distributed::Baggage.new(fetcher: Fetcher),
36
+ Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_NONE => Tracing::Distributed::None.new,
34
37
  },
35
38
  propagation_style_inject: propagation_style_inject,
36
39
  propagation_style_extract: propagation_style_extract,
@@ -35,11 +35,11 @@ module Datadog
35
35
  span.type = Tracing::Metadata::Ext::HTTP::TYPE_OUTBOUND
36
36
  span.resource = req.method
37
37
 
38
- if Datadog::AppSec::Utils::TraceOperation.appsec_standalone_reject?(trace)
39
- trace.sampling_priority = Tracing::Sampling::Ext::Priority::AUTO_REJECT
40
- end
41
-
42
- if Tracing.enabled? && !Contrib::HTTP.should_skip_distributed_tracing?(client_config)
38
+ if Tracing::Distributed::PropagationPolicy.enabled?(
39
+ pin_config: client_config,
40
+ global_config: Datadog.configuration.tracing[:http],
41
+ trace: trace
42
+ )
43
43
  Contrib::HTTP.inject(trace, req)
44
44
  end
45
45
 
@@ -30,11 +30,11 @@ module Datadog
30
30
  span.service = service_name(host, request_options, client_config)
31
31
  span.type = Tracing::Metadata::Ext::HTTP::TYPE_OUTBOUND
32
32
 
33
- if Datadog::AppSec::Utils::TraceOperation.appsec_standalone_reject?(trace)
34
- trace.sampling_priority = Tracing::Sampling::Ext::Priority::AUTO_REJECT
35
- end
36
-
37
- if Tracing.enabled? && !should_skip_distributed_tracing?(client_config)
33
+ if Tracing::Distributed::PropagationPolicy.enabled?(
34
+ pin_config: client_config,
35
+ global_config: Datadog.configuration.tracing[:httpclient],
36
+ trace: trace
37
+ )
38
38
  Contrib::HTTP.inject(trace, req.header)
39
39
  end
40
40
 
@@ -123,12 +123,6 @@ module Datadog
123
123
  Contrib::Analytics.enabled?(request_options[:analytics_enabled])
124
124
  end
125
125
 
126
- def should_skip_distributed_tracing?(client_config)
127
- return !client_config[:distributed_tracing] if client_config && client_config.key?(:distributed_tracing)
128
-
129
- !Datadog.configuration.tracing[:httpclient][:distributed_tracing]
130
- end
131
-
132
126
  def set_analytics_sample_rate(span, request_options)
133
127
  return unless analytics_enabled?(request_options)
134
128
 
@@ -30,12 +30,14 @@ module Datadog
30
30
  span.service = service_name(host, request_options, client_config)
31
31
  span.type = Tracing::Metadata::Ext::HTTP::TYPE_OUTBOUND
32
32
 
33
- if Datadog::AppSec::Utils::TraceOperation.appsec_standalone_reject?(trace)
34
- trace.sampling_priority = Tracing::Sampling::Ext::Priority::AUTO_REJECT
33
+ if Tracing::Distributed::PropagationPolicy.enabled?(
34
+ pin_config: client_config,
35
+ global_config: Datadog.configuration.tracing[:httprb],
36
+ trace: trace
37
+ )
38
+ Contrib::HTTP.inject(trace, req)
35
39
  end
36
40
 
37
- Contrib::HTTP.inject(trace, req) if Tracing.enabled? && !should_skip_distributed_tracing?(client_config)
38
-
39
41
  # Add additional request specific tags to the span.
40
42
  annotate_span_with_request!(span, req, request_options)
41
43
  rescue StandardError => e
@@ -135,12 +137,6 @@ module Datadog
135
137
  Datadog.logger
136
138
  end
137
139
 
138
- def should_skip_distributed_tracing?(client_config)
139
- return !client_config[:distributed_tracing] if client_config && client_config.key?(:distributed_tracing)
140
-
141
- !Datadog.configuration.tracing[:httprb][:distributed_tracing]
142
- end
143
-
144
140
  def set_analytics_sample_rate(span, request_options)
145
141
  return unless analytics_enabled?(request_options)
146
142
 
@@ -25,10 +25,12 @@ module Datadog
25
25
  return super(&block) unless Tracing.enabled?
26
26
 
27
27
  datadog_trace_request(uri) do |_span, trace|
28
- if Datadog::AppSec::Utils::TraceOperation.appsec_standalone_reject?(trace)
29
- trace.sampling_priority = Tracing::Sampling::Ext::Priority::AUTO_REJECT
28
+ if Tracing::Distributed::PropagationPolicy.enabled?(
29
+ global_config: datadog_configuration,
30
+ trace: trace
31
+ )
32
+ Contrib::HTTP.inject(trace, processed_headers)
30
33
  end
31
- Contrib::HTTP.inject(trace, processed_headers) if datadog_configuration[:distributed_tracing]
32
34
 
33
35
  super(&block)
34
36
  end
@@ -24,7 +24,12 @@ module Datadog
24
24
  resource = job_resource(job)
25
25
 
26
26
  Datadog::Tracing.trace(Ext::SPAN_PUSH, service: @sidekiq_service) do |span, trace_op|
27
- Sidekiq.inject(trace_op, job) if configuration[:distributed_tracing]
27
+ if Tracing::Distributed::PropagationPolicy.enabled?(
28
+ global_config: configuration,
29
+ trace: trace_op
30
+ )
31
+ Sidekiq.inject(trace_op, job)
32
+ end
28
33
 
29
34
  span.resource = resource
30
35
 
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative '../../../distributed/fetcher'
4
4
  require_relative '../../../distributed/propagation'
5
+ require_relative '../../../distributed/propagation_policy'
5
6
  require_relative '../../../distributed/b3_multi'
6
7
  require_relative '../../../distributed/b3_single'
7
8
  require_relative '../../../distributed/datadog'
@@ -31,6 +32,8 @@ module Datadog
31
32
  Tracing::Distributed::Datadog.new(fetcher: Tracing::Distributed::Fetcher),
32
33
  Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_TRACE_CONTEXT =>
33
34
  Tracing::Distributed::TraceContext.new(fetcher: Tracing::Distributed::Fetcher),
35
+ Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_BAGGAGE =>
36
+ Tracing::Distributed::Baggage.new(fetcher: Tracing::Distributed::Fetcher),
34
37
  Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_NONE => Tracing::Distributed::None.new
35
38
  },
36
39
  propagation_style_inject: propagation_style_inject,
@@ -94,8 +94,15 @@ module Datadog
94
94
  end
95
95
 
96
96
  def format_trace_id(trace_id)
97
- if Datadog.configuration.tracing.trace_id_128_bit_logging_enabled &&
98
- !Tracing::Utils::TraceId.to_high_order(trace_id).zero?
97
+ if Datadog.configuration.tracing.trace_id_128_bit_logging_enabled
98
+ format_trace_id_128(trace_id)
99
+ else
100
+ Tracing::Utils::TraceId.to_low_order(trace_id).to_s
101
+ end
102
+ end
103
+
104
+ def format_trace_id_128(trace_id)
105
+ if !Tracing::Utils::TraceId.to_high_order(trace_id).zero?
99
106
  Kernel.format('%032x', trace_id)
100
107
  else
101
108
  Tracing::Utils::TraceId.to_low_order(trace_id).to_s
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../metadata/ext'
4
+ require_relative '../trace_digest'
5
+ require_relative 'datadog_tags_codec'
6
+ require_relative '../utils'
7
+ require_relative 'helpers'
8
+ require 'uri'
9
+
10
+ module Datadog
11
+ module Tracing
12
+ module Distributed
13
+ # W3C Baggage propagator implementation.
14
+ # The baggage header is propagated through `baggage`.
15
+ # @see https://www.w3.org/TR/baggage/
16
+ class Baggage
17
+ BAGGAGE_KEY = 'baggage'
18
+ DD_TRACE_BAGGAGE_MAX_ITEMS = 64
19
+ DD_TRACE_BAGGAGE_MAX_BYTES = 8192
20
+ SAFE_CHARACTERS_KEY = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789$!#&'*+-.^_`|~"
21
+ SAFE_CHARACTERS_VALUE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789$!#&'()*+-./:<>?@[]^_`{|}~"
22
+
23
+ def initialize(
24
+ fetcher:,
25
+ baggage_key: BAGGAGE_KEY
26
+ )
27
+ @baggage_key = baggage_key
28
+ @fetcher = fetcher
29
+ end
30
+
31
+ def inject!(digest, data)
32
+ return if digest.nil? || digest.baggage.nil?
33
+
34
+ baggage_items = digest.baggage.reject { |k, v| k.nil? || v.nil? }
35
+ return if baggage_items.empty?
36
+
37
+ begin
38
+ if baggage_items.size > DD_TRACE_BAGGAGE_MAX_ITEMS
39
+ ::Datadog.logger.warn('Baggage item limit exceeded, dropping excess items')
40
+ baggage_items = baggage_items.first(DD_TRACE_BAGGAGE_MAX_ITEMS)
41
+ end
42
+
43
+ encoded_items = []
44
+ total_size = 0
45
+
46
+ baggage_items.each do |key, value|
47
+ item = "#{encode_item(key, SAFE_CHARACTERS_KEY)}=#{encode_item(value, SAFE_CHARACTERS_VALUE)}"
48
+ item_size = item.bytesize + (encoded_items.empty? ? 0 : 1) # +1 for comma if not first item
49
+ if total_size + item_size > DD_TRACE_BAGGAGE_MAX_BYTES
50
+ ::Datadog.logger.warn('Baggage header size exceeded, dropping excess items')
51
+ break # stop adding items when size limit is reached
52
+ end
53
+ encoded_items << item
54
+ total_size += item_size
55
+ end
56
+
57
+ # edge case where a single item is too large
58
+ return if encoded_items.empty?
59
+
60
+ header_value = encoded_items.join(',')
61
+ data[@baggage_key] = header_value
62
+ rescue => e
63
+ ::Datadog.logger.warn("Failed to encode and inject baggage header: #{e.message}")
64
+ end
65
+ end
66
+
67
+ def extract(data)
68
+ fetcher = @fetcher.new(data)
69
+ data = fetcher[@baggage_key]
70
+ return unless data
71
+
72
+ baggage = parse_baggage_header(fetcher[@baggage_key])
73
+ return unless baggage
74
+
75
+ TraceDigest.new(
76
+ baggage: baggage,
77
+ )
78
+ end
79
+
80
+ private
81
+
82
+ def encode_item(item, safe_characters)
83
+ # Strip whitespace and URL-encode the item
84
+ result = URI.encode_www_form_component(item.strip)
85
+ # Replace '+' with '%20' for space encoding consistency with W3C spec
86
+ result = result.gsub('+', '%20')
87
+ # Selectively decode percent-encoded characters that are considered "safe" in W3C Baggage spec
88
+ result.gsub(/%[0-9A-F]{2}/) do |encoded|
89
+ if encoded.size >= 3 && encoded[1..2] =~ /\A[0-9A-F]{2}\z/
90
+ hex_str = encoded[1..2]
91
+ next encoded unless hex_str && !hex_str.empty?
92
+
93
+ # Convert hex representation back to character
94
+ char = [hex_str.hex].pack('C')
95
+ # Keep the character as-is if it's in the safe character set, otherwise keep it encoded
96
+ safe_characters.include?(char) ? char : encoded
97
+ else
98
+ encoded
99
+ end
100
+ end
101
+ end
102
+
103
+ # Parses a W3C Baggage header string into a hash of key-value pairs
104
+ # The header format follows the W3C Baggage specification:
105
+ # - Multiple baggage items are separated by commas
106
+ # - Each baggage item is a key-value pair separated by '='
107
+ # - Keys and values are URL-encoded
108
+ # - Returns an empty hash if the baggage header is malformed
109
+ #
110
+ # @param baggage_header [String] The W3C Baggage header string to parse
111
+ # @return [Hash<String, String>] A hash of decoded baggage items
112
+ def parse_baggage_header(baggage_header)
113
+ baggage = {}
114
+ baggages = baggage_header.split(',')
115
+ baggages.each do |key_value|
116
+ key, value = key_value.split('=', 2)
117
+ # If baggage is malformed, return an empty hash
118
+ return {} unless key && value
119
+
120
+ key = URI.decode_www_form_component(key.strip)
121
+ value = URI.decode_www_form_component(value.strip)
122
+ return {} if key.empty? || value.empty?
123
+
124
+ baggage[key] = value
125
+ end
126
+ baggage
127
+ end
128
+ end
129
+ end
130
+ end
131
+ end
@@ -116,6 +116,8 @@ module Datadog
116
116
  def extract_trace_id!(trace_id, tags)
117
117
  return trace_id unless tags
118
118
  return trace_id unless (high_order = tags.delete(Tracing::Metadata::Ext::Distributed::TAG_TID))
119
+ return trace_id unless high_order.size == 16
120
+ return trace_id unless /\A[0-9a-f]+\z/i.match?(high_order)
119
121
 
120
122
  Tracing::Utils::TraceId.concatenate(high_order.to_i(16), trace_id)
121
123
  end
@@ -4,6 +4,7 @@ require_relative '../configuration/ext'
4
4
  require_relative '../trace_digest'
5
5
  require_relative '../trace_operation'
6
6
  require_relative '../../core/telemetry/logger'
7
+ require_relative 'baggage'
7
8
 
8
9
  module Datadog
9
10
  module Tracing
@@ -26,9 +27,13 @@ module Datadog
26
27
  )
27
28
  @propagation_styles = propagation_styles
28
29
  @propagation_extract_first = propagation_extract_first
29
-
30
30
  @propagation_style_inject = propagation_style_inject.map { |style| propagation_styles[style] }
31
31
  @propagation_style_extract = propagation_style_extract.map { |style| propagation_styles[style] }
32
+
33
+ # The baggage propagator is unique in that baggage should always be extracted, if present.
34
+ # Therefore we remove it from the `propagation_style_extract` list.
35
+ @baggage_propagator = @propagation_style_extract.find { |propagator| propagator.is_a?(Baggage) }
36
+ @propagation_style_extract.delete(@baggage_propagator) if @baggage_propagator
32
37
  end
33
38
 
34
39
  # inject! populates the env with span ID, trace ID and sampling priority
@@ -57,9 +62,8 @@ module Datadog
57
62
  end
58
63
 
59
64
  digest = digest.to_digest if digest.respond_to?(:to_digest)
60
-
61
- if digest.trace_id.nil?
62
- ::Datadog.logger.debug('Cannot inject distributed trace data: digest.trace_id is nil.')
65
+ if digest.trace_id.nil? && digest.baggage.nil?
66
+ ::Datadog.logger.debug('Cannot inject distributed trace data: digest.trace_id and digest.baggage are both nil.')
63
67
  return nil
64
68
  end
65
69
 
@@ -138,12 +142,29 @@ module Datadog
138
142
  "Error extracting distributed trace data. Cause: #{e} Location: #{Array(e.backtrace).first}"
139
143
  )
140
144
  end
145
+ # Handle baggage after all other styles if present
146
+ extracted_trace_digest = propagate_baggage(data, extracted_trace_digest) if @baggage_propagator
141
147
 
142
148
  extracted_trace_digest
143
149
  end
144
150
 
145
151
  private
146
152
 
153
+ def propagate_baggage(data, extracted_trace_digest)
154
+ if extracted_trace_digest
155
+ # Merge with baggage if present
156
+ digest = @baggage_propagator.extract(data)
157
+ if digest
158
+ extracted_trace_digest.merge(baggage: digest.baggage)
159
+ else
160
+ extracted_trace_digest
161
+ end
162
+ else
163
+ # Baggage is the only style
164
+ @baggage_propagator.extract(data)
165
+ end
166
+ end
167
+
147
168
  def last_datadog_parent_id(headers, tracecontext_tags)
148
169
  dd_propagator = @propagation_style_extract.find { |propagator| propagator.is_a?(Datadog) }
149
170
  if tracecontext_tags&.fetch(
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datadog
4
+ module Tracing
5
+ module Distributed
6
+ # Helper method to decide when to skip distributed tracing
7
+ module PropagationPolicy
8
+ module_function
9
+
10
+ # Skips distributed tracing if disabled for this instrumentation
11
+ # or if APM is disabled unless there is an AppSec event (from upstream distributed trace or local)
12
+ #
13
+ # Both pin_config and global_config are configuration for integrations.
14
+ # pin_config is a Datadog::Core::Pin object, which gives the configuration of a single instance of an integration.
15
+ # global_config is the config for all instances of an integration.
16
+ def enabled?(pin_config: nil, global_config: nil, trace: nil)
17
+ return false unless Tracing.enabled?
18
+
19
+ unless ::Datadog.configuration.apm.tracing.enabled
20
+ return false if trace.nil?
21
+
22
+ trace_source = trace.get_tag(::Datadog::Tracing::Metadata::Ext::Distributed::TAG_TRACE_SOURCE)&.to_i(16)
23
+ return false if trace_source.nil?
24
+
25
+ # If AppSec is enabled and AppSec bit is set in the trace, we should not skip distributed tracing
26
+ # Other products that will use dd.p.ts should implement similar behavior here
27
+ if ::Datadog.configuration.appsec.enabled && (trace_source & ::Datadog::AppSec::Ext::PRODUCT_BIT) != 0
28
+ return true
29
+ end
30
+
31
+ return false
32
+ end
33
+
34
+ return pin_config[:distributed_tracing] if pin_config && pin_config.key?(:distributed_tracing)
35
+ return global_config[:distributed_tracing] if global_config
36
+
37
+ true
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -31,6 +31,8 @@ module Datadog
31
31
  # See Datadog-internal "RFC: Identifying which spans have profiling enabled " for details
32
32
  TAG_PROFILING_ENABLED = '_dd.profiling.enabled'
33
33
 
34
+ TAG_APM_ENABLED = '_dd.apm.enabled'
35
+
34
36
  # Defines constants for trace analytics
35
37
  # @public_api
36
38
  module Analytics
@@ -55,6 +57,9 @@ module Datadog
55
57
  # @see Datadog::Tracing::Sampling::Ext::Mechanism
56
58
  TAG_DECISION_MAKER = '_dd.p.dm'
57
59
 
60
+ # Bitmask for which product generated an event. E.g.: 2 for an AppSec event.
61
+ TAG_TRACE_SOURCE = '_dd.p.ts'
62
+
58
63
  TAG_ORIGIN = '_dd.origin'
59
64
  TAG_SAMPLING_PRIORITY = '_sampling_priority_v1'
60
65
 
@@ -24,7 +24,6 @@ module Datadog
24
24
  sample_rate: Span::Ext::DEFAULT_SAMPLE_RATE,
25
25
  rate_limit: Span::Ext::DEFAULT_MAX_PER_SECOND
26
26
  )
27
-
28
27
  @matcher = matcher
29
28
  @sample_rate = sample_rate
30
29
  @rate_limit = rate_limit
@@ -269,7 +269,8 @@ module Datadog
269
269
 
270
270
  def duration
271
271
  return @duration_end - @duration_start if @duration_start && @duration_end
272
- return @end_time - @start_time if @start_time && @end_time
272
+
273
+ @end_time - @start_time if @start_time && @end_time
273
274
  end
274
275
 
275
276
  def set_error(e)
@@ -32,8 +32,7 @@ module Datadog
32
32
  @agent_settings = agent_settings
33
33
 
34
34
  @transport = transport || begin
35
- transport_options = transport_options.merge(agent_settings: agent_settings) if agent_settings
36
- Transport::HTTP.default(**transport_options)
35
+ Transport::HTTP.default(agent_settings: agent_settings, logger: logger, **transport_options)
37
36
  end
38
37
 
39
38
  @events = Writer::Events.new