ddtrace 1.7.0 → 1.9.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 (182) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +100 -1
  3. data/README.md +2 -2
  4. data/ext/ddtrace_profiling_loader/extconf.rb +4 -1
  5. data/ext/ddtrace_profiling_native_extension/NativeExtensionDesign.md +1 -1
  6. data/ext/ddtrace_profiling_native_extension/clock_id_from_pthread.c +3 -2
  7. data/ext/ddtrace_profiling_native_extension/collectors_cpu_and_wall_time.c +24 -50
  8. data/ext/ddtrace_profiling_native_extension/collectors_cpu_and_wall_time.h +1 -1
  9. data/ext/ddtrace_profiling_native_extension/collectors_cpu_and_wall_time_worker.c +284 -74
  10. data/ext/ddtrace_profiling_native_extension/collectors_dynamic_sampling_rate.c +142 -0
  11. data/ext/ddtrace_profiling_native_extension/collectors_dynamic_sampling_rate.h +14 -0
  12. data/ext/ddtrace_profiling_native_extension/collectors_idle_sampling_helper.c +241 -0
  13. data/ext/ddtrace_profiling_native_extension/collectors_idle_sampling_helper.h +3 -0
  14. data/ext/ddtrace_profiling_native_extension/collectors_stack.c +32 -32
  15. data/ext/ddtrace_profiling_native_extension/collectors_stack.h +2 -2
  16. data/ext/ddtrace_profiling_native_extension/extconf.rb +21 -7
  17. data/ext/ddtrace_profiling_native_extension/helpers.h +5 -0
  18. data/ext/ddtrace_profiling_native_extension/http_transport.c +50 -49
  19. data/ext/ddtrace_profiling_native_extension/libdatadog_helpers.h +5 -1
  20. data/ext/ddtrace_profiling_native_extension/native_extension_helpers.rb +42 -12
  21. data/ext/ddtrace_profiling_native_extension/private_vm_api_access.c +116 -22
  22. data/ext/ddtrace_profiling_native_extension/private_vm_api_access.h +9 -0
  23. data/ext/ddtrace_profiling_native_extension/profiling.c +205 -0
  24. data/ext/ddtrace_profiling_native_extension/ruby_helpers.c +86 -0
  25. data/ext/ddtrace_profiling_native_extension/ruby_helpers.h +28 -6
  26. data/ext/ddtrace_profiling_native_extension/setup_signal_handler.c +23 -4
  27. data/ext/ddtrace_profiling_native_extension/setup_signal_handler.h +4 -0
  28. data/ext/ddtrace_profiling_native_extension/stack_recorder.c +47 -50
  29. data/ext/ddtrace_profiling_native_extension/stack_recorder.h +4 -4
  30. data/ext/ddtrace_profiling_native_extension/time_helpers.c +17 -0
  31. data/ext/ddtrace_profiling_native_extension/time_helpers.h +10 -0
  32. data/lib/datadog/appsec/assets/waf_rules/recommended.json +75 -8
  33. data/lib/datadog/appsec/assets/waf_rules/risky.json +1 -1
  34. data/lib/datadog/appsec/assets/waf_rules/strict.json +1 -1
  35. data/lib/datadog/appsec/assets.rb +1 -1
  36. data/lib/datadog/appsec/configuration/settings.rb +35 -22
  37. data/lib/datadog/appsec/configuration.rb +4 -2
  38. data/lib/datadog/appsec/contrib/auto_instrument.rb +1 -1
  39. data/lib/datadog/appsec/contrib/configuration/settings.rb +1 -1
  40. data/lib/datadog/appsec/contrib/integration.rb +1 -1
  41. data/lib/datadog/appsec/contrib/patcher.rb +1 -1
  42. data/lib/datadog/appsec/contrib/rack/configuration/settings.rb +1 -1
  43. data/lib/datadog/appsec/contrib/rack/ext.rb +1 -1
  44. data/lib/datadog/appsec/contrib/rack/gateway/watcher.rb +1 -1
  45. data/lib/datadog/appsec/contrib/rack/reactive/request.rb +1 -1
  46. data/lib/datadog/appsec/contrib/rack/reactive/request_body.rb +1 -1
  47. data/lib/datadog/appsec/contrib/rack/reactive/response.rb +1 -1
  48. data/lib/datadog/appsec/contrib/rack/request.rb +1 -1
  49. data/lib/datadog/appsec/contrib/rack/response.rb +1 -1
  50. data/lib/datadog/appsec/contrib/rails/configuration/settings.rb +1 -1
  51. data/lib/datadog/appsec/contrib/rails/ext.rb +1 -1
  52. data/lib/datadog/appsec/contrib/rails/framework.rb +1 -1
  53. data/lib/datadog/appsec/contrib/rails/gateway/watcher.rb +1 -1
  54. data/lib/datadog/appsec/contrib/rails/reactive/action.rb +1 -1
  55. data/lib/datadog/appsec/contrib/rails/request.rb +1 -1
  56. data/lib/datadog/appsec/contrib/rails/request_middleware.rb +1 -1
  57. data/lib/datadog/appsec/contrib/sinatra/configuration/settings.rb +1 -1
  58. data/lib/datadog/appsec/contrib/sinatra/ext.rb +1 -1
  59. data/lib/datadog/appsec/contrib/sinatra/framework.rb +1 -1
  60. data/lib/datadog/appsec/contrib/sinatra/gateway/watcher.rb +1 -1
  61. data/lib/datadog/appsec/contrib/sinatra/reactive/routed.rb +1 -1
  62. data/lib/datadog/appsec/contrib/sinatra/request_middleware.rb +1 -1
  63. data/lib/datadog/appsec/event.rb +1 -1
  64. data/lib/datadog/appsec/extensions.rb +36 -26
  65. data/lib/datadog/appsec/instrumentation/gateway.rb +3 -3
  66. data/lib/datadog/appsec/processor.rb +15 -19
  67. data/lib/datadog/appsec/rate_limiter.rb +1 -1
  68. data/lib/datadog/appsec/reactive/address_hash.rb +1 -1
  69. data/lib/datadog/appsec/reactive/engine.rb +1 -1
  70. data/lib/datadog/appsec/reactive/operation.rb +2 -2
  71. data/lib/datadog/appsec/reactive/subscriber.rb +1 -1
  72. data/lib/datadog/appsec/response.rb +18 -9
  73. data/lib/datadog/appsec/utils/http/media_range.rb +201 -0
  74. data/lib/datadog/appsec/utils/http/media_type.rb +87 -0
  75. data/lib/datadog/appsec/utils/http.rb +9 -0
  76. data/lib/datadog/appsec/utils.rb +7 -0
  77. data/lib/datadog/appsec.rb +1 -1
  78. data/lib/datadog/ci/ext/environment.rb +57 -13
  79. data/lib/datadog/core/configuration/agent_settings_resolver.rb +2 -2
  80. data/lib/datadog/core/configuration/base.rb +3 -0
  81. data/lib/datadog/core/configuration/components.rb +27 -6
  82. data/lib/datadog/core/configuration/ext.rb +26 -0
  83. data/lib/datadog/core/configuration/option_definition.rb +11 -2
  84. data/lib/datadog/core/configuration/settings.rb +16 -341
  85. data/lib/datadog/core/diagnostics/environment_logger.rb +4 -3
  86. data/lib/datadog/core/diagnostics/health.rb +4 -22
  87. data/lib/datadog/core/environment/variable_helpers.rb +58 -10
  88. data/lib/datadog/core/metrics/client.rb +3 -2
  89. data/lib/datadog/core/metrics/ext.rb +0 -2
  90. data/lib/datadog/core/telemetry/collector.rb +1 -0
  91. data/lib/datadog/core/utils.rb +0 -21
  92. data/lib/datadog/core.rb +21 -1
  93. data/lib/datadog/kit/appsec/events.rb +75 -0
  94. data/lib/datadog/kit/enable_core_dumps.rb +1 -0
  95. data/lib/datadog/kit/identity.rb +8 -7
  96. data/lib/datadog/opentelemetry/api/context.rb +187 -0
  97. data/lib/datadog/opentelemetry/api/trace/span.rb +15 -0
  98. data/lib/datadog/opentelemetry/sdk/configurator.rb +38 -0
  99. data/lib/datadog/opentelemetry/sdk/id_generator.rb +27 -0
  100. data/lib/datadog/opentelemetry/sdk/propagator.rb +91 -0
  101. data/lib/datadog/opentelemetry/sdk/span_processor.rb +92 -0
  102. data/lib/datadog/opentelemetry.rb +48 -0
  103. data/lib/datadog/opentracer/distributed_headers.rb +2 -2
  104. data/lib/datadog/profiling/collectors/cpu_and_wall_time_worker.rb +16 -5
  105. data/lib/datadog/profiling/collectors/dynamic_sampling_rate.rb +14 -0
  106. data/lib/datadog/profiling/collectors/idle_sampling_helper.rb +68 -0
  107. data/lib/datadog/profiling/stack_recorder.rb +14 -0
  108. data/lib/datadog/profiling.rb +2 -0
  109. data/lib/datadog/tracing/configuration/ext.rb +33 -4
  110. data/lib/datadog/tracing/configuration/settings.rb +433 -0
  111. data/lib/datadog/tracing/contrib/aws/configuration/settings.rb +4 -1
  112. data/lib/datadog/tracing/contrib/aws/ext.rb +1 -0
  113. data/lib/datadog/tracing/contrib/dalli/configuration/settings.rb +4 -1
  114. data/lib/datadog/tracing/contrib/dalli/ext.rb +1 -0
  115. data/lib/datadog/tracing/contrib/elasticsearch/configuration/settings.rb +5 -1
  116. data/lib/datadog/tracing/contrib/elasticsearch/ext.rb +1 -0
  117. data/lib/datadog/tracing/contrib/ethon/configuration/settings.rb +6 -1
  118. data/lib/datadog/tracing/contrib/ethon/ext.rb +1 -0
  119. data/lib/datadog/tracing/contrib/excon/configuration/settings.rb +5 -1
  120. data/lib/datadog/tracing/contrib/excon/ext.rb +1 -0
  121. data/lib/datadog/tracing/contrib/faraday/configuration/settings.rb +5 -1
  122. data/lib/datadog/tracing/contrib/faraday/ext.rb +1 -0
  123. data/lib/datadog/tracing/contrib/grpc/configuration/settings.rb +6 -1
  124. data/lib/datadog/tracing/contrib/grpc/distributed/propagation.rb +9 -4
  125. data/lib/datadog/tracing/contrib/grpc/ext.rb +1 -0
  126. data/lib/datadog/tracing/contrib/http/configuration/settings.rb +11 -1
  127. data/lib/datadog/tracing/contrib/http/distributed/fetcher.rb +10 -3
  128. data/lib/datadog/tracing/contrib/http/distributed/propagation.rb +9 -4
  129. data/lib/datadog/tracing/contrib/http/ext.rb +2 -0
  130. data/lib/datadog/tracing/contrib/http/instrumentation.rb +3 -6
  131. data/lib/datadog/tracing/contrib/httpclient/configuration/settings.rb +11 -1
  132. data/lib/datadog/tracing/contrib/httpclient/ext.rb +2 -0
  133. data/lib/datadog/tracing/contrib/httpclient/instrumentation.rb +3 -4
  134. data/lib/datadog/tracing/contrib/httprb/configuration/settings.rb +11 -1
  135. data/lib/datadog/tracing/contrib/httprb/ext.rb +2 -0
  136. data/lib/datadog/tracing/contrib/httprb/instrumentation.rb +3 -4
  137. data/lib/datadog/tracing/contrib/mongodb/configuration/settings.rb +5 -1
  138. data/lib/datadog/tracing/contrib/mongodb/ext.rb +1 -0
  139. data/lib/datadog/tracing/contrib/mysql2/configuration/settings.rb +4 -1
  140. data/lib/datadog/tracing/contrib/mysql2/ext.rb +1 -0
  141. data/lib/datadog/tracing/contrib/mysql2/instrumentation.rb +2 -2
  142. data/lib/datadog/tracing/contrib/patcher.rb +3 -2
  143. data/lib/datadog/tracing/contrib/pg/configuration/settings.rb +4 -1
  144. data/lib/datadog/tracing/contrib/pg/ext.rb +1 -0
  145. data/lib/datadog/tracing/contrib/pg/instrumentation.rb +56 -33
  146. data/lib/datadog/tracing/contrib/presto/configuration/settings.rb +4 -1
  147. data/lib/datadog/tracing/contrib/presto/ext.rb +1 -0
  148. data/lib/datadog/tracing/contrib/propagation/sql_comment/ext.rb +1 -0
  149. data/lib/datadog/tracing/contrib/propagation/sql_comment.rb +10 -12
  150. data/lib/datadog/tracing/contrib/redis/configuration/settings.rb +4 -1
  151. data/lib/datadog/tracing/contrib/redis/ext.rb +1 -0
  152. data/lib/datadog/tracing/contrib/redis/instrumentation.rb +30 -23
  153. data/lib/datadog/tracing/contrib/redis/integration.rb +34 -2
  154. data/lib/datadog/tracing/contrib/redis/patcher.rb +18 -14
  155. data/lib/datadog/tracing/contrib/redis/quantize.rb +12 -9
  156. data/lib/datadog/tracing/contrib/redis/tags.rb +4 -6
  157. data/lib/datadog/tracing/contrib/redis/trace_middleware.rb +72 -0
  158. data/lib/datadog/tracing/contrib/rest_client/configuration/settings.rb +6 -1
  159. data/lib/datadog/tracing/contrib/rest_client/ext.rb +1 -0
  160. data/lib/datadog/tracing/contrib/stripe/configuration/settings.rb +33 -0
  161. data/lib/datadog/tracing/contrib/stripe/ext.rb +26 -0
  162. data/lib/datadog/tracing/contrib/stripe/integration.rb +43 -0
  163. data/lib/datadog/tracing/contrib/stripe/patcher.rb +29 -0
  164. data/lib/datadog/tracing/contrib/stripe/request.rb +67 -0
  165. data/lib/datadog/tracing/contrib.rb +1 -0
  166. data/lib/datadog/{core → tracing}/diagnostics/ext.rb +1 -6
  167. data/lib/datadog/tracing/diagnostics/health.rb +40 -0
  168. data/lib/datadog/tracing/distributed/{b3.rb → b3_multi.rb} +2 -2
  169. data/lib/datadog/tracing/distributed/helpers.rb +2 -1
  170. data/lib/datadog/tracing/distributed/none.rb +19 -0
  171. data/lib/datadog/tracing/distributed/trace_context.rb +378 -0
  172. data/lib/datadog/tracing/metadata/ext.rb +1 -1
  173. data/lib/datadog/tracing/metadata/tagging.rb +6 -0
  174. data/lib/datadog/tracing/sampling/priority_sampler.rb +11 -0
  175. data/lib/datadog/tracing/sampling/rate_sampler.rb +3 -3
  176. data/lib/datadog/tracing/span.rb +3 -19
  177. data/lib/datadog/tracing/span_operation.rb +5 -4
  178. data/lib/datadog/tracing/trace_digest.rb +85 -2
  179. data/lib/datadog/tracing/trace_operation.rb +13 -4
  180. data/lib/datadog/tracing/utils.rb +50 -0
  181. data/lib/ddtrace/version.rb +1 -1
  182. metadata +41 -9
