datadog 2.15.0 → 2.17.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 (194) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +74 -2
  3. data/ext/datadog_profiling_native_extension/datadog_ruby_common.c +1 -4
  4. data/ext/datadog_profiling_native_extension/datadog_ruby_common.h +7 -0
  5. data/ext/datadog_profiling_native_extension/encoded_profile.c +22 -12
  6. data/ext/datadog_profiling_native_extension/encoded_profile.h +1 -0
  7. data/ext/datadog_profiling_native_extension/extconf.rb +3 -0
  8. data/ext/datadog_profiling_native_extension/heap_recorder.c +8 -1
  9. data/ext/datadog_profiling_native_extension/http_transport.c +45 -72
  10. data/ext/datadog_profiling_native_extension/stack_recorder.c +4 -5
  11. data/ext/libdatadog_api/crashtracker.c +11 -12
  12. data/ext/libdatadog_api/crashtracker.h +5 -0
  13. data/ext/libdatadog_api/datadog_ruby_common.c +1 -4
  14. data/ext/libdatadog_api/datadog_ruby_common.h +7 -0
  15. data/ext/libdatadog_api/init.c +15 -0
  16. data/ext/libdatadog_api/library_config.c +122 -0
  17. data/ext/libdatadog_api/library_config.h +19 -0
  18. data/ext/libdatadog_api/macos_development.md +3 -3
  19. data/ext/libdatadog_api/process_discovery.c +117 -0
  20. data/ext/libdatadog_api/process_discovery.h +5 -0
  21. data/ext/libdatadog_extconf_helpers.rb +1 -1
  22. data/lib/datadog/appsec/actions_handler.rb +3 -2
  23. data/lib/datadog/appsec/api_security/lru_cache.rb +49 -0
  24. data/lib/datadog/appsec/api_security.rb +9 -0
  25. data/lib/datadog/appsec/assets/waf_rules/recommended.json +1344 -0
  26. data/lib/datadog/appsec/assets/waf_rules/strict.json +1344 -0
  27. data/lib/datadog/appsec/autoload.rb +1 -1
  28. data/lib/datadog/appsec/component.rb +11 -4
  29. data/lib/datadog/appsec/configuration/settings.rb +31 -18
  30. data/lib/datadog/appsec/context.rb +1 -1
  31. data/lib/datadog/appsec/contrib/active_record/instrumentation.rb +10 -12
  32. data/lib/datadog/appsec/contrib/active_record/integration.rb +1 -1
  33. data/lib/datadog/appsec/contrib/active_record/patcher.rb +22 -22
  34. data/lib/datadog/appsec/contrib/devise/data_extractor.rb +2 -3
  35. data/lib/datadog/appsec/contrib/devise/ext.rb +1 -0
  36. data/lib/datadog/appsec/contrib/devise/integration.rb +1 -1
  37. data/lib/datadog/appsec/contrib/devise/patcher.rb +3 -5
  38. data/lib/datadog/appsec/contrib/devise/tracking_middleware.rb +17 -4
  39. data/lib/datadog/appsec/contrib/excon/integration.rb +1 -1
  40. data/lib/datadog/appsec/contrib/excon/ssrf_detection_middleware.rb +9 -10
  41. data/lib/datadog/appsec/contrib/faraday/integration.rb +1 -1
  42. data/lib/datadog/appsec/contrib/faraday/ssrf_detection_middleware.rb +8 -9
  43. data/lib/datadog/appsec/contrib/graphql/gateway/watcher.rb +8 -9
  44. data/lib/datadog/appsec/contrib/graphql/integration.rb +1 -1
  45. data/lib/datadog/appsec/contrib/rack/gateway/watcher.rb +22 -32
  46. data/lib/datadog/appsec/contrib/rack/integration.rb +1 -1
  47. data/lib/datadog/appsec/contrib/rack/request_middleware.rb +16 -16
  48. data/lib/datadog/appsec/contrib/rails/gateway/watcher.rb +11 -13
  49. data/lib/datadog/appsec/contrib/rails/integration.rb +1 -1
  50. data/lib/datadog/appsec/contrib/rails/patcher.rb +21 -21
  51. data/lib/datadog/appsec/contrib/rest_client/integration.rb +1 -1
  52. data/lib/datadog/appsec/contrib/rest_client/request_ssrf_detection_patch.rb +10 -11
  53. data/lib/datadog/appsec/contrib/sinatra/gateway/watcher.rb +17 -23
  54. data/lib/datadog/appsec/contrib/sinatra/integration.rb +1 -1
  55. data/lib/datadog/appsec/event.rb +85 -95
  56. data/lib/datadog/appsec/instrumentation/gateway/argument.rb +5 -2
  57. data/lib/datadog/appsec/metrics/telemetry.rb +1 -1
  58. data/lib/datadog/appsec/monitor/gateway/watcher.rb +42 -12
  59. data/lib/datadog/appsec/processor/rule_loader.rb +26 -28
  60. data/lib/datadog/appsec/processor/rule_merger.rb +5 -5
  61. data/lib/datadog/appsec/processor.rb +1 -1
  62. data/lib/datadog/appsec/remote.rb +14 -13
  63. data/lib/datadog/appsec/response.rb +6 -6
  64. data/lib/datadog/appsec/security_engine/runner.rb +1 -1
  65. data/lib/datadog/appsec/security_event.rb +39 -0
  66. data/lib/datadog/appsec.rb +1 -1
  67. data/lib/datadog/core/buffer/random.rb +18 -2
  68. data/lib/datadog/core/configuration/agent_settings_resolver.rb +5 -5
  69. data/lib/datadog/core/configuration/agentless_settings_resolver.rb +176 -0
  70. data/lib/datadog/core/configuration/components.rb +48 -30
  71. data/lib/datadog/core/configuration/components_state.rb +23 -0
  72. data/lib/datadog/core/configuration/option.rb +79 -43
  73. data/lib/datadog/core/configuration/option_definition.rb +4 -4
  74. data/lib/datadog/core/configuration/options.rb +1 -1
  75. data/lib/datadog/core/configuration/settings.rb +20 -10
  76. data/lib/datadog/core/configuration/stable_config.rb +23 -0
  77. data/lib/datadog/core/configuration.rb +40 -16
  78. data/lib/datadog/core/crashtracking/component.rb +3 -10
  79. data/lib/datadog/core/encoding.rb +1 -1
  80. data/lib/datadog/core/environment/cgroup.rb +10 -12
  81. data/lib/datadog/core/environment/container.rb +38 -40
  82. data/lib/datadog/core/environment/ext.rb +6 -6
  83. data/lib/datadog/core/environment/git.rb +1 -0
  84. data/lib/datadog/core/environment/identity.rb +3 -3
  85. data/lib/datadog/core/environment/platform.rb +3 -3
  86. data/lib/datadog/core/environment/variable_helpers.rb +1 -1
  87. data/lib/datadog/core/error.rb +11 -9
  88. data/lib/datadog/core/logger.rb +2 -2
  89. data/lib/datadog/core/metrics/client.rb +20 -21
  90. data/lib/datadog/core/metrics/logging.rb +5 -5
  91. data/lib/datadog/core/process_discovery.rb +32 -0
  92. data/lib/datadog/core/rate_limiter.rb +4 -2
  93. data/lib/datadog/core/remote/client.rb +39 -31
  94. data/lib/datadog/core/remote/component.rb +3 -3
  95. data/lib/datadog/core/remote/configuration/digest.rb +7 -7
  96. data/lib/datadog/core/remote/configuration/path.rb +1 -1
  97. data/lib/datadog/core/remote/transport/http/client.rb +1 -1
  98. data/lib/datadog/core/remote/transport/http/config.rb +21 -5
  99. data/lib/datadog/core/remote/transport/http/negotiation.rb +1 -1
  100. data/lib/datadog/core/runtime/metrics.rb +4 -4
  101. data/lib/datadog/core/telemetry/component.rb +78 -53
  102. data/lib/datadog/core/telemetry/emitter.rb +23 -11
  103. data/lib/datadog/core/telemetry/event/app_client_configuration_change.rb +65 -0
  104. data/lib/datadog/core/telemetry/event/app_closing.rb +18 -0
  105. data/lib/datadog/core/telemetry/event/app_dependencies_loaded.rb +33 -0
  106. data/lib/datadog/core/telemetry/event/app_heartbeat.rb +18 -0
  107. data/lib/datadog/core/telemetry/event/app_integrations_change.rb +58 -0
  108. data/lib/datadog/core/telemetry/event/app_started.rb +179 -0
  109. data/lib/datadog/core/telemetry/event/base.rb +40 -0
  110. data/lib/datadog/core/telemetry/event/distributions.rb +18 -0
  111. data/lib/datadog/core/telemetry/event/generate_metrics.rb +43 -0
  112. data/lib/datadog/core/telemetry/event/log.rb +76 -0
  113. data/lib/datadog/core/telemetry/event/message_batch.rb +42 -0
  114. data/lib/datadog/core/telemetry/event/synth_app_client_configuration_change.rb +43 -0
  115. data/lib/datadog/core/telemetry/event.rb +17 -472
  116. data/lib/datadog/core/telemetry/http/adapters/net.rb +12 -97
  117. data/lib/datadog/core/telemetry/logger.rb +1 -1
  118. data/lib/datadog/core/telemetry/metric.rb +3 -3
  119. data/lib/datadog/core/telemetry/request.rb +3 -3
  120. data/lib/datadog/core/telemetry/transport/http/api.rb +43 -0
  121. data/lib/datadog/core/telemetry/transport/http/client.rb +49 -0
  122. data/lib/datadog/core/telemetry/transport/http/telemetry.rb +92 -0
  123. data/lib/datadog/core/telemetry/transport/http.rb +63 -0
  124. data/lib/datadog/core/telemetry/transport/telemetry.rb +51 -0
  125. data/lib/datadog/core/telemetry/worker.rb +90 -24
  126. data/lib/datadog/core/transport/http/adapters/test.rb +2 -1
  127. data/lib/datadog/core/transport/http/builder.rb +13 -13
  128. data/lib/datadog/core/utils/at_fork_monkey_patch.rb +6 -6
  129. data/lib/datadog/core/utils/duration.rb +32 -32
  130. data/lib/datadog/core/utils/forking.rb +2 -2
  131. data/lib/datadog/core/utils/network.rb +6 -6
  132. data/lib/datadog/core/utils/only_once_successful.rb +16 -5
  133. data/lib/datadog/core/utils/time.rb +20 -0
  134. data/lib/datadog/core/utils/truncation.rb +21 -0
  135. data/lib/datadog/core/vendor/multipart-post/multipart/post/composite_read_io.rb +1 -1
  136. data/lib/datadog/core/vendor/multipart-post/multipart/post/multipartable.rb +8 -8
  137. data/lib/datadog/core/vendor/multipart-post/multipart/post/parts.rb +7 -7
  138. data/lib/datadog/core/worker.rb +1 -1
  139. data/lib/datadog/core/workers/async.rb +29 -12
  140. data/lib/datadog/core/workers/interval_loop.rb +12 -1
  141. data/lib/datadog/core/workers/runtime_metrics.rb +2 -2
  142. data/lib/datadog/core.rb +8 -0
  143. data/lib/datadog/di/boot.rb +34 -0
  144. data/lib/datadog/di/remote.rb +2 -0
  145. data/lib/datadog/di.rb +5 -32
  146. data/lib/datadog/error_tracking/collector.rb +87 -0
  147. data/lib/datadog/error_tracking/component.rb +167 -0
  148. data/lib/datadog/error_tracking/configuration/settings.rb +63 -0
  149. data/lib/datadog/error_tracking/configuration.rb +11 -0
  150. data/lib/datadog/error_tracking/ext.rb +18 -0
  151. data/lib/datadog/error_tracking/extensions.rb +16 -0
  152. data/lib/datadog/error_tracking/filters.rb +77 -0
  153. data/lib/datadog/error_tracking.rb +18 -0
  154. data/lib/datadog/kit/identity.rb +1 -1
  155. data/lib/datadog/profiling/collectors/code_provenance.rb +1 -1
  156. data/lib/datadog/profiling/exporter.rb +1 -1
  157. data/lib/datadog/profiling/ext.rb +0 -1
  158. data/lib/datadog/profiling/flush.rb +1 -1
  159. data/lib/datadog/profiling/http_transport.rb +1 -6
  160. data/lib/datadog/profiling/scheduler.rb +8 -1
  161. data/lib/datadog/profiling/tag_builder.rb +1 -5
  162. data/lib/datadog/tracing/analytics.rb +1 -1
  163. data/lib/datadog/tracing/contrib/active_support/cache/events/cache.rb +4 -1
  164. data/lib/datadog/tracing/contrib/active_support/cache/instrumentation.rb +33 -0
  165. data/lib/datadog/tracing/contrib/active_support/cache/patcher.rb +4 -0
  166. data/lib/datadog/tracing/contrib/active_support/cache/redis.rb +2 -4
  167. data/lib/datadog/tracing/contrib/aws/instrumentation.rb +10 -0
  168. data/lib/datadog/tracing/contrib/aws/parsed_context.rb +5 -1
  169. data/lib/datadog/tracing/contrib/http/instrumentation.rb +1 -5
  170. data/lib/datadog/tracing/contrib/httpclient/instrumentation.rb +1 -5
  171. data/lib/datadog/tracing/contrib/httprb/instrumentation.rb +1 -5
  172. data/lib/datadog/tracing/contrib/karafka/distributed/propagation.rb +2 -0
  173. data/lib/datadog/tracing/contrib/karafka/monitor.rb +1 -1
  174. data/lib/datadog/tracing/contrib/mongodb/configuration/settings.rb +8 -0
  175. data/lib/datadog/tracing/contrib/mongodb/ext.rb +1 -0
  176. data/lib/datadog/tracing/contrib/mongodb/subscribers.rb +18 -1
  177. data/lib/datadog/tracing/contrib/patcher.rb +5 -2
  178. data/lib/datadog/tracing/contrib/support.rb +28 -0
  179. data/lib/datadog/tracing/distributed/b3_multi.rb +1 -1
  180. data/lib/datadog/tracing/distributed/b3_single.rb +1 -1
  181. data/lib/datadog/tracing/distributed/datadog.rb +2 -2
  182. data/lib/datadog/tracing/metadata/errors.rb +4 -4
  183. data/lib/datadog/tracing/sampling/rate_sampler.rb +2 -1
  184. data/lib/datadog/tracing/span_operation.rb +38 -14
  185. data/lib/datadog/tracing/trace_operation.rb +15 -7
  186. data/lib/datadog/tracing/tracer.rb +7 -3
  187. data/lib/datadog/tracing/utils.rb +1 -1
  188. data/lib/datadog/version.rb +1 -1
  189. data/lib/datadog.rb +2 -3
  190. metadata +53 -10
  191. data/lib/datadog/core/telemetry/http/env.rb +0 -20
  192. data/lib/datadog/core/telemetry/http/ext.rb +0 -28
  193. data/lib/datadog/core/telemetry/http/response.rb +0 -70
  194. data/lib/datadog/core/telemetry/http/transport.rb +0 -90
