datadog 2.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (764) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +4236 -0
  3. data/LICENSE +6 -0
  4. data/LICENSE-3rdparty.csv +7 -0
  5. data/LICENSE.Apache +200 -0
  6. data/LICENSE.BSD3 +24 -0
  7. data/NOTICE +4 -0
  8. data/README.md +25 -0
  9. data/bin/ddprofrb +15 -0
  10. data/ext/datadog_profiling_loader/datadog_profiling_loader.c +134 -0
  11. data/ext/datadog_profiling_loader/extconf.rb +72 -0
  12. data/ext/datadog_profiling_native_extension/NativeExtensionDesign.md +156 -0
  13. data/ext/datadog_profiling_native_extension/clock_id.h +22 -0
  14. data/ext/datadog_profiling_native_extension/clock_id_from_pthread.c +56 -0
  15. data/ext/datadog_profiling_native_extension/clock_id_noop.c +22 -0
  16. data/ext/datadog_profiling_native_extension/collectors_cpu_and_wall_time_worker.c +1153 -0
  17. data/ext/datadog_profiling_native_extension/collectors_discrete_dynamic_sampler.c +422 -0
  18. data/ext/datadog_profiling_native_extension/collectors_discrete_dynamic_sampler.h +101 -0
  19. data/ext/datadog_profiling_native_extension/collectors_dynamic_sampling_rate.c +150 -0
  20. data/ext/datadog_profiling_native_extension/collectors_dynamic_sampling_rate.h +18 -0
  21. data/ext/datadog_profiling_native_extension/collectors_gc_profiling_helper.c +156 -0
  22. data/ext/datadog_profiling_native_extension/collectors_gc_profiling_helper.h +5 -0
  23. data/ext/datadog_profiling_native_extension/collectors_idle_sampling_helper.c +244 -0
  24. data/ext/datadog_profiling_native_extension/collectors_idle_sampling_helper.h +3 -0
  25. data/ext/datadog_profiling_native_extension/collectors_stack.c +372 -0
  26. data/ext/datadog_profiling_native_extension/collectors_stack.h +27 -0
  27. data/ext/datadog_profiling_native_extension/collectors_thread_context.c +1391 -0
  28. data/ext/datadog_profiling_native_extension/collectors_thread_context.h +15 -0
  29. data/ext/datadog_profiling_native_extension/extconf.rb +302 -0
  30. data/ext/datadog_profiling_native_extension/heap_recorder.c +970 -0
  31. data/ext/datadog_profiling_native_extension/heap_recorder.h +155 -0
  32. data/ext/datadog_profiling_native_extension/helpers.h +23 -0
  33. data/ext/datadog_profiling_native_extension/http_transport.c +375 -0
  34. data/ext/datadog_profiling_native_extension/libdatadog_helpers.c +62 -0
  35. data/ext/datadog_profiling_native_extension/libdatadog_helpers.h +42 -0
  36. data/ext/datadog_profiling_native_extension/native_extension_helpers.rb +319 -0
  37. data/ext/datadog_profiling_native_extension/private_vm_api_access.c +892 -0
  38. data/ext/datadog_profiling_native_extension/private_vm_api_access.h +61 -0
  39. data/ext/datadog_profiling_native_extension/profiling.c +267 -0
  40. data/ext/datadog_profiling_native_extension/ruby_helpers.c +267 -0
  41. data/ext/datadog_profiling_native_extension/ruby_helpers.h +119 -0
  42. data/ext/datadog_profiling_native_extension/setup_signal_handler.c +115 -0
  43. data/ext/datadog_profiling_native_extension/setup_signal_handler.h +11 -0
  44. data/ext/datadog_profiling_native_extension/stack_recorder.c +941 -0
  45. data/ext/datadog_profiling_native_extension/stack_recorder.h +27 -0
  46. data/ext/datadog_profiling_native_extension/time_helpers.c +53 -0
  47. data/ext/datadog_profiling_native_extension/time_helpers.h +26 -0
  48. data/lib/datadog/appsec/assets/blocked.html +99 -0
  49. data/lib/datadog/appsec/assets/blocked.json +1 -0
  50. data/lib/datadog/appsec/assets/blocked.text +5 -0
  51. data/lib/datadog/appsec/assets/waf_rules/README.md +7 -0
  52. data/lib/datadog/appsec/assets/waf_rules/processors.json +92 -0
  53. data/lib/datadog/appsec/assets/waf_rules/recommended.json +7703 -0
  54. data/lib/datadog/appsec/assets/waf_rules/scanners.json +114 -0
  55. data/lib/datadog/appsec/assets/waf_rules/strict.json +1635 -0
  56. data/lib/datadog/appsec/assets.rb +46 -0
  57. data/lib/datadog/appsec/autoload.rb +13 -0
  58. data/lib/datadog/appsec/component.rb +94 -0
  59. data/lib/datadog/appsec/configuration/settings.rb +202 -0
  60. data/lib/datadog/appsec/configuration.rb +11 -0
  61. data/lib/datadog/appsec/contrib/auto_instrument.rb +25 -0
  62. data/lib/datadog/appsec/contrib/devise/event.rb +57 -0
  63. data/lib/datadog/appsec/contrib/devise/ext.rb +13 -0
  64. data/lib/datadog/appsec/contrib/devise/integration.rb +42 -0
  65. data/lib/datadog/appsec/contrib/devise/patcher/authenticatable_patch.rb +76 -0
  66. data/lib/datadog/appsec/contrib/devise/patcher/registration_controller_patch.rb +54 -0
  67. data/lib/datadog/appsec/contrib/devise/patcher.rb +45 -0
  68. data/lib/datadog/appsec/contrib/devise/resource.rb +35 -0
  69. data/lib/datadog/appsec/contrib/devise/tracking.rb +49 -0
  70. data/lib/datadog/appsec/contrib/integration.rb +37 -0
  71. data/lib/datadog/appsec/contrib/patcher.rb +12 -0
  72. data/lib/datadog/appsec/contrib/rack/ext.rb +13 -0
  73. data/lib/datadog/appsec/contrib/rack/gateway/request.rb +104 -0
  74. data/lib/datadog/appsec/contrib/rack/gateway/response.rb +30 -0
  75. data/lib/datadog/appsec/contrib/rack/gateway/watcher.rb +162 -0
  76. data/lib/datadog/appsec/contrib/rack/integration.rb +44 -0
  77. data/lib/datadog/appsec/contrib/rack/patcher.rb +34 -0
  78. data/lib/datadog/appsec/contrib/rack/reactive/request.rb +81 -0
  79. data/lib/datadog/appsec/contrib/rack/reactive/request_body.rb +60 -0
  80. data/lib/datadog/appsec/contrib/rack/reactive/response.rb +66 -0
  81. data/lib/datadog/appsec/contrib/rack/request_body_middleware.rb +44 -0
  82. data/lib/datadog/appsec/contrib/rack/request_middleware.rb +196 -0
  83. data/lib/datadog/appsec/contrib/rails/ext.rb +13 -0
  84. data/lib/datadog/appsec/contrib/rails/framework.rb +16 -0
  85. data/lib/datadog/appsec/contrib/rails/gateway/request.rb +67 -0
  86. data/lib/datadog/appsec/contrib/rails/gateway/watcher.rb +71 -0
  87. data/lib/datadog/appsec/contrib/rails/integration.rb +43 -0
  88. data/lib/datadog/appsec/contrib/rails/patcher.rb +166 -0
  89. data/lib/datadog/appsec/contrib/rails/reactive/action.rb +66 -0
  90. data/lib/datadog/appsec/contrib/rails/request.rb +36 -0
  91. data/lib/datadog/appsec/contrib/rails/request_middleware.rb +20 -0
  92. data/lib/datadog/appsec/contrib/sinatra/ext.rb +14 -0
  93. data/lib/datadog/appsec/contrib/sinatra/framework.rb +20 -0
  94. data/lib/datadog/appsec/contrib/sinatra/gateway/request.rb +17 -0
  95. data/lib/datadog/appsec/contrib/sinatra/gateway/route_params.rb +23 -0
  96. data/lib/datadog/appsec/contrib/sinatra/gateway/watcher.rb +117 -0
  97. data/lib/datadog/appsec/contrib/sinatra/integration.rb +43 -0
  98. data/lib/datadog/appsec/contrib/sinatra/patcher.rb +168 -0
  99. data/lib/datadog/appsec/contrib/sinatra/reactive/routed.rb +61 -0
  100. data/lib/datadog/appsec/contrib/sinatra/request_middleware.rb +20 -0
  101. data/lib/datadog/appsec/event.rb +171 -0
  102. data/lib/datadog/appsec/ext.rb +10 -0
  103. data/lib/datadog/appsec/extensions.rb +15 -0
  104. data/lib/datadog/appsec/instrumentation/gateway/argument.rb +22 -0
  105. data/lib/datadog/appsec/instrumentation/gateway.rb +64 -0
  106. data/lib/datadog/appsec/instrumentation.rb +9 -0
  107. data/lib/datadog/appsec/monitor/gateway/watcher.rb +67 -0
  108. data/lib/datadog/appsec/monitor/reactive/set_user.rb +58 -0
  109. data/lib/datadog/appsec/monitor.rb +11 -0
  110. data/lib/datadog/appsec/processor/actions.rb +49 -0
  111. data/lib/datadog/appsec/processor/rule_loader.rb +123 -0
  112. data/lib/datadog/appsec/processor/rule_merger.rb +152 -0
  113. data/lib/datadog/appsec/processor.rb +171 -0
  114. data/lib/datadog/appsec/rate_limiter.rb +60 -0
  115. data/lib/datadog/appsec/reactive/address_hash.rb +22 -0
  116. data/lib/datadog/appsec/reactive/engine.rb +47 -0
  117. data/lib/datadog/appsec/reactive/operation.rb +68 -0
  118. data/lib/datadog/appsec/reactive/subscriber.rb +19 -0
  119. data/lib/datadog/appsec/remote.rb +129 -0
  120. data/lib/datadog/appsec/response.rb +151 -0
  121. data/lib/datadog/appsec/sample_rate.rb +21 -0
  122. data/lib/datadog/appsec/scope.rb +61 -0
  123. data/lib/datadog/appsec/utils/http/media_range.rb +201 -0
  124. data/lib/datadog/appsec/utils/http/media_type.rb +87 -0
  125. data/lib/datadog/appsec/utils/http.rb +11 -0
  126. data/lib/datadog/appsec/utils.rb +9 -0
  127. data/lib/datadog/appsec.rb +60 -0
  128. data/lib/datadog/auto_instrument.rb +16 -0
  129. data/lib/datadog/auto_instrument_base.rb +8 -0
  130. data/lib/datadog/core/buffer/cruby.rb +55 -0
  131. data/lib/datadog/core/buffer/random.rb +134 -0
  132. data/lib/datadog/core/buffer/thread_safe.rb +58 -0
  133. data/lib/datadog/core/chunker.rb +35 -0
  134. data/lib/datadog/core/configuration/agent_settings_resolver.rb +352 -0
  135. data/lib/datadog/core/configuration/base.rb +91 -0
  136. data/lib/datadog/core/configuration/components.rb +177 -0
  137. data/lib/datadog/core/configuration/ext.rb +45 -0
  138. data/lib/datadog/core/configuration/option.rb +319 -0
  139. data/lib/datadog/core/configuration/option_definition.rb +165 -0
  140. data/lib/datadog/core/configuration/options.rb +128 -0
  141. data/lib/datadog/core/configuration/settings.rb +786 -0
  142. data/lib/datadog/core/configuration.rb +296 -0
  143. data/lib/datadog/core/diagnostics/environment_logger.rb +173 -0
  144. data/lib/datadog/core/diagnostics/health.rb +19 -0
  145. data/lib/datadog/core/encoding.rb +74 -0
  146. data/lib/datadog/core/environment/cgroup.rb +53 -0
  147. data/lib/datadog/core/environment/class_count.rb +21 -0
  148. data/lib/datadog/core/environment/container.rb +91 -0
  149. data/lib/datadog/core/environment/execution.rb +103 -0
  150. data/lib/datadog/core/environment/ext.rb +45 -0
  151. data/lib/datadog/core/environment/gc.rb +20 -0
  152. data/lib/datadog/core/environment/git.rb +25 -0
  153. data/lib/datadog/core/environment/identity.rb +84 -0
  154. data/lib/datadog/core/environment/platform.rb +40 -0
  155. data/lib/datadog/core/environment/socket.rb +24 -0
  156. data/lib/datadog/core/environment/thread_count.rb +20 -0
  157. data/lib/datadog/core/environment/variable_helpers.rb +53 -0
  158. data/lib/datadog/core/environment/vm_cache.rb +64 -0
  159. data/lib/datadog/core/environment/yjit.rb +58 -0
  160. data/lib/datadog/core/error.rb +100 -0
  161. data/lib/datadog/core/extensions.rb +16 -0
  162. data/lib/datadog/core/git/ext.rb +16 -0
  163. data/lib/datadog/core/header_collection.rb +43 -0
  164. data/lib/datadog/core/logger.rb +45 -0
  165. data/lib/datadog/core/logging/ext.rb +13 -0
  166. data/lib/datadog/core/metrics/client.rb +199 -0
  167. data/lib/datadog/core/metrics/ext.rb +18 -0
  168. data/lib/datadog/core/metrics/helpers.rb +25 -0
  169. data/lib/datadog/core/metrics/logging.rb +44 -0
  170. data/lib/datadog/core/metrics/metric.rb +14 -0
  171. data/lib/datadog/core/metrics/options.rb +52 -0
  172. data/lib/datadog/core/pin.rb +75 -0
  173. data/lib/datadog/core/remote/client/capabilities.rb +62 -0
  174. data/lib/datadog/core/remote/client.rb +234 -0
  175. data/lib/datadog/core/remote/component.rb +162 -0
  176. data/lib/datadog/core/remote/configuration/content.rb +111 -0
  177. data/lib/datadog/core/remote/configuration/digest.rb +62 -0
  178. data/lib/datadog/core/remote/configuration/path.rb +90 -0
  179. data/lib/datadog/core/remote/configuration/repository.rb +294 -0
  180. data/lib/datadog/core/remote/configuration/target.rb +74 -0
  181. data/lib/datadog/core/remote/configuration.rb +18 -0
  182. data/lib/datadog/core/remote/dispatcher.rb +59 -0
  183. data/lib/datadog/core/remote/ext.rb +13 -0
  184. data/lib/datadog/core/remote/negotiation.rb +70 -0
  185. data/lib/datadog/core/remote/tie/tracing.rb +39 -0
  186. data/lib/datadog/core/remote/tie.rb +27 -0
  187. data/lib/datadog/core/remote/transport/config.rb +60 -0
  188. data/lib/datadog/core/remote/transport/http/api/instance.rb +39 -0
  189. data/lib/datadog/core/remote/transport/http/api/spec.rb +21 -0
  190. data/lib/datadog/core/remote/transport/http/api.rb +58 -0
  191. data/lib/datadog/core/remote/transport/http/builder.rb +219 -0
  192. data/lib/datadog/core/remote/transport/http/client.rb +48 -0
  193. data/lib/datadog/core/remote/transport/http/config.rb +280 -0
  194. data/lib/datadog/core/remote/transport/http/negotiation.rb +146 -0
  195. data/lib/datadog/core/remote/transport/http.rb +147 -0
  196. data/lib/datadog/core/remote/transport/negotiation.rb +62 -0
  197. data/lib/datadog/core/remote/worker.rb +102 -0
  198. data/lib/datadog/core/remote.rb +24 -0
  199. data/lib/datadog/core/runtime/ext.rb +38 -0
  200. data/lib/datadog/core/runtime/metrics.rb +185 -0
  201. data/lib/datadog/core/telemetry/client.rb +87 -0
  202. data/lib/datadog/core/telemetry/collector.rb +248 -0
  203. data/lib/datadog/core/telemetry/emitter.rb +50 -0
  204. data/lib/datadog/core/telemetry/event.rb +83 -0
  205. data/lib/datadog/core/telemetry/ext.rb +15 -0
  206. data/lib/datadog/core/telemetry/heartbeat.rb +35 -0
  207. data/lib/datadog/core/telemetry/http/adapters/net.rb +113 -0
  208. data/lib/datadog/core/telemetry/http/env.rb +20 -0
  209. data/lib/datadog/core/telemetry/http/ext.rb +22 -0
  210. data/lib/datadog/core/telemetry/http/response.rb +66 -0
  211. data/lib/datadog/core/telemetry/http/transport.rb +56 -0
  212. data/lib/datadog/core/telemetry/v1/app_event.rb +59 -0
  213. data/lib/datadog/core/telemetry/v1/application.rb +94 -0
  214. data/lib/datadog/core/telemetry/v1/configuration.rb +27 -0
  215. data/lib/datadog/core/telemetry/v1/dependency.rb +45 -0
  216. data/lib/datadog/core/telemetry/v1/host.rb +59 -0
  217. data/lib/datadog/core/telemetry/v1/install_signature.rb +38 -0
  218. data/lib/datadog/core/telemetry/v1/integration.rb +66 -0
  219. data/lib/datadog/core/telemetry/v1/product.rb +36 -0
  220. data/lib/datadog/core/telemetry/v1/telemetry_request.rb +108 -0
  221. data/lib/datadog/core/telemetry/v2/app_client_configuration_change.rb +41 -0
  222. data/lib/datadog/core/telemetry/v2/request.rb +29 -0
  223. data/lib/datadog/core/transport/ext.rb +43 -0
  224. data/lib/datadog/core/transport/http/adapters/net.rb +159 -0
  225. data/lib/datadog/core/transport/http/adapters/registry.rb +29 -0
  226. data/lib/datadog/core/transport/http/adapters/test.rb +89 -0
  227. data/lib/datadog/core/transport/http/adapters/unix_socket.rb +83 -0
  228. data/lib/datadog/core/transport/http/api/endpoint.rb +31 -0
  229. data/lib/datadog/core/transport/http/api/fallbacks.rb +26 -0
  230. data/lib/datadog/core/transport/http/api/map.rb +18 -0
  231. data/lib/datadog/core/transport/http/env.rb +62 -0
  232. data/lib/datadog/core/transport/http/response.rb +60 -0
  233. data/lib/datadog/core/transport/parcel.rb +22 -0
  234. data/lib/datadog/core/transport/request.rb +17 -0
  235. data/lib/datadog/core/transport/response.rb +64 -0
  236. data/lib/datadog/core/utils/duration.rb +52 -0
  237. data/lib/datadog/core/utils/forking.rb +63 -0
  238. data/lib/datadog/core/utils/hash.rb +79 -0
  239. data/lib/datadog/core/utils/network.rb +121 -0
  240. data/lib/datadog/core/utils/only_once.rb +42 -0
  241. data/lib/datadog/core/utils/safe_dup.rb +40 -0
  242. data/lib/datadog/core/utils/sequence.rb +26 -0
  243. data/lib/datadog/core/utils/time.rb +52 -0
  244. data/lib/datadog/core/utils/url.rb +25 -0
  245. data/lib/datadog/core/utils.rb +94 -0
  246. data/lib/datadog/core/vendor/multipart-post/LICENSE +11 -0
  247. data/lib/datadog/core/vendor/multipart-post/multipart/post/composite_read_io.rb +118 -0
  248. data/lib/datadog/core/vendor/multipart-post/multipart/post/multipartable.rb +59 -0
  249. data/lib/datadog/core/vendor/multipart-post/multipart/post/parts.rb +137 -0
  250. data/lib/datadog/core/vendor/multipart-post/multipart/post/version.rb +11 -0
  251. data/lib/datadog/core/vendor/multipart-post/multipart/post.rb +10 -0
  252. data/lib/datadog/core/vendor/multipart-post/multipart.rb +14 -0
  253. data/lib/datadog/core/vendor/multipart-post/net/http/post/multipart.rb +34 -0
  254. data/lib/datadog/core/worker.rb +24 -0
  255. data/lib/datadog/core/workers/async.rb +185 -0
  256. data/lib/datadog/core/workers/interval_loop.rb +123 -0
  257. data/lib/datadog/core/workers/polling.rb +59 -0
  258. data/lib/datadog/core/workers/queue.rb +44 -0
  259. data/lib/datadog/core/workers/runtime_metrics.rb +62 -0
  260. data/lib/datadog/core.rb +45 -0
  261. data/lib/datadog/kit/appsec/events.rb +169 -0
  262. data/lib/datadog/kit/enable_core_dumps.rb +49 -0
  263. data/lib/datadog/kit/identity.rb +104 -0
  264. data/lib/datadog/kit.rb +11 -0
  265. data/lib/datadog/opentelemetry/api/context.rb +193 -0
  266. data/lib/datadog/opentelemetry/api/trace/span.rb +14 -0
  267. data/lib/datadog/opentelemetry/sdk/configurator.rb +37 -0
  268. data/lib/datadog/opentelemetry/sdk/id_generator.rb +26 -0
  269. data/lib/datadog/opentelemetry/sdk/propagator.rb +92 -0
  270. data/lib/datadog/opentelemetry/sdk/span_processor.rb +134 -0
  271. data/lib/datadog/opentelemetry/sdk/trace/span.rb +167 -0
  272. data/lib/datadog/opentelemetry/trace.rb +59 -0
  273. data/lib/datadog/opentelemetry.rb +51 -0
  274. data/lib/datadog/profiling/collectors/code_provenance.rb +113 -0
  275. data/lib/datadog/profiling/collectors/cpu_and_wall_time_worker.rb +114 -0
  276. data/lib/datadog/profiling/collectors/dynamic_sampling_rate.rb +14 -0
  277. data/lib/datadog/profiling/collectors/idle_sampling_helper.rb +70 -0
  278. data/lib/datadog/profiling/collectors/info.rb +103 -0
  279. data/lib/datadog/profiling/collectors/stack.rb +13 -0
  280. data/lib/datadog/profiling/collectors/thread_context.rb +61 -0
  281. data/lib/datadog/profiling/component.rb +418 -0
  282. data/lib/datadog/profiling/exporter.rb +103 -0
  283. data/lib/datadog/profiling/ext/forking.rb +98 -0
  284. data/lib/datadog/profiling/ext.rb +35 -0
  285. data/lib/datadog/profiling/flush.rb +43 -0
  286. data/lib/datadog/profiling/http_transport.rb +143 -0
  287. data/lib/datadog/profiling/load_native_extension.rb +28 -0
  288. data/lib/datadog/profiling/native_extension.rb +20 -0
  289. data/lib/datadog/profiling/preload.rb +5 -0
  290. data/lib/datadog/profiling/profiler.rb +64 -0
  291. data/lib/datadog/profiling/scheduler.rb +137 -0
  292. data/lib/datadog/profiling/stack_recorder.rb +69 -0
  293. data/lib/datadog/profiling/tag_builder.rb +60 -0
  294. data/lib/datadog/profiling/tasks/exec.rb +50 -0
  295. data/lib/datadog/profiling/tasks/help.rb +18 -0
  296. data/lib/datadog/profiling/tasks/setup.rb +60 -0
  297. data/lib/datadog/profiling.rb +152 -0
  298. data/lib/datadog/tracing/analytics.rb +25 -0
  299. data/lib/datadog/tracing/buffer.rb +129 -0
  300. data/lib/datadog/tracing/client_ip.rb +61 -0
  301. data/lib/datadog/tracing/component.rb +206 -0
  302. data/lib/datadog/tracing/configuration/dynamic/option.rb +71 -0
  303. data/lib/datadog/tracing/configuration/dynamic.rb +64 -0
  304. data/lib/datadog/tracing/configuration/ext.rb +98 -0
  305. data/lib/datadog/tracing/configuration/http.rb +74 -0
  306. data/lib/datadog/tracing/configuration/settings.rb +421 -0
  307. data/lib/datadog/tracing/context.rb +68 -0
  308. data/lib/datadog/tracing/context_provider.rb +82 -0
  309. data/lib/datadog/tracing/contrib/action_cable/configuration/settings.rb +39 -0
  310. data/lib/datadog/tracing/contrib/action_cable/event.rb +71 -0
  311. data/lib/datadog/tracing/contrib/action_cable/events/broadcast.rb +58 -0
  312. data/lib/datadog/tracing/contrib/action_cable/events/perform_action.rb +63 -0
  313. data/lib/datadog/tracing/contrib/action_cable/events/transmit.rb +59 -0
  314. data/lib/datadog/tracing/contrib/action_cable/events.rb +37 -0
  315. data/lib/datadog/tracing/contrib/action_cable/ext.rb +33 -0
  316. data/lib/datadog/tracing/contrib/action_cable/instrumentation.rb +90 -0
  317. data/lib/datadog/tracing/contrib/action_cable/integration.rb +50 -0
  318. data/lib/datadog/tracing/contrib/action_cable/patcher.rb +31 -0
  319. data/lib/datadog/tracing/contrib/action_mailer/configuration/settings.rb +43 -0
  320. data/lib/datadog/tracing/contrib/action_mailer/event.rb +52 -0
  321. data/lib/datadog/tracing/contrib/action_mailer/events/deliver.rb +60 -0
  322. data/lib/datadog/tracing/contrib/action_mailer/events/process.rb +47 -0
  323. data/lib/datadog/tracing/contrib/action_mailer/events.rb +34 -0
  324. data/lib/datadog/tracing/contrib/action_mailer/ext.rb +34 -0
  325. data/lib/datadog/tracing/contrib/action_mailer/integration.rb +50 -0
  326. data/lib/datadog/tracing/contrib/action_mailer/patcher.rb +29 -0
  327. data/lib/datadog/tracing/contrib/action_pack/action_controller/instrumentation.rb +138 -0
  328. data/lib/datadog/tracing/contrib/action_pack/action_controller/patcher.rb +29 -0
  329. data/lib/datadog/tracing/contrib/action_pack/configuration/settings.rb +40 -0
  330. data/lib/datadog/tracing/contrib/action_pack/ext.rb +23 -0
  331. data/lib/datadog/tracing/contrib/action_pack/integration.rb +51 -0
  332. data/lib/datadog/tracing/contrib/action_pack/patcher.rb +27 -0
  333. data/lib/datadog/tracing/contrib/action_pack/utils.rb +40 -0
  334. data/lib/datadog/tracing/contrib/action_view/configuration/settings.rb +43 -0
  335. data/lib/datadog/tracing/contrib/action_view/event.rb +35 -0
  336. data/lib/datadog/tracing/contrib/action_view/events/render_partial.rb +54 -0
  337. data/lib/datadog/tracing/contrib/action_view/events/render_template.rb +57 -0
  338. data/lib/datadog/tracing/contrib/action_view/events.rb +34 -0
  339. data/lib/datadog/tracing/contrib/action_view/ext.rb +25 -0
  340. data/lib/datadog/tracing/contrib/action_view/integration.rb +58 -0
  341. data/lib/datadog/tracing/contrib/action_view/patcher.rb +34 -0
  342. data/lib/datadog/tracing/contrib/action_view/utils.rb +36 -0
  343. data/lib/datadog/tracing/contrib/active_job/configuration/settings.rb +39 -0
  344. data/lib/datadog/tracing/contrib/active_job/event.rb +58 -0
  345. data/lib/datadog/tracing/contrib/active_job/events/discard.rb +50 -0
  346. data/lib/datadog/tracing/contrib/active_job/events/enqueue.rb +49 -0
  347. data/lib/datadog/tracing/contrib/active_job/events/enqueue_at.rb +49 -0
  348. data/lib/datadog/tracing/contrib/active_job/events/enqueue_retry.rb +51 -0
  349. data/lib/datadog/tracing/contrib/active_job/events/perform.rb +49 -0
  350. data/lib/datadog/tracing/contrib/active_job/events/retry_stopped.rb +50 -0
  351. data/lib/datadog/tracing/contrib/active_job/events.rb +42 -0
  352. data/lib/datadog/tracing/contrib/active_job/ext.rb +40 -0
  353. data/lib/datadog/tracing/contrib/active_job/integration.rb +50 -0
  354. data/lib/datadog/tracing/contrib/active_job/log_injection.rb +24 -0
  355. data/lib/datadog/tracing/contrib/active_job/patcher.rb +36 -0
  356. data/lib/datadog/tracing/contrib/active_model_serializers/configuration/settings.rb +37 -0
  357. data/lib/datadog/tracing/contrib/active_model_serializers/event.rb +68 -0
  358. data/lib/datadog/tracing/contrib/active_model_serializers/events/render.rb +45 -0
  359. data/lib/datadog/tracing/contrib/active_model_serializers/events/serialize.rb +47 -0
  360. data/lib/datadog/tracing/contrib/active_model_serializers/events.rb +34 -0
  361. data/lib/datadog/tracing/contrib/active_model_serializers/ext.rb +25 -0
  362. data/lib/datadog/tracing/contrib/active_model_serializers/integration.rb +45 -0
  363. data/lib/datadog/tracing/contrib/active_model_serializers/patcher.rb +32 -0
  364. data/lib/datadog/tracing/contrib/active_record/configuration/makara_resolver.rb +36 -0
  365. data/lib/datadog/tracing/contrib/active_record/configuration/resolver.rb +147 -0
  366. data/lib/datadog/tracing/contrib/active_record/configuration/settings.rb +48 -0
  367. data/lib/datadog/tracing/contrib/active_record/event.rb +30 -0
  368. data/lib/datadog/tracing/contrib/active_record/events/instantiation.rb +58 -0
  369. data/lib/datadog/tracing/contrib/active_record/events/sql.rb +77 -0
  370. data/lib/datadog/tracing/contrib/active_record/events.rb +34 -0
  371. data/lib/datadog/tracing/contrib/active_record/ext.rb +30 -0
  372. data/lib/datadog/tracing/contrib/active_record/integration.rb +57 -0
  373. data/lib/datadog/tracing/contrib/active_record/patcher.rb +27 -0
  374. data/lib/datadog/tracing/contrib/active_record/utils.rb +128 -0
  375. data/lib/datadog/tracing/contrib/active_support/cache/instrumentation.rb +186 -0
  376. data/lib/datadog/tracing/contrib/active_support/cache/patcher.rb +76 -0
  377. data/lib/datadog/tracing/contrib/active_support/cache/redis.rb +47 -0
  378. data/lib/datadog/tracing/contrib/active_support/configuration/settings.rb +47 -0
  379. data/lib/datadog/tracing/contrib/active_support/ext.rb +32 -0
  380. data/lib/datadog/tracing/contrib/active_support/integration.rb +52 -0
  381. data/lib/datadog/tracing/contrib/active_support/notifications/event.rb +71 -0
  382. data/lib/datadog/tracing/contrib/active_support/notifications/subscriber.rb +71 -0
  383. data/lib/datadog/tracing/contrib/active_support/notifications/subscription.rb +164 -0
  384. data/lib/datadog/tracing/contrib/active_support/patcher.rb +27 -0
  385. data/lib/datadog/tracing/contrib/analytics.rb +28 -0
  386. data/lib/datadog/tracing/contrib/auto_instrument.rb +53 -0
  387. data/lib/datadog/tracing/contrib/aws/configuration/settings.rb +53 -0
  388. data/lib/datadog/tracing/contrib/aws/ext.rb +50 -0
  389. data/lib/datadog/tracing/contrib/aws/instrumentation.rb +119 -0
  390. data/lib/datadog/tracing/contrib/aws/integration.rb +47 -0
  391. data/lib/datadog/tracing/contrib/aws/parsed_context.rb +64 -0
  392. data/lib/datadog/tracing/contrib/aws/patcher.rb +57 -0
  393. data/lib/datadog/tracing/contrib/aws/service/base.rb +16 -0
  394. data/lib/datadog/tracing/contrib/aws/service/dynamodb.rb +22 -0
  395. data/lib/datadog/tracing/contrib/aws/service/eventbridge.rb +22 -0
  396. data/lib/datadog/tracing/contrib/aws/service/kinesis.rb +32 -0
  397. data/lib/datadog/tracing/contrib/aws/service/s3.rb +22 -0
  398. data/lib/datadog/tracing/contrib/aws/service/sns.rb +30 -0
  399. data/lib/datadog/tracing/contrib/aws/service/sqs.rb +27 -0
  400. data/lib/datadog/tracing/contrib/aws/service/states.rb +40 -0
  401. data/lib/datadog/tracing/contrib/aws/services.rb +139 -0
  402. data/lib/datadog/tracing/contrib/component.rb +41 -0
  403. data/lib/datadog/tracing/contrib/concurrent_ruby/async_patch.rb +20 -0
  404. data/lib/datadog/tracing/contrib/concurrent_ruby/configuration/settings.rb +24 -0
  405. data/lib/datadog/tracing/contrib/concurrent_ruby/context_composite_executor_service.rb +53 -0
  406. data/lib/datadog/tracing/contrib/concurrent_ruby/ext.rb +16 -0
  407. data/lib/datadog/tracing/contrib/concurrent_ruby/future_patch.rb +20 -0
  408. data/lib/datadog/tracing/contrib/concurrent_ruby/integration.rb +44 -0
  409. data/lib/datadog/tracing/contrib/concurrent_ruby/patcher.rb +49 -0
  410. data/lib/datadog/tracing/contrib/concurrent_ruby/promises_future_patch.rb +22 -0
  411. data/lib/datadog/tracing/contrib/configurable.rb +102 -0
  412. data/lib/datadog/tracing/contrib/configuration/resolver.rb +85 -0
  413. data/lib/datadog/tracing/contrib/configuration/resolvers/pattern_resolver.rb +43 -0
  414. data/lib/datadog/tracing/contrib/configuration/settings.rb +43 -0
  415. data/lib/datadog/tracing/contrib/dalli/configuration/settings.rb +58 -0
  416. data/lib/datadog/tracing/contrib/dalli/ext.rb +40 -0
  417. data/lib/datadog/tracing/contrib/dalli/instrumentation.rb +75 -0
  418. data/lib/datadog/tracing/contrib/dalli/integration.rb +52 -0
  419. data/lib/datadog/tracing/contrib/dalli/patcher.rb +28 -0
  420. data/lib/datadog/tracing/contrib/dalli/quantize.rb +26 -0
  421. data/lib/datadog/tracing/contrib/delayed_job/configuration/settings.rb +49 -0
  422. data/lib/datadog/tracing/contrib/delayed_job/ext.rb +29 -0
  423. data/lib/datadog/tracing/contrib/delayed_job/integration.rb +43 -0
  424. data/lib/datadog/tracing/contrib/delayed_job/patcher.rb +37 -0
  425. data/lib/datadog/tracing/contrib/delayed_job/plugin.rb +108 -0
  426. data/lib/datadog/tracing/contrib/delayed_job/server_internal_tracer/worker.rb +34 -0
  427. data/lib/datadog/tracing/contrib/elasticsearch/configuration/settings.rb +57 -0
  428. data/lib/datadog/tracing/contrib/elasticsearch/ext.rb +34 -0
  429. data/lib/datadog/tracing/contrib/elasticsearch/integration.rb +50 -0
  430. data/lib/datadog/tracing/contrib/elasticsearch/patcher.rb +164 -0
  431. data/lib/datadog/tracing/contrib/elasticsearch/quantize.rb +87 -0
  432. data/lib/datadog/tracing/contrib/ethon/configuration/settings.rb +56 -0
  433. data/lib/datadog/tracing/contrib/ethon/easy_patch.rb +223 -0
  434. data/lib/datadog/tracing/contrib/ethon/ext.rb +32 -0
  435. data/lib/datadog/tracing/contrib/ethon/integration.rb +48 -0
  436. data/lib/datadog/tracing/contrib/ethon/multi_patch.rb +102 -0
  437. data/lib/datadog/tracing/contrib/ethon/patcher.rb +30 -0
  438. data/lib/datadog/tracing/contrib/excon/configuration/settings.rb +74 -0
  439. data/lib/datadog/tracing/contrib/excon/ext.rb +30 -0
  440. data/lib/datadog/tracing/contrib/excon/integration.rb +48 -0
  441. data/lib/datadog/tracing/contrib/excon/middleware.rb +196 -0
  442. data/lib/datadog/tracing/contrib/excon/patcher.rb +31 -0
  443. data/lib/datadog/tracing/contrib/ext.rb +55 -0
  444. data/lib/datadog/tracing/contrib/extensions.rb +228 -0
  445. data/lib/datadog/tracing/contrib/faraday/configuration/settings.rb +77 -0
  446. data/lib/datadog/tracing/contrib/faraday/connection.rb +22 -0
  447. data/lib/datadog/tracing/contrib/faraday/ext.rb +30 -0
  448. data/lib/datadog/tracing/contrib/faraday/integration.rb +48 -0
  449. data/lib/datadog/tracing/contrib/faraday/middleware.rb +112 -0
  450. data/lib/datadog/tracing/contrib/faraday/patcher.rb +56 -0
  451. data/lib/datadog/tracing/contrib/faraday/rack_builder.rb +22 -0
  452. data/lib/datadog/tracing/contrib/grape/configuration/settings.rb +55 -0
  453. data/lib/datadog/tracing/contrib/grape/endpoint.rb +256 -0
  454. data/lib/datadog/tracing/contrib/grape/ext.rb +30 -0
  455. data/lib/datadog/tracing/contrib/grape/instrumentation.rb +37 -0
  456. data/lib/datadog/tracing/contrib/grape/integration.rb +44 -0
  457. data/lib/datadog/tracing/contrib/grape/patcher.rb +33 -0
  458. data/lib/datadog/tracing/contrib/graphql/configuration/settings.rb +50 -0
  459. data/lib/datadog/tracing/contrib/graphql/ext.rb +20 -0
  460. data/lib/datadog/tracing/contrib/graphql/integration.rb +56 -0
  461. data/lib/datadog/tracing/contrib/graphql/patcher.rb +55 -0
  462. data/lib/datadog/tracing/contrib/graphql/trace_patcher.rb +24 -0
  463. data/lib/datadog/tracing/contrib/graphql/tracing_patcher.rb +28 -0
  464. data/lib/datadog/tracing/contrib/grpc/configuration/settings.rb +58 -0
  465. data/lib/datadog/tracing/contrib/grpc/datadog_interceptor/client.rb +117 -0
  466. data/lib/datadog/tracing/contrib/grpc/datadog_interceptor/server.rb +96 -0
  467. data/lib/datadog/tracing/contrib/grpc/datadog_interceptor.rb +107 -0
  468. data/lib/datadog/tracing/contrib/grpc/distributed/fetcher.rb +26 -0
  469. data/lib/datadog/tracing/contrib/grpc/distributed/propagation.rb +46 -0
  470. data/lib/datadog/tracing/contrib/grpc/ext.rb +29 -0
  471. data/lib/datadog/tracing/contrib/grpc/formatting.rb +127 -0
  472. data/lib/datadog/tracing/contrib/grpc/integration.rb +50 -0
  473. data/lib/datadog/tracing/contrib/grpc/intercept_with_datadog.rb +53 -0
  474. data/lib/datadog/tracing/contrib/grpc/patcher.rb +34 -0
  475. data/lib/datadog/tracing/contrib/grpc.rb +45 -0
  476. data/lib/datadog/tracing/contrib/hanami/action_tracer.rb +47 -0
  477. data/lib/datadog/tracing/contrib/hanami/configuration/settings.rb +23 -0
  478. data/lib/datadog/tracing/contrib/hanami/ext.rb +24 -0
  479. data/lib/datadog/tracing/contrib/hanami/integration.rb +44 -0
  480. data/lib/datadog/tracing/contrib/hanami/patcher.rb +33 -0
  481. data/lib/datadog/tracing/contrib/hanami/plugin.rb +23 -0
  482. data/lib/datadog/tracing/contrib/hanami/renderer_policy_tracing.rb +41 -0
  483. data/lib/datadog/tracing/contrib/hanami/router_tracing.rb +44 -0
  484. data/lib/datadog/tracing/contrib/http/circuit_breaker.rb +40 -0
  485. data/lib/datadog/tracing/contrib/http/configuration/settings.rb +69 -0
  486. data/lib/datadog/tracing/contrib/http/distributed/fetcher.rb +38 -0
  487. data/lib/datadog/tracing/contrib/http/distributed/propagation.rb +45 -0
  488. data/lib/datadog/tracing/contrib/http/ext.rb +29 -0
  489. data/lib/datadog/tracing/contrib/http/instrumentation.rb +144 -0
  490. data/lib/datadog/tracing/contrib/http/integration.rb +49 -0
  491. data/lib/datadog/tracing/contrib/http/patcher.rb +30 -0
  492. data/lib/datadog/tracing/contrib/http.rb +45 -0
  493. data/lib/datadog/tracing/contrib/http_annotation_helper.rb +17 -0
  494. data/lib/datadog/tracing/contrib/httpclient/configuration/settings.rb +68 -0
  495. data/lib/datadog/tracing/contrib/httpclient/ext.rb +30 -0
  496. data/lib/datadog/tracing/contrib/httpclient/instrumentation.rb +137 -0
  497. data/lib/datadog/tracing/contrib/httpclient/integration.rb +48 -0
  498. data/lib/datadog/tracing/contrib/httpclient/patcher.rb +42 -0
  499. data/lib/datadog/tracing/contrib/httprb/configuration/settings.rb +68 -0
  500. data/lib/datadog/tracing/contrib/httprb/ext.rb +29 -0
  501. data/lib/datadog/tracing/contrib/httprb/instrumentation.rb +145 -0
  502. data/lib/datadog/tracing/contrib/httprb/integration.rb +48 -0
  503. data/lib/datadog/tracing/contrib/httprb/patcher.rb +42 -0
  504. data/lib/datadog/tracing/contrib/integration.rb +78 -0
  505. data/lib/datadog/tracing/contrib/kafka/configuration/settings.rb +39 -0
  506. data/lib/datadog/tracing/contrib/kafka/consumer_event.rb +19 -0
  507. data/lib/datadog/tracing/contrib/kafka/consumer_group_event.rb +18 -0
  508. data/lib/datadog/tracing/contrib/kafka/event.rb +53 -0
  509. data/lib/datadog/tracing/contrib/kafka/events/connection/request.rb +42 -0
  510. data/lib/datadog/tracing/contrib/kafka/events/consumer/process_batch.rb +49 -0
  511. data/lib/datadog/tracing/contrib/kafka/events/consumer/process_message.rb +47 -0
  512. data/lib/datadog/tracing/contrib/kafka/events/consumer_group/heartbeat.rb +47 -0
  513. data/lib/datadog/tracing/contrib/kafka/events/consumer_group/join_group.rb +37 -0
  514. data/lib/datadog/tracing/contrib/kafka/events/consumer_group/leave_group.rb +37 -0
  515. data/lib/datadog/tracing/contrib/kafka/events/consumer_group/sync_group.rb +37 -0
  516. data/lib/datadog/tracing/contrib/kafka/events/produce_operation/send_messages.rb +41 -0
  517. data/lib/datadog/tracing/contrib/kafka/events/producer/deliver_messages.rb +44 -0
  518. data/lib/datadog/tracing/contrib/kafka/events.rb +48 -0
  519. data/lib/datadog/tracing/contrib/kafka/ext.rb +55 -0
  520. data/lib/datadog/tracing/contrib/kafka/integration.rb +44 -0
  521. data/lib/datadog/tracing/contrib/kafka/patcher.rb +29 -0
  522. data/lib/datadog/tracing/contrib/lograge/configuration/settings.rb +24 -0
  523. data/lib/datadog/tracing/contrib/lograge/ext.rb +15 -0
  524. data/lib/datadog/tracing/contrib/lograge/instrumentation.rb +31 -0
  525. data/lib/datadog/tracing/contrib/lograge/integration.rb +50 -0
  526. data/lib/datadog/tracing/contrib/lograge/patcher.rb +29 -0
  527. data/lib/datadog/tracing/contrib/mongodb/configuration/settings.rb +56 -0
  528. data/lib/datadog/tracing/contrib/mongodb/ext.rb +38 -0
  529. data/lib/datadog/tracing/contrib/mongodb/instrumentation.rb +47 -0
  530. data/lib/datadog/tracing/contrib/mongodb/integration.rb +48 -0
  531. data/lib/datadog/tracing/contrib/mongodb/parsers.rb +49 -0
  532. data/lib/datadog/tracing/contrib/mongodb/patcher.rb +34 -0
  533. data/lib/datadog/tracing/contrib/mongodb/subscribers.rb +141 -0
  534. data/lib/datadog/tracing/contrib/mysql2/configuration/settings.rb +64 -0
  535. data/lib/datadog/tracing/contrib/mysql2/ext.rb +28 -0
  536. data/lib/datadog/tracing/contrib/mysql2/instrumentation.rb +95 -0
  537. data/lib/datadog/tracing/contrib/mysql2/integration.rb +43 -0
  538. data/lib/datadog/tracing/contrib/mysql2/patcher.rb +31 -0
  539. data/lib/datadog/tracing/contrib/opensearch/configuration/settings.rb +54 -0
  540. data/lib/datadog/tracing/contrib/opensearch/ext.rb +38 -0
  541. data/lib/datadog/tracing/contrib/opensearch/integration.rb +44 -0
  542. data/lib/datadog/tracing/contrib/opensearch/patcher.rb +135 -0
  543. data/lib/datadog/tracing/contrib/opensearch/quantize.rb +81 -0
  544. data/lib/datadog/tracing/contrib/patchable.rb +109 -0
  545. data/lib/datadog/tracing/contrib/patcher.rb +85 -0
  546. data/lib/datadog/tracing/contrib/pg/configuration/settings.rb +64 -0
  547. data/lib/datadog/tracing/contrib/pg/ext.rb +35 -0
  548. data/lib/datadog/tracing/contrib/pg/instrumentation.rb +211 -0
  549. data/lib/datadog/tracing/contrib/pg/integration.rb +43 -0
  550. data/lib/datadog/tracing/contrib/pg/patcher.rb +31 -0
  551. data/lib/datadog/tracing/contrib/presto/configuration/settings.rb +52 -0
  552. data/lib/datadog/tracing/contrib/presto/ext.rb +38 -0
  553. data/lib/datadog/tracing/contrib/presto/instrumentation.rb +138 -0
  554. data/lib/datadog/tracing/contrib/presto/integration.rb +43 -0
  555. data/lib/datadog/tracing/contrib/presto/patcher.rb +37 -0
  556. data/lib/datadog/tracing/contrib/propagation/sql_comment/comment.rb +41 -0
  557. data/lib/datadog/tracing/contrib/propagation/sql_comment/ext.rb +33 -0
  558. data/lib/datadog/tracing/contrib/propagation/sql_comment/mode.rb +28 -0
  559. data/lib/datadog/tracing/contrib/propagation/sql_comment.rb +55 -0
  560. data/lib/datadog/tracing/contrib/que/configuration/settings.rb +55 -0
  561. data/lib/datadog/tracing/contrib/que/ext.rb +33 -0
  562. data/lib/datadog/tracing/contrib/que/integration.rb +44 -0
  563. data/lib/datadog/tracing/contrib/que/patcher.rb +26 -0
  564. data/lib/datadog/tracing/contrib/que/tracer.rb +63 -0
  565. data/lib/datadog/tracing/contrib/racecar/configuration/settings.rb +47 -0
  566. data/lib/datadog/tracing/contrib/racecar/event.rb +81 -0
  567. data/lib/datadog/tracing/contrib/racecar/events/batch.rb +38 -0
  568. data/lib/datadog/tracing/contrib/racecar/events/consume.rb +35 -0
  569. data/lib/datadog/tracing/contrib/racecar/events/message.rb +38 -0
  570. data/lib/datadog/tracing/contrib/racecar/events.rb +36 -0
  571. data/lib/datadog/tracing/contrib/racecar/ext.rb +33 -0
  572. data/lib/datadog/tracing/contrib/racecar/integration.rb +44 -0
  573. data/lib/datadog/tracing/contrib/racecar/patcher.rb +29 -0
  574. data/lib/datadog/tracing/contrib/rack/configuration/settings.rb +59 -0
  575. data/lib/datadog/tracing/contrib/rack/ext.rb +30 -0
  576. data/lib/datadog/tracing/contrib/rack/header_collection.rb +40 -0
  577. data/lib/datadog/tracing/contrib/rack/header_tagging.rb +63 -0
  578. data/lib/datadog/tracing/contrib/rack/integration.rb +50 -0
  579. data/lib/datadog/tracing/contrib/rack/middlewares.rb +265 -0
  580. data/lib/datadog/tracing/contrib/rack/patcher.rb +119 -0
  581. data/lib/datadog/tracing/contrib/rack/request_queue.rb +48 -0
  582. data/lib/datadog/tracing/contrib/rack/trace_proxy_middleware.rb +52 -0
  583. data/lib/datadog/tracing/contrib/rails/auto_instrument_railtie.rb +10 -0
  584. data/lib/datadog/tracing/contrib/rails/configuration/settings.rb +76 -0
  585. data/lib/datadog/tracing/contrib/rails/ext.rb +23 -0
  586. data/lib/datadog/tracing/contrib/rails/framework.rb +148 -0
  587. data/lib/datadog/tracing/contrib/rails/integration.rb +52 -0
  588. data/lib/datadog/tracing/contrib/rails/log_injection.rb +29 -0
  589. data/lib/datadog/tracing/contrib/rails/middlewares.rb +46 -0
  590. data/lib/datadog/tracing/contrib/rails/patcher.rb +88 -0
  591. data/lib/datadog/tracing/contrib/rails/railtie.rb +19 -0
  592. data/lib/datadog/tracing/contrib/rails/utils.rb +26 -0
  593. data/lib/datadog/tracing/contrib/rake/configuration/settings.rb +55 -0
  594. data/lib/datadog/tracing/contrib/rake/ext.rb +27 -0
  595. data/lib/datadog/tracing/contrib/rake/instrumentation.rb +103 -0
  596. data/lib/datadog/tracing/contrib/rake/integration.rb +43 -0
  597. data/lib/datadog/tracing/contrib/rake/patcher.rb +33 -0
  598. data/lib/datadog/tracing/contrib/redis/configuration/resolver.rb +49 -0
  599. data/lib/datadog/tracing/contrib/redis/configuration/settings.rb +57 -0
  600. data/lib/datadog/tracing/contrib/redis/ext.rb +35 -0
  601. data/lib/datadog/tracing/contrib/redis/instrumentation.rb +53 -0
  602. data/lib/datadog/tracing/contrib/redis/integration.rb +80 -0
  603. data/lib/datadog/tracing/contrib/redis/patcher.rb +92 -0
  604. data/lib/datadog/tracing/contrib/redis/quantize.rb +80 -0
  605. data/lib/datadog/tracing/contrib/redis/tags.rb +68 -0
  606. data/lib/datadog/tracing/contrib/redis/trace_middleware.rb +85 -0
  607. data/lib/datadog/tracing/contrib/redis/vendor/LICENSE +20 -0
  608. data/lib/datadog/tracing/contrib/redis/vendor/resolver.rb +160 -0
  609. data/lib/datadog/tracing/contrib/registerable.rb +50 -0
  610. data/lib/datadog/tracing/contrib/registry.rb +52 -0
  611. data/lib/datadog/tracing/contrib/resque/configuration/settings.rb +42 -0
  612. data/lib/datadog/tracing/contrib/resque/ext.rb +22 -0
  613. data/lib/datadog/tracing/contrib/resque/integration.rb +48 -0
  614. data/lib/datadog/tracing/contrib/resque/patcher.rb +29 -0
  615. data/lib/datadog/tracing/contrib/resque/resque_job.rb +106 -0
  616. data/lib/datadog/tracing/contrib/rest_client/configuration/settings.rb +55 -0
  617. data/lib/datadog/tracing/contrib/rest_client/ext.rb +28 -0
  618. data/lib/datadog/tracing/contrib/rest_client/integration.rb +43 -0
  619. data/lib/datadog/tracing/contrib/rest_client/patcher.rb +28 -0
  620. data/lib/datadog/tracing/contrib/rest_client/request_patch.rb +129 -0
  621. data/lib/datadog/tracing/contrib/roda/configuration/settings.rb +38 -0
  622. data/lib/datadog/tracing/contrib/roda/ext.rb +19 -0
  623. data/lib/datadog/tracing/contrib/roda/instrumentation.rb +76 -0
  624. data/lib/datadog/tracing/contrib/roda/integration.rb +45 -0
  625. data/lib/datadog/tracing/contrib/roda/patcher.rb +30 -0
  626. data/lib/datadog/tracing/contrib/semantic_logger/configuration/settings.rb +24 -0
  627. data/lib/datadog/tracing/contrib/semantic_logger/ext.rb +15 -0
  628. data/lib/datadog/tracing/contrib/semantic_logger/instrumentation.rb +35 -0
  629. data/lib/datadog/tracing/contrib/semantic_logger/integration.rb +52 -0
  630. data/lib/datadog/tracing/contrib/semantic_logger/patcher.rb +29 -0
  631. data/lib/datadog/tracing/contrib/sequel/configuration/settings.rb +37 -0
  632. data/lib/datadog/tracing/contrib/sequel/database.rb +62 -0
  633. data/lib/datadog/tracing/contrib/sequel/dataset.rb +67 -0
  634. data/lib/datadog/tracing/contrib/sequel/ext.rb +23 -0
  635. data/lib/datadog/tracing/contrib/sequel/integration.rb +43 -0
  636. data/lib/datadog/tracing/contrib/sequel/patcher.rb +37 -0
  637. data/lib/datadog/tracing/contrib/sequel/utils.rb +90 -0
  638. data/lib/datadog/tracing/contrib/shoryuken/configuration/settings.rb +43 -0
  639. data/lib/datadog/tracing/contrib/shoryuken/ext.rb +27 -0
  640. data/lib/datadog/tracing/contrib/shoryuken/integration.rb +44 -0
  641. data/lib/datadog/tracing/contrib/shoryuken/patcher.rb +28 -0
  642. data/lib/datadog/tracing/contrib/shoryuken/tracer.rb +65 -0
  643. data/lib/datadog/tracing/contrib/sidekiq/client_tracer.rb +62 -0
  644. data/lib/datadog/tracing/contrib/sidekiq/configuration/settings.rb +47 -0
  645. data/lib/datadog/tracing/contrib/sidekiq/distributed/propagation.rb +46 -0
  646. data/lib/datadog/tracing/contrib/sidekiq/ext.rb +44 -0
  647. data/lib/datadog/tracing/contrib/sidekiq/integration.rb +61 -0
  648. data/lib/datadog/tracing/contrib/sidekiq/patcher.rb +90 -0
  649. data/lib/datadog/tracing/contrib/sidekiq/server_internal_tracer/heartbeat.rb +61 -0
  650. data/lib/datadog/tracing/contrib/sidekiq/server_internal_tracer/job_fetch.rb +36 -0
  651. data/lib/datadog/tracing/contrib/sidekiq/server_internal_tracer/redis_info.rb +34 -0
  652. data/lib/datadog/tracing/contrib/sidekiq/server_internal_tracer/scheduled_poller.rb +57 -0
  653. data/lib/datadog/tracing/contrib/sidekiq/server_internal_tracer/stop.rb +34 -0
  654. data/lib/datadog/tracing/contrib/sidekiq/server_tracer.rb +88 -0
  655. data/lib/datadog/tracing/contrib/sidekiq/utils.rb +44 -0
  656. data/lib/datadog/tracing/contrib/sidekiq.rb +37 -0
  657. data/lib/datadog/tracing/contrib/sinatra/configuration/settings.rb +46 -0
  658. data/lib/datadog/tracing/contrib/sinatra/env.rb +38 -0
  659. data/lib/datadog/tracing/contrib/sinatra/ext.rb +31 -0
  660. data/lib/datadog/tracing/contrib/sinatra/framework.rb +116 -0
  661. data/lib/datadog/tracing/contrib/sinatra/integration.rb +43 -0
  662. data/lib/datadog/tracing/contrib/sinatra/patcher.rb +75 -0
  663. data/lib/datadog/tracing/contrib/sinatra/tracer.rb +86 -0
  664. data/lib/datadog/tracing/contrib/sinatra/tracer_middleware.rb +109 -0
  665. data/lib/datadog/tracing/contrib/sneakers/configuration/settings.rb +43 -0
  666. data/lib/datadog/tracing/contrib/sneakers/ext.rb +27 -0
  667. data/lib/datadog/tracing/contrib/sneakers/integration.rb +44 -0
  668. data/lib/datadog/tracing/contrib/sneakers/patcher.rb +27 -0
  669. data/lib/datadog/tracing/contrib/sneakers/tracer.rb +60 -0
  670. data/lib/datadog/tracing/contrib/span_attribute_schema.rb +92 -0
  671. data/lib/datadog/tracing/contrib/status_range_env_parser.rb +33 -0
  672. data/lib/datadog/tracing/contrib/status_range_matcher.rb +25 -0
  673. data/lib/datadog/tracing/contrib/stripe/configuration/settings.rb +37 -0
  674. data/lib/datadog/tracing/contrib/stripe/ext.rb +27 -0
  675. data/lib/datadog/tracing/contrib/stripe/integration.rb +43 -0
  676. data/lib/datadog/tracing/contrib/stripe/patcher.rb +28 -0
  677. data/lib/datadog/tracing/contrib/stripe/request.rb +67 -0
  678. data/lib/datadog/tracing/contrib/sucker_punch/configuration/settings.rb +39 -0
  679. data/lib/datadog/tracing/contrib/sucker_punch/exception_handler.rb +28 -0
  680. data/lib/datadog/tracing/contrib/sucker_punch/ext.rb +28 -0
  681. data/lib/datadog/tracing/contrib/sucker_punch/instrumentation.rb +104 -0
  682. data/lib/datadog/tracing/contrib/sucker_punch/integration.rb +43 -0
  683. data/lib/datadog/tracing/contrib/sucker_punch/patcher.rb +35 -0
  684. data/lib/datadog/tracing/contrib/trilogy/configuration/settings.rb +58 -0
  685. data/lib/datadog/tracing/contrib/trilogy/ext.rb +27 -0
  686. data/lib/datadog/tracing/contrib/trilogy/instrumentation.rb +94 -0
  687. data/lib/datadog/tracing/contrib/trilogy/integration.rb +43 -0
  688. data/lib/datadog/tracing/contrib/trilogy/patcher.rb +31 -0
  689. data/lib/datadog/tracing/contrib/utils/database.rb +31 -0
  690. data/lib/datadog/tracing/contrib/utils/quantization/hash.rb +111 -0
  691. data/lib/datadog/tracing/contrib/utils/quantization/http.rb +179 -0
  692. data/lib/datadog/tracing/contrib.rb +81 -0
  693. data/lib/datadog/tracing/correlation.rb +103 -0
  694. data/lib/datadog/tracing/diagnostics/environment_logger.rb +159 -0
  695. data/lib/datadog/tracing/diagnostics/ext.rb +36 -0
  696. data/lib/datadog/tracing/diagnostics/health.rb +40 -0
  697. data/lib/datadog/tracing/distributed/b3_multi.rb +73 -0
  698. data/lib/datadog/tracing/distributed/b3_single.rb +69 -0
  699. data/lib/datadog/tracing/distributed/datadog.rb +200 -0
  700. data/lib/datadog/tracing/distributed/datadog_tags_codec.rb +84 -0
  701. data/lib/datadog/tracing/distributed/fetcher.rb +21 -0
  702. data/lib/datadog/tracing/distributed/helpers.rb +65 -0
  703. data/lib/datadog/tracing/distributed/none.rb +18 -0
  704. data/lib/datadog/tracing/distributed/propagation.rb +121 -0
  705. data/lib/datadog/tracing/distributed/trace_context.rb +436 -0
  706. data/lib/datadog/tracing/event.rb +76 -0
  707. data/lib/datadog/tracing/flush.rb +96 -0
  708. data/lib/datadog/tracing/metadata/analytics.rb +26 -0
  709. data/lib/datadog/tracing/metadata/errors.rb +24 -0
  710. data/lib/datadog/tracing/metadata/ext.rb +193 -0
  711. data/lib/datadog/tracing/metadata/tagging.rb +131 -0
  712. data/lib/datadog/tracing/metadata.rb +20 -0
  713. data/lib/datadog/tracing/pipeline/span_filter.rb +46 -0
  714. data/lib/datadog/tracing/pipeline/span_processor.rb +39 -0
  715. data/lib/datadog/tracing/pipeline.rb +63 -0
  716. data/lib/datadog/tracing/remote.rb +78 -0
  717. data/lib/datadog/tracing/runtime/metrics.rb +17 -0
  718. data/lib/datadog/tracing/sampling/all_sampler.rb +24 -0
  719. data/lib/datadog/tracing/sampling/ext.rb +56 -0
  720. data/lib/datadog/tracing/sampling/matcher.rb +65 -0
  721. data/lib/datadog/tracing/sampling/priority_sampler.rb +160 -0
  722. data/lib/datadog/tracing/sampling/rate_by_key_sampler.rb +87 -0
  723. data/lib/datadog/tracing/sampling/rate_by_service_sampler.rb +63 -0
  724. data/lib/datadog/tracing/sampling/rate_limiter.rb +185 -0
  725. data/lib/datadog/tracing/sampling/rate_sampler.rb +58 -0
  726. data/lib/datadog/tracing/sampling/rule.rb +61 -0
  727. data/lib/datadog/tracing/sampling/rule_sampler.rb +148 -0
  728. data/lib/datadog/tracing/sampling/sampler.rb +32 -0
  729. data/lib/datadog/tracing/sampling/span/ext.rb +25 -0
  730. data/lib/datadog/tracing/sampling/span/matcher.rb +89 -0
  731. data/lib/datadog/tracing/sampling/span/rule.rb +78 -0
  732. data/lib/datadog/tracing/sampling/span/rule_parser.rb +104 -0
  733. data/lib/datadog/tracing/sampling/span/sampler.rb +77 -0
  734. data/lib/datadog/tracing/span.rb +207 -0
  735. data/lib/datadog/tracing/span_operation.rb +498 -0
  736. data/lib/datadog/tracing/sync_writer.rb +67 -0
  737. data/lib/datadog/tracing/trace_digest.rb +185 -0
  738. data/lib/datadog/tracing/trace_operation.rb +492 -0
  739. data/lib/datadog/tracing/trace_segment.rb +222 -0
  740. data/lib/datadog/tracing/tracer.rb +531 -0
  741. data/lib/datadog/tracing/transport/http/api/instance.rb +37 -0
  742. data/lib/datadog/tracing/transport/http/api/spec.rb +19 -0
  743. data/lib/datadog/tracing/transport/http/api.rb +43 -0
  744. data/lib/datadog/tracing/transport/http/builder.rb +162 -0
  745. data/lib/datadog/tracing/transport/http/client.rb +57 -0
  746. data/lib/datadog/tracing/transport/http/statistics.rb +47 -0
  747. data/lib/datadog/tracing/transport/http/traces.rb +152 -0
  748. data/lib/datadog/tracing/transport/http.rb +97 -0
  749. data/lib/datadog/tracing/transport/io/client.rb +89 -0
  750. data/lib/datadog/tracing/transport/io/response.rb +27 -0
  751. data/lib/datadog/tracing/transport/io/traces.rb +101 -0
  752. data/lib/datadog/tracing/transport/io.rb +30 -0
  753. data/lib/datadog/tracing/transport/serializable_trace.rb +126 -0
  754. data/lib/datadog/tracing/transport/statistics.rb +77 -0
  755. data/lib/datadog/tracing/transport/trace_formatter.rb +240 -0
  756. data/lib/datadog/tracing/transport/traces.rb +224 -0
  757. data/lib/datadog/tracing/utils.rb +83 -0
  758. data/lib/datadog/tracing/workers/trace_writer.rb +196 -0
  759. data/lib/datadog/tracing/workers.rb +125 -0
  760. data/lib/datadog/tracing/writer.rb +188 -0
  761. data/lib/datadog/tracing.rb +169 -0
  762. data/lib/datadog/version.rb +26 -0
  763. data/lib/datadog.rb +10 -0
  764. metadata +886 -0
