datadog 2.0.0.beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (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,1391 @@
1
+ #include <ruby.h>
2
+
3
+ #include "collectors_thread_context.h"
4
+ #include "clock_id.h"
5
+ #include "collectors_stack.h"
6
+ #include "collectors_gc_profiling_helper.h"
7
+ #include "helpers.h"
8
+ #include "libdatadog_helpers.h"
9
+ #include "private_vm_api_access.h"
10
+ #include "stack_recorder.h"
11
+ #include "time_helpers.h"
12
+
13
+ // Used to trigger sampling of threads, based on external "events", such as:
14
+ // * periodic timer for cpu-time and wall-time
15
+ // * VM garbage collection events
16
+ // * VM object allocation events
17
+ //
18
+ // This file implements the native bits of the Datadog::Profiling::Collectors::ThreadContext class
19
+ //
20
+ // Triggering of this component (e.g. watching for the above "events") is implemented by Collectors::CpuAndWallTimeWorker.
21
+
22
+ // ---
23
+ // ## Tracking of cpu-time and wall-time spent during garbage collection
24
+ //
25
+ // This feature works by having a special state that a thread can be in: doing garbage collection. This state is
26
+ // tracked inside the thread's `per_thread_context.gc_tracking` data, and three functions, listed below. The functions
27
+ // will get called by the `Collectors::CpuAndWallTimeWorker` at very specific times in the VM lifetime.
28
+ //
29
+ // * `thread_context_collector_on_gc_start`: Called at the very beginning of the garbage collection process.
30
+ // The internal VM `during_gc` flag is set to `true`, but Ruby has not done any work yet.
31
+ // * `thread_context_collector_on_gc_finish`: Called at the very end of the garbage collection process.
32
+ // The internal VM `during_gc` flag is still set to `true`, but all the work has been done.
33
+ // * `thread_context_collector_sample_after_gc`: Called shortly after the garbage collection process.
34
+ // The internal VM `during_gc` flag is set to `false`.
35
+ //
36
+ // Inside this component, here's what happens inside those three functions:
37
+ //
38
+ // When `thread_context_collector_on_gc_start` gets called, the current cpu and wall-time get recorded to the thread
39
+ // context: `cpu_time_at_gc_start_ns` and `wall_time_at_gc_start_ns`.
40
+ //
41
+ // While `cpu_time_at_gc_start_ns` is set, regular samples (if any) do not account for cpu-time any time that passes
42
+ // after this timestamp. The idea is that this cpu-time will be blamed separately on GC, and not on the user thread.
43
+ // Wall-time accounting is not affected by this (e.g. we still record 60 seconds every 60 seconds).
44
+ //
45
+ // (Regular samples can still account for the cpu-time between the previous sample and the start of GC.)
46
+ //
47
+ // When `thread_context_collector_on_gc_finish` gets called, the cpu-time and wall-time spent during GC gets recorded
48
+ // into the global gc_tracking structure, and further samples are not affected. (The `cpu_time_at_previous_sample_ns`
49
+ // of the thread that did GC also gets adjusted to avoid double-accounting.)
50
+ //
51
+ // Finally, when `thread_context_collector_sample_after_gc` gets called, a sample gets recorded with a stack having
52
+ // a single placeholder `Garbage Collection` frame. This sample gets
53
+ // assigned the cpu-time and wall-time that was recorded between calls to `on_gc_start` and `on_gc_finish`, as well
54
+ // as metadata for the last GC.
55
+ //
56
+ // Note that the Ruby GC does not usually do all of the GC work in one go. Instead, it breaks it up into smaller steps
57
+ // so that the application can keep doing user work in between GC steps.
58
+ // The `on_gc_start` / `on_gc_finish` will trigger each time the VM executes these smaller steps, and on a benchmark
59
+ // that executes `Object.new` in a loop, I measured more than 50k of this steps per second (!!).
60
+ // Creating these many events for every GC step is a lot of overhead, so instead `on_gc_finish` coalesces time
61
+ // spent in GC and only flushes it at most every 10 ms/every complete GC collection. This reduces the amount of
62
+ // individual GC events we need to record. We use the latest GC metadata for this event, reflecting the last GC that
63
+ // happened in the coalesced period.
64
+ //
65
+ // In an earlier attempt at implementing this functionality (https://github.com/DataDog/dd-trace-rb/pull/2308), we
66
+ // discovered that we needed to factor the sampling work away from `thread_context_collector_on_gc_finish` and into a
67
+ // separate `thread_context_collector_sample_after_gc` because (as documented in more detail below),
68
+ // `sample_after_gc` could trigger memory allocation in rare occasions (usually exceptions), which is actually not
69
+ // allowed to happen during Ruby's garbage collection start/finish hooks.
70
+ // ---
71
+
72
+ #define THREAD_ID_LIMIT_CHARS 44 // Why 44? "#{2**64} (#{2**64})".size + 1 for \0
73
+ #define THREAD_INVOKE_LOCATION_LIMIT_CHARS 512
74
+ #define IS_WALL_TIME true
75
+ #define IS_NOT_WALL_TIME false
76
+ #define MISSING_TRACER_CONTEXT_KEY 0
77
+ #define TIME_BETWEEN_GC_EVENTS_NS MILLIS_AS_NS(10)
78
+
79
+ static ID at_active_span_id; // id of :@active_span in Ruby
80
+ static ID at_active_trace_id; // id of :@active_trace in Ruby
81
+ static ID at_id_id; // id of :@id in Ruby
82
+ static ID at_resource_id; // id of :@resource in Ruby
83
+ static ID at_root_span_id; // id of :@root_span in Ruby
84
+ static ID at_type_id; // id of :@type in Ruby
85
+ static ID at_otel_values_id; // id of :@otel_values in Ruby
86
+ static ID at_parent_span_id_id; // id of :@parent_span_id in Ruby
87
+ static ID at_datadog_trace_id; // id of :@datadog_trace in Ruby
88
+
89
+ // Contains state for a single ThreadContext instance
90
+ struct thread_context_collector_state {
91
+ // Note: Places in this file that usually need to be changed when this struct is changed are tagged with
92
+ // "Update this when modifying state struct"
93
+
94
+ // Required by Datadog::Profiling::Collectors::Stack as a scratch buffer during sampling
95
+ sampling_buffer *sampling_buffer;
96
+ // Hashmap <Thread Object, struct per_thread_context>
97
+ st_table *hash_map_per_thread_context;
98
+ // Datadog::Profiling::StackRecorder instance
99
+ VALUE recorder_instance;
100
+ // If the tracer is available and enabled, this will be the fiber-local symbol for accessing its running context,
101
+ // to enable code hotspots and endpoint aggregation.
102
+ // When not available, this is set to MISSING_TRACER_CONTEXT_KEY.
103
+ ID tracer_context_key;
104
+ // Track how many regular samples we've taken. Does not include garbage collection samples.
105
+ // Currently **outside** of stats struct because we also use it to decide when to clean the contexts, and thus this
106
+ // is not (just) a stat.
107
+ unsigned int sample_count;
108
+ // Reusable array to get list of threads
109
+ VALUE thread_list_buffer;
110
+ // Used to omit endpoint names (retrieved from tracer) from collected data
111
+ bool endpoint_collection_enabled;
112
+ // Used to omit timestamps / timeline events from collected data
113
+ bool timeline_enabled;
114
+ // Used to omit class information from collected allocation data
115
+ bool allocation_type_enabled;
116
+ // Used when calling monotonic_to_system_epoch_ns
117
+ monotonic_to_system_epoch_state time_converter_state;
118
+ // Used to identify the main thread, to give it a fallback name
119
+ VALUE main_thread;
120
+ // Used when extracting trace identifiers from otel spans. Lazily initialized.
121
+ VALUE otel_current_span_key;
122
+
123
+ struct stats {
124
+ // Track how many garbage collection samples we've taken.
125
+ unsigned int gc_samples;
126
+ // See thread_context_collector_on_gc_start for details
127
+ unsigned int gc_samples_missed_due_to_missing_context;
128
+ } stats;
129
+
130
+ struct {
131
+ unsigned long accumulated_cpu_time_ns;
132
+ unsigned long accumulated_wall_time_ns;
133
+
134
+ long wall_time_at_previous_gc_ns; // Will be INVALID_TIME unless there's accumulated time above
135
+ long wall_time_at_last_flushed_gc_event_ns; // Starts at 0 and then will always be valid
136
+ } gc_tracking;
137
+ };
138
+
139
+ // Tracks per-thread state
140
+ struct per_thread_context {
141
+ char thread_id[THREAD_ID_LIMIT_CHARS];
142
+ ddog_CharSlice thread_id_char_slice;
143
+ char thread_invoke_location[THREAD_INVOKE_LOCATION_LIMIT_CHARS];
144
+ ddog_CharSlice thread_invoke_location_char_slice;
145
+ thread_cpu_time_id thread_cpu_time_id;
146
+ long cpu_time_at_previous_sample_ns; // Can be INVALID_TIME until initialized or if getting it fails for another reason
147
+ long wall_time_at_previous_sample_ns; // Can be INVALID_TIME until initialized
148
+
149
+ struct {
150
+ // Both of these fields are set by on_gc_start and kept until on_gc_finish is called.
151
+ // Outside of this window, they will be INVALID_TIME.
152
+ long cpu_time_at_start_ns;
153
+ long wall_time_at_start_ns;
154
+ } gc_tracking;
155
+ };
156
+
157
+ // Used to correlate profiles with traces
158
+ struct trace_identifiers {
159
+ bool valid;
160
+ uint64_t local_root_span_id;
161
+ uint64_t span_id;
162
+ VALUE trace_endpoint;
163
+ };
164
+
165
+ static void thread_context_collector_typed_data_mark(void *state_ptr);
166
+ static void thread_context_collector_typed_data_free(void *state_ptr);
167
+ static int hash_map_per_thread_context_mark(st_data_t key_thread, st_data_t _value, st_data_t _argument);
168
+ static int hash_map_per_thread_context_free_values(st_data_t _thread, st_data_t value_per_thread_context, st_data_t _argument);
169
+ static VALUE _native_new(VALUE klass);
170
+ static VALUE _native_initialize(
171
+ VALUE self,
172
+ VALUE collector_instance,
173
+ VALUE recorder_instance,
174
+ VALUE max_frames,
175
+ VALUE tracer_context_key,
176
+ VALUE endpoint_collection_enabled,
177
+ VALUE timeline_enabled,
178
+ VALUE allocation_type_enabled
179
+ );
180
+ static VALUE _native_sample(VALUE self, VALUE collector_instance, VALUE profiler_overhead_stack_thread);
181
+ static VALUE _native_on_gc_start(VALUE self, VALUE collector_instance);
182
+ static VALUE _native_on_gc_finish(VALUE self, VALUE collector_instance);
183
+ static VALUE _native_sample_after_gc(DDTRACE_UNUSED VALUE self, VALUE collector_instance);
184
+ void update_metrics_and_sample(
185
+ struct thread_context_collector_state *state,
186
+ VALUE thread_being_sampled,
187
+ VALUE profiler_overhead_stack_thread,
188
+ struct per_thread_context *thread_context,
189
+ long current_cpu_time_ns,
190
+ long current_monotonic_wall_time_ns
191
+ );
192
+ static void trigger_sample_for_thread(
193
+ struct thread_context_collector_state *state,
194
+ VALUE thread,
195
+ VALUE stack_from_thread,
196
+ struct per_thread_context *thread_context,
197
+ sample_values values,
198
+ long current_monotonic_wall_time_ns,
199
+ ddog_CharSlice *ruby_vm_type,
200
+ ddog_CharSlice *class_name
201
+ );
202
+ static VALUE _native_thread_list(VALUE self);
203
+ static struct per_thread_context *get_or_create_context_for(VALUE thread, struct thread_context_collector_state *state);
204
+ static struct per_thread_context *get_context_for(VALUE thread, struct thread_context_collector_state *state);
205
+ static void initialize_context(VALUE thread, struct per_thread_context *thread_context, struct thread_context_collector_state *state);
206
+ static VALUE _native_inspect(VALUE self, VALUE collector_instance);
207
+ static VALUE per_thread_context_st_table_as_ruby_hash(struct thread_context_collector_state *state);
208
+ static int per_thread_context_as_ruby_hash(st_data_t key_thread, st_data_t value_context, st_data_t result_hash);
209
+ static VALUE stats_as_ruby_hash(struct thread_context_collector_state *state);
210
+ static VALUE gc_tracking_as_ruby_hash(struct thread_context_collector_state *state);
211
+ static void remove_context_for_dead_threads(struct thread_context_collector_state *state);
212
+ static int remove_if_dead_thread(st_data_t key_thread, st_data_t value_context, st_data_t _argument);
213
+ static VALUE _native_per_thread_context(VALUE self, VALUE collector_instance);
214
+ static long update_time_since_previous_sample(long *time_at_previous_sample_ns, long current_time_ns, long gc_start_time_ns, bool is_wall_time);
215
+ static long cpu_time_now_ns(struct per_thread_context *thread_context);
216
+ static long thread_id_for(VALUE thread);
217
+ static VALUE _native_stats(VALUE self, VALUE collector_instance);
218
+ static VALUE _native_gc_tracking(VALUE self, VALUE collector_instance);
219
+ static void trace_identifiers_for(struct thread_context_collector_state *state, VALUE thread, struct trace_identifiers *trace_identifiers_result);
220
+ static bool should_collect_resource(VALUE root_span_type);
221
+ static VALUE _native_reset_after_fork(DDTRACE_UNUSED VALUE self, VALUE collector_instance);
222
+ static VALUE thread_list(struct thread_context_collector_state *state);
223
+ static VALUE _native_sample_allocation(DDTRACE_UNUSED VALUE self, VALUE collector_instance, VALUE sample_weight, VALUE new_object);
224
+ static VALUE _native_new_empty_thread(VALUE self);
225
+ static ddog_CharSlice ruby_value_type_to_class_name(enum ruby_value_type type);
226
+ static void ddtrace_otel_trace_identifiers_for(
227
+ struct thread_context_collector_state *state,
228
+ VALUE *active_trace,
229
+ VALUE *root_span,
230
+ VALUE *numeric_span_id,
231
+ VALUE active_span,
232
+ VALUE otel_values
233
+ );
234
+
235
+ void collectors_thread_context_init(VALUE profiling_module) {
236
+ VALUE collectors_module = rb_define_module_under(profiling_module, "Collectors");
237
+ VALUE collectors_thread_context_class = rb_define_class_under(collectors_module, "ThreadContext", rb_cObject);
238
+ // Hosts methods used for testing the native code using RSpec
239
+ VALUE testing_module = rb_define_module_under(collectors_thread_context_class, "Testing");
240
+
241
+ // Instances of the ThreadContext class are "TypedData" objects.
242
+ // "TypedData" objects are special objects in the Ruby VM that can wrap C structs.
243
+ // In this case, it wraps the thread_context_collector_state.
244
+ //
245
+ // Because Ruby doesn't know how to initialize native-level structs, we MUST override the allocation function for objects
246
+ // of this class so that we can manage this part. Not overriding or disabling the allocation function is a common
247
+ // gotcha for "TypedData" objects that can very easily lead to VM crashes, see for instance
248
+ // https://bugs.ruby-lang.org/issues/18007 for a discussion around this.
249
+ rb_define_alloc_func(collectors_thread_context_class, _native_new);
250
+
251
+ rb_define_singleton_method(collectors_thread_context_class, "_native_initialize", _native_initialize, 7);
252
+ rb_define_singleton_method(collectors_thread_context_class, "_native_inspect", _native_inspect, 1);
253
+ rb_define_singleton_method(collectors_thread_context_class, "_native_reset_after_fork", _native_reset_after_fork, 1);
254
+ rb_define_singleton_method(testing_module, "_native_sample", _native_sample, 2);
255
+ rb_define_singleton_method(testing_module, "_native_sample_allocation", _native_sample_allocation, 3);
256
+ rb_define_singleton_method(testing_module, "_native_on_gc_start", _native_on_gc_start, 1);
257
+ rb_define_singleton_method(testing_module, "_native_on_gc_finish", _native_on_gc_finish, 1);
258
+ rb_define_singleton_method(testing_module, "_native_sample_after_gc", _native_sample_after_gc, 1);
259
+ rb_define_singleton_method(testing_module, "_native_thread_list", _native_thread_list, 0);
260
+ rb_define_singleton_method(testing_module, "_native_per_thread_context", _native_per_thread_context, 1);
261
+ rb_define_singleton_method(testing_module, "_native_stats", _native_stats, 1);
262
+ rb_define_singleton_method(testing_module, "_native_gc_tracking", _native_gc_tracking, 1);
263
+ rb_define_singleton_method(testing_module, "_native_new_empty_thread", _native_new_empty_thread, 0);
264
+
265
+ at_active_span_id = rb_intern_const("@active_span");
266
+ at_active_trace_id = rb_intern_const("@active_trace");
267
+ at_id_id = rb_intern_const("@id");
268
+ at_resource_id = rb_intern_const("@resource");
269
+ at_root_span_id = rb_intern_const("@root_span");
270
+ at_type_id = rb_intern_const("@type");
271
+ at_otel_values_id = rb_intern_const("@otel_values");
272
+ at_parent_span_id_id = rb_intern_const("@parent_span_id");
273
+ at_datadog_trace_id = rb_intern_const("@datadog_trace");
274
+
275
+ gc_profiling_init();
276
+ }
277
+
278
+ // This structure is used to define a Ruby object that stores a pointer to a struct thread_context_collector_state
279
+ // See also https://github.com/ruby/ruby/blob/master/doc/extension.rdoc for how this works
280
+ static const rb_data_type_t thread_context_collector_typed_data = {
281
+ .wrap_struct_name = "Datadog::Profiling::Collectors::ThreadContext",
282
+ .function = {
283
+ .dmark = thread_context_collector_typed_data_mark,
284
+ .dfree = thread_context_collector_typed_data_free,
285
+ .dsize = NULL, // We don't track profile memory usage (although it'd be cool if we did!)
286
+ //.dcompact = NULL, // FIXME: Add support for compaction
287
+ },
288
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY
289
+ };
290
+
291
+ // This function is called by the Ruby GC to give us a chance to mark any Ruby objects that we're holding on to,
292
+ // so that they don't get garbage collected
293
+ static void thread_context_collector_typed_data_mark(void *state_ptr) {
294
+ struct thread_context_collector_state *state = (struct thread_context_collector_state *) state_ptr;
295
+
296
+ // Update this when modifying state struct
297
+ rb_gc_mark(state->recorder_instance);
298
+ st_foreach(state->hash_map_per_thread_context, hash_map_per_thread_context_mark, 0 /* unused */);
299
+ rb_gc_mark(state->thread_list_buffer);
300
+ rb_gc_mark(state->main_thread);
301
+ rb_gc_mark(state->otel_current_span_key);
302
+ }
303
+
304
+ static void thread_context_collector_typed_data_free(void *state_ptr) {
305
+ struct thread_context_collector_state *state = (struct thread_context_collector_state *) state_ptr;
306
+
307
+ // Update this when modifying state struct
308
+
309
+ // Important: Remember that we're only guaranteed to see here what's been set in _native_new, aka
310
+ // pointers that have been set NULL there may still be NULL here.
311
+ if (state->sampling_buffer != NULL) sampling_buffer_free(state->sampling_buffer);
312
+
313
+ // Free each entry in the map
314
+ st_foreach(state->hash_map_per_thread_context, hash_map_per_thread_context_free_values, 0 /* unused */);
315
+ // ...and then the map
316
+ st_free_table(state->hash_map_per_thread_context);
317
+
318
+ ruby_xfree(state);
319
+ }
320
+
321
+ // Mark Ruby thread references we keep as keys in hash_map_per_thread_context
322
+ static int hash_map_per_thread_context_mark(st_data_t key_thread, DDTRACE_UNUSED st_data_t _value, DDTRACE_UNUSED st_data_t _argument) {
323
+ VALUE thread = (VALUE) key_thread;
324
+ rb_gc_mark(thread);
325
+ return ST_CONTINUE;
326
+ }
327
+
328
+ // Used to clear each of the per_thread_contexts inside the hash_map_per_thread_context
329
+ static int hash_map_per_thread_context_free_values(DDTRACE_UNUSED st_data_t _thread, st_data_t value_per_thread_context, DDTRACE_UNUSED st_data_t _argument) {
330
+ struct per_thread_context *per_thread_context = (struct per_thread_context*) value_per_thread_context;
331
+ ruby_xfree(per_thread_context);
332
+ return ST_CONTINUE;
333
+ }
334
+
335
+ static VALUE _native_new(VALUE klass) {
336
+ struct thread_context_collector_state *state = ruby_xcalloc(1, sizeof(struct thread_context_collector_state));
337
+
338
+ // Note: Any exceptions raised from this note until the TypedData_Wrap_Struct call will lead to the state memory
339
+ // being leaked.
340
+
341
+ // Update this when modifying state struct
342
+ state->sampling_buffer = NULL;
343
+ state->hash_map_per_thread_context =
344
+ // "numtable" is an awful name, but TL;DR it's what should be used when keys are `VALUE`s.
345
+ st_init_numtable();
346
+ state->recorder_instance = Qnil;
347
+ state->tracer_context_key = MISSING_TRACER_CONTEXT_KEY;
348
+ state->thread_list_buffer = rb_ary_new();
349
+ state->endpoint_collection_enabled = true;
350
+ state->timeline_enabled = true;
351
+ state->allocation_type_enabled = true;
352
+ state->time_converter_state = (monotonic_to_system_epoch_state) MONOTONIC_TO_SYSTEM_EPOCH_INITIALIZER;
353
+ state->main_thread = rb_thread_main();
354
+ state->otel_current_span_key = Qnil;
355
+ state->gc_tracking.wall_time_at_previous_gc_ns = INVALID_TIME;
356
+ state->gc_tracking.wall_time_at_last_flushed_gc_event_ns = 0;
357
+
358
+ return TypedData_Wrap_Struct(klass, &thread_context_collector_typed_data, state);
359
+ }
360
+
361
+ static VALUE _native_initialize(
362
+ DDTRACE_UNUSED VALUE _self,
363
+ VALUE collector_instance,
364
+ VALUE recorder_instance,
365
+ VALUE max_frames,
366
+ VALUE tracer_context_key,
367
+ VALUE endpoint_collection_enabled,
368
+ VALUE timeline_enabled,
369
+ VALUE allocation_type_enabled
370
+ ) {
371
+ ENFORCE_BOOLEAN(endpoint_collection_enabled);
372
+ ENFORCE_BOOLEAN(timeline_enabled);
373
+ ENFORCE_BOOLEAN(allocation_type_enabled);
374
+
375
+ struct thread_context_collector_state *state;
376
+ TypedData_Get_Struct(collector_instance, struct thread_context_collector_state, &thread_context_collector_typed_data, state);
377
+
378
+ int max_frames_requested = NUM2INT(max_frames);
379
+ if (max_frames_requested < 0) rb_raise(rb_eArgError, "Invalid max_frames: value must not be negative");
380
+
381
+ // Update this when modifying state struct
382
+ state->sampling_buffer = sampling_buffer_new(max_frames_requested);
383
+ // hash_map_per_thread_context is already initialized, nothing to do here
384
+ state->recorder_instance = enforce_recorder_instance(recorder_instance);
385
+ state->endpoint_collection_enabled = (endpoint_collection_enabled == Qtrue);
386
+ state->timeline_enabled = (timeline_enabled == Qtrue);
387
+ state->allocation_type_enabled = (allocation_type_enabled == Qtrue);
388
+
389
+ if (RTEST(tracer_context_key)) {
390
+ ENFORCE_TYPE(tracer_context_key, T_SYMBOL);
391
+ // Note about rb_to_id and dynamic symbols: calling `rb_to_id` prevents symbols from ever being garbage collected.
392
+ // In this case, we can't really escape this because as of this writing, ruby master still calls `rb_to_id` inside
393
+ // the implementation of Thread#[]= so any symbol that gets used as a key there will already be prevented from GC.
394
+ state->tracer_context_key = rb_to_id(tracer_context_key);
395
+ }
396
+
397
+ return Qtrue;
398
+ }
399
+
400
+ // This method exists only to enable testing Datadog::Profiling::Collectors::ThreadContext behavior using RSpec.
401
+ // It SHOULD NOT be used for other purposes.
402
+ static VALUE _native_sample(DDTRACE_UNUSED VALUE _self, VALUE collector_instance, VALUE profiler_overhead_stack_thread) {
403
+ if (!is_thread_alive(profiler_overhead_stack_thread)) rb_raise(rb_eArgError, "Unexpected: profiler_overhead_stack_thread is not alive");
404
+
405
+ thread_context_collector_sample(collector_instance, monotonic_wall_time_now_ns(RAISE_ON_FAILURE), profiler_overhead_stack_thread);
406
+ return Qtrue;
407
+ }
408
+
409
+ // This method exists only to enable testing Datadog::Profiling::Collectors::ThreadContext behavior using RSpec.
410
+ // It SHOULD NOT be used for other purposes.
411
+ static VALUE _native_on_gc_start(DDTRACE_UNUSED VALUE self, VALUE collector_instance) {
412
+ thread_context_collector_on_gc_start(collector_instance);
413
+ return Qtrue;
414
+ }
415
+
416
+ // This method exists only to enable testing Datadog::Profiling::Collectors::ThreadContext behavior using RSpec.
417
+ // It SHOULD NOT be used for other purposes.
418
+ static VALUE _native_on_gc_finish(DDTRACE_UNUSED VALUE self, VALUE collector_instance) {
419
+ thread_context_collector_on_gc_finish(collector_instance);
420
+ return Qtrue;
421
+ }
422
+
423
+ // This method exists only to enable testing Datadog::Profiling::Collectors::ThreadContext behavior using RSpec.
424
+ // It SHOULD NOT be used for other purposes.
425
+ static VALUE _native_sample_after_gc(DDTRACE_UNUSED VALUE self, VALUE collector_instance) {
426
+ thread_context_collector_sample_after_gc(collector_instance);
427
+ return Qtrue;
428
+ }
429
+
430
+ // This function gets called from the Collectors::CpuAndWallTimeWorker to trigger the actual sampling.
431
+ //
432
+ // Assumption 1: This function is called in a thread that is holding the Global VM Lock. Caller is responsible for enforcing this.
433
+ // Assumption 2: This function is allowed to raise exceptions. Caller is responsible for handling them, if needed.
434
+ // Assumption 3: This function IS NOT called from a signal handler. This function is not async-signal-safe.
435
+ // Assumption 4: This function IS NOT called in a reentrant way.
436
+ // Assumption 5: This function is called from the main Ractor (if Ruby has support for Ractors).
437
+ //
438
+ // The `profiler_overhead_stack_thread` is used to attribute the profiler overhead to a stack borrowed from a different thread
439
+ // (belonging to ddtrace), so that the overhead is visible in the profile rather than blamed on user code.
440
+ void thread_context_collector_sample(VALUE self_instance, long current_monotonic_wall_time_ns, VALUE profiler_overhead_stack_thread) {
441
+ struct thread_context_collector_state *state;
442
+ TypedData_Get_Struct(self_instance, struct thread_context_collector_state, &thread_context_collector_typed_data, state);
443
+
444
+ VALUE current_thread = rb_thread_current();
445
+ struct per_thread_context *current_thread_context = get_or_create_context_for(current_thread, state);
446
+ long cpu_time_at_sample_start_for_current_thread = cpu_time_now_ns(current_thread_context);
447
+
448
+ VALUE threads = thread_list(state);
449
+
450
+ const long thread_count = RARRAY_LEN(threads);
451
+ for (long i = 0; i < thread_count; i++) {
452
+ VALUE thread = RARRAY_AREF(threads, i);
453
+ struct per_thread_context *thread_context = get_or_create_context_for(thread, state);
454
+
455
+ // We account for cpu-time for the current thread in a different way -- we use the cpu-time at sampling start, to avoid
456
+ // blaming the time the profiler took on whatever's running on the thread right now
457
+ long current_cpu_time_ns = thread != current_thread ? cpu_time_now_ns(thread_context) : cpu_time_at_sample_start_for_current_thread;
458
+
459
+ update_metrics_and_sample(
460
+ state,
461
+ /* thread_being_sampled: */ thread,
462
+ /* stack_from_thread: */ thread,
463
+ thread_context,
464
+ current_cpu_time_ns,
465
+ current_monotonic_wall_time_ns
466
+ );
467
+ }
468
+
469
+ state->sample_count++;
470
+
471
+ // TODO: This seems somewhat overkill and inefficient to do often; right now we just do it every few samples
472
+ // but there's probably a better way to do this if we actually track when threads finish
473
+ if (state->sample_count % 100 == 0) remove_context_for_dead_threads(state);
474
+
475
+ update_metrics_and_sample(
476
+ state,
477
+ /* thread_being_sampled: */ current_thread,
478
+ /* stack_from_thread: */ profiler_overhead_stack_thread,
479
+ current_thread_context,
480
+ cpu_time_now_ns(current_thread_context),
481
+ monotonic_wall_time_now_ns(RAISE_ON_FAILURE)
482
+ );
483
+ }
484
+
485
+ void update_metrics_and_sample(
486
+ struct thread_context_collector_state *state,
487
+ VALUE thread_being_sampled,
488
+ VALUE stack_from_thread, // This can be different when attributing profiler overhead using a different stack
489
+ struct per_thread_context *thread_context,
490
+ long current_cpu_time_ns,
491
+ long current_monotonic_wall_time_ns
492
+ ) {
493
+ long cpu_time_elapsed_ns = update_time_since_previous_sample(
494
+ &thread_context->cpu_time_at_previous_sample_ns,
495
+ current_cpu_time_ns,
496
+ thread_context->gc_tracking.cpu_time_at_start_ns,
497
+ IS_NOT_WALL_TIME
498
+ );
499
+ long wall_time_elapsed_ns = update_time_since_previous_sample(
500
+ &thread_context->wall_time_at_previous_sample_ns,
501
+ current_monotonic_wall_time_ns,
502
+ // We explicitly pass in `INVALID_TIME` as an argument for `gc_start_time_ns` here because we don't want wall-time
503
+ // accounting to change during GC.
504
+ // E.g. if 60 seconds pass in the real world, 60 seconds of wall-time are recorded, regardless of the thread doing
505
+ // GC or not.
506
+ INVALID_TIME,
507
+ IS_WALL_TIME
508
+ );
509
+
510
+ trigger_sample_for_thread(
511
+ state,
512
+ thread_being_sampled,
513
+ stack_from_thread,
514
+ thread_context,
515
+ (sample_values) {.cpu_time_ns = cpu_time_elapsed_ns, .cpu_or_wall_samples = 1, .wall_time_ns = wall_time_elapsed_ns},
516
+ current_monotonic_wall_time_ns,
517
+ NULL,
518
+ NULL
519
+ );
520
+ }
521
+
522
+ // This function gets called when Ruby is about to start running the Garbage Collector on the current thread.
523
+ // It updates the per_thread_context of the current thread to include the current cpu/wall times, to be used to later
524
+ // create an event including the cpu/wall time spent in garbage collector work.
525
+ //
526
+ // Safety: This function gets called while Ruby is doing garbage collection. While Ruby is doing garbage collection,
527
+ // *NO ALLOCATION* is allowed. This function, and any it calls must never trigger memory or object allocation.
528
+ // This includes exceptions and use of ruby_xcalloc (because xcalloc can trigger GC)!
529
+ //
530
+ // Assumption 1: This function is called in a thread that is holding the Global VM Lock. Caller is responsible for enforcing this.
531
+ // Assumption 2: This function is called from the main Ractor (if Ruby has support for Ractors).
532
+ void thread_context_collector_on_gc_start(VALUE self_instance) {
533
+ struct thread_context_collector_state *state;
534
+ if (!rb_typeddata_is_kind_of(self_instance, &thread_context_collector_typed_data)) return;
535
+ // This should never fail the the above check passes
536
+ TypedData_Get_Struct(self_instance, struct thread_context_collector_state, &thread_context_collector_typed_data, state);
537
+
538
+ struct per_thread_context *thread_context = get_context_for(rb_thread_current(), state);
539
+
540
+ // If there was no previously-existing context for this thread, we won't allocate one (see safety). For now we just drop
541
+ // the GC sample, under the assumption that "a thread that is so new that we never sampled it even once before it triggers
542
+ // GC" is a rare enough case that we can just ignore it.
543
+ // We can always improve this later if we find that this happens often (and we have the counter to help us figure that out)!
544
+ if (thread_context == NULL) {
545
+ state->stats.gc_samples_missed_due_to_missing_context++;
546
+ return;
547
+ }
548
+
549
+ // Here we record the wall-time first and in on_gc_finish we record it second to try to avoid having wall-time be slightly < cpu-time
550
+ thread_context->gc_tracking.wall_time_at_start_ns = monotonic_wall_time_now_ns(DO_NOT_RAISE_ON_FAILURE);
551
+ thread_context->gc_tracking.cpu_time_at_start_ns = cpu_time_now_ns(thread_context);
552
+ }
553
+
554
+ // This function gets called when Ruby has finished running the Garbage Collector on the current thread.
555
+ // It records the cpu/wall-time observed during GC, which will be used to later
556
+ // create an event including the cpu/wall time spent from the start of garbage collector work until now.
557
+ //
558
+ // Safety: This function gets called while Ruby is doing garbage collection. While Ruby is doing garbage collection,
559
+ // *NO ALLOCATION* is allowed. This function, and any it calls must never trigger memory or object allocation.
560
+ // This includes exceptions and use of ruby_xcalloc (because xcalloc can trigger GC)!
561
+ //
562
+ // Assumption 1: This function is called in a thread that is holding the Global VM Lock. Caller is responsible for enforcing this.
563
+ // Assumption 2: This function is called from the main Ractor (if Ruby has support for Ractors).
564
+ bool thread_context_collector_on_gc_finish(VALUE self_instance) {
565
+ struct thread_context_collector_state *state;
566
+ if (!rb_typeddata_is_kind_of(self_instance, &thread_context_collector_typed_data)) return false;
567
+ // This should never fail the the above check passes
568
+ TypedData_Get_Struct(self_instance, struct thread_context_collector_state, &thread_context_collector_typed_data, state);
569
+
570
+ struct per_thread_context *thread_context = get_context_for(rb_thread_current(), state);
571
+
572
+ // If there was no previously-existing context for this thread, we won't allocate one (see safety). We keep a metric for
573
+ // how often this happens -- see on_gc_start.
574
+ if (thread_context == NULL) return false;
575
+
576
+ long cpu_time_at_start_ns = thread_context->gc_tracking.cpu_time_at_start_ns;
577
+ long wall_time_at_start_ns = thread_context->gc_tracking.wall_time_at_start_ns;
578
+
579
+ if (cpu_time_at_start_ns == INVALID_TIME && wall_time_at_start_ns == INVALID_TIME) {
580
+ // If this happened, it means that on_gc_start was either never called for the thread OR it was called but no thread
581
+ // context existed at the time. The former can be the result of a bug, but since we can't distinguish them, we just
582
+ // do nothing.
583
+ return false;
584
+ }
585
+
586
+ // Mark thread as no longer in GC
587
+ thread_context->gc_tracking.cpu_time_at_start_ns = INVALID_TIME;
588
+ thread_context->gc_tracking.wall_time_at_start_ns = INVALID_TIME;
589
+
590
+ // Here we record the wall-time second and in on_gc_start we record it first to try to avoid having wall-time be slightly < cpu-time
591
+ long cpu_time_at_finish_ns = cpu_time_now_ns(thread_context);
592
+ long wall_time_at_finish_ns = monotonic_wall_time_now_ns(DO_NOT_RAISE_ON_FAILURE);
593
+
594
+ // If our end timestamp is not OK, we bail out
595
+ if (wall_time_at_finish_ns == 0) return false;
596
+
597
+ long gc_cpu_time_elapsed_ns = cpu_time_at_finish_ns - cpu_time_at_start_ns;
598
+ long gc_wall_time_elapsed_ns = wall_time_at_finish_ns - wall_time_at_start_ns;
599
+
600
+ // Wall-time can go backwards if the system clock gets changed (and we observed spurious jumps back on macOS as well)
601
+ // so let's ensure we don't get negative values for time deltas.
602
+ gc_cpu_time_elapsed_ns = long_max_of(gc_cpu_time_elapsed_ns, 0);
603
+ gc_wall_time_elapsed_ns = long_max_of(gc_wall_time_elapsed_ns, 0);
604
+
605
+ if (state->gc_tracking.wall_time_at_previous_gc_ns == INVALID_TIME) {
606
+ state->gc_tracking.accumulated_cpu_time_ns = 0;
607
+ state->gc_tracking.accumulated_wall_time_ns = 0;
608
+ }
609
+
610
+ state->gc_tracking.accumulated_cpu_time_ns += gc_cpu_time_elapsed_ns;
611
+ state->gc_tracking.accumulated_wall_time_ns += gc_wall_time_elapsed_ns;
612
+ state->gc_tracking.wall_time_at_previous_gc_ns = wall_time_at_finish_ns;
613
+
614
+ // Update cpu-time accounting so it doesn't include the cpu-time spent in GC during the next sample
615
+ // We don't update the wall-time because we don't subtract the wall-time spent in GC (see call to
616
+ // `update_time_since_previous_sample` for wall-time in `update_metrics_and_sample`).
617
+ if (thread_context->cpu_time_at_previous_sample_ns != INVALID_TIME) {
618
+ thread_context->cpu_time_at_previous_sample_ns += gc_cpu_time_elapsed_ns;
619
+ }
620
+
621
+ // Let the caller know if it should schedule a flush or not. Returning true every time would cause a lot of overhead
622
+ // on the application (see GC tracking introduction at the top of the file), so instead we try to accumulate a few
623
+ // samples first.
624
+ bool finished_major_gc = gc_profiling_has_major_gc_finished();
625
+ bool over_flush_time_treshold =
626
+ (wall_time_at_finish_ns - state->gc_tracking.wall_time_at_last_flushed_gc_event_ns) >= TIME_BETWEEN_GC_EVENTS_NS;
627
+
628
+ return finished_major_gc || over_flush_time_treshold;
629
+ }
630
+
631
+ // This function gets called after one or more GC work steps (calls to on_gc_start/on_gc_finish).
632
+ // It creates a new sample including the cpu and wall-time spent by the garbage collector work, and resets any
633
+ // GC-related tracking.
634
+ //
635
+ // Assumption 1: This function is called in a thread that is holding the Global VM Lock. Caller is responsible for enforcing this.
636
+ // Assumption 2: This function is allowed to raise exceptions. Caller is responsible for handling them, if needed.
637
+ // Assumption 3: Unlike `on_gc_start` and `on_gc_finish`, this method is allowed to allocate memory as needed.
638
+ // Assumption 4: This function is called from the main Ractor (if Ruby has support for Ractors).
639
+ VALUE thread_context_collector_sample_after_gc(VALUE self_instance) {
640
+ struct thread_context_collector_state *state;
641
+ TypedData_Get_Struct(self_instance, struct thread_context_collector_state, &thread_context_collector_typed_data, state);
642
+
643
+ if (state->gc_tracking.wall_time_at_previous_gc_ns == INVALID_TIME) {
644
+ rb_raise(rb_eRuntimeError, "BUG: Unexpected call to sample_after_gc without valid GC information available");
645
+ }
646
+
647
+ int max_labels_needed_for_gc = 7; // Magic number gets validated inside gc_profiling_set_metadata
648
+ ddog_prof_Label labels[max_labels_needed_for_gc];
649
+ uint8_t label_pos = gc_profiling_set_metadata(labels, max_labels_needed_for_gc);
650
+
651
+ ddog_prof_Slice_Label slice_labels = {.ptr = labels, .len = label_pos};
652
+
653
+ // The end_timestamp_ns is treated specially by libdatadog and that's why it's not added as a ddog_prof_Label
654
+ int64_t end_timestamp_ns = 0;
655
+
656
+ if (state->timeline_enabled) {
657
+ end_timestamp_ns = monotonic_to_system_epoch_ns(&state->time_converter_state, state->gc_tracking.wall_time_at_previous_gc_ns);
658
+ }
659
+
660
+ record_placeholder_stack(
661
+ state->sampling_buffer,
662
+ state->recorder_instance,
663
+ (sample_values) {
664
+ // This event gets both a regular cpu/wall-time duration, as a normal cpu/wall-time sample would, as well as a
665
+ // timeline duration.
666
+ // This is done to enable two use-cases:
667
+ // * regular cpu/wall-time makes this event show up as a regular stack in the flamegraph
668
+ // * the timeline duration is used when the event shows up in the timeline
669
+ .cpu_time_ns = state->gc_tracking.accumulated_cpu_time_ns,
670
+ .cpu_or_wall_samples = 1,
671
+ .wall_time_ns = state->gc_tracking.accumulated_wall_time_ns,
672
+ .timeline_wall_time_ns = state->gc_tracking.accumulated_wall_time_ns,
673
+ },
674
+ (sample_labels) {.labels = slice_labels, .state_label = NULL, .end_timestamp_ns = end_timestamp_ns},
675
+ DDOG_CHARSLICE_C("Garbage Collection")
676
+ );
677
+
678
+ state->gc_tracking.wall_time_at_last_flushed_gc_event_ns = state->gc_tracking.wall_time_at_previous_gc_ns;
679
+ state->gc_tracking.wall_time_at_previous_gc_ns = INVALID_TIME;
680
+
681
+ state->stats.gc_samples++;
682
+
683
+ // Return a VALUE to make it easier to call this function from Ruby APIs that expect a return value (such as rb_rescue2)
684
+ return Qnil;
685
+ }
686
+
687
+ static void trigger_sample_for_thread(
688
+ struct thread_context_collector_state *state,
689
+ VALUE thread,
690
+ VALUE stack_from_thread, // This can be different when attributing profiler overhead using a different stack
691
+ struct per_thread_context *thread_context,
692
+ sample_values values,
693
+ long current_monotonic_wall_time_ns,
694
+ // These two labels are only used for allocation profiling; @ivoanjo: may want to refactor this at some point?
695
+ ddog_CharSlice *ruby_vm_type,
696
+ ddog_CharSlice *class_name
697
+ ) {
698
+ int max_label_count =
699
+ 1 + // thread id
700
+ 1 + // thread name
701
+ 1 + // profiler overhead
702
+ 2 + // ruby vm type and allocation class
703
+ 1 + // state (only set for cpu/wall-time samples)
704
+ 2; // local root span id and span id
705
+ ddog_prof_Label labels[max_label_count];
706
+ int label_pos = 0;
707
+
708
+ labels[label_pos++] = (ddog_prof_Label) {
709
+ .key = DDOG_CHARSLICE_C("thread id"),
710
+ .str = thread_context->thread_id_char_slice
711
+ };
712
+
713
+ VALUE thread_name = thread_name_for(thread);
714
+ if (thread_name != Qnil) {
715
+ labels[label_pos++] = (ddog_prof_Label) {
716
+ .key = DDOG_CHARSLICE_C("thread name"),
717
+ .str = char_slice_from_ruby_string(thread_name)
718
+ };
719
+ } else if (thread == state->main_thread) { // Threads are often not named, but we can have a nice fallback for this special thread
720
+ ddog_CharSlice main_thread_name = DDOG_CHARSLICE_C("main");
721
+ labels[label_pos++] = (ddog_prof_Label) {
722
+ .key = DDOG_CHARSLICE_C("thread name"),
723
+ .str = main_thread_name
724
+ };
725
+ } else {
726
+ // For other threads without name, we use the "invoke location" (first file:line of the block used to start the thread), if any.
727
+ // This is what Ruby shows in `Thread#to_s`.
728
+ labels[label_pos++] = (ddog_prof_Label) {
729
+ .key = DDOG_CHARSLICE_C("thread name"),
730
+ .str = thread_context->thread_invoke_location_char_slice // This is an empty string if no invoke location was available
731
+ };
732
+ }
733
+
734
+ struct trace_identifiers trace_identifiers_result = {.valid = false, .trace_endpoint = Qnil};
735
+ trace_identifiers_for(state, thread, &trace_identifiers_result);
736
+
737
+ if (trace_identifiers_result.valid) {
738
+ labels[label_pos++] = (ddog_prof_Label) {.key = DDOG_CHARSLICE_C("local root span id"), .num = trace_identifiers_result.local_root_span_id};
739
+ labels[label_pos++] = (ddog_prof_Label) {.key = DDOG_CHARSLICE_C("span id"), .num = trace_identifiers_result.span_id};
740
+
741
+ if (trace_identifiers_result.trace_endpoint != Qnil) {
742
+ // The endpoint gets recorded in a different way because it is mutable in the tracer and can change during a
743
+ // trace.
744
+ //
745
+ // Instead of each sample for the same local_root_span_id getting a potentially-different endpoint,
746
+ // `record_endpoint` (via libdatadog) keeps a list of local_root_span_id values and their most-recently-seen
747
+ // endpoint values, and at serialization time the most-recently-seen endpoint is applied to all relevant samples.
748
+ //
749
+ // This is why the endpoint is not directly added in this function to the labels array, although it will later
750
+ // show up in the array in the output pprof.
751
+ record_endpoint(
752
+ state->recorder_instance,
753
+ trace_identifiers_result.local_root_span_id,
754
+ char_slice_from_ruby_string(trace_identifiers_result.trace_endpoint)
755
+ );
756
+ }
757
+ }
758
+
759
+ if (thread != stack_from_thread) {
760
+ labels[label_pos++] = (ddog_prof_Label) {
761
+ .key = DDOG_CHARSLICE_C("profiler overhead"),
762
+ .num = 1
763
+ };
764
+ }
765
+
766
+ if (ruby_vm_type != NULL) {
767
+ labels[label_pos++] = (ddog_prof_Label) {
768
+ .key = DDOG_CHARSLICE_C("ruby vm type"),
769
+ .str = *ruby_vm_type
770
+ };
771
+ }
772
+
773
+ if (class_name != NULL) {
774
+ labels[label_pos++] = (ddog_prof_Label) {
775
+ .key = DDOG_CHARSLICE_C("allocation class"),
776
+ .str = *class_name
777
+ };
778
+ }
779
+
780
+ // This label is handled specially:
781
+ // 1. It's only set for cpu/wall-time samples
782
+ // 2. We set it here to its default state of "unknown", but the `Collectors::Stack` may choose to override it with
783
+ // something more interesting.
784
+ ddog_prof_Label *state_label = NULL;
785
+ if (values.cpu_or_wall_samples > 0) {
786
+ state_label = &labels[label_pos++];
787
+ *state_label = (ddog_prof_Label) {
788
+ .key = DDOG_CHARSLICE_C("state"),
789
+ .str = DDOG_CHARSLICE_C("unknown"),
790
+ .num = 0, // This shouldn't be needed but the tracer-2.7 docker image ships a buggy gcc that complains about this
791
+ };
792
+ }
793
+
794
+ // The number of times `label_pos++` shows up in this function needs to match `max_label_count`. To avoid "oops I
795
+ // forgot to update max_label_count" in the future, we've also added this validation.
796
+ // @ivoanjo: I wonder if C compilers are smart enough to statically prove this check never triggers unless someone
797
+ // changes the code erroneously and remove it entirely?
798
+ if (label_pos > max_label_count) {
799
+ rb_raise(rb_eRuntimeError, "BUG: Unexpected label_pos (%d) > max_label_count (%d)", label_pos, max_label_count);
800
+ }
801
+
802
+ ddog_prof_Slice_Label slice_labels = {.ptr = labels, .len = label_pos};
803
+
804
+ // The end_timestamp_ns is treated specially by libdatadog and that's why it's not added as a ddog_prof_Label
805
+ int64_t end_timestamp_ns = 0;
806
+ if (state->timeline_enabled && current_monotonic_wall_time_ns != INVALID_TIME) {
807
+ end_timestamp_ns = monotonic_to_system_epoch_ns(&state->time_converter_state, current_monotonic_wall_time_ns);
808
+ }
809
+
810
+ sample_thread(
811
+ stack_from_thread,
812
+ state->sampling_buffer,
813
+ state->recorder_instance,
814
+ values,
815
+ (sample_labels) {.labels = slice_labels, .state_label = state_label, .end_timestamp_ns = end_timestamp_ns}
816
+ );
817
+ }
818
+
819
+ // This method exists only to enable testing Datadog::Profiling::Collectors::ThreadContext behavior using RSpec.
820
+ // It SHOULD NOT be used for other purposes.
821
+ static VALUE _native_thread_list(DDTRACE_UNUSED VALUE _self) {
822
+ VALUE result = rb_ary_new();
823
+ ddtrace_thread_list(result);
824
+ return result;
825
+ }
826
+
827
+ static struct per_thread_context *get_or_create_context_for(VALUE thread, struct thread_context_collector_state *state) {
828
+ struct per_thread_context* thread_context = NULL;
829
+ st_data_t value_context = 0;
830
+
831
+ if (st_lookup(state->hash_map_per_thread_context, (st_data_t) thread, &value_context)) {
832
+ thread_context = (struct per_thread_context*) value_context;
833
+ } else {
834
+ thread_context = ruby_xcalloc(1, sizeof(struct per_thread_context));
835
+ initialize_context(thread, thread_context, state);
836
+ st_insert(state->hash_map_per_thread_context, (st_data_t) thread, (st_data_t) thread_context);
837
+ }
838
+
839
+ return thread_context;
840
+ }
841
+
842
+ static struct per_thread_context *get_context_for(VALUE thread, struct thread_context_collector_state *state) {
843
+ struct per_thread_context* thread_context = NULL;
844
+ st_data_t value_context = 0;
845
+
846
+ if (st_lookup(state->hash_map_per_thread_context, (st_data_t) thread, &value_context)) {
847
+ thread_context = (struct per_thread_context*) value_context;
848
+ }
849
+
850
+ return thread_context;
851
+ }
852
+
853
+ #define LOGGING_GEM_PATH "/lib/logging/diagnostic_context.rb"
854
+
855
+ // The `logging` gem monkey patches thread creation, which makes the `invoke_location_for` useless, since every thread
856
+ // will point to the `logging` gem. When that happens, we avoid using the invoke location.
857
+ //
858
+ // TODO: This approach is a bit brittle, since it matches on the specific gem path, and only works for the `logging`
859
+ // gem.
860
+ // In the future we should probably explore a more generic fix (e.g. using Thread.method(:new).source_location or
861
+ // something like that to detect redefinition of the `Thread` methods). One difficulty of doing it is that we need
862
+ // to either run Ruby code during sampling (not great), or otherwise use some of the VM private APIs to detect this.
863
+ //
864
+ static bool is_logging_gem_monkey_patch(VALUE invoke_file_location) {
865
+ int logging_gem_path_len = strlen(LOGGING_GEM_PATH);
866
+ char *invoke_file = StringValueCStr(invoke_file_location);
867
+ int invoke_file_len = strlen(invoke_file);
868
+
869
+ if (invoke_file_len < logging_gem_path_len) return false;
870
+
871
+ return strncmp(invoke_file + invoke_file_len - logging_gem_path_len, LOGGING_GEM_PATH, logging_gem_path_len) == 0;
872
+ }
873
+
874
+ static void initialize_context(VALUE thread, struct per_thread_context *thread_context, struct thread_context_collector_state *state) {
875
+ snprintf(thread_context->thread_id, THREAD_ID_LIMIT_CHARS, "%"PRIu64" (%lu)", native_thread_id_for(thread), (unsigned long) thread_id_for(thread));
876
+ thread_context->thread_id_char_slice = (ddog_CharSlice) {.ptr = thread_context->thread_id, .len = strlen(thread_context->thread_id)};
877
+
878
+ int invoke_line_location;
879
+ VALUE invoke_file_location = invoke_location_for(thread, &invoke_line_location);
880
+ if (invoke_file_location != Qnil) {
881
+ if (!is_logging_gem_monkey_patch(invoke_file_location)) {
882
+ snprintf(
883
+ thread_context->thread_invoke_location,
884
+ THREAD_INVOKE_LOCATION_LIMIT_CHARS,
885
+ "%s:%d",
886
+ StringValueCStr(invoke_file_location),
887
+ invoke_line_location
888
+ );
889
+ } else {
890
+ snprintf(thread_context->thread_invoke_location, THREAD_INVOKE_LOCATION_LIMIT_CHARS, "%s", "(Unnamed thread)");
891
+ }
892
+ } else if (thread != state->main_thread) {
893
+ // If the first function of a thread is native code, there won't be an invoke location, so we use this fallback.
894
+ // NOTE: In the future, I wonder if we could take the pointer to the native function, and try to see if there's a native
895
+ // symbol attached to it.
896
+ snprintf(thread_context->thread_invoke_location, THREAD_INVOKE_LOCATION_LIMIT_CHARS, "%s", "(Unnamed thread from native code)");
897
+ }
898
+
899
+ thread_context->thread_invoke_location_char_slice = (ddog_CharSlice) {
900
+ .ptr = thread_context->thread_invoke_location,
901
+ .len = strlen(thread_context->thread_invoke_location)
902
+ };
903
+
904
+ thread_context->thread_cpu_time_id = thread_cpu_time_id_for(thread);
905
+
906
+ // These will get initialized during actual sampling
907
+ thread_context->cpu_time_at_previous_sample_ns = INVALID_TIME;
908
+ thread_context->wall_time_at_previous_sample_ns = INVALID_TIME;
909
+
910
+ // These will only be used during a GC operation
911
+ thread_context->gc_tracking.cpu_time_at_start_ns = INVALID_TIME;
912
+ thread_context->gc_tracking.wall_time_at_start_ns = INVALID_TIME;
913
+ }
914
+
915
+ static VALUE _native_inspect(DDTRACE_UNUSED VALUE _self, VALUE collector_instance) {
916
+ struct thread_context_collector_state *state;
917
+ TypedData_Get_Struct(collector_instance, struct thread_context_collector_state, &thread_context_collector_typed_data, state);
918
+
919
+ VALUE result = rb_str_new2(" (native state)");
920
+
921
+ // Update this when modifying state struct
922
+ rb_str_concat(result, rb_sprintf(" hash_map_per_thread_context=%"PRIsVALUE, per_thread_context_st_table_as_ruby_hash(state)));
923
+ rb_str_concat(result, rb_sprintf(" recorder_instance=%"PRIsVALUE, state->recorder_instance));
924
+ VALUE tracer_context_key = state->tracer_context_key == MISSING_TRACER_CONTEXT_KEY ? Qnil : ID2SYM(state->tracer_context_key);
925
+ rb_str_concat(result, rb_sprintf(" tracer_context_key=%+"PRIsVALUE, tracer_context_key));
926
+ rb_str_concat(result, rb_sprintf(" sample_count=%u", state->sample_count));
927
+ rb_str_concat(result, rb_sprintf(" stats=%"PRIsVALUE, stats_as_ruby_hash(state)));
928
+ rb_str_concat(result, rb_sprintf(" endpoint_collection_enabled=%"PRIsVALUE, state->endpoint_collection_enabled ? Qtrue : Qfalse));
929
+ rb_str_concat(result, rb_sprintf(" timeline_enabled=%"PRIsVALUE, state->timeline_enabled ? Qtrue : Qfalse));
930
+ rb_str_concat(result, rb_sprintf(" allocation_type_enabled=%"PRIsVALUE, state->allocation_type_enabled ? Qtrue : Qfalse));
931
+ rb_str_concat(result, rb_sprintf(
932
+ " time_converter_state={.system_epoch_ns_reference=%ld, .delta_to_epoch_ns=%ld}",
933
+ state->time_converter_state.system_epoch_ns_reference,
934
+ state->time_converter_state.delta_to_epoch_ns
935
+ ));
936
+ rb_str_concat(result, rb_sprintf(" main_thread=%"PRIsVALUE, state->main_thread));
937
+ rb_str_concat(result, rb_sprintf(" gc_tracking=%"PRIsVALUE, gc_tracking_as_ruby_hash(state)));
938
+ rb_str_concat(result, rb_sprintf(" otel_current_span_key=%"PRIsVALUE, state->otel_current_span_key));
939
+
940
+ return result;
941
+ }
942
+
943
+ static VALUE per_thread_context_st_table_as_ruby_hash(struct thread_context_collector_state *state) {
944
+ VALUE result = rb_hash_new();
945
+ st_foreach(state->hash_map_per_thread_context, per_thread_context_as_ruby_hash, result);
946
+ return result;
947
+ }
948
+
949
+ static int per_thread_context_as_ruby_hash(st_data_t key_thread, st_data_t value_context, st_data_t result_hash) {
950
+ VALUE thread = (VALUE) key_thread;
951
+ struct per_thread_context *thread_context = (struct per_thread_context*) value_context;
952
+ VALUE result = (VALUE) result_hash;
953
+ VALUE context_as_hash = rb_hash_new();
954
+ rb_hash_aset(result, thread, context_as_hash);
955
+
956
+ VALUE arguments[] = {
957
+ ID2SYM(rb_intern("thread_id")), /* => */ rb_str_new2(thread_context->thread_id),
958
+ ID2SYM(rb_intern("thread_invoke_location")), /* => */ rb_str_new2(thread_context->thread_invoke_location),
959
+ ID2SYM(rb_intern("thread_cpu_time_id_valid?")), /* => */ thread_context->thread_cpu_time_id.valid ? Qtrue : Qfalse,
960
+ ID2SYM(rb_intern("thread_cpu_time_id")), /* => */ CLOCKID2NUM(thread_context->thread_cpu_time_id.clock_id),
961
+ ID2SYM(rb_intern("cpu_time_at_previous_sample_ns")), /* => */ LONG2NUM(thread_context->cpu_time_at_previous_sample_ns),
962
+ ID2SYM(rb_intern("wall_time_at_previous_sample_ns")), /* => */ LONG2NUM(thread_context->wall_time_at_previous_sample_ns),
963
+
964
+ ID2SYM(rb_intern("gc_tracking.cpu_time_at_start_ns")), /* => */ LONG2NUM(thread_context->gc_tracking.cpu_time_at_start_ns),
965
+ ID2SYM(rb_intern("gc_tracking.wall_time_at_start_ns")), /* => */ LONG2NUM(thread_context->gc_tracking.wall_time_at_start_ns),
966
+ };
967
+ for (long unsigned int i = 0; i < VALUE_COUNT(arguments); i += 2) rb_hash_aset(context_as_hash, arguments[i], arguments[i+1]);
968
+
969
+ return ST_CONTINUE;
970
+ }
971
+
972
+ static VALUE stats_as_ruby_hash(struct thread_context_collector_state *state) {
973
+ // Update this when modifying state struct (stats inner struct)
974
+ VALUE stats_as_hash = rb_hash_new();
975
+ VALUE arguments[] = {
976
+ ID2SYM(rb_intern("gc_samples")), /* => */ UINT2NUM(state->stats.gc_samples),
977
+ ID2SYM(rb_intern("gc_samples_missed_due_to_missing_context")), /* => */ UINT2NUM(state->stats.gc_samples_missed_due_to_missing_context),
978
+ };
979
+ for (long unsigned int i = 0; i < VALUE_COUNT(arguments); i += 2) rb_hash_aset(stats_as_hash, arguments[i], arguments[i+1]);
980
+ return stats_as_hash;
981
+ }
982
+
983
+ static VALUE gc_tracking_as_ruby_hash(struct thread_context_collector_state *state) {
984
+ // Update this when modifying state struct (gc_tracking inner struct)
985
+ VALUE result = rb_hash_new();
986
+ VALUE arguments[] = {
987
+ ID2SYM(rb_intern("accumulated_cpu_time_ns")), /* => */ ULONG2NUM(state->gc_tracking.accumulated_cpu_time_ns),
988
+ ID2SYM(rb_intern("accumulated_wall_time_ns")), /* => */ ULONG2NUM(state->gc_tracking.accumulated_wall_time_ns),
989
+ ID2SYM(rb_intern("wall_time_at_previous_gc_ns")), /* => */ LONG2NUM(state->gc_tracking.wall_time_at_previous_gc_ns),
990
+ ID2SYM(rb_intern("wall_time_at_last_flushed_gc_event_ns")), /* => */ LONG2NUM(state->gc_tracking.wall_time_at_last_flushed_gc_event_ns),
991
+ };
992
+ for (long unsigned int i = 0; i < VALUE_COUNT(arguments); i += 2) rb_hash_aset(result, arguments[i], arguments[i+1]);
993
+ return result;
994
+ }
995
+
996
+ static void remove_context_for_dead_threads(struct thread_context_collector_state *state) {
997
+ st_foreach(state->hash_map_per_thread_context, remove_if_dead_thread, 0 /* unused */);
998
+ }
999
+
1000
+ static int remove_if_dead_thread(st_data_t key_thread, st_data_t value_context, DDTRACE_UNUSED st_data_t _argument) {
1001
+ VALUE thread = (VALUE) key_thread;
1002
+ struct per_thread_context* thread_context = (struct per_thread_context*) value_context;
1003
+
1004
+ if (is_thread_alive(thread)) return ST_CONTINUE;
1005
+
1006
+ ruby_xfree(thread_context);
1007
+ return ST_DELETE;
1008
+ }
1009
+
1010
+ // This method exists only to enable testing Datadog::Profiling::Collectors::ThreadContext behavior using RSpec.
1011
+ // It SHOULD NOT be used for other purposes.
1012
+ //
1013
+ // Returns the whole contents of the per_thread_context structs being tracked.
1014
+ static VALUE _native_per_thread_context(DDTRACE_UNUSED VALUE _self, VALUE collector_instance) {
1015
+ struct thread_context_collector_state *state;
1016
+ TypedData_Get_Struct(collector_instance, struct thread_context_collector_state, &thread_context_collector_typed_data, state);
1017
+
1018
+ return per_thread_context_st_table_as_ruby_hash(state);
1019
+ }
1020
+
1021
+ static long update_time_since_previous_sample(long *time_at_previous_sample_ns, long current_time_ns, long gc_start_time_ns, bool is_wall_time) {
1022
+ // If we didn't have a time for the previous sample, we use the current one
1023
+ if (*time_at_previous_sample_ns == INVALID_TIME) *time_at_previous_sample_ns = current_time_ns;
1024
+
1025
+ bool is_thread_doing_gc = gc_start_time_ns != INVALID_TIME;
1026
+ long elapsed_time_ns = -1;
1027
+
1028
+ if (is_thread_doing_gc) {
1029
+ bool previous_sample_was_during_gc = gc_start_time_ns <= *time_at_previous_sample_ns;
1030
+
1031
+ if (previous_sample_was_during_gc) {
1032
+ elapsed_time_ns = 0; // No time to account for -- any time since the last sample is going to get assigned to GC separately
1033
+ } else {
1034
+ elapsed_time_ns = gc_start_time_ns - *time_at_previous_sample_ns; // Capture time between previous sample and start of GC only
1035
+ }
1036
+
1037
+ // Remaining time (from gc_start_time to current_time_ns) will be accounted for inside `sample_after_gc`
1038
+ *time_at_previous_sample_ns = gc_start_time_ns;
1039
+ } else {
1040
+ elapsed_time_ns = current_time_ns - *time_at_previous_sample_ns; // Capture all time since previous sample
1041
+ *time_at_previous_sample_ns = current_time_ns;
1042
+ }
1043
+
1044
+ if (elapsed_time_ns < 0) {
1045
+ if (is_wall_time) {
1046
+ // Wall-time can actually go backwards (e.g. when the system clock gets set) so we can't assume time going backwards
1047
+ // was a bug.
1048
+ // @ivoanjo: I've also observed time going backwards spuriously on macOS, see discussion on
1049
+ // https://github.com/DataDog/dd-trace-rb/pull/2336.
1050
+ elapsed_time_ns = 0;
1051
+ } else {
1052
+ // We don't expect non-wall time to go backwards, so let's flag this as a bug
1053
+ rb_raise(rb_eRuntimeError, "BUG: Unexpected negative elapsed_time_ns between samples");
1054
+ }
1055
+ }
1056
+
1057
+ return elapsed_time_ns;
1058
+ }
1059
+
1060
+ // Safety: This function is assumed never to raise exceptions by callers
1061
+ static long cpu_time_now_ns(struct per_thread_context *thread_context) {
1062
+ thread_cpu_time cpu_time = thread_cpu_time_for(thread_context->thread_cpu_time_id);
1063
+
1064
+ if (!cpu_time.valid) {
1065
+ // Invalidate previous state of the counter (if any), it's no longer accurate. We need to get two good reads
1066
+ // in a row to have an accurate delta.
1067
+ thread_context->cpu_time_at_previous_sample_ns = INVALID_TIME;
1068
+ return 0;
1069
+ }
1070
+
1071
+ return cpu_time.result_ns;
1072
+ }
1073
+
1074
+ static long thread_id_for(VALUE thread) {
1075
+ VALUE object_id = rb_obj_id(thread);
1076
+
1077
+ // The API docs for Ruby state that `rb_obj_id` COULD be a BIGNUM and that if you want to be really sure you don't
1078
+ // get a BIGNUM, then you should use `rb_memory_id`. But `rb_memory_id` is less interesting because it's less visible
1079
+ // at the user level than the result of calling `#object_id`.
1080
+ //
1081
+ // It also seems uncommon to me that we'd ever get a BIGNUM; on old Ruby versions (pre-GC compaction), the object id
1082
+ // was the pointer to the object, so that's not going to be a BIGNUM; on modern Ruby versions, Ruby keeps
1083
+ // a counter, and only increments it for objects for which `#object_id`/`rb_obj_id` is called (e.g. most objects
1084
+ // won't actually have an object id allocated).
1085
+ //
1086
+ // So, for now, let's simplify: we only support FIXNUMs, and we won't break if we get a BIGNUM; we just won't
1087
+ // record the thread_id (but samples will still be collected).
1088
+ return FIXNUM_P(object_id) ? FIX2LONG(object_id) : -1;
1089
+ }
1090
+
1091
+ VALUE enforce_thread_context_collector_instance(VALUE object) {
1092
+ Check_TypedStruct(object, &thread_context_collector_typed_data);
1093
+ return object;
1094
+ }
1095
+
1096
+ // This method exists only to enable testing Datadog::Profiling::Collectors::ThreadContext behavior using RSpec.
1097
+ // It SHOULD NOT be used for other purposes.
1098
+ static VALUE _native_stats(DDTRACE_UNUSED VALUE _self, VALUE collector_instance) {
1099
+ struct thread_context_collector_state *state;
1100
+ TypedData_Get_Struct(collector_instance, struct thread_context_collector_state, &thread_context_collector_typed_data, state);
1101
+
1102
+ return stats_as_ruby_hash(state);
1103
+ }
1104
+
1105
+ // This method exists only to enable testing Datadog::Profiling::Collectors::ThreadContext behavior using RSpec.
1106
+ // It SHOULD NOT be used for other purposes.
1107
+ static VALUE _native_gc_tracking(DDTRACE_UNUSED VALUE _self, VALUE collector_instance) {
1108
+ struct thread_context_collector_state *state;
1109
+ TypedData_Get_Struct(collector_instance, struct thread_context_collector_state, &thread_context_collector_typed_data, state);
1110
+
1111
+ return gc_tracking_as_ruby_hash(state);
1112
+ }
1113
+
1114
+ // Assumption 1: This function is called in a thread that is holding the Global VM Lock. Caller is responsible for enforcing this.
1115
+ static void trace_identifiers_for(struct thread_context_collector_state *state, VALUE thread, struct trace_identifiers *trace_identifiers_result) {
1116
+ if (state->tracer_context_key == MISSING_TRACER_CONTEXT_KEY) return;
1117
+
1118
+ VALUE current_context = rb_thread_local_aref(thread, state->tracer_context_key);
1119
+ if (current_context == Qnil) return;
1120
+
1121
+ VALUE active_trace = rb_ivar_get(current_context, at_active_trace_id /* @active_trace */);
1122
+ if (active_trace == Qnil) return;
1123
+
1124
+ VALUE root_span = rb_ivar_get(active_trace, at_root_span_id /* @root_span */);
1125
+ VALUE active_span = rb_ivar_get(active_trace, at_active_span_id /* @active_span */);
1126
+ // Note: On Ruby 3.x `rb_attr_get` is exactly the same as `rb_ivar_get`. For Ruby 2.x, the difference is that
1127
+ // `rb_ivar_get` can trigger "warning: instance variable @otel_values not initialized" if warnings are enabled and
1128
+ // opentelemetry is not in use, whereas `rb_attr_get` does the lookup without generating the warning.
1129
+ VALUE otel_values = rb_attr_get(active_trace, at_otel_values_id /* @otel_values */);
1130
+
1131
+ VALUE numeric_span_id = Qnil;
1132
+
1133
+ if (otel_values != Qnil) ddtrace_otel_trace_identifiers_for(state, &active_trace, &root_span, &numeric_span_id, active_span, otel_values);
1134
+
1135
+ if (root_span == Qnil || (active_span == Qnil && numeric_span_id == Qnil)) return;
1136
+
1137
+ VALUE numeric_local_root_span_id = rb_ivar_get(root_span, at_id_id /* @id */);
1138
+ if (active_span != Qnil && numeric_span_id == Qnil) numeric_span_id = rb_ivar_get(active_span, at_id_id /* @id */);
1139
+ if (numeric_local_root_span_id == Qnil || numeric_span_id == Qnil) return;
1140
+
1141
+ trace_identifiers_result->local_root_span_id = NUM2ULL(numeric_local_root_span_id);
1142
+ trace_identifiers_result->span_id = NUM2ULL(numeric_span_id);
1143
+
1144
+ trace_identifiers_result->valid = true;
1145
+
1146
+ if (!state->endpoint_collection_enabled) return;
1147
+
1148
+ VALUE root_span_type = rb_ivar_get(root_span, at_type_id /* @type */);
1149
+ if (root_span_type == Qnil || !should_collect_resource(root_span_type)) return;
1150
+
1151
+ VALUE trace_resource = rb_ivar_get(active_trace, at_resource_id /* @resource */);
1152
+ if (RB_TYPE_P(trace_resource, T_STRING)) {
1153
+ trace_identifiers_result->trace_endpoint = trace_resource;
1154
+ } else if (trace_resource == Qnil) {
1155
+ // Fall back to resource from span, if any
1156
+ trace_identifiers_result->trace_endpoint = rb_ivar_get(root_span, at_resource_id /* @resource */);
1157
+ }
1158
+ }
1159
+
1160
+ // We only collect the resource for spans of types:
1161
+ // * 'web', for web requests
1162
+ // * proxy', used by the rack integration with request_queuing: true (e.g. also represents a web request)
1163
+ //
1164
+ // NOTE: Currently we're only interested in HTTP service endpoints. Over time, this list may be expanded.
1165
+ // Resources MUST NOT include personal identifiable information (PII); this should not be the case with
1166
+ // ddtrace integrations, but worth mentioning just in case :)
1167
+ static bool should_collect_resource(VALUE root_span_type) {
1168
+ ENFORCE_TYPE(root_span_type, T_STRING);
1169
+
1170
+ int root_span_type_length = RSTRING_LEN(root_span_type);
1171
+ const char *root_span_type_value = StringValuePtr(root_span_type);
1172
+
1173
+ return (root_span_type_length == strlen("web") && (memcmp("web", root_span_type_value, strlen("web")) == 0)) ||
1174
+ (root_span_type_length == strlen("proxy") && (memcmp("proxy", root_span_type_value, strlen("proxy")) == 0));
1175
+ }
1176
+
1177
+ // After the Ruby VM forks, this method gets called in the child process to clean up any leftover state from the parent.
1178
+ //
1179
+ // Assumption: This method gets called BEFORE restarting profiling -- e.g. there are no components attempting to
1180
+ // trigger samples at the same time.
1181
+ static VALUE _native_reset_after_fork(DDTRACE_UNUSED VALUE self, VALUE collector_instance) {
1182
+ struct thread_context_collector_state *state;
1183
+ TypedData_Get_Struct(collector_instance, struct thread_context_collector_state, &thread_context_collector_typed_data, state);
1184
+
1185
+ st_clear(state->hash_map_per_thread_context);
1186
+
1187
+ state->stats = (struct stats) {}; // Resets all stats back to zero
1188
+
1189
+ rb_funcall(state->recorder_instance, rb_intern("reset_after_fork"), 0);
1190
+
1191
+ return Qtrue;
1192
+ }
1193
+
1194
+ static VALUE thread_list(struct thread_context_collector_state *state) {
1195
+ VALUE result = state->thread_list_buffer;
1196
+ rb_ary_clear(result);
1197
+ ddtrace_thread_list(result);
1198
+ return result;
1199
+ }
1200
+
1201
+ void thread_context_collector_sample_allocation(VALUE self_instance, unsigned int sample_weight, VALUE new_object) {
1202
+ struct thread_context_collector_state *state;
1203
+ TypedData_Get_Struct(self_instance, struct thread_context_collector_state, &thread_context_collector_typed_data, state);
1204
+
1205
+ VALUE current_thread = rb_thread_current();
1206
+
1207
+ enum ruby_value_type type = rb_type(new_object);
1208
+
1209
+ // Tag samples with the VM internal types
1210
+ ddog_CharSlice ruby_vm_type = ruby_value_type_to_char_slice(type);
1211
+
1212
+ // Since this is stack allocated, be careful about moving it
1213
+ ddog_CharSlice class_name;
1214
+ ddog_CharSlice *optional_class_name = NULL;
1215
+ char imemo_type[100];
1216
+
1217
+ if (state->allocation_type_enabled) {
1218
+ optional_class_name = &class_name;
1219
+
1220
+ if (
1221
+ type == RUBY_T_OBJECT ||
1222
+ type == RUBY_T_CLASS ||
1223
+ type == RUBY_T_MODULE ||
1224
+ type == RUBY_T_FLOAT ||
1225
+ type == RUBY_T_STRING ||
1226
+ type == RUBY_T_REGEXP ||
1227
+ type == RUBY_T_ARRAY ||
1228
+ type == RUBY_T_HASH ||
1229
+ type == RUBY_T_STRUCT ||
1230
+ type == RUBY_T_BIGNUM ||
1231
+ type == RUBY_T_FILE ||
1232
+ type == RUBY_T_DATA ||
1233
+ type == RUBY_T_MATCH ||
1234
+ type == RUBY_T_COMPLEX ||
1235
+ type == RUBY_T_RATIONAL ||
1236
+ type == RUBY_T_NIL ||
1237
+ type == RUBY_T_TRUE ||
1238
+ type == RUBY_T_FALSE ||
1239
+ type == RUBY_T_SYMBOL ||
1240
+ type == RUBY_T_FIXNUM
1241
+ ) {
1242
+ VALUE klass = rb_class_of(new_object);
1243
+
1244
+ // Ruby sometimes plays a bit fast and loose with some of its internal objects, e.g.
1245
+ // `rb_str_tmp_frozen_acquire` allocates a string with no class (klass=0).
1246
+ // Thus, we need to make sure there's actually a class before getting its name.
1247
+
1248
+ if (klass != 0) {
1249
+ const char *name = rb_obj_classname(new_object);
1250
+ size_t name_length = name != NULL ? strlen(name) : 0;
1251
+
1252
+ if (name_length > 0) {
1253
+ class_name = (ddog_CharSlice) {.ptr = name, .len = name_length};
1254
+ } else {
1255
+ // @ivoanjo: I'm not sure this can ever happen, but just-in-case
1256
+ class_name = ruby_value_type_to_class_name(type);
1257
+ }
1258
+ } else {
1259
+ // Fallback for objects with no class
1260
+ class_name = ruby_value_type_to_class_name(type);
1261
+ }
1262
+ } else if (type == RUBY_T_IMEMO) {
1263
+ const char *imemo_string = imemo_kind(new_object);
1264
+ if (imemo_string != NULL) {
1265
+ snprintf(imemo_type, 100, "(VM Internal, T_IMEMO, %s)", imemo_string);
1266
+ class_name = (ddog_CharSlice) {.ptr = imemo_type, .len = strlen(imemo_type)};
1267
+ } else { // Ruby < 3
1268
+ class_name = DDOG_CHARSLICE_C("(VM Internal, T_IMEMO)");
1269
+ }
1270
+ } else {
1271
+ class_name = ruby_vm_type; // For other weird internal things we just use the VM type
1272
+ }
1273
+ }
1274
+
1275
+ track_object(state->recorder_instance, new_object, sample_weight, optional_class_name);
1276
+
1277
+ trigger_sample_for_thread(
1278
+ state,
1279
+ /* thread: */ current_thread,
1280
+ /* stack_from_thread: */ current_thread,
1281
+ get_or_create_context_for(current_thread, state),
1282
+ (sample_values) {.alloc_samples = sample_weight},
1283
+ INVALID_TIME, // For now we're not collecting timestamps for allocation events, as per profiling team internal discussions
1284
+ &ruby_vm_type,
1285
+ optional_class_name
1286
+ );
1287
+ }
1288
+
1289
+ // This method exists only to enable testing Datadog::Profiling::Collectors::ThreadContext behavior using RSpec.
1290
+ // It SHOULD NOT be used for other purposes.
1291
+ static VALUE _native_sample_allocation(DDTRACE_UNUSED VALUE self, VALUE collector_instance, VALUE sample_weight, VALUE new_object) {
1292
+ thread_context_collector_sample_allocation(collector_instance, NUM2UINT(sample_weight), new_object);
1293
+ return Qtrue;
1294
+ }
1295
+
1296
+ static VALUE new_empty_thread_inner(DDTRACE_UNUSED void *arg) { return Qnil; }
1297
+
1298
+ // This method exists only to enable testing Datadog::Profiling::Collectors::ThreadContext behavior using RSpec.
1299
+ // It SHOULD NOT be used for other purposes.
1300
+ // (It creates an empty native thread, so we can test our native thread naming fallback)
1301
+ static VALUE _native_new_empty_thread(DDTRACE_UNUSED VALUE self) {
1302
+ return rb_thread_create(new_empty_thread_inner, NULL);
1303
+ }
1304
+
1305
+ static ddog_CharSlice ruby_value_type_to_class_name(enum ruby_value_type type) {
1306
+ switch (type) {
1307
+ case(RUBY_T_OBJECT ): return DDOG_CHARSLICE_C("Object");
1308
+ case(RUBY_T_CLASS ): return DDOG_CHARSLICE_C("Class");
1309
+ case(RUBY_T_MODULE ): return DDOG_CHARSLICE_C("Module");
1310
+ case(RUBY_T_FLOAT ): return DDOG_CHARSLICE_C("Float");
1311
+ case(RUBY_T_STRING ): return DDOG_CHARSLICE_C("String");
1312
+ case(RUBY_T_REGEXP ): return DDOG_CHARSLICE_C("Regexp");
1313
+ case(RUBY_T_ARRAY ): return DDOG_CHARSLICE_C("Array");
1314
+ case(RUBY_T_HASH ): return DDOG_CHARSLICE_C("Hash");
1315
+ case(RUBY_T_STRUCT ): return DDOG_CHARSLICE_C("Struct");
1316
+ case(RUBY_T_BIGNUM ): return DDOG_CHARSLICE_C("Integer");
1317
+ case(RUBY_T_FILE ): return DDOG_CHARSLICE_C("File");
1318
+ case(RUBY_T_DATA ): return DDOG_CHARSLICE_C("(VM Internal, T_DATA)");
1319
+ case(RUBY_T_MATCH ): return DDOG_CHARSLICE_C("MatchData");
1320
+ case(RUBY_T_COMPLEX ): return DDOG_CHARSLICE_C("Complex");
1321
+ case(RUBY_T_RATIONAL): return DDOG_CHARSLICE_C("Rational");
1322
+ case(RUBY_T_NIL ): return DDOG_CHARSLICE_C("NilClass");
1323
+ case(RUBY_T_TRUE ): return DDOG_CHARSLICE_C("TrueClass");
1324
+ case(RUBY_T_FALSE ): return DDOG_CHARSLICE_C("FalseClass");
1325
+ case(RUBY_T_SYMBOL ): return DDOG_CHARSLICE_C("Symbol");
1326
+ case(RUBY_T_FIXNUM ): return DDOG_CHARSLICE_C("Integer");
1327
+ default: return DDOG_CHARSLICE_C("(VM Internal, Missing class)");
1328
+ }
1329
+ }
1330
+
1331
+ static VALUE get_otel_current_span_key(struct thread_context_collector_state *state) {
1332
+ if (state->otel_current_span_key == Qnil) {
1333
+ VALUE datadog_module = rb_const_get(rb_cObject, rb_intern("Datadog"));
1334
+ VALUE opentelemetry_module = rb_const_get(datadog_module, rb_intern("OpenTelemetry"));
1335
+ VALUE api_module = rb_const_get(opentelemetry_module, rb_intern("API"));
1336
+ VALUE context_module = rb_const_get(api_module, rb_intern_const("Context"));
1337
+ VALUE current_span_key = rb_const_get(context_module, rb_intern_const("CURRENT_SPAN_KEY"));
1338
+
1339
+ if (current_span_key == Qnil) {
1340
+ rb_raise(rb_eRuntimeError, "Unexpected: Missing Datadog::OpenTelemetry::API::Context::CURRENT_SPAN_KEY");
1341
+ }
1342
+
1343
+ state->otel_current_span_key = current_span_key;
1344
+ }
1345
+
1346
+ return state->otel_current_span_key;
1347
+ }
1348
+
1349
+ // This method gets used when ddtrace is being used indirectly via the otel APIs. Information gets stored slightly
1350
+ // differently, and this codepath handles it.
1351
+ static void ddtrace_otel_trace_identifiers_for(
1352
+ struct thread_context_collector_state *state,
1353
+ VALUE *active_trace,
1354
+ VALUE *root_span,
1355
+ VALUE *numeric_span_id,
1356
+ VALUE active_span,
1357
+ VALUE otel_values
1358
+ ) {
1359
+ VALUE resolved_numeric_span_id =
1360
+ active_span == Qnil ?
1361
+ // For traces started from otel spans, the span id will be empty, and the @parent_span_id has the right value
1362
+ rb_ivar_get(*active_trace, at_parent_span_id_id /* @parent_span_id */) :
1363
+ // Regular span created by ddtrace
1364
+ rb_ivar_get(active_span, at_id_id /* @id */);
1365
+
1366
+ if (resolved_numeric_span_id == Qnil) return;
1367
+
1368
+ VALUE otel_current_span_key = get_otel_current_span_key(state);
1369
+ VALUE current_trace = *active_trace;
1370
+
1371
+ // ddtrace uses a different structure when spans are created from otel, where each otel span will have a unique ddtrace
1372
+ // trace and span representing it. Each ddtrace trace is then connected to the previous otel span, forming a linked
1373
+ // list. The local root span is going to be the trace/span we find at the end of this linked list.
1374
+ while (otel_values != Qnil) {
1375
+ VALUE otel_span = rb_hash_lookup(otel_values, otel_current_span_key);
1376
+ if (otel_span == Qnil) break;
1377
+ VALUE next_trace = rb_ivar_get(otel_span, at_datadog_trace_id);
1378
+ if (next_trace == Qnil) break;
1379
+
1380
+ current_trace = next_trace;
1381
+ otel_values = rb_ivar_get(current_trace, at_otel_values_id /* @otel_values */);
1382
+ }
1383
+
1384
+ // We found the last trace in the linked list. This contains the local root span
1385
+ VALUE resolved_root_span = rb_ivar_get(current_trace, at_root_span_id /* @root_span */);
1386
+ if (resolved_root_span == Qnil) return;
1387
+
1388
+ *root_span = resolved_root_span;
1389
+ *active_trace = current_trace;
1390
+ *numeric_span_id = resolved_numeric_span_id;
1391
+ }