@@ -5,14 +5,25 @@ require_relative 'only_once'
5
5
  module Datadog
6
6
  module Core
7
7
  module Utils
8
- # Helper class to execute something with only one success.
8
+ # Helper class to execute something with only one successful execution.
9
9
  #
10
- # This is useful for cases where we want to ensure that a block of code is only executed once, and only if it
11
- # succeeds. One such example is sending app-started telemetry event.
10
+ # If limit is not provided to the constructor, +run+ will execute the
11
+ # block an unlimited number of times until the block indicates that it
12
+ # executed successfully by returning a truthy value. After a block
13
+ # executes successfully, subsequent +run+ calls will not invoke the
14
+ # block.
12
15
  #
13
- # Successful execution is determined by the return value of the block: any truthy value is considered success.
16
+ # If a non-zero limit is provided to the constructor, +run+ will
17
+ # execute the block up to that many times, and will mark the instance
18
+ # of OnlyOneSuccessful as failed if none of the executions succeeded.
14
19
  #
15
- # Thread-safe when used correctly (e.g. be careful of races when lazily initializing instances of this class).
20
+ # One consumer of this class is sending the app-started telemetry event.
21
+ #
22
+ # Successful execution is determined by the return value of the block:
23
+ # any truthy value is considered success.
24
+ #
25
+ # This class is thread-safe (however, instances of it must also be
26
+ # created in a thread-safe manner).
16
27
  #