@@ -20,12 +20,14 @@ module Datadog
20
20
  max_frames:,
21
21
  tracer:,
22
22
  gc_profiling_enabled:,
23
- cpu_and_wall_time_collector: CpuAndWallTime.new(recorder: recorder, max_frames: max_frames, tracer: tracer)
23
+ cpu_and_wall_time_collector: CpuAndWallTime.new(recorder: recorder, max_frames: max_frames, tracer: tracer),
24
+ idle_sampling_helper: IdleSamplingHelper.new
24
25
  )
25
- self.class._native_initialize(self, cpu_and_wall_time_collector, gc_profiling_enabled)
26
+ self.class._native_initialize(self, cpu_and_wall_time_collector, gc_profiling_enabled, idle_sampling_helper)
26
27
  @worker_thread = nil
27
28
  @failure_exception = nil
28
29
  @start_stop_mutex = Mutex.new
30
+ @idle_sampling_helper = idle_sampling_helper
29
31
  end
30
32
 
31
33
  def start
@@ -33,6 +35,9 @@ module Datadog
33
35
  return if @worker_thread && @worker_thread.alive?
34
36
 
35
37
  Datadog.logger.debug { "Starting thread for: #{self}" }
38
+
39
+ @idle_sampling_helper.start
40
+
36
41
  @worker_thread = Thread.new do