@@ -0,0 +1,1153 @@
1
+ #include <ruby.h>
2
+ #include <ruby/thread.h>
3
+ #include <ruby/thread_native.h>
4
+ #include <ruby/debug.h>
5
+ #include <stdbool.h>
6
+ #include <stdatomic.h>
7
+ #include <signal.h>
8
+ #include <errno.h>
9
+
10
+ #include "helpers.h"
11
+ #include "ruby_helpers.h"
12
+ #include "collectors_thread_context.h"
13
+ #include "collectors_dynamic_sampling_rate.h"
14
+ #include "collectors_idle_sampling_helper.h"
15
+ #include "collectors_discrete_dynamic_sampler.h"
16
+ #include "private_vm_api_access.h"
17
+ #include "setup_signal_handler.h"
18
+ #include "time_helpers.h"
19
+
20
+ #define ERR_CLOCK_FAIL "failed to get clock time"
21
+
22
+ // Maximum allowed value for an allocation weight. Attempts to use higher values will result in clamping.
23
+ unsigned int MAX_ALLOC_WEIGHT = 65535;
24
+
25
+ // Used to trigger the execution of Collectors::ThreadState, which implements all of the sampling logic
26
+ // itself; this class only implements the "when to do it" part.
27
+ //
28
+ // This file implements the native bits of the Datadog::Profiling::Collectors::CpuAndWallTimeWorker class
29
+
30
+ // ---
31
+ // Here be dragons: This component is quite fiddly and probably one of the more complex in the profiler as it deals with
32
+ // multiple threads, signal handlers, global state, etc.
33
+ //
34
+ // ## Design notes for this class:
35
+ //
36
+ // ### Constraints
37
+ //
38
+ // Currently, sampling Ruby threads requires calling Ruby VM APIs that are only safe to call while holding on to the
39
+ // global VM lock (and are not async-signal safe -- cannot be called from a signal handler).
40
+ //
41
+ // @ivoanjo: As a note, I don't think we should think of this constraint as set in stone. Since can reach into the Ruby
42
+ // internals, we may be able to figure out a way of overcoming it. But it's definitely going to be hard so for now
43
+ // we're considering it as a given.
44
+ //
45
+ // ### Flow for triggering CPU/Wall-time samples
46
+ //
47
+ // The flow for triggering samples is as follows:
48
+ //
49
+ // 1. Inside the `run_sampling_trigger_loop` function (running in the `CpuAndWallTimeWorker` background thread),
50
+ // a `SIGPROF` signal gets sent to the current process.
51
+ //
52
+ // 2. The `handle_sampling_signal` signal handler function gets called to handle the `SIGPROF` signal.
53
+ //
54
+ // Which thread the signal handler function gets called on by the operating system is quite important. We need to perform
55
+ // an operation -- calling the `rb_postponed_job_register_one` API -- that can only be called from the thread that
56
+ // is holding on to the global VM lock. So this is the thread we're "hoping" our signal lands on.
57
+ //
58
+ // The signal never lands on the `CpuAndWallTimeWorker` background thread because we explicitly block it off from that
59
+ // thread in `block_sigprof_signal_handler_from_running_in_current_thread`.
60
+ //
61
+ // If the signal lands on a thread that is not holding onto the global VM lock, we can't proceed to the next step,
62
+ // and we need to restart the sampling flow from step 1. (There's still quite a few improvements we can make here,
63
+ // but this is the current state of the implementation).
64
+ //
65
+ // 3. Inside `handle_sampling_signal`, if it's getting executed by the Ruby thread that is holding the global VM lock,
66
+ // we can call `rb_postponed_job_register_one` to ask the Ruby VM to call our `sample_from_postponed_job` function
67
+ // "as soon as it can".
68
+ //
69
+ // 4. The Ruby VM calls our `sample_from_postponed_job` from a thread holding the global VM lock. A sample is recorded by
70
+ // calling `thread_context_collector_sample`.
71
+ //
72
+ // ### TracePoints and Forking
73
+ //
74
+ // When the Ruby VM forks, the CPU/Wall-time profiling stops naturally because it's triggered by a background thread
75
+ // that doesn't get automatically restarted by the VM on the child process. (The profiler does trigger its restart at
76
+ // some point -- see `Profiling::Tasks::Setup` for details).
77
+ //
78
+ // But this doesn't apply to any `TracePoint`s this class may use, which will continue to be active. Thus, we need to
79
+ // always remember consider this case of -- the worker thread may not be alive but the `TracePoint`s can continue to
80
+ // trigger samples.
81
+ //
82
+ // ---
83
+
84
+ #ifndef NO_POSTPONED_TRIGGER
85
+ // Used to call the rb_postponed_job_trigger from Ruby 3.3+. These get initialized in
86
+ // `collectors_cpu_and_wall_time_worker_init` below and always get reused after that.
87
+ static rb_postponed_job_handle_t sample_from_postponed_job_handle;
88
+ static rb_postponed_job_handle_t after_gc_from_postponed_job_handle;
89
+ #endif
90
+
91
+ // Contains state for a single CpuAndWallTimeWorker instance
92
+ struct cpu_and_wall_time_worker_state {
93
+ // These are immutable after initialization
94
+
95
+ bool gc_profiling_enabled;
96
+ bool no_signals_workaround_enabled;
97
+ bool dynamic_sampling_rate_enabled;
98
+ bool allocation_profiling_enabled;
99
+ VALUE self_instance;
100
+ VALUE thread_context_collector_instance;
101
+ VALUE idle_sampling_helper_instance;
102
+ VALUE owner_thread;
103
+ dynamic_sampling_rate_state cpu_dynamic_sampling_rate;
104
+ discrete_dynamic_sampler allocation_sampler;
105
+ VALUE gc_tracepoint; // Used to get gc start/finish information
106
+ VALUE object_allocation_tracepoint; // Used to get allocation counts and allocation profiling
107
+
108
+ // These are mutable and used to signal things between the worker thread and other threads
109
+
110
+ atomic_bool should_run;
111
+ // When something goes wrong during sampling, we record the Ruby exception here, so that it can be "re-raised" on
112
+ // the CpuAndWallTimeWorker thread
113
+ VALUE failure_exception;
114
+ // Used by `_native_stop` to flag the worker thread to start (see comment on `_native_sampling_loop`)
115
+ VALUE stop_thread;
116
+
117
+ // Others
118
+
119
+ // Used to detect/avoid nested sampling, e.g. when the object_allocation_tracepoint gets triggered by a memory allocation
120
+ // that happens during another sample.
121
+ bool during_sample;
122
+
123
+ struct stats {
124
+ // # Generic stats
125
+ // How many times we tried to trigger a sample
126
+ unsigned int trigger_sample_attempts;
127
+ // How many times we tried to simulate signal delivery
128
+ unsigned int trigger_simulated_signal_delivery_attempts;
129
+ // How many times we actually simulated signal delivery
130
+ unsigned int simulated_signal_delivery;
131
+ // How many times we actually called rb_postponed_job_register_one from a signal handler
132
+ unsigned int signal_handler_enqueued_sample;
133
+ // How many times the signal handler was called from the wrong thread
134
+ unsigned int signal_handler_wrong_thread;
135
+
136
+ // # Stats for the results of calling rb_postponed_job_register_one
137
+ // The same function was already waiting to be executed
138
+ unsigned int postponed_job_skipped_already_existed;
139
+ // The function was added to the queue successfully
140
+ unsigned int postponed_job_success;
141
+ // The queue was full
142
+ unsigned int postponed_job_full;
143
+ // The function returned an unknown result code
144
+ unsigned int postponed_job_unknown_result;
145
+
146
+ // # CPU/Walltime sampling stats
147
+ // How many times we actually CPU/wall sampled
148
+ unsigned int cpu_sampled;
149
+ // How many times we skipped a CPU/wall sample because of the dynamic sampling rate mechanism
150
+ unsigned int cpu_skipped;
151
+ // Min/max/total wall-time spent on CPU/wall sampling
152
+ uint64_t cpu_sampling_time_ns_min;
153
+ uint64_t cpu_sampling_time_ns_max;
154
+ uint64_t cpu_sampling_time_ns_total;
155
+
156
+ // # Allocation sampling stats
157
+ // How many times we actually allocation sampled
158
+ uint64_t allocation_sampled;
159
+ // How many times we skipped an allocation sample because of the dynamic sampling rate mechanism
160
+ uint64_t allocation_skipped;
161
+ // Min/max/total wall-time spent on allocation sampling
162
+ uint64_t allocation_sampling_time_ns_min;
163
+ uint64_t allocation_sampling_time_ns_max;
164
+ uint64_t allocation_sampling_time_ns_total;
165
+ // How many times we saw allocations being done inside a sample
166
+ unsigned int allocations_during_sample;
167
+ } stats;
168
+ };
169
+
170
+ static VALUE _native_new(VALUE klass);
171
+ static VALUE _native_initialize(
172
+ DDTRACE_UNUSED VALUE _self,
173
+ VALUE self_instance,
174
+ VALUE thread_context_collector_instance,
175
+ VALUE gc_profiling_enabled,
176
+ VALUE idle_sampling_helper_instance,
177
+ VALUE no_signals_workaround_enabled,
178
+ VALUE dynamic_sampling_rate_enabled,
179
+ VALUE dynamic_sampling_rate_overhead_target_percentage,
180
+ VALUE allocation_profiling_enabled
181
+ );
182
+ static void cpu_and_wall_time_worker_typed_data_mark(void *state_ptr);
183
+ static VALUE _native_sampling_loop(VALUE self, VALUE instance);
184
+ static VALUE _native_stop(DDTRACE_UNUSED VALUE _self, VALUE self_instance, VALUE worker_thread);
185
+ static VALUE stop(VALUE self_instance, VALUE optional_exception);
186
+ static void stop_state(struct cpu_and_wall_time_worker_state *state, VALUE optional_exception);
187
+ static void handle_sampling_signal(DDTRACE_UNUSED int _signal, DDTRACE_UNUSED siginfo_t *_info, DDTRACE_UNUSED void *_ucontext);
188
+ static void *run_sampling_trigger_loop(void *state_ptr);
189
+ static void interrupt_sampling_trigger_loop(void *state_ptr);
190
+ static void sample_from_postponed_job(DDTRACE_UNUSED void *_unused);
191
+ static VALUE rescued_sample_from_postponed_job(VALUE self_instance);
192
+ static VALUE handle_sampling_failure(VALUE self_instance, VALUE exception);
193
+ static VALUE _native_current_sigprof_signal_handler(DDTRACE_UNUSED VALUE self);
194
+ static VALUE release_gvl_and_run_sampling_trigger_loop(VALUE instance);
195
+ static VALUE _native_is_running(DDTRACE_UNUSED VALUE self, VALUE instance);
196
+ static void testing_signal_handler(DDTRACE_UNUSED int _signal, DDTRACE_UNUSED siginfo_t *_info, DDTRACE_UNUSED void *_ucontext);
197
+ static VALUE _native_install_testing_signal_handler(DDTRACE_UNUSED VALUE self);
198
+ static VALUE _native_remove_testing_signal_handler(DDTRACE_UNUSED VALUE self);
199
+ static VALUE _native_trigger_sample(DDTRACE_UNUSED VALUE self);
200
+ static VALUE _native_gc_tracepoint(DDTRACE_UNUSED VALUE self, VALUE instance);
201
+ static void on_gc_event(VALUE tracepoint_data, DDTRACE_UNUSED void *unused);
202
+ static void after_gc_from_postponed_job(DDTRACE_UNUSED void *_unused);
203
+ static VALUE safely_call(VALUE (*function_to_call_safely)(VALUE), VALUE function_to_call_safely_arg, VALUE instance);
204
+ static VALUE _native_simulate_handle_sampling_signal(DDTRACE_UNUSED VALUE self);
205
+ static VALUE _native_simulate_sample_from_postponed_job(DDTRACE_UNUSED VALUE self);
206
+ static VALUE _native_reset_after_fork(DDTRACE_UNUSED VALUE self, VALUE instance);
207
+ static VALUE _native_is_sigprof_blocked_in_current_thread(DDTRACE_UNUSED VALUE self);
208
+ static VALUE _native_stats(DDTRACE_UNUSED VALUE self, VALUE instance);
209
+ static VALUE _native_stats_reset_not_thread_safe(DDTRACE_UNUSED VALUE self, VALUE instance);
210
+ void *simulate_sampling_signal_delivery(DDTRACE_UNUSED void *_unused);
211
+ static void grab_gvl_and_sample(void);
212
+ static void reset_stats_not_thread_safe(struct cpu_and_wall_time_worker_state *state);
213
+ static void sleep_for(uint64_t time_ns);
214
+ static VALUE _native_allocation_count(DDTRACE_UNUSED VALUE self);
215
+ static void on_newobj_event(VALUE tracepoint_data, DDTRACE_UNUSED void *unused);
216
+ static void disable_tracepoints(struct cpu_and_wall_time_worker_state *state);
217
+ static VALUE _native_with_blocked_sigprof(DDTRACE_UNUSED VALUE self);
218
+ static VALUE rescued_sample_allocation(VALUE tracepoint_data);
219
+ static void delayed_error(struct cpu_and_wall_time_worker_state *state, const char *error);
220
+ static VALUE _native_delayed_error(DDTRACE_UNUSED VALUE self, VALUE instance, VALUE error_msg);
221
+
222
+ // Note on sampler global state safety:
223
+ //
224
+ // Both `active_sampler_instance` and `active_sampler_instance_state` are **GLOBAL** state. Be careful when accessing
225
+ // or modifying them.
226
+ // In particular, it's important to only mutate them while holding the global VM lock, to ensure correctness.
227
+ //
228
+ // This global state is needed because a bunch of functions on this file need to access it from situations
229
+ // (e.g. signal handler) where it's impossible or just awkward to pass it as an argument.
230
+ static VALUE active_sampler_instance = Qnil;
231
+ static struct cpu_and_wall_time_worker_state *active_sampler_instance_state = NULL;
232
+
233
+ // See handle_sampling_signal for details on what this does
234
+ #ifdef NO_POSTPONED_TRIGGER
235
+ static void *gc_finalize_deferred_workaround;
236
+ #endif
237
+
238
+ // Used to implement CpuAndWallTimeWorker._native_allocation_count . To be able to use cheap thread-local variables
239
+ // (here with `__thread`, see https://gcc.gnu.org/onlinedocs/gcc/Thread-Local.html), this needs to be global.
240
+ //
241
+ // Carryover of state between profiler instances can happen and is not considered to be a problem -- see expectations for this
242
+ // API documented in profiling.rb .
243
+ __thread uint64_t allocation_count = 0;
244
+
245
+ void collectors_cpu_and_wall_time_worker_init(VALUE profiling_module) {
246
+ rb_global_variable(&active_sampler_instance);
247
+
248
+ #ifndef NO_POSTPONED_TRIGGER
249
+ int unused_flags = 0;
250
+ sample_from_postponed_job_handle = rb_postponed_job_preregister(unused_flags, sample_from_postponed_job, NULL);
251
+ after_gc_from_postponed_job_handle = rb_postponed_job_preregister(unused_flags, after_gc_from_postponed_job, NULL);
252
+
253
+ if (sample_from_postponed_job_handle == POSTPONED_JOB_HANDLE_INVALID || after_gc_from_postponed_job_handle == POSTPONED_JOB_HANDLE_INVALID) {
254
+ rb_raise(rb_eRuntimeError, "Failed to register profiler postponed jobs (got POSTPONED_JOB_HANDLE_INVALID)");
255
+ }
256
+ #else
257
+ gc_finalize_deferred_workaround = objspace_ptr_for_gc_finalize_deferred_workaround();
258
+ #endif
259
+
260
+ VALUE collectors_module = rb_define_module_under(profiling_module, "Collectors");
261
+ VALUE collectors_cpu_and_wall_time_worker_class = rb_define_class_under(collectors_module, "CpuAndWallTimeWorker", rb_cObject);
262
+ // Hosts methods used for testing the native code using RSpec
263
+ VALUE testing_module = rb_define_module_under(collectors_cpu_and_wall_time_worker_class, "Testing");
264
+
265
+ // Instances of the CpuAndWallTimeWorker class are "TypedData" objects.
266
+ // "TypedData" objects are special objects in the Ruby VM that can wrap C structs.
267
+ // In this case, it wraps the cpu_and_wall_time_worker_state.
268
+ //
269
+ // Because Ruby doesn't know how to initialize native-level structs, we MUST override the allocation function for objects
270
+ // of this class so that we can manage this part. Not overriding or disabling the allocation function is a common
271
+ // gotcha for "TypedData" objects that can very easily lead to VM crashes, see for instance
272
+ // https://bugs.ruby-lang.org/issues/18007 for a discussion around this.
273
+ rb_define_alloc_func(collectors_cpu_and_wall_time_worker_class, _native_new);
274
+
275
+ rb_define_singleton_method(collectors_cpu_and_wall_time_worker_class, "_native_initialize", _native_initialize, 8);
276
+ rb_define_singleton_method(collectors_cpu_and_wall_time_worker_class, "_native_sampling_loop", _native_sampling_loop, 1);
277
+ rb_define_singleton_method(collectors_cpu_and_wall_time_worker_class, "_native_stop", _native_stop, 2);
278
+ rb_define_singleton_method(collectors_cpu_and_wall_time_worker_class, "_native_reset_after_fork", _native_reset_after_fork, 1);
279
+ rb_define_singleton_method(collectors_cpu_and_wall_time_worker_class, "_native_stats", _native_stats, 1);
280
+ rb_define_singleton_method(collectors_cpu_and_wall_time_worker_class, "_native_stats_reset_not_thread_safe", _native_stats_reset_not_thread_safe, 1);
281
+ rb_define_singleton_method(collectors_cpu_and_wall_time_worker_class, "_native_allocation_count", _native_allocation_count, 0);
282
+ rb_define_singleton_method(testing_module, "_native_current_sigprof_signal_handler", _native_current_sigprof_signal_handler, 0);
283
+ rb_define_singleton_method(testing_module, "_native_is_running?", _native_is_running, 1);
284
+ rb_define_singleton_method(testing_module, "_native_install_testing_signal_handler", _native_install_testing_signal_handler, 0);
285
+ rb_define_singleton_method(testing_module, "_native_remove_testing_signal_handler", _native_remove_testing_signal_handler, 0);
286
+ rb_define_singleton_method(testing_module, "_native_trigger_sample", _native_trigger_sample, 0);
287
+ rb_define_singleton_method(testing_module, "_native_gc_tracepoint", _native_gc_tracepoint, 1);
288
+ rb_define_singleton_method(testing_module, "_native_simulate_handle_sampling_signal", _native_simulate_handle_sampling_signal, 0);
289
+ rb_define_singleton_method(testing_module, "_native_simulate_sample_from_postponed_job", _native_simulate_sample_from_postponed_job, 0);
290
+ rb_define_singleton_method(testing_module, "_native_is_sigprof_blocked_in_current_thread", _native_is_sigprof_blocked_in_current_thread, 0);
291
+ rb_define_singleton_method(testing_module, "_native_with_blocked_sigprof", _native_with_blocked_sigprof, 0);
292
+ rb_define_singleton_method(testing_module, "_native_delayed_error", _native_delayed_error, 2);
293
+ }
294
+
295
+ // This structure is used to define a Ruby object that stores a pointer to a struct cpu_and_wall_time_worker_state
296
+ // See also https://github.com/ruby/ruby/blob/master/doc/extension.rdoc for how this works
297
+ static const rb_data_type_t cpu_and_wall_time_worker_typed_data = {
298
+ .wrap_struct_name = "Datadog::Profiling::Collectors::CpuAndWallTimeWorker",
299
+ .function = {
300
+ .dmark = cpu_and_wall_time_worker_typed_data_mark,
301
+ .dfree = RUBY_DEFAULT_FREE,
302
+ .dsize = NULL, // We don't track memory usage (although it'd be cool if we did!)
303
+ //.dcompact = NULL, // FIXME: Add support for compaction
304
+ },
305
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY
306
+ };
307
+
308
+ static VALUE _native_new(VALUE klass) {
309
+ struct cpu_and_wall_time_worker_state *state = ruby_xcalloc(1, sizeof(struct cpu_and_wall_time_worker_state));
310
+
311
+ // Note: Any exceptions raised from this note until the TypedData_Wrap_Struct call will lead to the state memory
312
+ // being leaked.
313
+
314
+ state->gc_profiling_enabled = false;
315
+ state->no_signals_workaround_enabled = false;
316
+ state->dynamic_sampling_rate_enabled = true;
317
+ state->allocation_profiling_enabled = false;
318
+ state->thread_context_collector_instance = Qnil;
319
+ state->idle_sampling_helper_instance = Qnil;
320
+ state->owner_thread = Qnil;
321
+ dynamic_sampling_rate_init(&state->cpu_dynamic_sampling_rate);
322
+ state->gc_tracepoint = Qnil;
323
+ state->object_allocation_tracepoint = Qnil;
324
+
325
+ atomic_init(&state->should_run, false);
326
+ state->failure_exception = Qnil;
327
+ state->stop_thread = Qnil;
328
+
329
+ state->during_sample = false;
330
+
331
+ reset_stats_not_thread_safe(state);
332
+
333
+ long now = monotonic_wall_time_now_ns(DO_NOT_RAISE_ON_FAILURE);
334
+ if (now == 0) {
335
+ ruby_xfree(state);
336
+ rb_raise(rb_eRuntimeError, ERR_CLOCK_FAIL);
337
+ }
338
+
339
+ discrete_dynamic_sampler_init(&state->allocation_sampler, "allocation", now);
340
+
341
+ return state->self_instance = TypedData_Wrap_Struct(klass, &cpu_and_wall_time_worker_typed_data, state);
342
+ }
343
+
344
+ static VALUE _native_initialize(
345
+ DDTRACE_UNUSED VALUE _self,
346
+ VALUE self_instance,
347
+ VALUE thread_context_collector_instance,
348
+ VALUE gc_profiling_enabled,
349
+ VALUE idle_sampling_helper_instance,
350
+ VALUE no_signals_workaround_enabled,
351
+ VALUE dynamic_sampling_rate_enabled,
352
+ VALUE dynamic_sampling_rate_overhead_target_percentage,
353
+ VALUE allocation_profiling_enabled
354
+ ) {
355
+ ENFORCE_BOOLEAN(gc_profiling_enabled);
356
+ ENFORCE_BOOLEAN(no_signals_workaround_enabled);
357
+ ENFORCE_BOOLEAN(dynamic_sampling_rate_enabled);
358
+ ENFORCE_TYPE(dynamic_sampling_rate_overhead_target_percentage, T_FLOAT);
359
+ ENFORCE_BOOLEAN(allocation_profiling_enabled);
360
+
361
+ struct cpu_and_wall_time_worker_state *state;
362
+ TypedData_Get_Struct(self_instance, struct cpu_and_wall_time_worker_state, &cpu_and_wall_time_worker_typed_data, state);
363
+
364
+ state->gc_profiling_enabled = (gc_profiling_enabled == Qtrue);
365
+ state->no_signals_workaround_enabled = (no_signals_workaround_enabled == Qtrue);
366
+ state->dynamic_sampling_rate_enabled = (dynamic_sampling_rate_enabled == Qtrue);
367
+ state->allocation_profiling_enabled = (allocation_profiling_enabled == Qtrue);
368
+
369
+ double total_overhead_target_percentage = NUM2DBL(dynamic_sampling_rate_overhead_target_percentage);
370
+ if (!state->allocation_profiling_enabled) {
371
+ dynamic_sampling_rate_set_overhead_target_percentage(&state->cpu_dynamic_sampling_rate, total_overhead_target_percentage);
372
+ } else {
373
+ // TODO: May be nice to offer customization here? Distribute available "overhead" margin with a bias towards one or the other
374
+ // sampler.
375
+ dynamic_sampling_rate_set_overhead_target_percentage(&state->cpu_dynamic_sampling_rate, total_overhead_target_percentage / 2);
376
+ long now = monotonic_wall_time_now_ns(RAISE_ON_FAILURE);
377
+ discrete_dynamic_sampler_set_overhead_target_percentage(&state->allocation_sampler, total_overhead_target_percentage / 2, now);
378
+ }
379
+
380
+ state->thread_context_collector_instance = enforce_thread_context_collector_instance(thread_context_collector_instance);
381
+ state->idle_sampling_helper_instance = idle_sampling_helper_instance;
382
+ state->gc_tracepoint = rb_tracepoint_new(Qnil, RUBY_INTERNAL_EVENT_GC_ENTER | RUBY_INTERNAL_EVENT_GC_EXIT, on_gc_event, NULL /* unused */);
383
+ state->object_allocation_tracepoint = rb_tracepoint_new(Qnil, RUBY_INTERNAL_EVENT_NEWOBJ, on_newobj_event, NULL /* unused */);
384
+
385
+ return Qtrue;
386
+ }
387
+
388
+ // Since our state contains references to Ruby objects, we need to tell the Ruby GC about them
389
+ static void cpu_and_wall_time_worker_typed_data_mark(void *state_ptr) {
390
+ struct cpu_and_wall_time_worker_state *state = (struct cpu_and_wall_time_worker_state *) state_ptr;
391
+
392
+ rb_gc_mark(state->thread_context_collector_instance);
393
+ rb_gc_mark(state->idle_sampling_helper_instance);
394
+ rb_gc_mark(state->owner_thread);
395
+ rb_gc_mark(state->failure_exception);
396
+ rb_gc_mark(state->stop_thread);
397
+ rb_gc_mark(state->gc_tracepoint);
398
+ rb_gc_mark(state->object_allocation_tracepoint);
399
+ }
400
+
401
+ // Called in a background thread created in CpuAndWallTimeWorker#start
402
+ static VALUE _native_sampling_loop(DDTRACE_UNUSED VALUE _self, VALUE instance) {
403
+ struct cpu_and_wall_time_worker_state *state;
404
+ TypedData_Get_Struct(instance, struct cpu_and_wall_time_worker_state, &cpu_and_wall_time_worker_typed_data, state);
405
+
406
+ // If we already got a delayed exception registered even before starting, raise before starting
407
+ if (state->failure_exception != Qnil) {
408
+ disable_tracepoints(state);
409
+ rb_exc_raise(state->failure_exception);
410
+ }
411
+
412
+ struct cpu_and_wall_time_worker_state *old_state = active_sampler_instance_state;
413
+ if (old_state != NULL) {
414
+ if (is_thread_alive(old_state->owner_thread)) {
415
+ rb_raise(
416
+ rb_eRuntimeError,
417
+ "Could not start CpuAndWallTimeWorker: There's already another instance of CpuAndWallTimeWorker active in a different thread"
418
+ );
419
+ } else {
420
+ // The previously active thread seems to have died without cleaning up after itself.
421
+ // In this case, we can still go ahead and start the profiler BUT we make sure to disable any existing tracepoint
422
+ // first as:
423
+ // a) If this is a new instance of the CpuAndWallTimeWorker, we don't want the tracepoint from the old instance
424
+ // being kept around
425
+ // b) If this is the same instance of the CpuAndWallTimeWorker if we call enable on a tracepoint that is already
426
+ // enabled, it will start firing more than once, see https://bugs.ruby-lang.org/issues/19114 for details.
427
+ disable_tracepoints(old_state);
428
+ }
429
+ }
430
+
431
+ // We use `stop_thread` to distinguish when `_native_stop` was called before we actually had a chance to start. In this
432
+ // situation we stop immediately and never even start the sampling trigger loop.
433
+ if (state->stop_thread == rb_thread_current()) return Qnil;
434
+
435
+ // Reset the dynamic sampling rate state, if any (reminder: the monotonic clock reference may change after a fork)
436
+ dynamic_sampling_rate_reset(&state->cpu_dynamic_sampling_rate);
437
+ long now = monotonic_wall_time_now_ns(RAISE_ON_FAILURE);
438
+ discrete_dynamic_sampler_reset(&state->allocation_sampler, now);
439
+
440
+ // This write to a global is thread-safe BECAUSE we're still holding on to the global VM lock at this point
441
+ active_sampler_instance_state = state;
442
+ active_sampler_instance = instance;
443
+ state->owner_thread = rb_thread_current();
444
+
445
+ atomic_store(&state->should_run, true);
446
+
447
+ block_sigprof_signal_handler_from_running_in_current_thread(); // We want to interrupt the thread with the global VM lock, never this one
448
+
449
+ // Release GVL, get to the actual work!
450
+ int exception_state;
451
+ rb_protect(release_gvl_and_run_sampling_trigger_loop, instance, &exception_state);
452
+
453
+ // The sample trigger loop finished (either cleanly or with an error); let's clean up
454
+
455
+ disable_tracepoints(state);
456
+
457
+ active_sampler_instance_state = NULL;
458
+ active_sampler_instance = Qnil;
459
+ state->owner_thread = Qnil;
460
+
461
+ // If this `Thread` is about to die, why is this important? It's because Ruby caches native threads for a period after
462
+ // the `Thread` dies, and reuses them if a new Ruby `Thread` gets created. This means that while conceptually the
463
+ // worker background `Thread` is about to die, the low-level native OS thread can be reused for something else in the Ruby app.
464
+ // Then, the reused thread would "inherit" the SIGPROF blocking, which is... really unexpected.
465
+ // This actually caused a flaky test -- the `native_extension_spec.rb` creates a `Thread` and tries to specifically
466
+ // send SIGPROF signals to it, and oops it could fail if it got the reused native thread from the worker which still
467
+ // had SIGPROF delivery blocked. :hide_the_pain_harold:
468
+ unblock_sigprof_signal_handler_from_running_in_current_thread();
469
+
470
+ // Why replace and not use remove the signal handler? We do this because when a process receives a SIGPROF without
471
+ // having an explicit signal handler set up, the process will instantly terminate with a confusing
472
+ // "Profiling timer expired" message left behind. (This message doesn't come from us -- it's the default message for
473
+ // an unhandled SIGPROF. Pretty confusing UNIX/POSIX behavior...)
474
+ //
475
+ // Unfortunately, because signal delivery is asynchronous, there's no way to guarantee that there are no pending
476
+ // profiler-sent signals by the time we get here and want to clean up.
477
+ // @ivoanjo: I suspect this will never happen, but the cost of getting it wrong is really high (VM terminates) so this
478
+ // is a just-in-case situation.
479
+ //
480
+ // Note 2: This can raise exceptions as well, so make sure that all cleanups are done by the time we get here.
481
+ replace_sigprof_signal_handler_with_empty_handler(handle_sampling_signal);
482
+
483
+ // Ensure that instance is not garbage collected while the native sampling loop is running; this is probably not needed, but just in case
484
+ RB_GC_GUARD(instance);
485
+
486
+ if (exception_state) rb_jump_tag(exception_state); // Re-raise any exception that happened
487
+
488
+ return Qnil;
489
+ }
490
+
491
+ static VALUE _native_stop(DDTRACE_UNUSED VALUE _self, VALUE self_instance, VALUE worker_thread) {
492
+ struct cpu_and_wall_time_worker_state *state;
493
+ TypedData_Get_Struct(self_instance, struct cpu_and_wall_time_worker_state, &cpu_and_wall_time_worker_typed_data, state);
494
+
495
+ state->stop_thread = worker_thread;
496
+
497
+ return stop(self_instance, /* optional_exception: */ Qnil);
498
+ }
499
+
500
+ static void stop_state(struct cpu_and_wall_time_worker_state *state, VALUE optional_exception) {
501
+ atomic_store(&state->should_run, false);
502
+ state->failure_exception = optional_exception;
503
+
504
+ // Disable the tracepoints as soon as possible, so the VM doesn't keep on calling them
505
+ disable_tracepoints(state);
506
+ }
507
+
508
+ static VALUE stop(VALUE self_instance, VALUE optional_exception) {
509
+ struct cpu_and_wall_time_worker_state *state;
510
+ TypedData_Get_Struct(self_instance, struct cpu_and_wall_time_worker_state, &cpu_and_wall_time_worker_typed_data, state);
511
+
512
+ stop_state(state, optional_exception);
513
+
514
+ return Qtrue;
515
+ }
516
+
517
+ // NOTE: Remember that this will run in the thread and within the scope of user code, including user C code.
518
+ // We need to be careful not to change any state that may be observed OR to restore it if we do. For instance, if anything
519
+ // we do here can set `errno`, then we must be careful to restore the old `errno` after the fact.
520
+ static void handle_sampling_signal(DDTRACE_UNUSED int _signal, DDTRACE_UNUSED siginfo_t *_info, DDTRACE_UNUSED void *_ucontext) {
521
+ struct cpu_and_wall_time_worker_state *state = active_sampler_instance_state; // Read from global variable, see "sampler global state safety" note above
522
+
523
+ // This can potentially happen if the CpuAndWallTimeWorker was stopped while the signal delivery was happening; nothing to do
524
+ if (state == NULL) return;
525
+
526
+ if (
527
+ !ruby_native_thread_p() || // Not a Ruby thread
528
+ !is_current_thread_holding_the_gvl() || // Not safe to enqueue a sample from this thread
529
+ !ddtrace_rb_ractor_main_p() // We're not on the main Ractor; we currently don't support profiling non-main Ractors
530
+ ) {
531
+ state->stats.signal_handler_wrong_thread++;
532
+ return;
533
+ }
534
+
535
+ // We implicitly assume there can be no concurrent nor nested calls to handle_sampling_signal because
536
+ // a) we get triggered using SIGPROF, and the docs state a second SIGPROF will not interrupt an existing one
537
+ // b) we validate we are in the thread that has the global VM lock; if a different thread gets a signal, it will return early
538
+ // because it will not have the global VM lock
539
+
540
+ // Note: rb_postponed_job_register_one ensures that if there's a previous sample_from_postponed_job queued for execution
541
+ // then we will not queue a second one. It does this by doing a linear scan on the existing jobs; in the future we
542
+ // may want to implement that check ourselves.
543
+
544
+ state->stats.signal_handler_enqueued_sample++;
545
+
546
+ // Note: If we ever want to get rid of rb_postponed_job_register_one, remember not to clobber Ruby exceptions, as
547
+ // this function does this helpful job for us now -- https://github.com/ruby/ruby/commit/a98e343d39c4d7bf1e2190b076720f32d9f298b3.
548
+ #ifndef NO_POSTPONED_TRIGGER // Ruby 3.3+
549
+ rb_postponed_job_trigger(sample_from_postponed_job_handle);
550
+ state->stats.postponed_job_success++; // Always succeeds
551
+ #else
552
+
553
+ // This is a workaround for https://bugs.ruby-lang.org/issues/19991 (for Ruby < 3.3)
554
+ //
555
+ // TL;DR the `rb_postponed_job_register_one` API is not atomic (which is why it got replaced by `rb_postponed_job_trigger`)
556
+ // and in rare cases can cause VM crashes.
557
+ //
558
+ // Specifically, if we're interrupting `rb_postponed_job_flush` (the function that processes postponed jobs), the way
559
+ // that this function reads the jobs is not atomic, and can cause our call to
560
+ // `rb_postponed_job_register(function, arg)` to clobber an existing job that is getting dequeued.
561
+ // Clobbering an existing job is somewhat annoying, but the worst part is that it can happen that we clobber only
562
+ // the existing job's arguments.
563
+ // As surveyed in https://github.com/ruby/ruby/pull/8949#issuecomment-1821441370 clobbering the arguments turns out
564
+ // to not matter in many cases as usually `rb_postponed_job_register` calls in the VM and ecosystem ignore the argument.
565
+ //
566
+ // https://bugs.ruby-lang.org/issues/19991 is the exception: inside Ruby's `gc.c`, when dealing with object
567
+ // finalizers, Ruby calls `gc_finalize_deferred_register` which internally calls
568
+ // `rb_postponed_job_register_one(gc_finalize_deferred, objspace)`.
569
+ // Clobbering this call means that `gc_finalize_deferred` would get called with `NULL`, causing a segmentation fault.
570
+ //
571
+ // Note that this is quite rare: our signal needs to land at exactly the point where the VM has read the function
572
+ // to execute, but has yet to read the arguments. @ivoanjo: I could only reproduce it by manually changing the VM
573
+ // code to simulate this happening.
574
+ //
575
+ // Thus, our workaround is simple: we pass in objspace as our argument, just in case the clobbering happens.
576
+ // In the happy path, we never use this argument so it makes no difference. In the buggy path, we avoid crashing the VM.
577
+ int result = rb_postponed_job_register(0, sample_from_postponed_job, gc_finalize_deferred_workaround /* instead of NULL */);
578
+
579
+ // Officially, the result of rb_postponed_job_register_one is documented as being opaque, but in practice it does not
580
+ // seem to have changed between Ruby 2.3 and 3.2, and so we track it as a debugging mechanism
581
+ switch (result) {
582
+ case 0:
583
+ state->stats.postponed_job_full++; break;
584
+ case 1:
585
+ state->stats.postponed_job_success++; break;
586
+ case 2:
587
+ state->stats.postponed_job_skipped_already_existed++; break;
588
+ default:
589
+ state->stats.postponed_job_unknown_result++;
590
+ }
591
+ #endif
592
+ }
593
+
594
+ // The actual sampling trigger loop always runs **without** the global vm lock.
595
+ static void *run_sampling_trigger_loop(void *state_ptr) {
596
+ struct cpu_and_wall_time_worker_state *state = (struct cpu_and_wall_time_worker_state *) state_ptr;
597
+
598
+ uint64_t minimum_time_between_signals = MILLIS_AS_NS(10);
599
+
600
+ while (atomic_load(&state->should_run)) {
601
+ state->stats.trigger_sample_attempts++;
602
+
603
+ if (state->no_signals_workaround_enabled) {
604
+ // In the no_signals_workaround_enabled mode, the profiler never sends SIGPROF signals.
605
+ //
606
+ // This is a fallback for a few incompatibilities and limitations -- see the code that decides when to enable
607
+ // `no_signals_workaround_enabled` in `Profiling::Component` for details.
608
+ //
609
+ // Thus, we instead pretty please ask Ruby to let us run. This means profiling data can be biased by when the Ruby
610
+ // scheduler chooses to schedule us.
611
+ state->stats.trigger_simulated_signal_delivery_attempts++;
612
+ grab_gvl_and_sample(); // Note: Can raise exceptions
613
+ } else {
614
+ current_gvl_owner owner = gvl_owner();
615
+ if (owner.valid) {
616
+ // Note that reading the GVL owner and sending them a signal is a race -- the Ruby VM keeps on executing while
617
+ // we're doing this, so we may still not signal the correct thread from time to time, but our signal handler
618
+ // includes a check to see if it got called in the right thread
619
+ pthread_kill(owner.owner, SIGPROF);
620
+ } else {
621
+ // If no thread owns the Global VM Lock, the application is probably idle at the moment. We still want to sample
622
+ // so we "ask a friend" (the IdleSamplingHelper component) to grab the GVL and simulate getting a SIGPROF.
623
+ //
624
+ // In a previous version of the code, we called `grab_gvl_and_sample` directly BUT this was problematic because
625
+ // Ruby may concurrently get busy and so the CpuAndWallTimeWorker would be blocked in line to acquire the GVL
626
+ // for an uncontrolled amount of time. (This can still happen to the IdleSamplingHelper, but the
627
+ // CpuAndWallTimeWorker will still be free to interrupt the Ruby VM and keep sampling for the entire blocking period).
628
+ state->stats.trigger_simulated_signal_delivery_attempts++;
629
+ idle_sampling_helper_request_action(state->idle_sampling_helper_instance, grab_gvl_and_sample);
630
+ }
631
+ }
632
+
633
+ sleep_for(minimum_time_between_signals);
634
+
635
+ // The dynamic sampling rate module keeps track of how long samples are taking, and in here we extend our sleep time
636
+ // to take that into account.
637
+ // Note that we deliberately should NOT combine this sleep_for with the one above because the result of
638
+ // `dynamic_sampling_rate_get_sleep` may have changed while the above sleep was ongoing.
639
+ uint64_t extra_sleep =
640
+ dynamic_sampling_rate_get_sleep(&state->cpu_dynamic_sampling_rate, monotonic_wall_time_now_ns(DO_NOT_RAISE_ON_FAILURE));
641
+ if (state->dynamic_sampling_rate_enabled && extra_sleep > 0) sleep_for(extra_sleep);
642
+ }
643
+
644
+ return NULL; // Unused
645
+ }
646
+
647
+ // This is called by the Ruby VM when it wants to shut down the background thread
648
+ static void interrupt_sampling_trigger_loop(void *state_ptr) {
649
+ struct cpu_and_wall_time_worker_state *state = (struct cpu_and_wall_time_worker_state *) state_ptr;
650
+
651
+ atomic_store(&state->should_run, false);
652
+ }
653
+
654
+ static void sample_from_postponed_job(DDTRACE_UNUSED void *_unused) {
655
+ struct cpu_and_wall_time_worker_state *state = active_sampler_instance_state; // Read from global variable, see "sampler global state safety" note above
656
+
657
+ // This can potentially happen if the CpuAndWallTimeWorker was stopped while the postponed job was waiting to be executed; nothing to do
658
+ if (state == NULL) return;
659
+
660
+ // @ivoanjo: I'm not sure this can ever happen because `handle_sampling_signal` only enqueues this callback if
661
+ // it's running on the main Ractor, but just in case...
662
+ if (!ddtrace_rb_ractor_main_p()) {
663
+ return; // We're not on the main Ractor; we currently don't support profiling non-main Ractors
664
+ }
665
+
666
+ state->during_sample = true;
667
+
668
+ // Rescue against any exceptions that happen during sampling
669
+ safely_call(rescued_sample_from_postponed_job, state->self_instance, state->self_instance);
670
+
671
+ state->during_sample = false;
672
+ }
673
+
674
+ static VALUE rescued_sample_from_postponed_job(VALUE self_instance) {
675
+ struct cpu_and_wall_time_worker_state *state;
676
+ TypedData_Get_Struct(self_instance, struct cpu_and_wall_time_worker_state, &cpu_and_wall_time_worker_typed_data, state);
677
+
678
+ long wall_time_ns_before_sample = monotonic_wall_time_now_ns(RAISE_ON_FAILURE);
679
+
680
+ if (state->dynamic_sampling_rate_enabled && !dynamic_sampling_rate_should_sample(&state->cpu_dynamic_sampling_rate, wall_time_ns_before_sample)) {
681
+ state->stats.cpu_skipped++;
682
+ return Qnil;
683
+ }
684
+
685
+ state->stats.cpu_sampled++;
686
+
687
+ VALUE profiler_overhead_stack_thread = state->owner_thread; // Used to attribute profiler overhead to a different stack
688
+ thread_context_collector_sample(state->thread_context_collector_instance, wall_time_ns_before_sample, profiler_overhead_stack_thread);
689
+
690
+ long wall_time_ns_after_sample = monotonic_wall_time_now_ns(RAISE_ON_FAILURE);
691
+ long delta_ns = wall_time_ns_after_sample - wall_time_ns_before_sample;
692
+
693
+ // Guard against wall-time going backwards, see https://github.com/DataDog/dd-trace-rb/pull/2336 for discussion.
694
+ uint64_t sampling_time_ns = delta_ns < 0 ? 0 : delta_ns;
695
+
696
+ state->stats.cpu_sampling_time_ns_min = uint64_min_of(sampling_time_ns, state->stats.cpu_sampling_time_ns_min);
697
+ state->stats.cpu_sampling_time_ns_max = uint64_max_of(sampling_time_ns, state->stats.cpu_sampling_time_ns_max);
698
+ state->stats.cpu_sampling_time_ns_total += sampling_time_ns;
699
+
700
+ dynamic_sampling_rate_after_sample(&state->cpu_dynamic_sampling_rate, wall_time_ns_after_sample, sampling_time_ns);
701
+
702
+ // Return a dummy VALUE because we're called from rb_rescue2 which requires it
703
+ return Qnil;
704
+ }
705
+
706
+ static VALUE handle_sampling_failure(VALUE self_instance, VALUE exception) {
707
+ stop(self_instance, exception);
708
+ return Qnil;
709
+ }
710
+
711
+ // This method exists only to enable testing Datadog::Profiling::Collectors::CpuAndWallTimeWorker behavior using RSpec.
712
+ // It SHOULD NOT be used for other purposes.
713
+ static VALUE _native_current_sigprof_signal_handler(DDTRACE_UNUSED VALUE self) {
714
+ struct sigaction existing_signal_handler_config = {.sa_sigaction = NULL};
715
+ if (sigaction(SIGPROF, NULL, &existing_signal_handler_config) != 0) {
716
+ rb_sys_fail("Failed to probe existing handler");
717
+ }
718
+
719
+ if (existing_signal_handler_config.sa_sigaction == handle_sampling_signal) {
720
+ return ID2SYM(rb_intern("profiling"));
721
+ } else if (existing_signal_handler_config.sa_sigaction == empty_signal_handler) {
722
+ return ID2SYM(rb_intern("empty"));
723
+ } else if (existing_signal_handler_config.sa_sigaction != NULL) {
724
+ return ID2SYM(rb_intern("other"));
725
+ } else {
726
+ return Qnil;
727
+ }
728
+ }
729
+
730
+ static VALUE release_gvl_and_run_sampling_trigger_loop(VALUE instance) {
731
+ struct cpu_and_wall_time_worker_state *state;
732
+ TypedData_Get_Struct(instance, struct cpu_and_wall_time_worker_state, &cpu_and_wall_time_worker_typed_data, state);
733
+
734
+ // Final preparations: Setup signal handler and enable tracepoints. We run these here and not in `_native_sampling_loop`
735
+ // because they may raise exceptions.
736
+ install_sigprof_signal_handler(handle_sampling_signal, "handle_sampling_signal");
737
+ if (state->gc_profiling_enabled) rb_tracepoint_enable(state->gc_tracepoint);
738
+ if (state->allocation_profiling_enabled) rb_tracepoint_enable(state->object_allocation_tracepoint);
739
+
740
+ rb_thread_call_without_gvl(run_sampling_trigger_loop, state, interrupt_sampling_trigger_loop, state);
741
+
742
+ // If we stopped sampling due to an exception, re-raise it (now in the worker thread)
743
+ if (state->failure_exception != Qnil) rb_exc_raise(state->failure_exception);
744
+
745
+ return Qnil;
746
+ }
747
+
748
+ // This method exists only to enable testing Datadog::Profiling::Collectors::CpuAndWallTimeWorker behavior using RSpec.
749
+ // It SHOULD NOT be used for other purposes.
750
+ static VALUE _native_is_running(DDTRACE_UNUSED VALUE self, VALUE instance) {
751
+ struct cpu_and_wall_time_worker_state *state = active_sampler_instance_state; // Read from global variable, see "sampler global state safety" note above
752
+
753
+ return (state != NULL && is_thread_alive(state->owner_thread) && state->self_instance == instance) ? Qtrue : Qfalse;
754
+ }
755
+
756
+ static void testing_signal_handler(DDTRACE_UNUSED int _signal, DDTRACE_UNUSED siginfo_t *_info, DDTRACE_UNUSED void *_ucontext) {
757
+ /* Does nothing on purpose */
758
+ }
759
+
760
+ // This method exists only to enable testing Datadog::Profiling::Collectors::CpuAndWallTimeWorker behavior using RSpec.
761
+ // It SHOULD NOT be used for other purposes.
762
+ static VALUE _native_install_testing_signal_handler(DDTRACE_UNUSED VALUE self) {
763
+ install_sigprof_signal_handler(testing_signal_handler, "testing_signal_handler");
764
+ return Qtrue;
765
+ }
766
+
767
+ // This method exists only to enable testing Datadog::Profiling::Collectors::CpuAndWallTimeWorker behavior using RSpec.
768
+ // It SHOULD NOT be used for other purposes.
769
+ static VALUE _native_remove_testing_signal_handler(DDTRACE_UNUSED VALUE self) {
770
+ remove_sigprof_signal_handler();
771
+ return Qtrue;
772
+ }
773
+
774
+ // This method exists only to enable testing Datadog::Profiling::Collectors::CpuAndWallTimeWorker behavior using RSpec.
775
+ // It SHOULD NOT be used for other purposes.
776
+ static VALUE _native_trigger_sample(DDTRACE_UNUSED VALUE self) {
777
+ sample_from_postponed_job(NULL);
778
+ return Qtrue;
779
+ }
780
+
781
+ // This method exists only to enable testing Datadog::Profiling::Collectors::CpuAndWallTimeWorker behavior using RSpec.
782
+ // It SHOULD NOT be used for other purposes.
783
+ static VALUE _native_gc_tracepoint(DDTRACE_UNUSED VALUE self, VALUE instance) {
784
+ struct cpu_and_wall_time_worker_state *state;
785
+ TypedData_Get_Struct(instance, struct cpu_and_wall_time_worker_state, &cpu_and_wall_time_worker_typed_data, state);
786
+
787
+ return state->gc_tracepoint;
788
+ }
789
+
790
+ // Implements tracking of cpu-time and wall-time spent doing GC. This function is called by Ruby from the `gc_tracepoint`
791
+ // when the RUBY_INTERNAL_EVENT_GC_ENTER and RUBY_INTERNAL_EVENT_GC_EXIT events are triggered.
792
+ //
793
+ // See the comments on
794
+ // * thread_context_collector_on_gc_start
795
+ // * thread_context_collector_on_gc_finish
796
+ // * thread_context_collector_sample_after_gc
797
+ //
798
+ // For the expected times in which to call them, and their assumptions.
799
+ //
800
+ // Safety: This function gets called while Ruby is doing garbage collection. While Ruby is doing garbage collection,
801
+ // *NO ALLOCATION* is allowed. This function, and any it calls must never trigger memory or object allocation.
802
+ // This includes exceptions and use of ruby_xcalloc (because xcalloc can trigger GC)!
803
+ static void on_gc_event(VALUE tracepoint_data, DDTRACE_UNUSED void *unused) {
804
+ if (!ddtrace_rb_ractor_main_p()) {
805
+ return; // We're not on the main Ractor; we currently don't support profiling non-main Ractors
806
+ }
807
+
808
+ int event = rb_tracearg_event_flag(rb_tracearg_from_tracepoint(tracepoint_data));
809
+ if (event != RUBY_INTERNAL_EVENT_GC_ENTER && event != RUBY_INTERNAL_EVENT_GC_EXIT) return; // Unknown event
810
+
811
+ struct cpu_and_wall_time_worker_state *state = active_sampler_instance_state; // Read from global variable, see "sampler global state safety" note above
812
+
813
+ // This should not happen in a normal situation because the tracepoint is always enabled after the instance is set
814
+ // and disabled before it is cleared, but just in case...
815
+ if (state == NULL) return;
816
+
817
+ if (event == RUBY_INTERNAL_EVENT_GC_ENTER) {
818
+ thread_context_collector_on_gc_start(state->thread_context_collector_instance);
819
+ } else if (event == RUBY_INTERNAL_EVENT_GC_EXIT) {
820
+ bool should_flush = thread_context_collector_on_gc_finish(state->thread_context_collector_instance);
821
+
822
+ // We use rb_postponed_job_register_one to ask Ruby to run thread_context_collector_sample_after_gc when the
823
+ // thread collector flags it's time to flush.
824
+ if (should_flush) {
825
+ #ifndef NO_POSTPONED_TRIGGER // Ruby 3.3+
826
+ rb_postponed_job_trigger(after_gc_from_postponed_job_handle);
827
+ #else
828
+ rb_postponed_job_register_one(0, after_gc_from_postponed_job, NULL);
829
+ #endif
830
+ }
831
+ }
832
+ }
833
+
834
+ static void after_gc_from_postponed_job(DDTRACE_UNUSED void *_unused) {
835
+ struct cpu_and_wall_time_worker_state *state = active_sampler_instance_state; // Read from global variable, see "sampler global state safety" note above
836
+
837
+ // This can potentially happen if the CpuAndWallTimeWorker was stopped while the postponed job was waiting to be executed; nothing to do
838
+ if (state == NULL) return;
839
+
840
+ // @ivoanjo: I'm not sure this can ever happen because `on_gc_event` only enqueues this callback if
841
+ // it's running on the main Ractor, but just in case...
842
+ if (!ddtrace_rb_ractor_main_p()) {
843
+ return; // We're not on the main Ractor; we currently don't support profiling non-main Ractors
844
+ }
845
+
846
+ state->during_sample = true;
847
+
848
+ // Trigger sampling using the Collectors::ThreadState; rescue against any exceptions that happen during sampling
849
+ safely_call(thread_context_collector_sample_after_gc, state->thread_context_collector_instance, state->self_instance);
850
+
851
+ state->during_sample = false;
852
+ }
853
+
854
+ // Equivalent to Ruby begin/rescue call, where we call a C function and jump to the exception handler if an
855
+ // exception gets raised within
856
+ static VALUE safely_call(VALUE (*function_to_call_safely)(VALUE), VALUE function_to_call_safely_arg, VALUE instance) {
857
+ VALUE exception_handler_function_arg = instance;
858
+ return rb_rescue2(
859
+ function_to_call_safely,
860
+ function_to_call_safely_arg,
861
+ handle_sampling_failure,
862
+ exception_handler_function_arg,
863
+ rb_eException, // rb_eException is the base class of all Ruby exceptions
864
+ 0 // Required by API to be the last argument
865
+ );
866
+ }
867
+
868
+ // This method exists only to enable testing Datadog::Profiling::Collectors::CpuAndWallTimeWorker behavior using RSpec.
869
+ // It SHOULD NOT be used for other purposes.
870
+ static VALUE _native_simulate_handle_sampling_signal(DDTRACE_UNUSED VALUE self) {
871
+ handle_sampling_signal(0, NULL, NULL);
872
+ return Qtrue;
873
+ }
874
+
875
+ // This method exists only to enable testing Datadog::Profiling::Collectors::CpuAndWallTimeWorker behavior using RSpec.
876
+ // It SHOULD NOT be used for other purposes.
877
+ static VALUE _native_simulate_sample_from_postponed_job(DDTRACE_UNUSED VALUE self) {
878
+ sample_from_postponed_job(NULL);
879
+ return Qtrue;
880
+ }
881
+
882
+ // After the Ruby VM forks, this method gets called in the child process to clean up any leftover state from the parent.
883
+ //
884
+ // Assumption: This method gets called BEFORE restarting profiling. Note that profiling-related tracepoints may still
885
+ // be active, so we make sure to disable them before calling into anything else, so that there are no components
886
+ // attempting to trigger samples at the same time as the reset is done.
887
+ //
888
+ // In the future, if we add more other components with tracepoints, we will need to coordinate stopping all such
889
+ // tracepoints before doing the other cleaning steps.
890
+ static VALUE _native_reset_after_fork(DDTRACE_UNUSED VALUE self, VALUE instance) {
891
+ struct cpu_and_wall_time_worker_state *state;
892
+ TypedData_Get_Struct(instance, struct cpu_and_wall_time_worker_state, &cpu_and_wall_time_worker_typed_data, state);
893
+
894
+ // Disable all tracepoints, so that there are no more attempts to mutate the profile
895
+ disable_tracepoints(state);
896
+
897
+ reset_stats_not_thread_safe(state);
898
+
899
+ // Remove all state from the `Collectors::ThreadState` and connected downstream components
900
+ rb_funcall(state->thread_context_collector_instance, rb_intern("reset_after_fork"), 0);
901
+
902
+ return Qtrue;
903
+ }
904
+
905
+ static VALUE _native_is_sigprof_blocked_in_current_thread(DDTRACE_UNUSED VALUE self) {
906
+ return is_sigprof_blocked_in_current_thread();
907
+ }
908
+
909
+ static VALUE _native_stats(DDTRACE_UNUSED VALUE self, VALUE instance) {
910
+ struct cpu_and_wall_time_worker_state *state;
911
+ TypedData_Get_Struct(instance, struct cpu_and_wall_time_worker_state, &cpu_and_wall_time_worker_typed_data, state);
912
+
913
+ VALUE pretty_cpu_sampling_time_ns_min = state->stats.cpu_sampling_time_ns_min == UINT64_MAX ? Qnil : ULL2NUM(state->stats.cpu_sampling_time_ns_min);
914
+ VALUE pretty_cpu_sampling_time_ns_max = state->stats.cpu_sampling_time_ns_max == 0 ? Qnil : ULL2NUM(state->stats.cpu_sampling_time_ns_max);
915
+ VALUE pretty_cpu_sampling_time_ns_total = state->stats.cpu_sampling_time_ns_total == 0 ? Qnil : ULL2NUM(state->stats.cpu_sampling_time_ns_total);
916
+ VALUE pretty_cpu_sampling_time_ns_avg =
917
+ state->stats.cpu_sampled == 0 ? Qnil : DBL2NUM(((double) state->stats.cpu_sampling_time_ns_total) / state->stats.cpu_sampled);
918
+
919
+ VALUE pretty_allocation_sampling_time_ns_min = state->stats.allocation_sampling_time_ns_min == UINT64_MAX ? Qnil : ULL2NUM(state->stats.allocation_sampling_time_ns_min);
920
+ VALUE pretty_allocation_sampling_time_ns_max = state->stats.allocation_sampling_time_ns_max == 0 ? Qnil : ULL2NUM(state->stats.allocation_sampling_time_ns_max);
921
+ VALUE pretty_allocation_sampling_time_ns_total = state->stats.allocation_sampling_time_ns_total == 0 ? Qnil : ULL2NUM(state->stats.allocation_sampling_time_ns_total);
922
+ VALUE pretty_allocation_sampling_time_ns_avg =
923
+ state->stats.allocation_sampled == 0 ? Qnil : DBL2NUM(((double) state->stats.allocation_sampling_time_ns_total) / state->stats.allocation_sampled);
924
+
925
+ unsigned long total_cpu_samples_attempted = state->stats.cpu_sampled + state->stats.cpu_skipped;
926
+ VALUE effective_cpu_sample_rate =
927
+ total_cpu_samples_attempted == 0 ? Qnil : DBL2NUM(((double) state->stats.cpu_sampled) / total_cpu_samples_attempted);
928
+ unsigned long total_allocation_samples_attempted = state->stats.allocation_sampled + state->stats.allocation_skipped;
929
+ VALUE effective_allocation_sample_rate =
930
+ total_allocation_samples_attempted == 0 ? Qnil : DBL2NUM(((double) state->stats.allocation_sampled) / total_allocation_samples_attempted);
931
+
932
+ VALUE allocation_sampler_snapshot = state->allocation_profiling_enabled && state->dynamic_sampling_rate_enabled ?
933
+ discrete_dynamic_sampler_state_snapshot(&state->allocation_sampler) : Qnil;
934
+
935
+ VALUE stats_as_hash = rb_hash_new();
936
+ VALUE arguments[] = {
937
+ ID2SYM(rb_intern("trigger_sample_attempts")), /* => */ UINT2NUM(state->stats.trigger_sample_attempts),
938
+ ID2SYM(rb_intern("trigger_simulated_signal_delivery_attempts")), /* => */ UINT2NUM(state->stats.trigger_simulated_signal_delivery_attempts),
939
+ ID2SYM(rb_intern("simulated_signal_delivery")), /* => */ UINT2NUM(state->stats.simulated_signal_delivery),
940
+ ID2SYM(rb_intern("signal_handler_enqueued_sample")), /* => */ UINT2NUM(state->stats.signal_handler_enqueued_sample),
941
+ ID2SYM(rb_intern("signal_handler_wrong_thread")), /* => */ UINT2NUM(state->stats.signal_handler_wrong_thread),
942
+ ID2SYM(rb_intern("postponed_job_skipped_already_existed")), /* => */ UINT2NUM(state->stats.postponed_job_skipped_already_existed),
943
+ ID2SYM(rb_intern("postponed_job_success")), /* => */ UINT2NUM(state->stats.postponed_job_success),
944
+ ID2SYM(rb_intern("postponed_job_full")), /* => */ UINT2NUM(state->stats.postponed_job_full),
945
+ ID2SYM(rb_intern("postponed_job_unknown_result")), /* => */ UINT2NUM(state->stats.postponed_job_unknown_result),
946
+
947
+ // CPU Stats
948
+ ID2SYM(rb_intern("cpu_sampled")), /* => */ UINT2NUM(state->stats.cpu_sampled),
949
+ ID2SYM(rb_intern("cpu_skipped")), /* => */ UINT2NUM(state->stats.cpu_skipped),
950
+ ID2SYM(rb_intern("cpu_effective_sample_rate")), /* => */ effective_cpu_sample_rate,
951
+ ID2SYM(rb_intern("cpu_sampling_time_ns_min")), /* => */ pretty_cpu_sampling_time_ns_min,
952
+ ID2SYM(rb_intern("cpu_sampling_time_ns_max")), /* => */ pretty_cpu_sampling_time_ns_max,
953
+ ID2SYM(rb_intern("cpu_sampling_time_ns_total")), /* => */ pretty_cpu_sampling_time_ns_total,
954
+ ID2SYM(rb_intern("cpu_sampling_time_ns_avg")), /* => */ pretty_cpu_sampling_time_ns_avg,
955
+
956
+ // Allocation stats
957
+ ID2SYM(rb_intern("allocation_sampled")), /* => */ state->allocation_profiling_enabled ? ULONG2NUM(state->stats.allocation_sampled) : Qnil,
958
+ ID2SYM(rb_intern("allocation_skipped")), /* => */ state->allocation_profiling_enabled ? ULONG2NUM(state->stats.allocation_skipped) : Qnil,
959
+ ID2SYM(rb_intern("allocation_effective_sample_rate")), /* => */ effective_allocation_sample_rate,
960
+ ID2SYM(rb_intern("allocation_sampling_time_ns_min")), /* => */ pretty_allocation_sampling_time_ns_min,
961
+ ID2SYM(rb_intern("allocation_sampling_time_ns_max")), /* => */ pretty_allocation_sampling_time_ns_max,
962
+ ID2SYM(rb_intern("allocation_sampling_time_ns_total")), /* => */ pretty_allocation_sampling_time_ns_total,
963
+ ID2SYM(rb_intern("allocation_sampling_time_ns_avg")), /* => */ pretty_allocation_sampling_time_ns_avg,
964
+ ID2SYM(rb_intern("allocation_sampler_snapshot")), /* => */ allocation_sampler_snapshot,
965
+ ID2SYM(rb_intern("allocations_during_sample")), /* => */ state->allocation_profiling_enabled ? UINT2NUM(state->stats.allocations_during_sample) : Qnil,
966
+ };
967
+ for (long unsigned int i = 0; i < VALUE_COUNT(arguments); i += 2) rb_hash_aset(stats_as_hash, arguments[i], arguments[i+1]);
968
+ return stats_as_hash;
969
+ }
970
+
971
+ static VALUE _native_stats_reset_not_thread_safe(DDTRACE_UNUSED VALUE self, VALUE instance) {
972
+ struct cpu_and_wall_time_worker_state *state;
973
+ TypedData_Get_Struct(instance, struct cpu_and_wall_time_worker_state, &cpu_and_wall_time_worker_typed_data, state);
974
+ reset_stats_not_thread_safe(state);
975
+ return Qnil;
976
+ }
977
+
978
+ void *simulate_sampling_signal_delivery(DDTRACE_UNUSED void *_unused) {
979
+ struct cpu_and_wall_time_worker_state *state = active_sampler_instance_state; // Read from global variable, see "sampler global state safety" note above
980
+
981
+ // This can potentially happen if the CpuAndWallTimeWorker was stopped while the IdleSamplingHelper was trying to execute this action
982
+ if (state == NULL) return NULL;
983
+
984
+ state->stats.simulated_signal_delivery++;
985
+
986
+ // @ivoanjo: We could instead directly call sample_from_postponed_job, but I chose to go through the signal handler
987
+ // so that the simulated case is as close to the original one as well (including any metrics increases, etc).
988
+ handle_sampling_signal(0, NULL, NULL);
989
+
990
+ return NULL; // Unused
991
+ }
992
+
993
+ static void grab_gvl_and_sample(void) { rb_thread_call_with_gvl(simulate_sampling_signal_delivery, NULL); }
994
+
995
+ static void reset_stats_not_thread_safe(struct cpu_and_wall_time_worker_state *state) {
996
+ // NOTE: This is not really thread safe so ongoing sampling operations that are concurrent with a reset can have their stats:
997
+ // * Lost (writes after stats retrieval but before reset).
998
+ // * Included in the previous stats window (writes before stats retrieval and reset).
999
+ // * Included in the following stats window (writes after stats retrieval and reset).
1000
+ // Given the expected infrequency of resetting (~once per 60s profile) and the auxiliary/non-critical nature of these stats
1001
+ // this momentary loss of accuracy is deemed acceptable to keep overhead to a minimum.
1002
+ state->stats = (struct stats) {
1003
+ .cpu_sampling_time_ns_min = UINT64_MAX, // Since we always take the min between existing and latest sample
1004
+ .allocation_sampling_time_ns_min = UINT64_MAX, // Since we always take the min between existing and latest sample
1005
+ };
1006
+ }
1007
+
1008
+ static void sleep_for(uint64_t time_ns) {
1009
+ // As a simplification, we currently only support setting .tv_nsec
1010
+ if (time_ns >= SECONDS_AS_NS(1)) {
1011
+ grab_gvl_and_raise(rb_eArgError, "sleep_for can only sleep for less than 1 second, time_ns: %"PRIu64, time_ns);
1012
+ }
1013
+
1014
+ struct timespec time_to_sleep = {.tv_nsec = time_ns};
1015
+
1016
+ while (nanosleep(&time_to_sleep, &time_to_sleep) != 0) {
1017
+ if (errno == EINTR) {
1018
+ // We were interrupted. nanosleep updates "time_to_sleep" to contain only the remaining time, so we just let the
1019
+ // loop keep going.
1020
+ } else {
1021
+ ENFORCE_SUCCESS_NO_GVL(errno);
1022
+ }
1023
+ }
1024
+ }
1025
+
1026
+ static VALUE _native_allocation_count(DDTRACE_UNUSED VALUE self) {
1027
+ bool are_allocations_being_tracked = active_sampler_instance_state != NULL && active_sampler_instance_state->allocation_profiling_enabled;
1028
+
1029
+ return are_allocations_being_tracked ? ULL2NUM(allocation_count) : Qnil;
1030
+ }
1031
+
1032
+ // Implements memory-related profiling events. This function is called by Ruby via the `object_allocation_tracepoint`
1033
+ // when the RUBY_INTERNAL_EVENT_NEWOBJ event is triggered.
1034
+ static void on_newobj_event(VALUE tracepoint_data, DDTRACE_UNUSED void *unused) {
1035
+ // Update thread-local allocation count
1036
+ if (RB_UNLIKELY(allocation_count == UINT64_MAX)) {
1037
+ allocation_count = 0;
1038
+ } else {
1039
+ allocation_count++;
1040
+ }
1041
+
1042
+ struct cpu_and_wall_time_worker_state *state = active_sampler_instance_state; // Read from global variable, see "sampler global state safety" note above
1043
+
1044
+ // This should not happen in a normal situation because the tracepoint is always enabled after the instance is set
1045
+ // and disabled before it is cleared, but just in case...
1046
+ if (state == NULL) return;
1047
+
1048
+ // In a few cases, we may actually be allocating an object as part of profiler sampling. We don't want to recursively
1049
+ // sample, so we just return early
1050
+ if (state->during_sample) {
1051
+ state->stats.allocations_during_sample++;
1052
+ return;
1053
+ }
1054
+
1055
+ if (state->dynamic_sampling_rate_enabled) {
1056
+ long now = monotonic_wall_time_now_ns(DO_NOT_RAISE_ON_FAILURE);
1057
+ if (now == 0) {
1058
+ delayed_error(state, ERR_CLOCK_FAIL);
1059
+ return;
1060
+ }
1061
+ if (!discrete_dynamic_sampler_should_sample(&state->allocation_sampler, now)) {
1062
+ state->stats.allocation_skipped++;
1063
+ return;
1064
+ }
1065
+ }
1066
+
1067
+ // @ivoanjo: Strictly speaking, this is not needed because Ruby should not call the same tracepoint while a previous
1068
+ // invocation is still pending, (e.g. it wouldn't call `on_newobj_event` while it's already running), but I decided
1069
+ // to keep this here for consistency -- every call to the thread context (other than the special gc calls which are
1070
+ // defined as not being able to allocate) sets this.
1071
+ state->during_sample = true;
1072
+
1073
+ // Rescue against any exceptions that happen during sampling
1074
+ safely_call(rescued_sample_allocation, tracepoint_data, state->self_instance);
1075
+
1076
+ if (state->dynamic_sampling_rate_enabled) {
1077
+ long now = monotonic_wall_time_now_ns(DO_NOT_RAISE_ON_FAILURE);
1078
+ if (now == 0) {
1079
+ delayed_error(state, ERR_CLOCK_FAIL);
1080
+ // NOTE: Not short-circuiting here to make sure cleanup happens
1081
+ }
1082
+ uint64_t sampling_time_ns = discrete_dynamic_sampler_after_sample(&state->allocation_sampler, now);
1083
+ // NOTE: To keep things lean when dynamic sampling rate is disabled we skip clock interactions which is
1084
+ // why we're fine with having this inside this conditional.
1085
+ state->stats.allocation_sampling_time_ns_min = uint64_min_of(sampling_time_ns, state->stats.allocation_sampling_time_ns_min);
1086
+ state->stats.allocation_sampling_time_ns_max = uint64_max_of(sampling_time_ns, state->stats.allocation_sampling_time_ns_max);
1087
+ state->stats.allocation_sampling_time_ns_total += sampling_time_ns;
1088
+ }
1089
+
1090
+ state->stats.allocation_sampled++;
1091
+
1092
+ state->during_sample = false;
1093
+ }
1094
+
1095
+ static void disable_tracepoints(struct cpu_and_wall_time_worker_state *state) {
1096
+ if (state->gc_tracepoint != Qnil) {
1097
+ rb_tracepoint_disable(state->gc_tracepoint);
1098
+ }
1099
+ if (state->object_allocation_tracepoint != Qnil) {
1100
+ rb_tracepoint_disable(state->object_allocation_tracepoint);
1101
+ }
1102
+ }
1103
+
1104
+ static VALUE _native_with_blocked_sigprof(DDTRACE_UNUSED VALUE self) {
1105
+ block_sigprof_signal_handler_from_running_in_current_thread();
1106
+ int exception_state;
1107
+ VALUE result = rb_protect(rb_yield, Qundef, &exception_state);
1108
+ unblock_sigprof_signal_handler_from_running_in_current_thread();
1109
+
1110
+ if (exception_state) {
1111
+ rb_jump_tag(exception_state);
1112
+ } else {
1113
+ return result;
1114
+ }
1115
+ }
1116
+
1117
+ static VALUE rescued_sample_allocation(VALUE tracepoint_data) {
1118
+ struct cpu_and_wall_time_worker_state *state = active_sampler_instance_state; // Read from global variable, see "sampler global state safety" note above
1119
+
1120
+ // This should not happen in a normal situation because on_newobj_event already checked for this, but just in case...
1121
+ if (state == NULL) return Qnil;
1122
+
1123
+ rb_trace_arg_t *data = rb_tracearg_from_tracepoint(tracepoint_data);
1124
+ VALUE new_object = rb_tracearg_object(data);
1125
+
1126
+ unsigned long allocations_since_last_sample = state->dynamic_sampling_rate_enabled ?
1127
+ // if we're doing dynamic sampling, ask the sampler how many events since last sample
1128
+ discrete_dynamic_sampler_events_since_last_sample(&state->allocation_sampler) :
1129
+ // if we aren't, then we're sampling every event
1130
+ 1;
1131
+ // TODO: Signal in the profile that clamping happened?
1132
+ unsigned int weight = allocations_since_last_sample > MAX_ALLOC_WEIGHT ? MAX_ALLOC_WEIGHT : (unsigned int) allocations_since_last_sample;
1133
+ thread_context_collector_sample_allocation(state->thread_context_collector_instance, weight, new_object);
1134
+
1135
+ // Return a dummy VALUE because we're called from rb_rescue2 which requires it
1136
+ return Qnil;
1137
+ }
1138
+
1139
+ static void delayed_error(struct cpu_and_wall_time_worker_state *state, const char *error) {
1140
+ // If we can't raise an immediate exception at the calling site, use the asynchronous flow through the main worker loop.
1141
+ stop_state(state, rb_exc_new_cstr(rb_eRuntimeError, error));
1142
+ }
1143
+
1144
+ static VALUE _native_delayed_error(DDTRACE_UNUSED VALUE self, VALUE instance, VALUE error_msg) {
1145
+ ENFORCE_TYPE(error_msg, T_STRING);
1146
+
1147
+ struct cpu_and_wall_time_worker_state *state;
1148
+ TypedData_Get_Struct(instance, struct cpu_and_wall_time_worker_state, &cpu_and_wall_time_worker_typed_data, state);
1149
+
1150
+ delayed_error(state, rb_string_value_cstr(&error_msg));
1151
+
1152
+ return Qnil;
1153
+ }