17
28
  # Note: In its current state, this class is not Ractor-safe.
18
29
  # In https://github.com/DataDog/dd-trace-rb/pull/1398#issuecomment-797378810 we have a discussion of alternatives,
@@ -31,6 +31,16 @@ module Datadog
31
31
  #
32
32
  # @param block [Proc] block that returns a `Time` object representing the current wall time
33
33
  def now_provider=(block)
34
+ class << self
35
+ # Avoid method redefinition warning.
36
+ # `rescue nil` is added in case customers remove the method
37
+ # themselves to squelch the warning.
38
+ begin
39
+ remove_method(:now)
40
+ rescue
41
+ nil
42
+ end
43
+ end
34
44
  define_singleton_method(:now, &block)
35
45
  end
36
46
 
@@ -43,6 +53,16 @@ module Datadog
43
53
  #
44
54
  # @param block [Proc] block that accepts unit and returns timestamp in the requested unit
45
55
  def get_time_provider=(block)
56
+ class << self
57
+ # Avoid method redefinition warning
58
+ # `rescue nil` is added in case customers remove the method
59
+ # themselves to squelch the warning.
60
+ begin
61
+ remove_method(:get_time)
62
+ rescue
63
+ nil
64
+ end
65
+ end
46
66
  define_singleton_method(:get_time, &block)