37
42
  begin
38
43
  Thread.current.name = self.class.name unless Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.3')
@@ -43,7 +48,8 @@ module Datadog
43
48
  rescue Exception => e # rubocop:disable Lint/RescueException
44
49
  @failure_exception = e
45
50
  Datadog.logger.warn(
46
- "Worker thread error. Cause: #{e.class.name} #{e.message} Location: #{Array(e.backtrace).first}"
51
+ 'CpuAndWallTimeWorker thread error. ' \
52
+ "Cause: #{e.class.name} #{e.message} Location: #{Array(e.backtrace).first}"
47
53
  )
48
54
  end
49
55
  end
@@ -60,10 +66,11 @@ module Datadog
60
66
  @start_stop_mutex.synchronize do
61
67
  Datadog.logger.debug('Requesting CpuAndWallTimeWorker thread shut down')
62
68
 
69
+ @idle_sampling_helper.stop
70
+
63
71
  return unless @worker_thread
64
72
 
65
- @worker_thread.kill
66
- self.class._native_stop(self)
73
+ self.class._native_stop(self, @worker_thread)
67
74
 
68
75
  @worker_thread.join
69
76
  @worker_thread = nil
@@ -74,6 +81,10 @@ module Datadog
74
81
  def reset_after_fork
75
82
  self.class._native_reset_after_fork(self)