47
67
  end
48
68
 
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datadog
4
+ module Core
5
+ module Utils
6
+ # Helper methods for truncating data
7
+ module Truncation
8
+ module_function
9
+
10
+ def truncate_in_middle(string, max_prefix_length, max_suffix_length)
11
+ max_length = max_prefix_length + 3 + max_suffix_length
12
+ if string.length > max_length
13
+ "#{string[0...max_prefix_length]}...#{string[-max_suffix_length..-1]}"
14
+ else
15
+ string
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -108,7 +108,7 @@ module Datadog
108
108
  end
109
109
 
110
110
  def respond_to?(meth, include_all = false)
111
- @io.respond_to?(meth, include_all) || super(meth, include_all)
111
+ @io.respond_to?(meth, include_all) || super
112
112
  end
113
113
  end
114
114
  end
@@ -28,29 +28,29 @@ module Datadog
28
28
  "--#{SecureRandom.uuid}"
29
29
  end
30
30
 
31
- def initialize(path, params, headers={}, boundary = Multipartable.secure_boundary)
31
+ def initialize(path, params, headers = {}, boundary = Multipartable.secure_boundary)
32
32
  headers = headers.clone # don't want to modify the original variable
33
33
  parts_headers = headers.delete(:parts) || {}
34
34
  super(path, headers)
35
- parts = params.map do |k,v|
35
+ parts = params.map do |k, v|
36
36
  case v
37
37
  when Array
38
- v.map {|item| Parts::Part.new(boundary, "#{k}[]", item, parts_headers[k]) }
38
+ v.map { |item| Parts::Part.new(boundary, "#{k}[]", item, parts_headers[k]) }
39
39
  else
40
40
  Parts::Part.new(boundary, k, v, parts_headers[k])
41
41
  end
42
42
  end.flatten
43
43
  parts << Parts::EpiloguePart.new(boundary)
44
- ios = parts.map {|p| p.to_io }
45
- self.set_content_type(headers["Content-Type"] || "multipart/form-data",
46
- { "boundary" => boundary })
47
- self.content_length = parts.inject(0) {|sum,i| sum + i.length }
44
+ ios = parts.map { |p| p.to_io }
45
+ set_content_type(headers["Content-Type"] || "multipart/form-data",
46
+ {"boundary" => boundary})
47
+ self.content_length = parts.inject(0) { |sum, i| sum + i.length }
48
48
  self.body_stream = CompositeReadIO.new(*ios)
49
49
 
50
50
  @boundary = boundary
51
51
  end
52
52
 
53
- attr :boundary
53
+ attr_reader :boundary
54
54
  end
55
55
  end
56
56
  end
@@ -58,7 +58,7 @@ module Datadog
58
58
  part = ''
59
59
  part << "--#{boundary}\r\n"
60
60
  part << "Content-ID: #{headers["Content-ID"]}\r\n" if headers["Content-ID"]
61
- part << "Content-Disposition: form-data; name=\"#{name.to_s}\"\r\n"
61
+ part << "Content-Disposition: form-data; name=\"#{name}\"\r\n"
62
62
  part << "Content-Type: #{headers["Content-Type"]}\r\n" if headers["Content-Type"]
63
63
  part << "\r\n"
64
64
  part << "#{value}\r\n"
@@ -76,9 +76,9 @@ module Datadog
76
76
  # @param io [IO]
77
77
  # @param headers [Hash]
78
78
  def initialize(boundary, name, io, headers = {})
79
- file_length = io.respond_to?(:length) ? io.length : File.size(io.local_path)
79
+ file_length = io.respond_to?(:length) ? io.length : File.size(io.local_path)
80
80
  @head = build_head(boundary, name, io.original_filename, io.content_type, file_length,
81
- io.respond_to?(:opts) ? io.opts.merge(headers) : headers)
81
+ io.respond_to?(:opts) ? io.opts.merge(headers) : headers)
82
82
  @foot = "\r\n"
83
83
  @length = @head.bytesize + file_length + @foot.length
84
84
  @io = CompositeReadIO.new(StringIO.new(@head), io, StringIO.new(@foot))
@@ -98,16 +98,16 @@ module Datadog
98
98
 
99
99
  part = ''
100
100
  part << "--#{boundary}\r\n"
101
- part << "Content-Disposition: #{content_disposition}; name=\"#{name.to_s}\"; filename=\"#{filename}\"\r\n"
101
+ part << "Content-Disposition: #{content_disposition}; name=\"#{name}\"; filename=\"#{filename}\"\r\n"
102
102
  part << "Content-Length: #{content_len}\r\n"
103
103
  if content_id = opts.delete("Content-ID")
104
104
  part << "Content-ID: #{content_id}\r\n"
105
105
  end
106
106
 
107
- if opts["Content-Type"] != nil
108
- part << "Content-Type: " + opts["Content-Type"] + "\r\n"
107
+ part << if !opts["Content-Type"].nil?
108
+ "Content-Type: " + opts["Content-Type"] + "\r\n"
109
109
  else
110
- part << "Content-Type: #{type}\r\n"
110
+ "Content-Type: #{type}\r\n"
111
111
  end
112
112
 
113
113
  part << "Content-Transfer-Encoding: #{trans_encoding}\r\n"
@@ -12,7 +12,7 @@ module Datadog
12
12
  end
13
13
 
14
14
  def perform(*args)
15
- task.call(*args) unless task.nil?
15
+ task&.call(*args)
16
16
  end
17
17
 
18
18
  protected
@@ -47,6 +47,14 @@ module Datadog
47
47
  @run_async = false
48
48
  Datadog.logger.debug { "Forcibly terminating worker thread for: #{self}" }
49
49
  worker.terminate
50
+ # Wait for the worker thread to end
51
+ begin
52
+ Timeout.timeout(SHUTDOWN_TIMEOUT) do
53
+ worker.join
54
+ end
55
+ rescue Timeout::Error
56
+ Datadog.logger.debug { "Worker thread did not end after #{SHUTDOWN_TIMEOUT} seconds: #{self}" }
57
+ end
50
58
  true
51
59
  end
52
60
 
@@ -112,23 +120,33 @@ module Datadog
112
120
  @worker ||= nil
113
121
  end
114
122
 
123
+ # Returns true if worker thread is successfully started,
124
+ # false if it is not started. Reasons for not starting the worker
125
+ # thread: it is already running, or the process forked and fork
126
+ # policy is to stop the workers on fork (which means they are
127
+ # not started in children, really).
115
128
  def start_async(&block)
116
129
  mutex.synchronize do
117
- return if running?
130
+ return false if running?
118
131
 
119
132
  if forked?
120
133
  case fork_policy
121
134
  when FORK_POLICY_STOP
122
135
  stop_fork
136
+ false
123
137
  when FORK_POLICY_RESTART
138
+ # restart_after_fork should return true
124
139
  restart_after_fork(&block)
125
140
  end
126
141
  elsif !run_async?
142
+ # start_worker should return true
127
143
  start_worker(&block)
128
144
  end
129
145
  end
130
146
  end
131
147
 
148
+ # Returns true if worker thread is successfully started,
149
+ # which should generally be always.
132
150
  def start_worker
133
151
  @run_async = true
134
152
  @pid = Process.pid
@@ -136,22 +154,21 @@ module Datadog
136
154
  Datadog.logger.debug { "Starting thread for: #{self}" }
137
155
 
138
156
  @worker = ::Thread.new do
139
- begin
140
- yield
141
- # rubocop:disable Lint/RescueException
142
- rescue Exception => e
143
- @error = e
144
- Datadog.logger.debug(
145
- "Worker thread error. Cause: #{e.class.name} #{e.message} Location: #{Array(e.backtrace).first}"
146
- )
147
- raise
148
- end
157
+ yield
158
+ # rubocop:disable Lint/RescueException
159
+ rescue Exception => e
160
+ @error = e
161
+ Datadog.logger.debug(
162
+ "Worker thread error. Cause: #{e.class.name} #{e.message} Location: #{Array(e.backtrace).first}"
163
+ )
164
+ raise
165
+
149
166
  # rubocop:enable Lint/RescueException
150
167
  end
151
168
  @worker.name = self.class.name
152
169
  @worker.thread_variable_set(:fork_safe, true)