76
83
  end
84
+
85
+ def stats
86
+ self.class._native_stats(self)
87
+ end
77
88
  end
78
89
  end
79
90
  end
@@ -0,0 +1,14 @@
1
+ # typed: false
2
+
3
+ module Datadog
4
+ module Profiling
5
+ module Collectors
6
+ # Used to pace the rate of profiling samples based on the last observed time for a sample.
7
+ # All of this module is implemented as native code.
8
+ #
9
+ # Methods prefixed with _native_ are implemented in `collectors_dynamic_sampling_rate.c`
10
+ module DynamicSamplingRate
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,68 @@
1
+ # typed: false
2
+
3
+ module Datadog
4
+ module Profiling
5
+ module Collectors
6
+ # Used by the Collectors::CpuAndWallTimeWorker to gather samples when the Ruby process is idle.
7
+ # Almost all of this class is implemented as native code.
8
+ #
9
+ # Methods prefixed with _native_ are implemented in `collectors_idle_sampling_helper.c`
10
+ class IdleSamplingHelper
11
+ private
12
+
13
+ attr_accessor :failure_exception
14
+
15
+ public
16
+
17
+ def initialize
18
+ @worker_thread = nil
19
+ @start_stop_mutex = Mutex.new
20
+ end
21
+
22
+ def start
23
+ @start_stop_mutex.synchronize do
24
+ return if @worker_thread && @worker_thread.alive?
25
+
26
+ Datadog.logger.debug { "Starting thread for: #{self}" }
27
+
28
+ # The same instance of the IdleSamplingHelper can be reused multiple times, and this resets it back to
29
+ # a pristine state before recreating the worker thread
30
+ self.class._native_reset(self)
31
+
32
+ @worker_thread = Thread.new do
33
+ begin
34
+ Thread.current.name = self.class.name unless Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.3')
35
+
36
+ self.class._native_idle_sampling_loop(self)
37
+
38
+ Datadog.logger.debug('IdleSamplingHelper thread stopping cleanly')
39
+ rescue Exception => e # rubocop:disable Lint/RescueException
40
+ @failure_exception = e
41
+ Datadog.logger.warn(
42
+ 'IdleSamplingHelper thread error. ' \
43
+ "Cause: #{e.class.name} #{e.message} Location: #{Array(e.backtrace).first}"
44
+ )
45
+ end
46
+ end
47
+ end
48
+
49
+ true
50
+ end
51
+
52
+ def stop(*_)
53
+ @start_stop_mutex.synchronize do
54
+ Datadog.logger.debug('Requesting IdleSamplingHelper thread shut down')
55
+
56
+ return unless @worker_thread
57
+
58
+ self.class._native_stop(self)
59
+
60
+ @worker_thread.join
61
+ @worker_thread = nil
62
+ @failure_exception = nil
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -34,6 +34,20 @@ module Datadog
34
34
  end
35
35
  end
36
36
 
37
+ def serialize!
38
+ status, result = @no_concurrent_synchronize_mutex.synchronize { self.class._native_serialize(self) }
39
+
40
+ if status == :ok
41
+ _start, _finish, encoded_pprof = result
42
+
43
+ encoded_pprof
44
+ else
45
+ error_message = result
46
+
47
+ raise("Failed to serialize profiling data: #{error_message}")
48
+ end
49
+ end
50
+
37
51
  def clear
38
52
  status, result = @no_concurrent_synchronize_mutex.synchronize { self.class._native_clear(self) }
39
53
 
@@ -149,6 +149,8 @@ module Datadog
149
149
  require_relative 'profiling/collectors/code_provenance'
150
150
  require_relative 'profiling/collectors/cpu_and_wall_time'
151
151
  require_relative 'profiling/collectors/cpu_and_wall_time_worker'
152
+ require_relative 'profiling/collectors/dynamic_sampling_rate'
153
+ require_relative 'profiling/collectors/idle_sampling_helper'
152
154
  require_relative 'profiling/collectors/old_stack'
153
155
  require_relative 'profiling/collectors/stack'
154
156
  require_relative 'profiling/stack_recorder'
@@ -3,7 +3,11 @@
3
3
  module Datadog
4
4
  module Tracing
5
5
  module Configuration
6
+ # Constants for configuration settings
7
+ # e.g. Env vars, default values, enums, etc...
6
8
  module Ext
9
+ ENV_ENABLED = 'DD_TRACE_ENABLED'.freeze
10
+
7
11
  # @public_api
8
12
  module Analytics
9
13
  ENV_TRACE_ANALYTICS_ENABLED = 'DD_TRACE_ANALYTICS_ENABLED'.freeze
@@ -16,11 +20,37 @@ module Datadog
16
20
 