153
170
 
154
- nil
171
+ true
155
172
  end
156
173
 
157
174
  def stop_fork
@@ -21,7 +21,18 @@ module Datadog
21
21
  # Methods that must be prepended
22
22
  module PrependedMethods
23
23
  def perform(*args)
24
- perform_loop { super(*args) }
24
+ perform_loop do
25
+ @in_iteration = true
26
+ begin
27
+ super(*args)
28
+ ensure
29
+ @in_iteration = false
30
+ end
31
+ end
32
+ end
33
+
34
+ def in_iteration?
35
+ defined?(@in_iteration) && @in_iteration
25
36
  end
26
37
  end
27
38
 
@@ -20,8 +20,8 @@ module Datadog
20
20
  attr_reader \
21
21
  :metrics
22
22
 
23
- def initialize(options = {})
24
- @metrics = options.fetch(:metrics) { Core::Runtime::Metrics.new(logger: options[:logger]) }
23
+ def initialize(telemetry:, **options)
24
+ @metrics = options.fetch(:metrics) { Core::Runtime::Metrics.new(logger: options[:logger], telemetry: telemetry) }
25
25
 
26
26
  # Workers::Async::Thread settings
27
27
  self.fork_policy = options.fetch(:fork_policy, Workers::Async::Thread::FORK_POLICY_STOP)
data/lib/datadog/core.rb CHANGED
@@ -11,6 +11,14 @@ module Datadog
11
11
  # for higher-level features.
12
12
  module Core
13
13
  extend Core::Deprecations
14
+
15
+ LIBDATADOG_API_FAILURE =
16
+ begin
17
+ require "libdatadog_api.#{RUBY_VERSION[/\d+.\d+/]}_#{RUBY_PLATFORM}"
18
+ nil
19
+ rescue LoadError => e
20
+ e.message
21
+ end
14
22
  end
15
23
 
16
24
  extend Core::Extensions
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'logger'
4
+ require_relative 'base'
5
+ require_relative 'error'
6
+ require_relative 'code_tracker'
7
+ require_relative 'component'
8
+ require_relative 'instrumenter'
9
+ require_relative 'probe'
10
+ require_relative 'probe_builder'
11
+ require_relative 'probe_manager'
12
+ require_relative 'probe_notification_builder'
13
+ require_relative 'probe_notifier_worker'
14
+ require_relative 'redactor'
15
+ require_relative 'serializer'
16
+ require_relative 'transport/http'
17
+ require_relative 'utils'
18
+
19
+ if %w[1 true yes].include?(ENV['DD_DYNAMIC_INSTRUMENTATION_ENABLED']) # steep:ignore
20
+ # For initial release of Dynamic Instrumentation, activate code tracking
21
+ # only if DI is explicitly requested in the environment.
22
+ # Code tracking is required for line probes to work; see the comments
23
+ # above for the implementation of the method.
24
+ #
25
+ # If DI is enabled programmatically, the application can (and must,
26
+ # for line probes to work) activate tracking in an initializer.
27
+ # We seem to have Datadog.logger here already
28
+ Datadog.logger.debug("di: activating code tracking")
29
+ Datadog::DI.activate_tracking
30
+ end
31
+
32
+ require_relative 'contrib'
33
+
34
+ Datadog::DI::Contrib.load_now_or_later
@@ -18,6 +18,8 @@ module Datadog
18
18
  PRODUCT = 'LIVE_DEBUGGING'
19
19
 
20
20
  def products
21
+ # TODO: do not send our product on unsupported runtimes
22
+ # (Ruby 2.5 / JRuby)
21
23
  [PRODUCT]
22
24
  end
23
25
 
data/lib/datadog/di.rb CHANGED
@@ -1,24 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'di/logger'
4
- require_relative 'di/base'
5
- require_relative 'di/error'
6
- require_relative 'di/code_tracker'
7
- require_relative 'di/component'
8
3
  require_relative 'di/configuration'
9
4
  require_relative 'di/extensions'
10
- require_relative 'di/instrumenter'
11
- require_relative 'di/probe'
12
- require_relative 'di/probe_builder'
13
- require_relative 'di/probe_manager'
14
- require_relative 'di/probe_notification_builder'
15
- require_relative 'di/probe_notifier_worker'
16
- require_relative 'di/redactor'
17
5
  require_relative 'di/remote'
18
- require_relative 'di/serializer'
19
- #require_relative 'di/transport'
20
- require_relative 'di/transport/http'
21
- require_relative 'di/utils'
22
6
 
23
7
  module Datadog
24
8
  # Namespace for Datadog dynamic instrumentation.
@@ -52,19 +36,8 @@ module Datadog
52
36
  end
53
37
  end
54
38
 