17
21
  # @public_api
18
22
  module Distributed
23
+ # Custom Datadog format
19
24
  PROPAGATION_STYLE_DATADOG = 'Datadog'.freeze
25
+
26
+ PROPAGATION_STYLE_B3_MULTI_HEADER = 'b3multi'.freeze
27
+ # @deprecated Use `b3multi` instead.
20
28
  PROPAGATION_STYLE_B3 = 'B3'.freeze
21
- PROPAGATION_STYLE_B3_SINGLE_HEADER = 'B3 single header'.freeze
22
- ENV_PROPAGATION_STYLE_INJECT = 'DD_PROPAGATION_STYLE_INJECT'.freeze
23
- ENV_PROPAGATION_STYLE_EXTRACT = 'DD_PROPAGATION_STYLE_EXTRACT'.freeze
29
+
30
+ PROPAGATION_STYLE_B3_SINGLE_HEADER = 'b3'.freeze
31
+ # @deprecated Use `b3` instead.
32
+ PROPAGATION_STYLE_B3_SINGLE_HEADER_OLD = 'B3 single header'.freeze
33
+
34
+ # W3C Trace Context
35
+ PROPAGATION_STYLE_TRACE_CONTEXT = 'tracecontext'.freeze
36
+
37
+ # Sets both extract and inject propagation style tho the provided value.
38
+ # Has lower precedence than `DD_TRACE_PROPAGATION_STYLE_INJECT` or
39
+ # `DD_TRACE_PROPAGATION_STYLE_EXTRACT`.
40
+ ENV_PROPAGATION_STYLE = 'DD_TRACE_PROPAGATION_STYLE'.freeze
41
+
42
+ ENV_PROPAGATION_STYLE_INJECT = 'DD_TRACE_PROPAGATION_STYLE_INJECT'.freeze
43
+ # @deprecated Use `DD_TRACE_PROPAGATION_STYLE_INJECT` instead.
44
+ ENV_PROPAGATION_STYLE_INJECT_OLD = 'DD_PROPAGATION_STYLE_INJECT'.freeze
45
+
46
+ ENV_PROPAGATION_STYLE_EXTRACT = 'DD_TRACE_PROPAGATION_STYLE_EXTRACT'.freeze
47
+ # @deprecated Use `DD_TRACE_PROPAGATION_STYLE_EXTRACT` instead.
48
+ ENV_PROPAGATION_STYLE_EXTRACT_OLD = 'DD_PROPAGATION_STYLE_EXTRACT'.freeze
49
+
50
+ # A no-op propagator. Compatible with OpenTelemetry's `none` propagator.
51
+ # @see https://opentelemetry.io/docs/concepts/sdk-configuration/general-sdk-configuration/#get_otel__propagators
52
+ PROPAGATION_STYLE_NONE = 'none'.freeze
53
+
24
54
  ENV_X_DATADOG_TAGS_MAX_LENGTH = 'DD_TRACE_X_DATADOG_TAGS_MAX_LENGTH'.freeze
25
55
  end
26
56
 
@@ -48,7 +78,6 @@ module Datadog
48
78
 
49
79
  # @public_api
50
80
  module Transport
51
- ENV_DEFAULT_HOST = 'DD_AGENT_HOST'.freeze
52
81
  ENV_DEFAULT_PORT = 'DD_TRACE_AGENT_PORT'.freeze
53
82
  ENV_DEFAULT_URL = 'DD_TRACE_AGENT_URL'.freeze
54
83
  end