55
- if %w(1 true).include?(ENV['DD_DYNAMIC_INSTRUMENTATION_ENABLED']) # steep:ignore
56
- # For initial release of Dynamic Instrumentation, activate code tracking
57
- # only if DI is explicitly requested in the environment.
58
- # Code tracking is required for line probes to work; see the comments
59
- # above for the implementation of the method.
60
- #
61
- # If DI is enabled programmatically, the application can (and must,
62
- # for line probes to work) activate tracking in an initializer.
63
- # We seem to have Datadog.logger here already
64
- Datadog.logger.debug("di: activating code tracking")
65
- Datadog::DI.activate_tracking
66
- end
67
-
68
- require_relative 'di/contrib'
69
-
70
- Datadog::DI::Contrib.load_now_or_later
39
+ # Line probes will not work on Ruby < 2.6 because of lack of :script_compiled
40
+ # trace point. Activate DI automatically on supported Ruby versions but
41
+ # always load its settings so that, for example, turning DI off when
42
+ # we are on Ruby 2.5 does not produce exceptions.
43
+ require_relative 'di/boot' if RUBY_VERSION >= '2.6' && RUBY_ENGINE != 'jruby'
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'ext'
4
+
5
+ module Datadog
6
+ module ErrorTracking
7
+ # The Collector is in charge, for a SpanOperation of storing the span events
8
+ # created when an error is handled. Each SpanOperation has a Collector as soon
9
+ # as a span event is created and the Collector has the same life time as the SpanOp.
10
+ #
11
+ # If an error is handled then rethrown, the SpanEvent corresponding to the error
12
+ # will be deleted. That is why we do not add directly the SpanEvent to the SpanOp.
13
+ #
14
+ # @api private
15
+ class Collector
16
+ SPAN_EVENTS_LIMIT = 100
17
+ LOCK = Mutex.new
18
+ # Proc called when the span_operation :after_stop event is published
19
+ def self.after_stop
20
+ @after_stop ||= proc do |span_op, error|
21
+ # if this proc is called, we are sure that span_op has a collector
22
+ collector = span_op.get_collector_or_initialize
23
+ # if an error exited the scope of the span, we delete the corresponding SpanEvent.
24
+ collector.on_error(span_op, error) if error
25
+
26
+ span_events = collector.span_events
27
+ span_op.span_events.concat(span_events)
28
+ end
29
+ end
30
+
31
+ def initialize
32
+ @span_event_per_error = {}
33
+ end
34
+
35
+ def add_span_event(span_op, span_event, error)
36
+ # When this is the first time we add a span event for a span,
37
+ # we suscribe to the :after_stop event
38
+ if @span_event_per_error.empty?
39
+ events = span_op.send(:events)
40
+ events.after_stop.subscribe(&self.class.after_stop)
41
+
42
+ # This tag is used by the Error Tracking product to report
43
+ # the error in Error Tracking
44
+ span_op.set_tag(Ext::SPAN_EVENTS_HAS_EXCEPTION, true)
45
+ end
46
+ # Set a limit to the number of span event we can store per SpanOp
47
+ # If an error has been handled several times in the same span we can still
48
+ # modify the event (even if the capacity is reached) in order to report
49
+ # the information of the last rescue
50
+ if @span_event_per_error.key?(error) || @span_event_per_error.length < SPAN_EVENTS_LIMIT
51
+ @span_event_per_error[error] = span_event
52
+ end
53
+ end
54
+
55
+ if RUBY_VERSION >= Ext::RUBY_VERSION_WITH_RESCUE_EVENT
56
+ # Starting from ruby3.3, as we are listening to :rescue event,
57
+ # we just want to remove the span event if the error was
58
+ # previously handled
59
+ def on_error(_span_op, error)
60
+ @span_event_per_error.delete(error)
61
+ end
62
+ else
63
+ # Up to ruby3.2, we are listening to :raise event. We need to ensure
64
+ # that an error exiting the scope of a span is not handled in a parent span.
65
+ # This function will propagate the span event to the parent span. If the
66
+ # error is not handled in the parent span, it will be deleted by design.
67
+ def on_error(span_op, error)
68
+ return unless @span_event_per_error.key?(error)
69
+
70
+ unless span_op.root?
71
+ parent = span_op.send(:parent)
72
+ LOCK.synchronize do
73
+ parent_collector = parent.get_collector_or_initialize { Collector.new }
74
+ parent_collector.add_span_event(parent, @span_event_per_error[error], error)
75
+ end
76
+ end
77
+
78
+ @span_event_per_error.delete(error)
79
+ end
80
+ end
81
+
82
+ def span_events
83
+ @span_event_per_error.values
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,167 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'set'
4
+ require_relative 'collector'
5
+ require_relative 'filters'
6
+
7
+ module Datadog
8
+ module ErrorTracking
9
+ # Component for Error Tracking.
10
+ #
11
+ # Only one instance of the Component should ever be active.
12
+ #
13
+ # The component instance records every handled exceptions from the configured scopes
14
+ # (user, third_party packages, specified files or everything).
15
+ class Component
16
+ LOCK = Mutex.new
17
+
18
+ class << self
19
+ def build(settings, tracer, logger)
20
+ return if !settings.respond_to?(:error_tracking) || (settings.error_tracking.handled_errors.nil? &&
21
+ settings.error_tracking.handled_errors_include.empty?)
22
+
23
+ return unless environment_supported?(logger)
24
+
25
+ new(
26
+ tracer: tracer,
27
+ handled_errors: settings.error_tracking.handled_errors,
28
+ handled_errors_include: settings.error_tracking.handled_errors_include,
29
+ ).tap(&:start)
30
+ end
31
+
32
+ def environment_supported?(logger)
33
+ if RUBY_ENGINE != 'ruby'
34
+ logger.warn("error tracking: cannot enable error tracking: MRI is required, but running on #{RUBY_ENGINE}")
35
+ false
36
+ elsif RUBY_VERSION < '2.7'
37
+ logger.warn(
38
+ "error tracking: cannot enable error tracking: Ruby 2.7+ is required, but running
39
+ on #{RUBY_VERSION}"
40
+ )
41
+ false
42
+ else
43
+ true
44
+ end
45
+ end
46
+ end
47
+
48
+ def initialize(tracer:, handled_errors:, handled_errors_include:)
49
+ @tracer = tracer
50
+
51
+ # Hash containing the paths to the instrumented files
52
+ @instrumented_files = Set.new unless handled_errors_include.empty?
53
+ # Array containing file paths, file names and gems names to instrument.
54
+ # This is coming from the DD_ERROR_TRACKING_HANDLED_ERRORS_INCLUDE env variable
55
+ @handled_errors_include = handled_errors_include
56
+
57
+ # Filter function is used to filter out the exception
58
+ # we do not want to report. For instance exception from gems.
59
+ @filter_function = Filters.generate_filter(handled_errors, @instrumented_files)
60
+
61
+ # :rescue event was added in Ruby 3.3
62
+ #
63
+ # Before Ruby3.3 the TracePoint listen for :raise events.
64
+ # If an error is not handled, we will delete the according
65
+ # span event in the collector.
66
+ event = (RUBY_VERSION >= '3.3') ? :rescue : :raise
67
+
68
+ # This TracePoint is in charge of capturing the handled exceptions
69
+ # and of adding the corresponding span events to the collector
70
+ @handled_exc_tracker = create_exc_tracker_trace_point(event)
71
+
72
+ if @instrumented_files
73
+ # The only thing we know about the handled errors is the path of the file
74
+ # in which the error was rescued. Therefore, we need to retrieve the path
75
+ # of the files the user want to instrument. This TracePoint is used for that
76
+ # purpose
77
+ @include_path_getter = create_script_compiled_trace_point
78
+ end
79
+ end
80
+
81
+ def create_exc_tracker_trace_point(event)
82
+ TracePoint.new(event) do |tp|
83
+ active_span = @tracer.active_span
84
+ if active_span
85
+ raised_exception = tp.raised_exception
86
+ # Note that in 3.2, this will give the path of where the error was raised
87
+ # which may cause the handled_error_include env variable to malfunction.
88
+ rescue_file_path = tp.path
89
+ if @filter_function.call(rescue_file_path)
90
+ span_event = generate_span_event(raised_exception)
91
+ LOCK.synchronize do
92
+ collector = active_span.get_collector_or_initialize { Collector.new }
93
+ collector.add_span_event(active_span, span_event, raised_exception)
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
99
+
100
+ def create_script_compiled_trace_point
101
+ TracePoint.new(:script_compiled) do |tp|
102
+ next if tp.eval_script
103
+
104
+ path = tp.instruction_sequence.path
105
+ next if path.nil?
106
+
107
+ @handled_errors_include.each do |file_to_instr|
108
+ # The user can provide either
109
+ # - absolute_path starting with '/'. In that case the path of the file
110
+ # should begin with file_to_instr
111
+ # - a relative_path starting with './'. In that case, we extend the path
112
+ # and it is the same as above
113
+ # - otherwise we just check if the name provided is in the path and is
114
+ # either the name of a folder or of a ruby file.
115
+ regex =
116
+ if file_to_instr.start_with?('/')
117
+ %r{\A#{Regexp.escape(file_to_instr)}(?:/|\.rb\z|\z)}
118
+ elsif file_to_instr.start_with?('./')
119
+ abs_path = File.expand_path(file_to_instr)
120
+ %r{\A#{Regexp.escape(abs_path)}(?:/|\.rb\z|\z)}
121
+ else
122
+ %r{/#{Regexp.escape(file_to_instr)}(?:/|\.rb\z|\z)}
123
+ end
124
+
125
+ add_instrumented_file(path) if path.match?(regex)
126
+ end
127
+ end
128
+ end
129
+
130
+ # Starts the TracePoints.
131
+ #
132
+ # Enables the script_compiled TracePoint if handled_errors_include is not empty.
133
+ def start
134
+ @handled_exc_tracker.enable
135
+ @include_path_getter&.enable
136
+ end
137
+
138
+ # Shuts down error tracker.
139
+ #
140
+ # Disables the TracePoints.
141
+ def shutdown!
142
+ @handled_exc_tracker.disable
143
+ @include_path_getter&.disable
144
+ end
145
+
146
+ private
147
+
148
+ # Generates a span event from the exception info.
149
+ #
150
+ # The event follows the otel semantics.
151
+ # https://opentelemetry.io/docs/specs/otel/trace/exceptions/
152
+ def generate_span_event(exception)
153
+ formatted_exception = Datadog::Core::Error.build_from(exception)
154
+ attributes = {
155
+ 'exception.type' => formatted_exception.type,
156
+ 'exception.message' => formatted_exception.message,
157
+ 'exception.stacktrace' => formatted_exception.backtrace
158
+ }
159
+ Datadog::Tracing::SpanEvent.new('exception', attributes: attributes)
160
+ end
161
+
162
+ def add_instrumented_file(file_path)
163
+ @instrumented_files&.add(file_path)
164
+ end
165
+ end
166
+ end
167
+ end