@@ -0,0 +1,433 @@
1
+ # typed: false
2
+
3
+ require_relative '../../tracing/configuration/ext'
4
+
5
+ module Datadog
6
+ module Tracing
7
+ module Configuration
8
+ # Configuration settings for tracing.
9
+ # @public_api
10
+ # rubocop:disable Metrics/AbcSize
11
+ # rubocop:disable Metrics/BlockLength
12
+ # rubocop:disable Metrics/CyclomaticComplexity
13
+ # rubocop:disable Metrics/MethodLength
14
+ # rubocop:disable Layout/LineLength
15
+ module Settings
16
+ def self.extended(base)
17
+ base.class_eval do
18
+ # Tracer specific configurations.
19
+ # @public_api
20
+ settings :tracing do
21
+ # Legacy [App Analytics](https://docs.datadoghq.com/tracing/legacy_app_analytics/) configuration.
22
+ #
23
+ # @configure_with {Datadog::Tracing}
24
+ # @deprecated Use [Trace Retention and Ingestion](https://docs.datadoghq.com/tracing/trace_retention_and_ingestion/)
25
+ # controls.
26
+ # @public_api
27
+ settings :analytics do
28
+ # @default `DD_TRACE_ANALYTICS_ENABLED` environment variable, otherwise `nil`
29
+ # @return [Boolean,nil]
30
+ option :enabled do |o|
31
+ o.default { env_to_bool(Tracing::Configuration::Ext::Analytics::ENV_TRACE_ANALYTICS_ENABLED, nil) }
32
+ o.lazy
33
+ end
34
+ end
35
+
36
+ # [Distributed Tracing](https://docs.datadoghq.com/tracing/setup_overview/setup/ruby/#distributed-tracing) propagation
37
+ # style configuration.
38
+ #
39
+ # The supported formats are:
40
+ # * `Datadog`: Datadog propagation format, described by [Distributed Tracing](https://docs.datadoghq.com/tracing/setup_overview/setup/ruby/#distributed-tracing).
41
+ # * `b3multi`: B3 Propagation using multiple headers, described by [openzipkin/b3-propagation](https://github.com/openzipkin/b3-propagation#multiple-headers).
42
+ # * `b3`: B3 Propagation using a single header, described by [openzipkin/b3-propagation](https://github.com/openzipkin/b3-propagation#single-header).
43
+ #
44
+ # @public_api
45
+ settings :distributed_tracing do
46
+ # An ordered list of what data propagation styles the tracer will use to extract distributed tracing propagation
47
+ # data from incoming requests and messages.
48
+ #
49
+ # The tracer will try to find distributed headers in the order they are present in the list provided to this option.
50
+ # The first format to have valid data present will be used.
51
+ #
52
+ # @default `DD_TRACE_PROPAGATION_STYLE_EXTRACT` environment variable (comma-separated list),
53
+ # otherwise `['Datadog','b3multi','b3']`.
54
+ # @return [Array<String>]
55
+ option :propagation_extract_style do |o|
56
+ o.default do
57
+ # DEV-2.0: Change default value to `tracecontext, Datadog`.
58
+ # Look for all headers by default
59
+ env_to_list(
60
+ [
61
+ Tracing::Configuration::Ext::Distributed::ENV_PROPAGATION_STYLE_EXTRACT,
62
+ Tracing::Configuration::Ext::Distributed::ENV_PROPAGATION_STYLE_EXTRACT_OLD
63
+ ],
64
+ [
65
+ Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_DATADOG,
66
+ Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_B3_MULTI_HEADER,
67
+ Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_B3_SINGLE_HEADER
68
+ ],
69
+ comma_separated_only: true
70
+ )
71
+ end
72
+
73
+ o.on_set do |styles|
74
+ # Modernize B3 options
75
+ # DEV-2.0: Can be removed with the removal of deprecated B3 constants.
76
+ styles.map! do |style|
77
+ case style
78
+ when Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_B3
79
+ Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_B3_MULTI_HEADER
80
+ when Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_B3_SINGLE_HEADER_OLD
81
+ Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_B3_SINGLE_HEADER
82
+ else
83
+ style
84
+ end
85
+ end
86
+ end
87
+
88
+ o.lazy
89
+ end
90
+
91
+ # The data propagation styles the tracer will use to inject distributed tracing propagation
92
+ # data into outgoing requests and messages.
93
+ #
94
+ # The tracer will inject data from all styles specified in this option.
95
+ #
96
+ # @default `DD_TRACE_PROPAGATION_STYLE_INJECT` environment variable (comma-separated list), otherwise `['Datadog']`.
97
+ # @return [Array<String>]
98
+ option :propagation_inject_style do |o|
99
+ o.default do
100
+ # DEV-2.0: Change default value to `tracecontext, Datadog`.
101
+ env_to_list(
102
+ [
103
+ Tracing::Configuration::Ext::Distributed::ENV_PROPAGATION_STYLE_INJECT,
104
+ Tracing::Configuration::Ext::Distributed::ENV_PROPAGATION_STYLE_INJECT_OLD
105
+ ],
106
+ [Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_DATADOG],
107
+ comma_separated_only: true # Only inject Datadog headers by default
108
+ )
109
+ end
110
+
111
+ o.on_set do |styles|
112
+ # Modernize B3 options
113
+ # DEV-2.0: Can be removed with the removal of deprecated B3 constants.
114
+ styles.map! do |style|
115
+ case style
116
+ when Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_B3
117
+ Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_B3_MULTI_HEADER
118
+ when Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_B3_SINGLE_HEADER_OLD
119
+ Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_B3_SINGLE_HEADER
120
+ else
121
+ style
122
+ end
123
+ end
124
+ end
125
+
126
+ o.lazy
127
+ end
128
+
129
+ # An ordered list of what data propagation styles the tracer will use to extract distributed tracing propagation
130
+ # data from incoming requests and inject into outgoing requests.
131
+ #
132
+ # This configuration is the equivalent of configuring both {propagation_extract_style}
133
+ # {propagation_inject_style} to value set to {propagation_style}.
134
+ #
135
+ # @default `DD_TRACE_PROPAGATION_STYLE` environment variable (comma-separated list).
136
+ # @return [Array<String>]
137
+ option :propagation_style do |o|
138
+ o.default do
139
+ env_to_list(Configuration::Ext::Distributed::ENV_PROPAGATION_STYLE, nil, comma_separated_only: true)
140
+ end
141
+
142
+ o.on_set do |styles|
143
+ next unless styles
144
+
145
+ # Modernize B3 options
146
+ # DEV-2.0: Can be removed with the removal of deprecated B3 constants.
147
+ styles.map! do |style|
148
+ case style
149
+ when Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_B3
150
+ Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_B3_MULTI_HEADER
151
+ when Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_B3_SINGLE_HEADER_OLD
152
+ Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_B3_SINGLE_HEADER
153
+ else
154
+ style
155
+ end
156
+ end
157
+
158
+ set_option(:propagation_extract_style, styles)
159
+ set_option(:propagation_inject_style, styles)
160
+ end
161
+
162
+ o.lazy
163
+ end
164
+ end
165
+
166
+ # Enable trace collection and span generation.
167
+ #
168
+ # You can use this option to disable tracing without having to
169
+ # remove the library as a whole.
170
+ #
171
+ # @default `DD_TRACE_ENABLED` environment variable, otherwise `true`
172
+ # @return [Boolean]
173
+ option :enabled do |o|
174
+ o.default { env_to_bool(Tracing::Configuration::Ext::ENV_ENABLED, true) }
175
+ o.lazy
176
+ end
177
+
178
+ # A custom tracer instance.
179
+ #
180
+ # It must respect the contract of {Datadog::Tracing::Tracer}.
181
+ # It's recommended to delegate methods to {Datadog::Tracing::Tracer} to ease the implementation
182
+ # of a custom tracer.
183
+ #
184
+ # This option will not return the live tracer instance: it only holds a custom tracing instance, if any.
185
+ #
186
+ # For internal use only.
187
+ #
188
+ # @default `nil`
189
+ # @return [Object,nil]
190
+ option :instance
191
+
192
+ # Automatic correlation between tracing and logging.
193
+ # @see https://docs.datadoghq.com/tracing/setup_overview/setup/ruby/#trace-correlation
194
+ # @return [Boolean]
195
+ option :log_injection do |o|
196
+ o.default { env_to_bool(Tracing::Configuration::Ext::Correlation::ENV_LOGS_INJECTION_ENABLED, true) }
197
+ o.lazy
198
+ end
199
+
200
+ # Configures an alternative trace transport behavior, where
201
+ # traces can be sent to the agent and backend before all spans
202
+ # have finished.
203
+ #
204
+ # This is useful for long-running jobs or very large traces.
205
+ #
206
+ # The trace flame graph will display the partial trace as it is received and constantly
207
+ # update with new spans as they are flushed.
208
+ # @public_api
209
+ settings :partial_flush do
210
+ # Enable partial trace flushing.
211
+ #
212
+ # @default `false`
213
+ # @return [Boolean]
214
+ option :enabled, default: false
215
+
216
+ # Minimum number of finished spans required in a single unfinished trace before
217
+ # the tracer will consider that trace for partial flushing.
218
+ #
219
+ # This option helps preserve a minimum amount of batching in the
220
+ # flushing process, reducing network overhead.
221
+ #
222
+ # This threshold only applies to unfinished traces. Traces that have finished
223
+ # are always flushed immediately.
224
+ #
225
+ # @default 500
226
+ # @return [Integer]
227
+ option :min_spans_threshold, default: 500
228
+ end
229
+
230
+ # Enables {https://docs.datadoghq.com/tracing/trace_retention_and_ingestion/#datadog-intelligent-retention-filter
231
+ # Datadog intelligent retention filter}.
232
+ # @default `true`
233
+ # @return [Boolean,nil]
234
+ option :priority_sampling
235
+
236
+ option :report_hostname do |o|
237
+ o.default { env_to_bool(Tracing::Configuration::Ext::NET::ENV_REPORT_HOSTNAME, false) }
238
+ o.lazy
239
+ end
240
+
241
+ # A custom sampler instance.
242
+ # The object must respect the {Datadog::Tracing::Sampling::Sampler} interface.
243
+ # @default `nil`
244
+ # @return [Object,nil]
245
+ option :sampler
246
+
247
+ # Client-side sampling configuration.
248
+ # @see https://docs.datadoghq.com/tracing/trace_ingestion/mechanisms/
249
+ # @public_api
250
+ settings :sampling do
251
+ # Default sampling rate for the tracer.
252
+ #
253
+ # If `nil`, the trace uses an automatic sampling strategy that tries to ensure
254
+ # the collection of traces that are considered important (e.g. traces with an error, traces
255
+ # for resources not seen recently).
256
+ #
257
+ # @default `DD_TRACE_SAMPLE_RATE` environment variable, otherwise `nil`.
258
+ # @return [Float,nil]
259
+ option :default_rate do |o|
260
+ o.default { env_to_float(Tracing::Configuration::Ext::Sampling::ENV_SAMPLE_RATE, nil) }
261
+ o.lazy
262
+ end
263
+
264
+ # Rate limit for number of spans per second.
265
+ #
266
+ # Spans created above the limit will contribute to service metrics, but won't
267
+ # have their payload stored.
268
+ #
269
+ # @default `DD_TRACE_RATE_LIMIT` environment variable, otherwise 100.
270
+ # @return [Numeric,nil]
271
+ option :rate_limit do |o|
272
+ o.default { env_to_float(Tracing::Configuration::Ext::Sampling::ENV_RATE_LIMIT, 100) }
273
+ o.lazy
274
+ end
275
+
276
+ # Single span sampling rules.
277
+ # These rules allow a span to be kept when its encompassing trace is dropped.
278
+ #
279
+ # The syntax for single span sampling rules can be found here:
280
+ # TODO: <Single Span Sampling documentation URL here>
281
+ #
282
+ # @default `DD_SPAN_SAMPLING_RULES` environment variable.
283
+ # Otherwise, `ENV_SPAN_SAMPLING_RULES_FILE` environment variable.
284
+ # Otherwise `nil`.
285
+ # @return [String,nil]
286
+ # @public_api
287
+ option :span_rules do |o|
288
+ o.default do
289
+ rules = ENV[Tracing::Configuration::Ext::Sampling::Span::ENV_SPAN_SAMPLING_RULES]
290
+ rules_file = ENV[Tracing::Configuration::Ext::Sampling::Span::ENV_SPAN_SAMPLING_RULES_FILE]
291
+
292
+ if rules
293
+ if rules_file
294
+ Datadog.logger.warn(
295
+ 'Both DD_SPAN_SAMPLING_RULES and DD_SPAN_SAMPLING_RULES_FILE were provided: only ' \
296
+ 'DD_SPAN_SAMPLING_RULES will be used. Please do not provide DD_SPAN_SAMPLING_RULES_FILE when ' \
297
+ 'also providing DD_SPAN_SAMPLING_RULES as their configuration conflicts. ' \
298
+ "DD_SPAN_SAMPLING_RULES_FILE=#{rules_file} DD_SPAN_SAMPLING_RULES=#{rules}"
299
+ )
300
+ end
301
+ rules
302
+ elsif rules_file
303
+ begin
304
+ File.read(rules_file)
305
+ rescue => e
306
+ # `File#read` errors have clear and actionable messages, no need to add extra exception info.
307
+ Datadog.logger.warn(
308
+ "Cannot read span sampling rules file `#{rules_file}`: #{e.message}." \
309
+ 'No span sampling rules will be applied.'
310
+ )
311
+ nil
312
+ end
313
+ end
314
+ end
315
+ o.lazy
316
+ end
317
+ end
318
+
319
+ # [Continuous Integration Visibility](https://docs.datadoghq.com/continuous_integration/) configuration.
320
+ # @public_api
321
+ settings :test_mode do
322
+ # Enable test mode. This allows the tracer to collect spans from test runs.
323
+ #
324
+ # It also prevents the tracer from collecting spans in a production environment. Only use in a test environment.
325
+ #
326
+ # @default `DD_TRACE_TEST_MODE_ENABLED` environment variable, otherwise `false`
327
+ # @return [Boolean]
328
+ option :enabled do |o|
329
+ o.default { env_to_bool(Tracing::Configuration::Ext::Test::ENV_MODE_ENABLED, false) }
330
+ o.lazy
331
+ end
332
+
333
+ option :trace_flush do |o|
334
+ o.default { nil }
335
+ o.lazy
336
+ end
337
+
338
+ option :writer_options do |o|
339
+ o.default { {} }
340
+ o.lazy
341
+ end
342
+ end
343
+
344
+ # @see file:docs/GettingStarted.md#configuring-the-transport-layer Configuring the transport layer
345
+ #
346
+ # A {Proc} that configures a custom tracer transport.
347
+ # @yield Receives a {Datadog::Transport::HTTP} that can be modified with custom adapters and settings.
348
+ # @yieldparam [Datadog::Transport::HTTP] t transport to be configured.
349
+ # @default `nil`
350
+ # @return [Proc,nil]
351
+ option :transport_options, default: nil
352
+
353
+ # A custom writer instance.
354
+ # The object must respect the {Datadog::Tracing::Writer} interface.
355
+ #
356
+ # This option is recommended for internal use only.
357
+ #
358
+ # @default `nil`
359
+ # @return [Object,nil]
360
+ option :writer
361
+
362
+ # A custom {Hash} with keyword options to be passed to {Datadog::Tracing::Writer#initialize}.
363
+ #
364
+ # This option is recommended for internal use only.
365
+ #
366
+ # @default `{}`
367
+ # @return [Hash,nil]
368
+ option :writer_options, default: ->(_i) { {} }, lazy: true
369
+
370
+ # Client IP configuration
371
+ # @public_api
372
+ settings :client_ip do
373
+ # Whether client IP collection is enabled. When enabled client IPs from HTTP requests will
374
+ # be reported in traces.
375
+ #
376
+ # Usage of the DD_TRACE_CLIENT_IP_HEADER_DISABLED environment variable is deprecated.
377
+ #
378
+ # @see https://docs.datadoghq.com/tracing/configure_data_security#configuring-a-client-ip-header
379
+ #
380
+ # @default `DD_TRACE_CLIENT_IP_ENABLED` environment variable, otherwise `false`.
381
+ # @return [Boolean]
382
+ option :enabled do |o|
383
+ o.default do
384
+ disabled = env_to_bool(Tracing::Configuration::Ext::ClientIp::ENV_DISABLED)
385
+
386
+ enabled = if disabled.nil?
387
+ false
388
+ else
389
+ Datadog.logger.warn { "#{Tracing::Configuration::Ext::ClientIp::ENV_DISABLED} environment variable is deprecated, found set to #{disabled}, use #{Tracing::Configuration::Ext::ClientIp::ENV_ENABLED}=#{!disabled}" }
390
+
391
+ !disabled
392
+ end
393
+
394
+ # ENABLED env var takes precedence over deprecated DISABLED
395
+ env_to_bool(Tracing::Configuration::Ext::ClientIp::ENV_ENABLED, enabled)
396
+ end
397
+ o.lazy
398
+ end
399
+
400
+ # An optional name of a custom header to resolve the client IP from.
401
+ #
402
+ # @default `DD_TRACE_CLIENT_IP_HEADER` environment variable, otherwise `nil`.
403
+ # @return [String,nil]
404
+ option :header_name do |o|
405
+ o.default { ENV.fetch(Tracing::Configuration::Ext::ClientIp::ENV_HEADER_NAME, nil) }
406
+ o.lazy
407
+ end
408
+ end
409
+
410
+ # Maximum size for the `x-datadog-tags` distributed trace tags header.
411
+ #
412
+ # If the serialized size of distributed trace tags is larger than this value, it will
413
+ # not be parsed if incoming, nor exported if outgoing. An error message will be logged
414
+ # in this case.
415
+ #
416
+ # @default `DD_TRACE_X_DATADOG_TAGS_MAX_LENGTH` environment variable, otherwise `512`
417
+ # @return [Integer]
418
+ option :x_datadog_tags_max_length do |o|
419
+ o.default { env_to_int(Tracing::Configuration::Ext::Distributed::ENV_X_DATADOG_TAGS_MAX_LENGTH, 512) }
420
+ o.lazy
421
+ end
422
+ end
423
+ end
424
+ end
425
+ end
426
+ # rubocop:enable Metrics/AbcSize
427
+ # rubocop:enable Metrics/BlockLength
428
+ # rubocop:enable Metrics/CyclomaticComplexity
429
+ # rubocop:enable Metrics/MethodLength
430
+ # rubocop:enable Layout/LineLength
431
+ end
432
+ end
433
+ end
@@ -26,7 +26,10 @@ module Datadog
26
26
  o.lazy
27
27
  end
28
28
 
29
- option :service_name, default: Ext::DEFAULT_PEER_SERVICE_NAME
29
+ option :service_name do |o|
30
+ o.default { ENV.fetch(Ext::ENV_SERVICE_NAME, Ext::DEFAULT_PEER_SERVICE_NAME) }
31
+ o.lazy
32
+ end
30
33
  end
31
34
  end
32
35
  end