ls-trace 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (356) hide show
  1. checksums.yaml +7 -0
  2. data/.circleci/config.yml +673 -0
  3. data/.circleci/images/primary/Dockerfile-2.0.0 +73 -0
  4. data/.circleci/images/primary/Dockerfile-2.1.10 +73 -0
  5. data/.circleci/images/primary/Dockerfile-2.2.10 +73 -0
  6. data/.circleci/images/primary/Dockerfile-2.3.8 +75 -0
  7. data/.circleci/images/primary/Dockerfile-2.4.6 +73 -0
  8. data/.circleci/images/primary/Dockerfile-2.5.6 +73 -0
  9. data/.circleci/images/primary/Dockerfile-2.6.4 +73 -0
  10. data/.dockerignore +1 -0
  11. data/.env +24 -0
  12. data/.github/CODEOWNERS +1 -0
  13. data/.gitignore +59 -0
  14. data/.rspec +1 -0
  15. data/.rubocop.yml +77 -0
  16. data/.yardopts +5 -0
  17. data/Appraisals +820 -0
  18. data/CHANGELOG.md +1051 -0
  19. data/CONTRIBUTING.md +85 -0
  20. data/Gemfile +7 -0
  21. data/LICENSE +24 -0
  22. data/README.md +108 -0
  23. data/Rakefile +635 -0
  24. data/benchmarks/postgres_database.yml +9 -0
  25. data/benchmarks/sidekiq_test.rb +154 -0
  26. data/ddtrace.gemspec +63 -0
  27. data/docker-compose.yml +276 -0
  28. data/docs/DevelopmentGuide.md +195 -0
  29. data/docs/GettingStarted.md +1981 -0
  30. data/lib/ddtrace.rb +63 -0
  31. data/lib/ddtrace/analytics.rb +29 -0
  32. data/lib/ddtrace/augmentation.rb +13 -0
  33. data/lib/ddtrace/augmentation/method_wrapper.rb +20 -0
  34. data/lib/ddtrace/augmentation/method_wrapping.rb +38 -0
  35. data/lib/ddtrace/augmentation/shim.rb +102 -0
  36. data/lib/ddtrace/buffer.rb +119 -0
  37. data/lib/ddtrace/configuration.rb +30 -0
  38. data/lib/ddtrace/configuration/base.rb +82 -0
  39. data/lib/ddtrace/configuration/dependency_resolver.rb +24 -0
  40. data/lib/ddtrace/configuration/option.rb +55 -0
  41. data/lib/ddtrace/configuration/option_definition.rb +127 -0
  42. data/lib/ddtrace/configuration/option_definition_set.rb +18 -0
  43. data/lib/ddtrace/configuration/option_set.rb +6 -0
  44. data/lib/ddtrace/configuration/options.rb +107 -0
  45. data/lib/ddtrace/configuration/pin_setup.rb +30 -0
  46. data/lib/ddtrace/configuration/settings.rb +105 -0
  47. data/lib/ddtrace/context.rb +284 -0
  48. data/lib/ddtrace/context_flush.rb +132 -0
  49. data/lib/ddtrace/contrib/action_pack/action_controller/instrumentation.rb +144 -0
  50. data/lib/ddtrace/contrib/action_pack/action_controller/patcher.rb +37 -0
  51. data/lib/ddtrace/contrib/action_pack/configuration/settings.rb +27 -0
  52. data/lib/ddtrace/contrib/action_pack/ext.rb +16 -0
  53. data/lib/ddtrace/contrib/action_pack/integration.rb +36 -0
  54. data/lib/ddtrace/contrib/action_pack/patcher.rb +29 -0
  55. data/lib/ddtrace/contrib/action_pack/utils.rb +36 -0
  56. data/lib/ddtrace/contrib/action_view/configuration/settings.rb +26 -0
  57. data/lib/ddtrace/contrib/action_view/ext.rb +17 -0
  58. data/lib/ddtrace/contrib/action_view/instrumentation/partial_renderer.rb +78 -0
  59. data/lib/ddtrace/contrib/action_view/instrumentation/template_renderer.rb +167 -0
  60. data/lib/ddtrace/contrib/action_view/integration.rb +43 -0
  61. data/lib/ddtrace/contrib/action_view/patcher.rb +53 -0
  62. data/lib/ddtrace/contrib/action_view/utils.rb +32 -0
  63. data/lib/ddtrace/contrib/active_model_serializers/configuration/settings.rb +25 -0
  64. data/lib/ddtrace/contrib/active_model_serializers/event.rb +65 -0
  65. data/lib/ddtrace/contrib/active_model_serializers/events.rb +30 -0
  66. data/lib/ddtrace/contrib/active_model_serializers/events/render.rb +32 -0
  67. data/lib/ddtrace/contrib/active_model_serializers/events/serialize.rb +35 -0
  68. data/lib/ddtrace/contrib/active_model_serializers/ext.rb +17 -0
  69. data/lib/ddtrace/contrib/active_model_serializers/integration.rb +39 -0
  70. data/lib/ddtrace/contrib/active_model_serializers/patcher.rb +36 -0
  71. data/lib/ddtrace/contrib/active_record/configuration/resolver.rb +46 -0
  72. data/lib/ddtrace/contrib/active_record/configuration/settings.rb +30 -0
  73. data/lib/ddtrace/contrib/active_record/event.rb +30 -0
  74. data/lib/ddtrace/contrib/active_record/events.rb +30 -0
  75. data/lib/ddtrace/contrib/active_record/events/instantiation.rb +57 -0
  76. data/lib/ddtrace/contrib/active_record/events/sql.rb +64 -0
  77. data/lib/ddtrace/contrib/active_record/ext.rb +21 -0
  78. data/lib/ddtrace/contrib/active_record/integration.rb +44 -0
  79. data/lib/ddtrace/contrib/active_record/patcher.rb +29 -0
  80. data/lib/ddtrace/contrib/active_record/utils.rb +76 -0
  81. data/lib/ddtrace/contrib/active_support/cache/instrumentation.rb +157 -0
  82. data/lib/ddtrace/contrib/active_support/cache/patcher.rb +62 -0
  83. data/lib/ddtrace/contrib/active_support/cache/redis.rb +47 -0
  84. data/lib/ddtrace/contrib/active_support/configuration/settings.rb +25 -0
  85. data/lib/ddtrace/contrib/active_support/ext.rb +21 -0
  86. data/lib/ddtrace/contrib/active_support/integration.rb +38 -0
  87. data/lib/ddtrace/contrib/active_support/notifications/event.rb +62 -0
  88. data/lib/ddtrace/contrib/active_support/notifications/subscriber.rb +66 -0
  89. data/lib/ddtrace/contrib/active_support/notifications/subscription.rb +159 -0
  90. data/lib/ddtrace/contrib/active_support/patcher.rb +29 -0
  91. data/lib/ddtrace/contrib/analytics.rb +20 -0
  92. data/lib/ddtrace/contrib/aws/configuration/settings.rb +25 -0
  93. data/lib/ddtrace/contrib/aws/ext.rb +20 -0
  94. data/lib/ddtrace/contrib/aws/instrumentation.rb +56 -0
  95. data/lib/ddtrace/contrib/aws/integration.rb +36 -0
  96. data/lib/ddtrace/contrib/aws/parsed_context.rb +56 -0
  97. data/lib/ddtrace/contrib/aws/patcher.rb +49 -0
  98. data/lib/ddtrace/contrib/aws/services.rb +115 -0
  99. data/lib/ddtrace/contrib/concurrent_ruby/configuration/settings.rb +15 -0
  100. data/lib/ddtrace/contrib/concurrent_ruby/context_composite_executor_service.rb +35 -0
  101. data/lib/ddtrace/contrib/concurrent_ruby/ext.rb +11 -0
  102. data/lib/ddtrace/contrib/concurrent_ruby/future_patch.rb +23 -0
  103. data/lib/ddtrace/contrib/concurrent_ruby/integration.rb +32 -0
  104. data/lib/ddtrace/contrib/concurrent_ruby/patcher.rb +35 -0
  105. data/lib/ddtrace/contrib/configurable.rb +59 -0
  106. data/lib/ddtrace/contrib/configuration/resolver.rb +12 -0
  107. data/lib/ddtrace/contrib/configuration/settings.rb +35 -0
  108. data/lib/ddtrace/contrib/dalli/configuration/settings.rb +25 -0
  109. data/lib/ddtrace/contrib/dalli/ext.rb +17 -0
  110. data/lib/ddtrace/contrib/dalli/instrumentation.rb +50 -0
  111. data/lib/ddtrace/contrib/dalli/integration.rb +36 -0
  112. data/lib/ddtrace/contrib/dalli/patcher.rb +73 -0
  113. data/lib/ddtrace/contrib/dalli/quantize.rb +22 -0
  114. data/lib/ddtrace/contrib/delayed_job/configuration/settings.rb +25 -0
  115. data/lib/ddtrace/contrib/delayed_job/ext.rb +18 -0
  116. data/lib/ddtrace/contrib/delayed_job/integration.rb +32 -0
  117. data/lib/ddtrace/contrib/delayed_job/patcher.rb +34 -0
  118. data/lib/ddtrace/contrib/delayed_job/plugin.rb +57 -0
  119. data/lib/ddtrace/contrib/elasticsearch/configuration/settings.rb +26 -0
  120. data/lib/ddtrace/contrib/elasticsearch/ext.rb +19 -0
  121. data/lib/ddtrace/contrib/elasticsearch/integration.rb +37 -0
  122. data/lib/ddtrace/contrib/elasticsearch/patcher.rb +124 -0
  123. data/lib/ddtrace/contrib/elasticsearch/quantize.rb +80 -0
  124. data/lib/ddtrace/contrib/ethon/configuration/settings.rb +26 -0
  125. data/lib/ddtrace/contrib/ethon/easy_patch.rb +139 -0
  126. data/lib/ddtrace/contrib/ethon/ext.rb +15 -0
  127. data/lib/ddtrace/contrib/ethon/integration.rb +31 -0
  128. data/lib/ddtrace/contrib/ethon/multi_patch.rb +80 -0
  129. data/lib/ddtrace/contrib/ethon/patcher.rb +27 -0
  130. data/lib/ddtrace/contrib/excon/configuration/settings.rb +28 -0
  131. data/lib/ddtrace/contrib/excon/ext.rb +14 -0
  132. data/lib/ddtrace/contrib/excon/integration.rb +32 -0
  133. data/lib/ddtrace/contrib/excon/middleware.rb +154 -0
  134. data/lib/ddtrace/contrib/excon/patcher.rb +34 -0
  135. data/lib/ddtrace/contrib/extensions.rb +59 -0
  136. data/lib/ddtrace/contrib/faraday/configuration/settings.rb +33 -0
  137. data/lib/ddtrace/contrib/faraday/ext.rb +14 -0
  138. data/lib/ddtrace/contrib/faraday/integration.rb +36 -0
  139. data/lib/ddtrace/contrib/faraday/middleware.rb +93 -0
  140. data/lib/ddtrace/contrib/faraday/patcher.rb +82 -0
  141. data/lib/ddtrace/contrib/faraday/rack_builder.rb +18 -0
  142. data/lib/ddtrace/contrib/grape/configuration/settings.rb +27 -0
  143. data/lib/ddtrace/contrib/grape/endpoint.rb +199 -0
  144. data/lib/ddtrace/contrib/grape/ext.rb +19 -0
  145. data/lib/ddtrace/contrib/grape/instrumentation.rb +33 -0
  146. data/lib/ddtrace/contrib/grape/integration.rb +36 -0
  147. data/lib/ddtrace/contrib/grape/patcher.rb +79 -0
  148. data/lib/ddtrace/contrib/graphql/configuration/settings.rb +27 -0
  149. data/lib/ddtrace/contrib/graphql/ext.rb +13 -0
  150. data/lib/ddtrace/contrib/graphql/integration.rb +38 -0
  151. data/lib/ddtrace/contrib/graphql/patcher.rb +63 -0
  152. data/lib/ddtrace/contrib/grpc/configuration/settings.rb +25 -0
  153. data/lib/ddtrace/contrib/grpc/datadog_interceptor.rb +74 -0
  154. data/lib/ddtrace/contrib/grpc/datadog_interceptor/client.rb +56 -0
  155. data/lib/ddtrace/contrib/grpc/datadog_interceptor/server.rb +73 -0
  156. data/lib/ddtrace/contrib/grpc/ext.rb +15 -0
  157. data/lib/ddtrace/contrib/grpc/integration.rb +36 -0
  158. data/lib/ddtrace/contrib/grpc/intercept_with_datadog.rb +49 -0
  159. data/lib/ddtrace/contrib/grpc/patcher.rb +78 -0
  160. data/lib/ddtrace/contrib/http/circuit_breaker.rb +63 -0
  161. data/lib/ddtrace/contrib/http/configuration/settings.rb +26 -0
  162. data/lib/ddtrace/contrib/http/ext.rb +14 -0
  163. data/lib/ddtrace/contrib/http/instrumentation.rb +114 -0
  164. data/lib/ddtrace/contrib/http/integration.rb +32 -0
  165. data/lib/ddtrace/contrib/http/patcher.rb +32 -0
  166. data/lib/ddtrace/contrib/integration.rb +16 -0
  167. data/lib/ddtrace/contrib/mongodb/configuration/settings.rb +28 -0
  168. data/lib/ddtrace/contrib/mongodb/ext.rb +20 -0
  169. data/lib/ddtrace/contrib/mongodb/instrumentation.rb +68 -0
  170. data/lib/ddtrace/contrib/mongodb/integration.rb +36 -0
  171. data/lib/ddtrace/contrib/mongodb/parsers.rb +68 -0
  172. data/lib/ddtrace/contrib/mongodb/patcher.rb +37 -0
  173. data/lib/ddtrace/contrib/mongodb/subscribers.rb +108 -0
  174. data/lib/ddtrace/contrib/mysql2/configuration/settings.rb +25 -0
  175. data/lib/ddtrace/contrib/mysql2/ext.rb +15 -0
  176. data/lib/ddtrace/contrib/mysql2/instrumentation.rb +60 -0
  177. data/lib/ddtrace/contrib/mysql2/integration.rb +32 -0
  178. data/lib/ddtrace/contrib/mysql2/patcher.rb +33 -0
  179. data/lib/ddtrace/contrib/patchable.rb +42 -0
  180. data/lib/ddtrace/contrib/patcher.rb +28 -0
  181. data/lib/ddtrace/contrib/racecar/configuration/settings.rb +25 -0
  182. data/lib/ddtrace/contrib/racecar/event.rb +67 -0
  183. data/lib/ddtrace/contrib/racecar/events.rb +30 -0
  184. data/lib/ddtrace/contrib/racecar/events/batch.rb +27 -0
  185. data/lib/ddtrace/contrib/racecar/events/message.rb +27 -0
  186. data/lib/ddtrace/contrib/racecar/ext.rb +21 -0
  187. data/lib/ddtrace/contrib/racecar/integration.rb +36 -0
  188. data/lib/ddtrace/contrib/racecar/patcher.rb +32 -0
  189. data/lib/ddtrace/contrib/rack/configuration/settings.rb +41 -0
  190. data/lib/ddtrace/contrib/rack/ext.rb +18 -0
  191. data/lib/ddtrace/contrib/rack/integration.rb +32 -0
  192. data/lib/ddtrace/contrib/rack/middlewares.rb +283 -0
  193. data/lib/ddtrace/contrib/rack/patcher.rb +72 -0
  194. data/lib/ddtrace/contrib/rack/request_queue.rb +39 -0
  195. data/lib/ddtrace/contrib/rails/configuration/settings.rb +80 -0
  196. data/lib/ddtrace/contrib/rails/ext.rb +12 -0
  197. data/lib/ddtrace/contrib/rails/framework.rb +100 -0
  198. data/lib/ddtrace/contrib/rails/integration.rb +37 -0
  199. data/lib/ddtrace/contrib/rails/middlewares.rb +38 -0
  200. data/lib/ddtrace/contrib/rails/patcher.rb +78 -0
  201. data/lib/ddtrace/contrib/rails/railtie.rb +17 -0
  202. data/lib/ddtrace/contrib/rails/utils.rb +20 -0
  203. data/lib/ddtrace/contrib/rake/configuration/settings.rb +27 -0
  204. data/lib/ddtrace/contrib/rake/ext.rb +18 -0
  205. data/lib/ddtrace/contrib/rake/instrumentation.rb +84 -0
  206. data/lib/ddtrace/contrib/rake/integration.rb +32 -0
  207. data/lib/ddtrace/contrib/rake/patcher.rb +36 -0
  208. data/lib/ddtrace/contrib/redis/configuration/settings.rb +25 -0
  209. data/lib/ddtrace/contrib/redis/ext.rb +18 -0
  210. data/lib/ddtrace/contrib/redis/integration.rb +36 -0
  211. data/lib/ddtrace/contrib/redis/patcher.rb +94 -0
  212. data/lib/ddtrace/contrib/redis/quantize.rb +47 -0
  213. data/lib/ddtrace/contrib/redis/tags.rb +38 -0
  214. data/lib/ddtrace/contrib/registerable.rb +33 -0
  215. data/lib/ddtrace/contrib/registry.rb +42 -0
  216. data/lib/ddtrace/contrib/resque/configuration/settings.rb +26 -0
  217. data/lib/ddtrace/contrib/resque/ext.rb +14 -0
  218. data/lib/ddtrace/contrib/resque/integration.rb +37 -0
  219. data/lib/ddtrace/contrib/resque/patcher.rb +35 -0
  220. data/lib/ddtrace/contrib/resque/resque_job.rb +76 -0
  221. data/lib/ddtrace/contrib/rest_client/configuration/settings.rb +26 -0
  222. data/lib/ddtrace/contrib/rest_client/ext.rb +14 -0
  223. data/lib/ddtrace/contrib/rest_client/integration.rb +31 -0
  224. data/lib/ddtrace/contrib/rest_client/patcher.rb +25 -0
  225. data/lib/ddtrace/contrib/rest_client/request_patch.rb +89 -0
  226. data/lib/ddtrace/contrib/sequel/configuration/settings.rb +23 -0
  227. data/lib/ddtrace/contrib/sequel/database.rb +61 -0
  228. data/lib/ddtrace/contrib/sequel/dataset.rb +62 -0
  229. data/lib/ddtrace/contrib/sequel/ext.rb +15 -0
  230. data/lib/ddtrace/contrib/sequel/integration.rb +32 -0
  231. data/lib/ddtrace/contrib/sequel/patcher.rb +39 -0
  232. data/lib/ddtrace/contrib/sequel/utils.rb +46 -0
  233. data/lib/ddtrace/contrib/shoryuken/configuration/settings.rb +24 -0
  234. data/lib/ddtrace/contrib/shoryuken/ext.rb +18 -0
  235. data/lib/ddtrace/contrib/shoryuken/integration.rb +35 -0
  236. data/lib/ddtrace/contrib/shoryuken/patcher.rb +30 -0
  237. data/lib/ddtrace/contrib/shoryuken/tracer.rb +45 -0
  238. data/lib/ddtrace/contrib/sidekiq/client_tracer.rb +43 -0
  239. data/lib/ddtrace/contrib/sidekiq/configuration/settings.rb +26 -0
  240. data/lib/ddtrace/contrib/sidekiq/ext.rb +21 -0
  241. data/lib/ddtrace/contrib/sidekiq/integration.rb +36 -0
  242. data/lib/ddtrace/contrib/sidekiq/patcher.rb +40 -0
  243. data/lib/ddtrace/contrib/sidekiq/server_tracer.rb +58 -0
  244. data/lib/ddtrace/contrib/sidekiq/tracing.rb +28 -0
  245. data/lib/ddtrace/contrib/sinatra/configuration/settings.rb +34 -0
  246. data/lib/ddtrace/contrib/sinatra/env.rb +38 -0
  247. data/lib/ddtrace/contrib/sinatra/ext.rb +18 -0
  248. data/lib/ddtrace/contrib/sinatra/headers.rb +31 -0
  249. data/lib/ddtrace/contrib/sinatra/integration.rb +36 -0
  250. data/lib/ddtrace/contrib/sinatra/patcher.rb +33 -0
  251. data/lib/ddtrace/contrib/sinatra/tracer.rb +84 -0
  252. data/lib/ddtrace/contrib/sinatra/tracer_middleware.rb +72 -0
  253. data/lib/ddtrace/contrib/sucker_punch/configuration/settings.rb +25 -0
  254. data/lib/ddtrace/contrib/sucker_punch/exception_handler.rb +26 -0
  255. data/lib/ddtrace/contrib/sucker_punch/ext.rb +18 -0
  256. data/lib/ddtrace/contrib/sucker_punch/instrumentation.rb +70 -0
  257. data/lib/ddtrace/contrib/sucker_punch/integration.rb +36 -0
  258. data/lib/ddtrace/contrib/sucker_punch/patcher.rb +48 -0
  259. data/lib/ddtrace/correlation.rb +28 -0
  260. data/lib/ddtrace/diagnostics/health.rb +30 -0
  261. data/lib/ddtrace/distributed_tracing/headers/b3.rb +44 -0
  262. data/lib/ddtrace/distributed_tracing/headers/b3_single.rb +56 -0
  263. data/lib/ddtrace/distributed_tracing/headers/datadog.rb +42 -0
  264. data/lib/ddtrace/distributed_tracing/headers/headers.rb +70 -0
  265. data/lib/ddtrace/distributed_tracing/headers/helpers.rb +45 -0
  266. data/lib/ddtrace/encoding.rb +65 -0
  267. data/lib/ddtrace/environment.rb +23 -0
  268. data/lib/ddtrace/error.rb +27 -0
  269. data/lib/ddtrace/ext/analytics.rb +11 -0
  270. data/lib/ddtrace/ext/app_types.rb +11 -0
  271. data/lib/ddtrace/ext/diagnostics.rb +25 -0
  272. data/lib/ddtrace/ext/distributed.rb +33 -0
  273. data/lib/ddtrace/ext/errors.rb +10 -0
  274. data/lib/ddtrace/ext/forced_tracing.rb +25 -0
  275. data/lib/ddtrace/ext/http.rb +46 -0
  276. data/lib/ddtrace/ext/manual_tracing.rb +9 -0
  277. data/lib/ddtrace/ext/metrics.rb +15 -0
  278. data/lib/ddtrace/ext/net.rb +10 -0
  279. data/lib/ddtrace/ext/priority.rb +16 -0
  280. data/lib/ddtrace/ext/runtime.rb +26 -0
  281. data/lib/ddtrace/ext/sql.rb +8 -0
  282. data/lib/ddtrace/ext/transport.rb +17 -0
  283. data/lib/ddtrace/forced_tracing.rb +36 -0
  284. data/lib/ddtrace/logger.rb +39 -0
  285. data/lib/ddtrace/metrics.rb +215 -0
  286. data/lib/ddtrace/monkey.rb +58 -0
  287. data/lib/ddtrace/opentracer.rb +40 -0
  288. data/lib/ddtrace/opentracer/binary_propagator.rb +24 -0
  289. data/lib/ddtrace/opentracer/carrier.rb +6 -0
  290. data/lib/ddtrace/opentracer/distributed_headers.rb +52 -0
  291. data/lib/ddtrace/opentracer/global_tracer.rb +15 -0
  292. data/lib/ddtrace/opentracer/propagator.rb +22 -0
  293. data/lib/ddtrace/opentracer/rack_propagator.rb +60 -0
  294. data/lib/ddtrace/opentracer/scope.rb +15 -0
  295. data/lib/ddtrace/opentracer/scope_manager.rb +6 -0
  296. data/lib/ddtrace/opentracer/span.rb +98 -0
  297. data/lib/ddtrace/opentracer/span_context.rb +14 -0
  298. data/lib/ddtrace/opentracer/span_context_factory.rb +23 -0
  299. data/lib/ddtrace/opentracer/text_map_propagator.rb +75 -0
  300. data/lib/ddtrace/opentracer/thread_local_scope.rb +30 -0
  301. data/lib/ddtrace/opentracer/thread_local_scope_manager.rb +40 -0
  302. data/lib/ddtrace/opentracer/tracer.rb +208 -0
  303. data/lib/ddtrace/patcher.rb +47 -0
  304. data/lib/ddtrace/pin.rb +114 -0
  305. data/lib/ddtrace/pipeline.rb +46 -0
  306. data/lib/ddtrace/pipeline/span_filter.rb +38 -0
  307. data/lib/ddtrace/pipeline/span_processor.rb +20 -0
  308. data/lib/ddtrace/propagation/grpc_propagator.rb +61 -0
  309. data/lib/ddtrace/propagation/http_propagator.rb +75 -0
  310. data/lib/ddtrace/provider.rb +21 -0
  311. data/lib/ddtrace/quantization/hash.rb +103 -0
  312. data/lib/ddtrace/quantization/http.rb +86 -0
  313. data/lib/ddtrace/runtime/cgroup.rb +44 -0
  314. data/lib/ddtrace/runtime/class_count.rb +17 -0
  315. data/lib/ddtrace/runtime/container.rb +73 -0
  316. data/lib/ddtrace/runtime/gc.rb +16 -0
  317. data/lib/ddtrace/runtime/identity.rb +41 -0
  318. data/lib/ddtrace/runtime/metrics.rb +93 -0
  319. data/lib/ddtrace/runtime/object_space.rb +19 -0
  320. data/lib/ddtrace/runtime/socket.rb +14 -0
  321. data/lib/ddtrace/runtime/thread_count.rb +16 -0
  322. data/lib/ddtrace/sampler.rb +195 -0
  323. data/lib/ddtrace/span.rb +260 -0
  324. data/lib/ddtrace/sync_writer.rb +62 -0
  325. data/lib/ddtrace/tracer.rb +459 -0
  326. data/lib/ddtrace/transport/http.rb +91 -0
  327. data/lib/ddtrace/transport/http/adapters/net.rb +112 -0
  328. data/lib/ddtrace/transport/http/adapters/registry.rb +24 -0
  329. data/lib/ddtrace/transport/http/adapters/test.rb +77 -0
  330. data/lib/ddtrace/transport/http/adapters/unix_socket.rb +64 -0
  331. data/lib/ddtrace/transport/http/api.rb +46 -0
  332. data/lib/ddtrace/transport/http/api/endpoint.rb +27 -0
  333. data/lib/ddtrace/transport/http/api/fallbacks.rb +22 -0
  334. data/lib/ddtrace/transport/http/api/instance.rb +29 -0
  335. data/lib/ddtrace/transport/http/api/map.rb +14 -0
  336. data/lib/ddtrace/transport/http/api/spec.rb +15 -0
  337. data/lib/ddtrace/transport/http/builder.rb +165 -0
  338. data/lib/ddtrace/transport/http/client.rb +107 -0
  339. data/lib/ddtrace/transport/http/env.rb +48 -0
  340. data/lib/ddtrace/transport/http/response.rb +26 -0
  341. data/lib/ddtrace/transport/http/statistics.rb +30 -0
  342. data/lib/ddtrace/transport/http/traces.rb +140 -0
  343. data/lib/ddtrace/transport/parcel.rb +13 -0
  344. data/lib/ddtrace/transport/request.rb +13 -0
  345. data/lib/ddtrace/transport/response.rb +49 -0
  346. data/lib/ddtrace/transport/statistics.rb +72 -0
  347. data/lib/ddtrace/transport/traces.rb +33 -0
  348. data/lib/ddtrace/utils.rb +65 -0
  349. data/lib/ddtrace/utils/database.rb +25 -0
  350. data/lib/ddtrace/utils/time.rb +14 -0
  351. data/lib/ddtrace/vendor/active_record/connection_specification.rb +301 -0
  352. data/lib/ddtrace/version.rb +12 -0
  353. data/lib/ddtrace/workers.rb +125 -0
  354. data/lib/ddtrace/writer.rb +157 -0
  355. data/tasks/release_gem.rake +28 -0
  356. metadata +682 -0
@@ -0,0 +1,195 @@
1
+ # Developing
2
+
3
+ This guide covers some of the common how-tos and technical reference material for developing changes within the trace library.
4
+
5
+ ## Table of Contents
6
+
7
+ - [Setting up](#setting-up)
8
+ - [Testing](#testing)
9
+ - [Writing tests](#writing-tests)
10
+ - [Running tests](#running-tests)
11
+ - [Checking code quality](#checking-code-quality)
12
+ - [Appendix](#appendix)
13
+ - [Writing new integrations](#writing-new-integrations)
14
+ - [Custom transport adapters](#custom-transport-adapters)
15
+
16
+ ## Setting up
17
+
18
+ *NOTE: To test locally, you must have `Docker` and `Docker Compose` installed. See the [Docker documentation](https://docs.docker.com/compose/install/) for details.*
19
+
20
+ The trace library uses Docker Compose to create a Ruby environment to develop and test within, as well as containers for any dependencies that might be necessary for certain kinds of tests.
21
+
22
+ To start a development environment, choose a target Ruby version then run the following:
23
+
24
+ ```
25
+ # In the root directory of the project...
26
+ cd ~/dd-trace-rb
27
+
28
+ # Create and start a Ruby 2.3 test environment with its dependencies
29
+ docker-compose run --rm tracer-2.3 /bin/bash
30
+
31
+ # Then inside the container (e.g. `root@2a73c6d8673e:/app`)...
32
+ # Install the library dependencies
33
+ bundle install
34
+
35
+ # Install build targets
36
+ appraisal install
37
+ ```
38
+
39
+ Then within this container you can [run tests](#running-tests), or [run code quality checks](#checking-code-quality).
40
+
41
+ ## Testing
42
+
43
+ The test suite uses both [Minitest](https://github.com/seattlerb/minitest) and [RSpec](https://rspec.info/) tests to verify the correctness of both the core trace library and its integrations.
44
+
45
+ Minitest is deprecated in favor of RSpec; all new tests should be written in RSpec, and only existing minitests should be updated.
46
+
47
+ ### Writing tests
48
+
49
+ New tests should be written as RSpec tests in the `spec/ddtrace` folder. Test files should generally mirror the structure of `lib`.
50
+
51
+ All changes should be covered by a corresponding RSpec tests. Unit tests are preferred, and integration tests are accepted where appropriate (e.g. acceptance tests, verifying compatibility with datastores, etc) but should be kept to a minimum.
52
+
53
+ **Considerations for CI**
54
+
55
+ All tests should run in CI. When adding new `spec.rb` files, you may need to add a test task to ensure your test file is run in CI.
56
+
57
+ - Ensure that there is a corresponding Rake task defined in `Rakefile` under the the `spec` namespace, whose pattern matches your test file.
58
+ - Verify the Rake task is configured to run for the appropriate Ruby runtimes in the `ci` Rake task.
59
+
60
+ ### Running tests
61
+
62
+ Simplest way to run tests is to run `bundle exec rake ci`, which will run the entire test suite, just as CI does.
63
+
64
+ **For the core library**
65
+
66
+ Run the tests for the core library with:
67
+
68
+ ```
69
+ # Run Minitest
70
+ $ bundle exec rake test:main
71
+ # Run RSpec
72
+ $ bundle exec rake spec:main
73
+ ```
74
+
75
+ **For integrations**
76
+
77
+ Integrations which interact with dependencies not listed in the `ddtrace` gemspec will need to load these dependencies to run their tests.
78
+
79
+ To do so, load the dependencies using [Appraisal](https://github.com/thoughtbot/appraisal). You can see a list of available appraisals with `bundle exec appraisal list`, or examine the `Appraisals` file.
80
+
81
+ Then to run tests, prefix the test commain with the appraisal. For example:
82
+
83
+ ```
84
+ # Runs tests for Rails 3.2 + Postgres
85
+ $ bundle exec appraisal rails32-postgres spec:rails
86
+ # Runs tests for Redis
87
+ $ bundle exec appraisal contrib rake spec:redis
88
+ ```
89
+
90
+ **Passing arguments to tests**
91
+
92
+ When running RSpec tests, you may pass additional args as parameters to the Rake task. For example:
93
+
94
+ ```
95
+ # Runs Redis tests with seed 1234
96
+ $ bundle exec appraisal contrib rake spec:redis'[--seed,1234]'
97
+ ```
98
+
99
+ This can be useful for replicating conditions from CI or isolating certain tests.
100
+
101
+ ### Checking code quality
102
+
103
+ **Linting**
104
+
105
+ The trace library uses Rubocop to enforce [code style](https://github.com/bbatsov/ruby-style-guide) and quality. To check, run:
106
+
107
+ ```
108
+ $ bundle exec rake rubocop
109
+ ```
110
+
111
+ ## Appendix
112
+
113
+ ### Writing new integrations
114
+
115
+ Integrations are extensions to the trace library that add support for external dependencies (gems); they typically add auto-instrumentation to popular gems and frameworks. You will find many of our integrations in the `contrib` folder.
116
+
117
+ Some general guidelines for adding new integrations:
118
+
119
+ - An integration can either be added directly to `dd-trace-rb`, or developed as its own gem that depends on `ddtrace`.
120
+ - Integrations should implement the configuration API for easy, consistent implementation. (See existing integrations as examples of this.)
121
+ - All new integrations require documentation, unit/integration tests written in RSpec, and passing CI builds.
122
+ - It's highly encouraged to share screenshots or other demos of how the new integration looks and works.
123
+
124
+ To get started quickly, it's perfectly fine to copy-paste an existing integration to use as a template, then modify it to match your needs. This is usually the fastest, easiest way to bootstrap a new integration and makes the time-to-first-trace often very quick, usually less than an hour if it's a simple implementation.
125
+
126
+ Once you have it working in your application, you can [add unit tests](#writing-tests), [run them locally](#running-tests), and [check for code quality](#checking-code-quality) using Docker Compose.
127
+
128
+ Then [open a pull request](https://github.com/DataDog/dd-trace-rb/CONTRIBUTING.md#have-a-patch) and be sure to add the following to the description:
129
+
130
+ - [Documentation](https://github.com/DataDog/dd-trace-rb/docs/GettingStarted.md) for the integration, including versions supported.
131
+ - Links to the repository/website of the library being integrated
132
+ - Screenshots showing a sample trace
133
+ - Any additional code snippets, sample apps, benchmarks, or other resources that demonstrate its implementation are a huge plus!
134
+
135
+ ### Custom transport adapters
136
+
137
+ The tracer can be configured with transports that customize how data is sent and where it is sent to. This is done through the use of adapters: classes that receive generic requests, process them, and return appropriate responses.
138
+
139
+ #### Developing HTTP transport adapters
140
+
141
+ To create a custom HTTP adapter, define a class that responds to `call(env)` which returns a kind of `Datadog::Transport::Response`:
142
+
143
+ ```ruby
144
+ require 'ddtrace/transport/response'
145
+
146
+ class CustomAdapter
147
+ # Sends HTTP request
148
+ # env: Datadog::Transport::HTTP::Env
149
+ def call(env)
150
+ # Add custom code here to send data.
151
+ # Then return a Response object.
152
+ Response.new
153
+ end
154
+
155
+ class Response
156
+ include Datadog::Transport::Response
157
+
158
+ # Implement the following methods as appropriate
159
+ # for your adapter.
160
+
161
+ # Return a String
162
+ def payload; end
163
+
164
+ # Return true/false
165
+ # Return nil if it does not apply
166
+ def ok?; end
167
+ def unsupported?; end
168
+ def not_found?; end
169
+ def client_error?; end
170
+ def server_error?; end
171
+ def internal_error?; end
172
+ end
173
+ end
174
+ ```
175
+
176
+ Optionally, you can register the adapter as a well-known type:
177
+
178
+ ```ruby
179
+ Datadog::Transport::HTTP::Builder::REGISTRY.set(CustomAdapter, :custom)
180
+ ```
181
+
182
+ Then pass an adapter instance to the tracer configuration:
183
+
184
+ ```ruby
185
+ Datadog.configure do |c|
186
+ c.tracer transport_options: proc { |t|
187
+ # By name
188
+ t.adapter :custom
189
+
190
+ # By instance
191
+ custom_adapter = CustomAdapter.new
192
+ t.adapter custom_adapter
193
+ }
194
+ end
195
+ ```
@@ -0,0 +1,1981 @@
1
+ # Datadog Ruby Trace Client
2
+
3
+ `ddtrace` is Datadog’s tracing client for Ruby. It is used to trace requests as they flow across web servers,
4
+ databases and microservices so that developers have high visibility into bottlenecks and troublesome requests.
5
+
6
+ ## Getting started
7
+
8
+ For the general APM documentation, see our [setup documentation][setup docs].
9
+
10
+ For more information about what APM looks like once your application is sending information to Datadog, take a look at [Visualizing your APM data][visualization docs].
11
+
12
+ To contribute, check out the [contribution guidelines][contribution docs] and [development guide][development docs].
13
+
14
+ [setup docs]: https://docs.datadoghq.com/tracing/
15
+ [development docs]: https://github.com/DataDog/dd-trace-rb/blob/master/README.md#development
16
+ [visualization docs]: https://docs.datadoghq.com/tracing/visualization/
17
+ [contribution docs]: https://github.com/DataDog/dd-trace-rb/blob/master/CONTRIBUTING.md
18
+ [development docs]: https://github.com/DataDog/dd-trace-rb/blob/master/docs/DevelopmentGuide.md
19
+
20
+ ## Table of Contents
21
+
22
+ - [Compatibility](#compatibility)
23
+ - [Installation](#installation)
24
+ - [Quickstart for Rails applications](#quickstart-for-rails-applications)
25
+ - [Quickstart for Ruby applications](#quickstart-for-ruby-applications)
26
+ - [Quickstart for OpenTracing](#quickstart-for-opentracing)
27
+ - [Manual instrumentation](#manual-instrumentation)
28
+ - [Integration instrumentation](#integration-instrumentation)
29
+ - [Action View](#action-view)
30
+ - [Active Model Serializers](#active-model-serializers)
31
+ - [Action Pack](#action-pack)
32
+ - [Active Record](#active-record)
33
+ - [Active Support](#active-support)
34
+ - [AWS](#aws)
35
+ - [Concurrent Ruby](#concurrent-ruby)
36
+ - [Dalli](#dalli)
37
+ - [DelayedJob](#delayedjob)
38
+ - [Elastic Search](#elastic-search)
39
+ - [Ethon & Typhoeus](#ethon)
40
+ - [Excon](#excon)
41
+ - [Faraday](#faraday)
42
+ - [Grape](#grape)
43
+ - [GraphQL](#graphql)
44
+ - [gRPC](#grpc)
45
+ - [MongoDB](#mongodb)
46
+ - [MySQL2](#mysql2)
47
+ - [Net/HTTP](#nethttp)
48
+ - [Racecar](#racecar)
49
+ - [Rack](#rack)
50
+ - [Rails](#rails)
51
+ - [Rake](#rake)
52
+ - [Redis](#redis)
53
+ - [Rest Client](#rest-client)
54
+ - [Resque](#resque)
55
+ - [Shoryuken](#shoryuken)
56
+ - [Sequel](#sequel)
57
+ - [Sidekiq](#sidekiq)
58
+ - [Sinatra](#sinatra)
59
+ - [Sucker Punch](#sucker-punch)
60
+ - [Advanced configuration](#advanced-configuration)
61
+ - [Tracer settings](#tracer-settings)
62
+ - [Custom logging](#custom-logging)
63
+ - [Environment and tags](#environment-and-tags)
64
+ - [Sampling](#sampling)
65
+ - [Priority sampling](#priority-sampling)
66
+ - [Distributed tracing](#distributed-tracing)
67
+ - [HTTP request queuing](#http-request-queuing)
68
+ - [Processing pipeline](#processing-pipeline)
69
+ - [Filtering](#filtering)
70
+ - [Processing](#processing)
71
+ - [Trace correlation](#trace-correlation)
72
+ - [Configuring the transport layer](#configuring-the-transport-layer)
73
+ - [Metrics](#metrics)
74
+ - [For application runtime](#for-application-runtime)
75
+ - [OpenTracing](#opentracing)
76
+
77
+ ## Compatibility
78
+
79
+ **Supported Ruby interpreters**:
80
+
81
+ | Type | Documentation | Version | Support type | Gem version support |
82
+ | ----- | -------------------------- | ----- | ------------------------------------ | ------------------- |
83
+ | MRI | https://www.ruby-lang.org/ | 2.6 | Full | Latest |
84
+ | | | 2.5 | Full | Latest |
85
+ | | | 2.4 | Full | Latest |
86
+ | | | 2.3 | Full | Latest |
87
+ | | | 2.2 | Full | Latest |
88
+ | | | 2.1 | Full | Latest |
89
+ | | | 2.0 | Full | Latest |
90
+ | | | 1.9.3 | Maintenance (until August 6th, 2020) | < 0.27.0 |
91
+ | | | 1.9.1 | Maintenance (until August 6th, 2020) | < 0.27.0 |
92
+ | JRuby | http://jruby.org/ | 9.1.5 | Alpha | Latest |
93
+
94
+ **Supported web servers**:
95
+
96
+ | Type | Documentation | Version | Support type |
97
+ | --------- | --------------------------------- | ------------ | ------------ |
98
+ | Puma | http://puma.io/ | 2.16+ / 3.6+ | Full |
99
+ | Unicorn | https://bogomips.org/unicorn/ | 4.8+ / 5.1+ | Full |
100
+ | Passenger | https://www.phusionpassenger.com/ | 5.0+ | Full |
101
+
102
+ **Supported tracing frameworks**:
103
+
104
+ | Type | Documentation | Version | Gem version support |
105
+ | ----------- | ----------------------------------------------- | --------------------- | ------------------- |
106
+ | OpenTracing | https://github.com/opentracing/opentracing-ruby | 0.4.1+ (w/ Ruby 2.1+) | >= 0.16.0 |
107
+
108
+ *Full* support indicates all tracer features are available.
109
+
110
+ *Deprecated* indicates support will transition to *Maintenance* in a future release.
111
+
112
+ *Maintenance* indicates only critical bugfixes are backported until EOL.
113
+
114
+ *EOL* indicates support is no longer provided.
115
+
116
+ ## Installation
117
+
118
+ The following steps will help you quickly start tracing your Ruby application.
119
+
120
+ ### Setup the Datadog Agent
121
+
122
+ Before downloading tracing on your application, install the Datadog Agent. The Ruby APM tracer sends trace data through the Datadog Agent.
123
+
124
+ [Install and configure the Datadog Agent](https://docs.datadoghq.com/tracing/setup), see additional documentation for [tracing Docker applications](https://docs.datadoghq.com/tracing/setup/docker/).
125
+
126
+ ### Quickstart for Rails applications
127
+
128
+ 1. Add the `ddtrace` gem to your Gemfile:
129
+
130
+ ```ruby
131
+ source 'https://rubygems.org'
132
+ gem 'ddtrace'
133
+ ```
134
+
135
+ 2. Install the gem with `bundle install`
136
+ 3. Create a `config/initializers/datadog.rb` file containing:
137
+
138
+ ```ruby
139
+ Datadog.configure do |c|
140
+ # This will activate auto-instrumentation for Rails
141
+ c.use :rails
142
+ end
143
+ ```
144
+
145
+ You can also activate additional integrations here (see [Integration instrumentation](#integration-instrumentation))
146
+
147
+ ### Quickstart for Ruby applications
148
+
149
+ 1. Install the gem with `gem install ddtrace`
150
+ 2. Add a configuration block to your Ruby application:
151
+
152
+ ```ruby
153
+ require 'ddtrace'
154
+ Datadog.configure do |c|
155
+ # Configure the tracer here.
156
+ # Activate integrations, change tracer settings, etc...
157
+ # By default without additional configuration, nothing will be traced.
158
+ end
159
+ ```
160
+
161
+ 3. Add or activate instrumentation by doing either of the following:
162
+ - Activate integration instrumentation (see [Integration instrumentation](#integration-instrumentation))
163
+ - Add manual instrumentation around your code (see [Manual instrumentation](#manual-instrumentation))
164
+
165
+ ### Quickstart for OpenTracing
166
+
167
+ 1. Install the gem with `gem install ddtrace`
168
+ 2. To your OpenTracing configuration file, add the following:
169
+
170
+ ```ruby
171
+ require 'opentracing'
172
+ require 'ddtrace'
173
+ require 'ddtrace/opentracer'
174
+
175
+ # Activate the Datadog tracer for OpenTracing
176
+ OpenTracing.global_tracer = Datadog::OpenTracer::Tracer.new
177
+ ```
178
+
179
+ 3. (Optional) Add a configuration block to your Ruby application to configure Datadog with:
180
+
181
+ ```ruby
182
+ Datadog.configure do |c|
183
+ # Configure the Datadog tracer here.
184
+ # Activate integrations, change tracer settings, etc...
185
+ # By default without additional configuration,
186
+ # no additional integrations will be traced, only
187
+ # what you have instrumented with OpenTracing.
188
+ end
189
+ ```
190
+
191
+ 4. (Optional) Add or activate additional instrumentation by doing either of the following:
192
+ - Activate Datadog integration instrumentation (see [Integration instrumentation](#integration-instrumentation))
193
+ - Add Datadog manual instrumentation around your code (see [Manual instrumentation](#manual-instrumentation))
194
+
195
+ ### Final steps for installation
196
+
197
+ After setting up, your services will appear on the [APM services page](https://app.datadoghq.com/apm/services) within a few minutes. Learn more about [using the APM UI][visualization docs].
198
+
199
+ ## Manual Instrumentation
200
+
201
+ If you aren't using a supported framework instrumentation, you may want to manually instrument your code.
202
+
203
+ To trace any Ruby code, you can use the `Datadog.tracer.trace` method:
204
+
205
+ ```ruby
206
+ Datadog.tracer.trace(name, options) do |span|
207
+ # Wrap this block around the code you want to instrument
208
+ # Additionally, you can modify the span here.
209
+ # e.g. Change the resource name, set tags, etc...
210
+ end
211
+ ```
212
+
213
+ Where `name` should be a `String` that describes the generic kind of operation being done (e.g. `'web.request'`, or `'request.parse'`)
214
+
215
+ And `options` is an optional `Hash` that accepts the following parameters:
216
+
217
+ | Key | Type | Description | Default |
218
+ | --- | --- | --- | --- |
219
+ | `service` | `String` | The service name which this span belongs (e.g. `'my-web-service'`) | Tracer `default-service`, `$PROGRAM_NAME` or `'ruby'` |
220
+ | `resource` | `String` | Name of the resource or action being operated on. Traces with the same resource value will be grouped together for the purpose of metrics (but still independently viewable.) Usually domain specific, such as a URL, query, request, etc. (e.g. `'Article#submit'`, `http://example.com/articles/list`.) | `name` of Span. |
221
+ | `span_type` | `String` | The type of the span (such as `'http'`, `'db'`, etc.) | `nil` |
222
+ | `child_of` | `Datadog::Span` / `Datadog::Context` | Parent for this span. If not provided, will automatically become current active span. | `nil` |
223
+ | `start_time` | `Integer` | When the span actually starts. Useful when tracing events that have already happened. | `Time.now.utc` |
224
+ | `tags` | `Hash` | Extra tags which should be added to the span. | `{}` |
225
+ | `on_error` | `Proc` | Handler invoked when a block is provided to trace, and it raises an error. Provided `span` and `error` as arguments. Sets error on the span by default. | `proc { |span, error| span.set_error(error) unless span.nil? }` |
226
+
227
+ It's highly recommended you set both `service` and `resource` at a minimum. Spans without a `service` or `resource` as `nil` will be discarded by the Datadog agent.
228
+
229
+ Example of manual instrumentation in action:
230
+
231
+ ```ruby
232
+ get '/posts' do
233
+ Datadog.tracer.trace('web.request', service: 'my-blog', resource: 'GET /posts') do |span|
234
+ # Trace the activerecord call
235
+ Datadog.tracer.trace('posts.fetch') do
236
+ @posts = Posts.order(created_at: :desc).limit(10)
237
+ end
238
+
239
+ # Add some APM tags
240
+ span.set_tag('http.method', request.request_method)
241
+ span.set_tag('posts.count', @posts.length)
242
+
243
+ # Trace the template rendering
244
+ Datadog.tracer.trace('template.render') do
245
+ erb :index
246
+ end
247
+ end
248
+ end
249
+ ```
250
+
251
+ ### Asynchronous tracing
252
+
253
+ It might not always be possible to wrap `Datadog.tracer.trace` around a block of code. Some event or notification based instrumentation might only notify you when an event begins or ends.
254
+
255
+ To trace these operations, you can trace code asynchronously by calling `Datadog.tracer.trace` without a block:
256
+
257
+ ```ruby
258
+ # Some instrumentation framework calls this after an event finishes...
259
+ def db_query(start, finish, query)
260
+ span = Datadog.tracer.trace('database.query')
261
+ span.resource = query
262
+ span.start_time = start
263
+ span.finish(finish)
264
+ end
265
+ ```
266
+
267
+ Calling `Datadog.tracer.trace` without a block will cause the function to return a `Datadog::Span` that is started, but not finished. You can then modify this span however you wish, then close it `finish`.
268
+
269
+ *You must not leave any unfinished spans.* If any spans are left open when the trace completes, the trace will be discarded. You can [activate debug mode](#tracer-settings) to check for warnings if you suspect this might be happening.
270
+
271
+ To avoid this scenario when handling start/finish events, you can use `Datadog.tracer.active_span` to get the current active span.
272
+
273
+ ```ruby
274
+ # e.g. ActiveSupport::Notifications calls this when an event starts
275
+ def start(name, id, payload)
276
+ # Start a span
277
+ Datadog.tracer.trace(name)
278
+ end
279
+
280
+ # e.g. ActiveSupport::Notifications calls this when an event finishes
281
+ def finish(name, id, payload)
282
+ # Retrieve current active span (thread-safe)
283
+ current_span = Datadog.tracer.active_span
284
+ unless current_span.nil?
285
+ current_span.resource = payload[:query]
286
+ current_span.finish
287
+ end
288
+ end
289
+ ```
290
+ ### Enriching traces from nested methods
291
+
292
+ You can tag additional information to the current active span from any method. Note however that if the method is called and there is no span currently active `active_span` will be nil.
293
+
294
+ ```ruby
295
+ # e.g. adding tag to active span
296
+
297
+ current_span = Datadog.tracer.active_span
298
+ current_span.set_tag('my_tag', 'my_value') unless current_span.nil?
299
+ ```
300
+
301
+ You can also get the root span of the current active trace using the `active_root_span` method. This method will return `nil` if there is no active trace.
302
+
303
+ ```ruby
304
+ # e.g. adding tag to active root span
305
+
306
+ current_root_span = Datadog.tracer.active_root_span
307
+ current_root_span.set_tag('my_tag', 'my_value') unless current_root_span.nil?
308
+ ```
309
+
310
+ ## Integration instrumentation
311
+
312
+ Many popular libraries and frameworks are supported out-of-the-box, which can be auto-instrumented. Although they are not activated automatically, they can be easily activated and configured by using the `Datadog.configure` API:
313
+
314
+ ```ruby
315
+ Datadog.configure do |c|
316
+ # Activates and configures an integration
317
+ c.use :integration_name, options
318
+ end
319
+ ```
320
+
321
+ `options` is a `Hash` of integration-specific configuration settings.
322
+
323
+ For a list of available integrations, and their configuration options, please refer to the following:
324
+
325
+ | Name | Key | Versions Supported | How to configure | Gem source |
326
+ | ------------------------ | -------------------------- | ------------------------ | ----------------------------------- | ------------------------------------------------------------------------------ |
327
+ | Action View | `action_view` | `>= 3.2` | *[Link](#action-view)* | *[Link](https://github.com/rails/rails/tree/master/actionview)* |
328
+ | Active Model Serializers | `active_model_serializers` | `>= 0.9` | *[Link](#active-model-serializers)* | *[Link](https://github.com/rails-api/active_model_serializers)* |
329
+ | Action Pack | `action_pack` | `>= 3.2` | *[Link](#action-pack)* | *[Link](https://github.com/rails/rails/tree/master/actionpack)* |
330
+ | Active Record | `active_record` | `>= 3.2` | *[Link](#active-record)* | *[Link](https://github.com/rails/rails/tree/master/activerecord)* |
331
+ | Active Support | `active_support` | `>= 3.2` | *[Link](#active-support)* | *[Link](https://github.com/rails/rails/tree/master/activesupport)* |
332
+ | AWS | `aws` | `>= 2.0` | *[Link](#aws)* | *[Link](https://github.com/aws/aws-sdk-ruby)* |
333
+ | Concurrent Ruby | `concurrent_ruby` | `>= 0.9` | *[Link](#concurrent-ruby)* | *[Link](https://github.com/ruby-concurrency/concurrent-ruby)* |
334
+ | Dalli | `dalli` | `>= 2.7` | *[Link](#dalli)* | *[Link](https://github.com/petergoldstein/dalli)* |
335
+ | DelayedJob | `delayed_job` | `>= 4.1` | *[Link](#delayedjob)* | *[Link](https://github.com/collectiveidea/delayed_job)* |
336
+ | Elastic Search | `elasticsearch` | `>= 6.0` | *[Link](#elastic-search)* | *[Link](https://github.com/elastic/elasticsearch-ruby)* |
337
+ | Ethon | `ethon` | `>= 0.11.0` | *[Link](#ethon)* | *[Link](https://github.com/typhoeus/ethon)* |
338
+ | Excon | `excon` | `>= 0.62` | *[Link](#excon)* | *[Link](https://github.com/excon/excon)* |
339
+ | Faraday | `faraday` | `>= 0.14` | *[Link](#faraday)* | *[Link](https://github.com/lostisland/faraday)* |
340
+ | Grape | `grape` | `>= 1.0` | *[Link](#grape)* | *[Link](https://github.com/ruby-grape/grape)* |
341
+ | GraphQL | `graphql` | `>= 1.7.9` | *[Link](#graphql)* | *[Link](https://github.com/rmosolgo/graphql-ruby)* |
342
+ | gRPC | `grpc` | `>= 1.10` | *[Link](#grpc)* | *[Link](https://github.com/grpc/grpc/tree/master/src/rubyc)* |
343
+ | MongoDB | `mongo` | `>= 2.0` | *[Link](#mongodb)* | *[Link](https://github.com/mongodb/mongo-ruby-driver)* |
344
+ | MySQL2 | `mysql2` | `>= 0.3.21` | *[Link](#mysql2)* | *[Link](https://github.com/brianmario/mysql2)* |
345
+ | Net/HTTP | `http` | *(Any supported Ruby)* | *[Link](#nethttp)* | *[Link](https://ruby-doc.org/stdlib-2.4.0/libdoc/net/http/rdoc/Net/HTTP.html)* |
346
+ | Racecar | `racecar` | `>= 0.3.5` | *[Link](#racecar)* | *[Link](https://github.com/zendesk/racecar)* |
347
+ | Rack | `rack` | `>= 1.4.7` | *[Link](#rack)* | *[Link](https://github.com/rack/rack)* |
348
+ | Rails | `rails` | `>= 3.2` | *[Link](#rails)* | *[Link](https://github.com/rails/rails)* |
349
+ | Rake | `rake` | `>= 12.0` | *[Link](#rake)* | *[Link](https://github.com/ruby/rake)* |
350
+ | Redis | `redis` | `>= 3.2` | *[Link](#redis)* | *[Link](https://github.com/redis/redis-rb)* |
351
+ | Resque | `resque` | `>= 1.0, < 2.0` | *[Link](#resque)* | *[Link](https://github.com/resque/resque)* |
352
+ | Rest Client | `rest-client` | `>= 1.8` | *[Link](#rest-client)* | *[Link](https://github.com/rest-client/rest-client)* |
353
+ | Sequel | `sequel` | `>= 3.41` | *[Link](#sequel)* | *[Link](https://github.com/jeremyevans/sequel)* |
354
+ | Shoryuken | `shoryuken` | `>= 4.0.2` | *[Link](#shoryuken)* | *[Link](https://github.com/phstc/shoryuken)* |
355
+ | Sidekiq | `sidekiq` | `>= 3.5.4` | *[Link](#sidekiq)* | *[Link](https://github.com/mperham/sidekiq)* |
356
+ | Sinatra | `sinatra` | `>= 1.4.5` | *[Link](#sinatra)* | *[Link](https://github.com/sinatra/sinatra)* |
357
+ | Sucker Punch | `sucker_punch` | `>= 2.0` | *[Link](#sucker-punch)* | *[Link](https://github.com/brandonhilkert/sucker_punch)* |
358
+
359
+ ### Action View
360
+
361
+ Most of the time, Active Support is set up as part of Rails, but it can be activated separately:
362
+
363
+ ```ruby
364
+ require 'actionview'
365
+ require 'ddtrace'
366
+
367
+ Datadog.configure do |c|
368
+ c.use :action_view, options
369
+ end
370
+ ```
371
+
372
+ Where `options` is an optional `Hash` that accepts the following parameters:
373
+
374
+ | Key | Description | Default |
375
+ | ---| --- | --- |
376
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to global setting, `false` for off. | `false` |
377
+ | `service_name` | Service name used for rendering instrumentation. | `action_view` |
378
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually you don't need to set this. | `Datadog.tracer` |
379
+ | `template_base_path` | Used when the template name is parsed. If you don't store your templates in the `views/` folder, you may need to change this value | `'views/'` |
380
+
381
+ ### Active Model Serializers
382
+
383
+ The Active Model Serializers integration traces the `serialize` event for version 0.9+ and the `render` event for version 0.10+.
384
+
385
+ ```ruby
386
+ require 'active_model_serializers'
387
+ require 'ddtrace'
388
+
389
+ Datadog.configure do |c|
390
+ c.use :active_model_serializers, options
391
+ end
392
+
393
+ my_object = MyModel.new(name: 'my object')
394
+ ActiveModelSerializers::SerializableResource.new(test_obj).serializable_hash
395
+ ```
396
+
397
+ | Key | Description | Default |
398
+ | --- | ----------- | ------- |
399
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to global setting, `false` for off. | `false` |
400
+ | `service_name` | Service name used for `active_model_serializers` instrumentation. | `'active_model_serializers'` |
401
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually you don't need to set this. | `Datadog.tracer` |
402
+
403
+ ### Action Pack
404
+
405
+ Most of the time, Action Pack is set up as part of Rails, but it can be activated separately:
406
+
407
+ ```ruby
408
+ require 'actionpack'
409
+ require 'ddtrace'
410
+
411
+ Datadog.configure do |c|
412
+ c.use :action_pack, options
413
+ end
414
+ ```
415
+
416
+ Where `options` is an optional `Hash` that accepts the following parameters:
417
+
418
+ | Key | Description | Default |
419
+ | ---| --- | --- |
420
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to global setting, `false` for off. | `false` |
421
+ | `service_name` | Service name used for rendering instrumentation. | `action_pack` |
422
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually you don't need to set this. | `Datadog.tracer` |
423
+
424
+ ### Active Record
425
+
426
+ Most of the time, Active Record is set up as part of a web framework (Rails, Sinatra...) however, it can be set up alone:
427
+
428
+ ```ruby
429
+ require 'tmpdir'
430
+ require 'sqlite3'
431
+ require 'active_record'
432
+ require 'ddtrace'
433
+
434
+ Datadog.configure do |c|
435
+ c.use :active_record, options
436
+ end
437
+
438
+ Dir::Tmpname.create(['test', '.sqlite']) do |db|
439
+ conn = ActiveRecord::Base.establish_connection(adapter: 'sqlite3',
440
+ database: db)
441
+ conn.connection.execute('SELECT 42') # traced!
442
+ end
443
+ ```
444
+
445
+ Where `options` is an optional `Hash` that accepts the following parameters:
446
+
447
+ | Key | Description | Default |
448
+ | ---| --- | --- |
449
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to the global setting, `false` for off. | `false` |
450
+ | `orm_service_name` | Service name used for the Ruby ORM portion of `active_record` instrumentation. Overrides service name for ORM spans if explicitly set, which otherwise inherit their service from their parent. | `'active_record'` |
451
+ | `service_name` | Service name used for database portion of `active_record` instrumentation. | Name of database adapter (e.g. `'mysql2'`) |
452
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually, you don't need to set this. | `Datadog.tracer` |
453
+
454
+ **Configuring trace settings per database**
455
+
456
+ You can configure trace settings per database connection by using the `describes` option:
457
+
458
+ ```ruby
459
+ # Provide a `:describes` option with a connection key.
460
+ # Any of the following keys are acceptable and equivalent to one another.
461
+ # If a block is provided, it yields a Settings object that
462
+ # accepts any of the configuration options listed above.
463
+
464
+ Datadog.configure do |c|
465
+ # Symbol matching your database connection in config/database.yml
466
+ # Only available if you are using Rails with ActiveRecord.
467
+ c.use :active_record, describes: :secondary_database, service_name: 'secondary-db'
468
+
469
+ c.use :active_record, describes: :secondary_database do |second_db|
470
+ second_db.service_name = 'secondary-db'
471
+ end
472
+
473
+ # Connection string with the following connection settings:
474
+ # Adapter, user, host, port, database
475
+ c.use :active_record, describes: 'mysql2://root@127.0.0.1:3306/mysql', service_name: 'secondary-db'
476
+
477
+ # Hash with following connection settings
478
+ # Adapter, user, host, port, database
479
+ c.use :active_record, describes: {
480
+ adapter: 'mysql2',
481
+ host: '127.0.0.1',
482
+ port: '3306',
483
+ database: 'mysql',
484
+ username: 'root'
485
+ },
486
+ service_name: 'secondary-db'
487
+ end
488
+ ```
489
+
490
+ If ActiveRecord traces an event that uses a connection that matches a key defined by `describes`, it will use the trace settings assigned to that connection. If the connection does not match any of the described connections, it will use default settings defined by `c.use :active_record` instead.
491
+
492
+ ### Active Support
493
+
494
+ Most of the time, Active Support is set up as part of Rails, but it can be activated separately:
495
+
496
+ ```ruby
497
+ require 'activesupport'
498
+ require 'ddtrace'
499
+
500
+ Datadog.configure do |c|
501
+ c.use :active_support, options
502
+ end
503
+
504
+ cache = ActiveSupport::Cache::MemoryStore.new
505
+ cache.read('city')
506
+ ```
507
+
508
+ Where `options` is an optional `Hash` that accepts the following parameters:
509
+
510
+ | Key | Description | Default |
511
+ | ---| --- | --- |
512
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to global setting, `false` for off. | `false` |
513
+ | `cache_service` | Service name used for caching with `active_support` instrumentation. | `active_support-cache` |
514
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually you don't need to set this. | `Datadog.tracer` |
515
+
516
+ ### AWS
517
+
518
+ The AWS integration will trace every interaction (e.g. API calls) with AWS services (S3, ElastiCache etc.).
519
+
520
+ ```ruby
521
+ require 'aws-sdk'
522
+ require 'ddtrace'
523
+
524
+ Datadog.configure do |c|
525
+ c.use :aws, options
526
+ end
527
+
528
+ # Perform traced call
529
+ Aws::S3::Client.new.list_buckets
530
+ ```
531
+
532
+ Where `options` is an optional `Hash` that accepts the following parameters:
533
+
534
+ | Key | Description | Default |
535
+ | --- | ----------- | ------- |
536
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to global setting, `false` for off. | `false` |
537
+ | `service_name` | Service name used for `aws` instrumentation | `'aws'` |
538
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually you don't need to set this. | `Datadog.tracer` |
539
+
540
+ ### Concurrent Ruby
541
+
542
+ The Concurrent Ruby integration adds support for context propagation when using `::Concurrent::Future`.
543
+ Making sure that code traced within the `Future#execute` will have correct parent set.
544
+
545
+ To activate your integration, use the `Datadog.configure` method:
546
+
547
+ ```ruby
548
+ # Inside Rails initializer or equivalent
549
+ Datadog.configure do |c|
550
+ # Patches ::Concurrent::Future to use ExecutorService that propagates context
551
+ c.use :concurrent_ruby, options
552
+ end
553
+
554
+ # Pass context into code executed within Concurrent::Future
555
+ Datadog.tracer.trace('outer') do
556
+ Concurrent::Future.execute { Datadog.tracer.trace('inner') { } }.wait
557
+ end
558
+ ```
559
+
560
+ Where `options` is an optional `Hash` that accepts the following parameters:
561
+
562
+ | Key | Description | Default |
563
+ | --- | ----------- | ------- |
564
+ | `service_name` | Service name used for `concurrent-ruby` instrumentation | `'concurrent-ruby'` |
565
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually you don't need to set this. | `Datadog.tracer` |
566
+
567
+ ### Dalli
568
+
569
+ Dalli integration will trace all calls to your `memcached` server:
570
+
571
+ ```ruby
572
+ require 'dalli'
573
+ require 'ddtrace'
574
+
575
+ # Configure default Dalli tracing behavior
576
+ Datadog.configure do |c|
577
+ c.use :dalli, options
578
+ end
579
+
580
+ # Configure Dalli tracing behavior for single client
581
+ client = Dalli::Client.new('localhost:11211', options)
582
+ client.set('abc', 123)
583
+ ```
584
+
585
+ Where `options` is an optional `Hash` that accepts the following parameters:
586
+
587
+ | Key | Description | Default |
588
+ | --- | ----------- | ------- |
589
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to global setting, `false` for off. | `false` |
590
+ | `service_name` | Service name used for `dalli` instrumentation | `'memcached'` |
591
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually you don't need to set this. | `Datadog.tracer` |
592
+
593
+ ### DelayedJob
594
+
595
+ The DelayedJob integration uses lifecycle hooks to trace the job executions.
596
+
597
+ You can enable it through `Datadog.configure`:
598
+
599
+ ```ruby
600
+ require 'ddtrace'
601
+
602
+ Datadog.configure do |c|
603
+ c.use :delayed_job, options
604
+ end
605
+ ```
606
+
607
+ Where `options` is an optional `Hash` that accepts the following parameters:
608
+
609
+ | Key | Description | Default |
610
+ | --- | ----------- | ------- |
611
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to global setting, `false` for off. | `false` |
612
+ | `service_name` | Service name used for `DelayedJob` instrumentation | `'delayed_job'` |
613
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually you don't need to set this. | `Datadog.tracer` |
614
+
615
+ ### Elastic Search
616
+
617
+ The Elasticsearch integration will trace any call to `perform_request` in the `Client` object:
618
+
619
+ ```ruby
620
+ require 'elasticsearch/transport'
621
+ require 'ddtrace'
622
+
623
+ Datadog.configure do |c|
624
+ c.use :elasticsearch, options
625
+ end
626
+
627
+ # Perform a query to ElasticSearch
628
+ client = Elasticsearch::Client.new url: 'http://127.0.0.1:9200'
629
+ response = client.perform_request 'GET', '_cluster/health'
630
+ ```
631
+
632
+ Where `options` is an optional `Hash` that accepts the following parameters:
633
+
634
+ | Key | Description | Default |
635
+ | --- | ----------- | ------- |
636
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to global setting, `false` for off. | `false` |
637
+ | `quantize` | Hash containing options for quantization. May include `:show` with an Array of keys to not quantize (or `:all` to skip quantization), or `:exclude` with Array of keys to exclude entirely. | `{}` |
638
+ | `service_name` | Service name used for `elasticsearch` instrumentation | `'elasticsearch'` |
639
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually you don't need to set this. | `Datadog.tracer` |
640
+
641
+ ### Ethon
642
+
643
+ The `ethon` integration will trace any HTTP request through `Easy` or `Multi` objects. Note that this integration also supports `Typhoeus` library which is based on `Ethon`.
644
+
645
+ ```ruby
646
+ require 'ddtrace'
647
+
648
+ Datadog.configure do |c|
649
+ c.use :ethon, options
650
+ end
651
+ ```
652
+
653
+ Where `options` is an optional `Hash` that accepts the following parameters:
654
+
655
+ | Key | Description | Default |
656
+ | --- | ----------- | ------- |
657
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to global setting, `false` for off. | `false` |
658
+ | `distributed_tracing` | Enables [distributed tracing](#distributed-tracing) | `true` |
659
+ | `service_name` | Service name for `ethon` instrumentation. | `'ethon'` |
660
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually you don't need to set this. | `Datadog.tracer` |
661
+
662
+ ### Excon
663
+
664
+ The `excon` integration is available through the `ddtrace` middleware:
665
+
666
+ ```ruby
667
+ require 'excon'
668
+ require 'ddtrace'
669
+
670
+ # Configure default Excon tracing behavior
671
+ Datadog.configure do |c|
672
+ c.use :excon, options
673
+ end
674
+
675
+ connection = Excon.new('https://example.com')
676
+ connection.get
677
+ ```
678
+
679
+ Where `options` is an optional `Hash` that accepts the following parameters:
680
+
681
+ | Key | Description | Default |
682
+ | --- | ----------- | ------- |
683
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to global setting, `false` for off. | `false` |
684
+ | `distributed_tracing` | Enables [distributed tracing](#distributed-tracing) | `true` |
685
+ | `error_handler` | A `Proc` that accepts a `response` parameter. If it evaluates to a *truthy* value, the trace span is marked as an error. By default only sets 5XX responses as errors. | `nil` |
686
+ | `service_name` | Service name for Excon instrumentation. When provided to middleware for a specific connection, it applies only to that connection object. | `'excon'` |
687
+ | `split_by_domain` | Uses the request domain as the service name when set to `true`. | `false` |
688
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually you don't need to set this. | `Datadog.tracer` |
689
+
690
+ **Configuring connections to use different settings**
691
+
692
+ If you use multiple connections with Excon, you can give each of them different settings by configuring their constructors with middleware:
693
+
694
+ ```ruby
695
+ # Wrap the Datadog tracing middleware around the default middleware stack
696
+ Excon.new(
697
+ 'http://example.com',
698
+ middlewares: Datadog::Contrib::Excon::Middleware.with(options).around_default_stack
699
+ )
700
+
701
+ # Insert the middleware into a custom middleware stack.
702
+ # NOTE: Trace middleware must be inserted after ResponseParser!
703
+ Excon.new(
704
+ 'http://example.com',
705
+ middlewares: [
706
+ Excon::Middleware::ResponseParser,
707
+ Datadog::Contrib::Excon::Middleware.with(options),
708
+ Excon::Middleware::Idempotent
709
+ ]
710
+ )
711
+ ```
712
+
713
+ Where `options` is a Hash that contains any of the parameters listed in the table above.
714
+
715
+ ### Faraday
716
+
717
+ The `faraday` integration is available through the `ddtrace` middleware:
718
+
719
+ ```ruby
720
+ require 'faraday'
721
+ require 'ddtrace'
722
+
723
+ # Configure default Faraday tracing behavior
724
+ Datadog.configure do |c|
725
+ c.use :faraday, options
726
+ end
727
+
728
+ # Configure Faraday tracing behavior for single connection
729
+ connection = Faraday.new('https://example.com') do |builder|
730
+ builder.use(:ddtrace, options)
731
+ builder.adapter Faraday.default_adapter
732
+ end
733
+
734
+ connection.get('/foo')
735
+ ```
736
+
737
+ Where `options` is an optional `Hash` that accepts the following parameters:
738
+
739
+ | Key | Description | Default |
740
+ | --- | ----------- | ------- |
741
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to global setting, `false` for off. | `false` |
742
+ | `distributed_tracing` | Enables [distributed tracing](#distributed-tracing) | `true` |
743
+ | `error_handler` | A `Proc` that accepts a `response` parameter. If it evaluates to a *truthy* value, the trace span is marked as an error. By default only sets 5XX responses as errors. | `nil` |
744
+ | `service_name` | Service name for Faraday instrumentation. When provided to middleware for a specific connection, it applies only to that connection object. | `'faraday'` |
745
+ | `split_by_domain` | Uses the request domain as the service name when set to `true`. | `false` |
746
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually, you don't need to set this. | `Datadog.tracer` |
747
+
748
+ ### Grape
749
+
750
+ The Grape integration adds the instrumentation to Grape endpoints and filters. This integration can work side by side with other integrations like Rack and Rails.
751
+
752
+ To activate your integration, use the `Datadog.configure` method before defining your Grape application:
753
+
754
+ ```ruby
755
+ # api.rb
756
+ require 'grape'
757
+ require 'ddtrace'
758
+
759
+ Datadog.configure do |c|
760
+ c.use :grape, options
761
+ end
762
+
763
+ # Then define your application
764
+ class RackTestingAPI < Grape::API
765
+ desc 'main endpoint'
766
+ get :success do
767
+ 'Hello world!'
768
+ end
769
+ end
770
+ ```
771
+
772
+ Where `options` is an optional `Hash` that accepts the following parameters:
773
+
774
+ | Key | Description | Default |
775
+ | --- | ----------- | ------- |
776
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to global setting, `false` for off. | `nil` |
777
+ | `enabled` | Defines whether Grape should be traced. Useful for temporarily disabling tracing. `true` or `false` | `true` |
778
+ | `service_name` | Service name used for `grape` instrumentation | `'grape'` |
779
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually you don't need to set this. | `Datadog.tracer` |
780
+
781
+ ### GraphQL
782
+
783
+ The GraphQL integration activates instrumentation for GraphQL queries.
784
+
785
+ To activate your integration, use the `Datadog.configure` method:
786
+
787
+ ```ruby
788
+ # Inside Rails initializer or equivalent
789
+ Datadog.configure do |c|
790
+ c.use :graphql, schemas: [YourSchema], options
791
+ end
792
+
793
+ # Then run a GraphQL query
794
+ YourSchema.execute(query, variables: {}, context: {}, operation_name: nil)
795
+ ```
796
+
797
+ The `use :graphql` method accepts the following parameters. Additional options can be substituted in for `options`:
798
+
799
+ | Key | Description | Default |
800
+ | --- | ----------- | ------- |
801
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to global setting, `false` for off. | `nil` |
802
+ | `service_name` | Service name used for `graphql` instrumentation | `'ruby-graphql'` |
803
+ | `schemas` | Required. Array of `GraphQL::Schema` objects which to trace. Tracing will be added to all the schemas listed, using the options provided to this configuration. If you do not provide any, then tracing will not be activated. | `[]` |
804
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually you don't need to set this. | `Datadog.tracer` |
805
+
806
+ **Manually configuring GraphQL schemas**
807
+
808
+ If you prefer to individually configure the tracer settings for a schema (e.g. you have multiple schemas with different service names), in the schema definition, you can add the following [using the GraphQL API](http://graphql-ruby.org/queries/tracing.html):
809
+
810
+ ```ruby
811
+ YourSchema = GraphQL::Schema.define do
812
+ use(
813
+ GraphQL::Tracing::DataDogTracing,
814
+ service: 'graphql'
815
+ )
816
+ end
817
+ ```
818
+
819
+ Or you can modify an already defined schema:
820
+
821
+ ```ruby
822
+ YourSchema.define do
823
+ use(
824
+ GraphQL::Tracing::DataDogTracing,
825
+ service: 'graphql'
826
+ )
827
+ end
828
+ ```
829
+
830
+ Do *NOT* `use :graphql` in `Datadog.configure` if you choose to configure manually, as to avoid double tracing. These two means of configuring GraphQL tracing are considered mutually exclusive.
831
+
832
+ ### gRPC
833
+
834
+ The `grpc` integration adds both client and server interceptors, which run as middleware before executing the service's remote procedure call. As gRPC applications are often distributed, the integration shares trace information between client and server.
835
+
836
+ To setup your integration, use the `Datadog.configure` method like so:
837
+
838
+ ```ruby
839
+ require 'grpc'
840
+ require 'ddtrace'
841
+
842
+ Datadog.configure do |c|
843
+ c.use :grpc, options
844
+ end
845
+
846
+ # Server side
847
+ server = GRPC::RpcServer.new
848
+ server.add_http2_port('localhost:50051', :this_port_is_insecure)
849
+ server.handle(Demo)
850
+ server.run_till_terminated
851
+
852
+ # Client side
853
+ client = Demo.rpc_stub_class.new('localhost:50051', :this_channel_is_insecure)
854
+ client.my_endpoint(DemoMessage.new(contents: 'hello!'))
855
+ ```
856
+
857
+ Where `options` is an optional `Hash` that accepts the following parameters:
858
+
859
+ | Key | Description | Default |
860
+ | --- | ----------- | ------- |
861
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to global setting, `false` for off. | `false` |
862
+ | `service_name` | Service name used for `grpc` instrumentation | `'grpc'` |
863
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually you don't need to set this. | `Datadog.tracer` |
864
+
865
+ **Configuring clients to use different settings**
866
+
867
+ In situations where you have multiple clients calling multiple distinct services, you may pass the Datadog interceptor directly, like so
868
+
869
+ ```ruby
870
+ configured_interceptor = Datadog::Contrib::GRPC::DatadogInterceptor::Client.new do |c|
871
+ c.service_name = "Alternate"
872
+ end
873
+
874
+ alternate_client = Demo::Echo::Service.rpc_stub_class.new(
875
+ 'localhost:50052',
876
+ :this_channel_is_insecure,
877
+ :interceptors => [configured_interceptor]
878
+ )
879
+ ```
880
+
881
+ The integration will ensure that the `configured_interceptor` establishes a unique tracing setup for that client instance.
882
+
883
+ ### MongoDB
884
+
885
+ The integration traces any `Command` that is sent from the [MongoDB Ruby Driver](https://github.com/mongodb/mongo-ruby-driver) to a MongoDB cluster. By extension, Object Document Mappers (ODM) such as Mongoid are automatically instrumented if they use the official Ruby driver. To activate the integration, simply:
886
+
887
+ ```ruby
888
+ require 'mongo'
889
+ require 'ddtrace'
890
+
891
+ Datadog.configure do |c|
892
+ c.use :mongo, options
893
+ end
894
+
895
+ # Create a MongoDB client and use it as usual
896
+ client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'artists')
897
+ collection = client[:people]
898
+ collection.insert_one({ name: 'Steve' })
899
+
900
+ # In case you want to override the global configuration for a certain client instance
901
+ Datadog.configure(client, options)
902
+ ```
903
+
904
+ Where `options` is an optional `Hash` that accepts the following parameters:
905
+
906
+ | Key | Description | Default |
907
+ | --- | ----------- | ------- |
908
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to global setting, `false` for off. | `false` |
909
+ | `quantize` | Hash containing options for quantization. May include `:show` with an Array of keys to not quantize (or `:all` to skip quantization), or `:exclude` with Array of keys to exclude entirely. | `{ show: [:collection, :database, :operation] }` |
910
+ | `service_name` | Service name used for `mongo` instrumentation | `'mongodb'` |
911
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually you don't need to set this. | `Datadog.tracer` |
912
+
913
+ ### MySQL2
914
+
915
+ The MySQL2 integration traces any SQL command sent through `mysql2` gem.
916
+
917
+ ```ruby
918
+ require 'mysql2'
919
+ require 'ddtrace'
920
+
921
+ Datadog.configure do |c|
922
+ c.use :mysql2, options
923
+ end
924
+
925
+ client = Mysql2::Client.new(:host => "localhost", :username => "root")
926
+ client.query("SELECT * FROM users WHERE group='x'")
927
+ ```
928
+
929
+ Where `options` is an optional `Hash` that accepts the following parameters:
930
+
931
+ | Key | Description | Default |
932
+ | --- | ----------- | ------- |
933
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to global setting, `false` for off. | `false` |
934
+ | `service_name` | Service name used for `mysql2` instrumentation | `'mysql2'` |
935
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually you don't need to set this. | `Datadog.tracer` |
936
+
937
+ ### Net/HTTP
938
+
939
+ The Net/HTTP integration will trace any HTTP call using the standard lib Net::HTTP module.
940
+
941
+ ```ruby
942
+ require 'net/http'
943
+ require 'ddtrace'
944
+
945
+ Datadog.configure do |c|
946
+ c.use :http, options
947
+ end
948
+
949
+ Net::HTTP.start('127.0.0.1', 8080) do |http|
950
+ request = Net::HTTP::Get.new '/index'
951
+ response = http.request(request)
952
+ end
953
+
954
+ content = Net::HTTP.get(URI('http://127.0.0.1/index.html'))
955
+ ```
956
+
957
+ Where `options` is an optional `Hash` that accepts the following parameters:
958
+
959
+ | Key | Description | Default |
960
+ | --- | ----------- | ------- |
961
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to global setting, `false` for off. | `false` |
962
+ | `distributed_tracing` | Enables [distributed tracing](#distributed-tracing) | `true` |
963
+ | `service_name` | Service name used for `http` instrumentation | `'net/http'` |
964
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually you don't need to set this. | `Datadog.tracer` |
965
+
966
+ If you wish to configure each connection object individually, you may use the `Datadog.configure` as it follows:
967
+
968
+ ```ruby
969
+ client = Net::HTTP.new(host, port)
970
+ Datadog.configure(client, options)
971
+ ```
972
+
973
+ ### Racecar
974
+
975
+ The Racecar integration provides tracing for Racecar jobs.
976
+
977
+ You can enable it through `Datadog.configure`:
978
+
979
+ ```ruby
980
+ require 'ddtrace'
981
+
982
+ Datadog.configure do |c|
983
+ c.use :racecar, options
984
+ end
985
+ ```
986
+
987
+ Where `options` is an optional `Hash` that accepts the following parameters:
988
+
989
+ | Key | Description | Default |
990
+ | --- | ----------- | ------- |
991
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to global setting, `false` for off. | `false` |
992
+ | `service_name` | Service name used for `racecar` instrumentation | `'racecar'` |
993
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually you don't need to set this. | `Datadog.tracer` |
994
+
995
+ ### Rack
996
+
997
+ The Rack integration provides a middleware that traces all requests before they reach the underlying framework or application. It responds to the Rack minimal interface, providing reasonable values that can be retrieved at the Rack level.
998
+
999
+ This integration is automatically activated with web frameworks like Rails. If you're using a plain Rack application, enable the integration it to your `config.ru`:
1000
+
1001
+ ```ruby
1002
+ # config.ru example
1003
+ require 'ddtrace'
1004
+
1005
+ Datadog.configure do |c|
1006
+ c.use :rack, options
1007
+ end
1008
+
1009
+ use Datadog::Contrib::Rack::TraceMiddleware
1010
+
1011
+ app = proc do |env|
1012
+ [ 200, {'Content-Type' => 'text/plain'}, ['OK'] ]
1013
+ end
1014
+
1015
+ run app
1016
+ ```
1017
+
1018
+ Where `options` is an optional `Hash` that accepts the following parameters:
1019
+
1020
+ | Key | Description | Default |
1021
+ | --- | ----------- | ------- |
1022
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to global setting, `false` for off. | `nil` |
1023
+ | `application` | Your Rack application. Required for `middleware_names`. | `nil` |
1024
+ | `distributed_tracing` | Enables [distributed tracing](#distributed-tracing) so that this service trace is connected with a trace of another service if tracing headers are received | `true` |
1025
+ | `headers` | Hash of HTTP request or response headers to add as tags to the `rack.request`. Accepts `request` and `response` keys with Array values e.g. `['Last-Modified']`. Adds `http.request.headers.*` and `http.response.headers.*` tags respectively. | `{ response: ['Content-Type', 'X-Request-ID'] }` |
1026
+ | `middleware_names` | Enable this if you want to use the middleware classes as the resource names for `rack` spans. Requires `application` option to use. | `false` |
1027
+ | `quantize` | Hash containing options for quantization. May include `:query` or `:fragment`. | `{}` |
1028
+ | `quantize.query` | Hash containing options for query portion of URL quantization. May include `:show` or `:exclude`. See options below. Option must be nested inside the `quantize` option. | `{}` |
1029
+ | `quantize.query.show` | Defines which values should always be shown. Shows no values by default. May be an Array of strings, or `:all` to show all values. Option must be nested inside the `query` option. | `nil` |
1030
+ | `quantize.query.exclude` | Defines which values should be removed entirely. Excludes nothing by default. May be an Array of strings, or `:all` to remove the query string entirely. Option must be nested inside the `query` option. | `nil` |
1031
+ | `quantize.fragment` | Defines behavior for URL fragments. Removes fragments by default. May be `:show` to show URL fragments. Option must be nested inside the `quantize` option. | `nil` |
1032
+ | `request_queuing` | Track HTTP request time spent in the queue of the frontend server. See [HTTP request queuing](#http-request-queuing) for setup details. Set to `true` to enable. | `false` |
1033
+ | `service_name` | Service name used for `rack` instrumentation | `'rack'` |
1034
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually you don't need to set this. | `Datadog.tracer` |
1035
+ | `web_service_name` | Service name for frontend server request queuing spans. (e.g. `'nginx'`) | `'web-server'` |
1036
+
1037
+
1038
+ **Configuring URL quantization behavior**
1039
+
1040
+ ```ruby
1041
+ Datadog.configure do |c|
1042
+ # Default behavior: all values are quantized, fragment is removed.
1043
+ # http://example.com/path?category_id=1&sort_by=asc#featured --> http://example.com/path?category_id&sort_by
1044
+ # http://example.com/path?categories[]=1&categories[]=2 --> http://example.com/path?categories[]
1045
+
1046
+ # Show values for any query string parameter matching 'category_id' exactly
1047
+ # http://example.com/path?category_id=1&sort_by=asc#featured --> http://example.com/path?category_id=1&sort_by
1048
+ c.use :rack, quantize: { query: { show: ['category_id'] } }
1049
+
1050
+ # Show all values for all query string parameters
1051
+ # http://example.com/path?category_id=1&sort_by=asc#featured --> http://example.com/path?category_id=1&sort_by=asc
1052
+ c.use :rack, quantize: { query: { show: :all } }
1053
+
1054
+ # Totally exclude any query string parameter matching 'sort_by' exactly
1055
+ # http://example.com/path?category_id=1&sort_by=asc#featured --> http://example.com/path?category_id
1056
+ c.use :rack, quantize: { query: { exclude: ['sort_by'] } }
1057
+
1058
+ # Remove the query string entirely
1059
+ # http://example.com/path?category_id=1&sort_by=asc#featured --> http://example.com/path
1060
+ c.use :rack, quantize: { query: { exclude: :all } }
1061
+
1062
+ # Show URL fragments
1063
+ # http://example.com/path?category_id=1&sort_by=asc#featured --> http://example.com/path?category_id&sort_by#featured
1064
+ c.use :rack, quantize: { fragment: :show }
1065
+ end
1066
+ ```
1067
+
1068
+ ### Rails
1069
+
1070
+ The Rails integration will trace requests, database calls, templates rendering, and cache read/write/delete operations. The integration makes use of the Active Support Instrumentation, listening to the Notification API so that any operation instrumented by the API is traced.
1071
+
1072
+ To enable the Rails instrumentation, create an initializer file in your `config/initializers` folder:
1073
+
1074
+ ```ruby
1075
+ # config/initializers/datadog.rb
1076
+ require 'ddtrace'
1077
+
1078
+ Datadog.configure do |c|
1079
+ c.use :rails, options
1080
+ end
1081
+ ```
1082
+
1083
+ Where `options` is an optional `Hash` that accepts the following parameters:
1084
+
1085
+ | Key | Description | Default |
1086
+ | --- | ----------- | ------- |
1087
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to the global setting, `false` for off. | `nil` |
1088
+ | `cache_service` | Cache service name used when tracing cache activity | `'<app_name>-cache'` |
1089
+ | `controller_service` | Service name used when tracing a Rails action controller | `'<app_name>'` |
1090
+ | `database_service` | Database service name used when tracing database activity | `'<app_name>-<adapter_name>'` |
1091
+ | `distributed_tracing` | Enables [distributed tracing](#distributed-tracing) so that this service trace is connected with a trace of another service if tracing headers are received | `true` |
1092
+ | `exception_controller` | Class or Module which identifies a custom exception controller class. Tracer provides improved error behavior when it can identify custom exception controllers. By default, without this option, it 'guesses' what a custom exception controller looks like. Providing this option aids this identification. | `nil` |
1093
+ | `middleware` | Add the trace middleware to the Rails application. Set to `false` if you don't want the middleware to load. | `true` |
1094
+ | `middleware_names` | Enables any short-circuited middleware requests to display the middleware name as a resource for the trace. | `false` |
1095
+ | `service_name` | Service name used when tracing application requests (on the `rack` level) | `'<app_name>'` (inferred from your Rails application namespace) |
1096
+ | `template_base_path` | Used when the template name is parsed. If you don't store your templates in the `views/` folder, you may need to change this value | `'views/'` |
1097
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually, you don't need to set this. | `Datadog.tracer` |
1098
+
1099
+ **Supported versions**
1100
+
1101
+ | Ruby Versions | Supported Rails Versions |
1102
+ | ------------- | ------------------------ |
1103
+ | 2.0 | 3.0 - 3.2 |
1104
+ | 2.1 | 3.0 - 4.2 |
1105
+ | 2.2 - 2.3 | 3.0 - 5.2 |
1106
+ | 2.4 | 4.2.8 - 5.2 |
1107
+ | 2.5 | 4.2.8 - 6.0 |
1108
+ | 2.6 | 5.0 - 6.0 |
1109
+
1110
+ ### Rake
1111
+
1112
+ You can add instrumentation around your Rake tasks by activating the `rake` integration. Each task and its subsequent subtasks will be traced.
1113
+
1114
+ To activate Rake task tracing, add the following to your `Rakefile`:
1115
+
1116
+ ```ruby
1117
+ # At the top of your Rakefile:
1118
+ require 'rake'
1119
+ require 'ddtrace'
1120
+
1121
+ Datadog.configure do |c|
1122
+ c.use :rake, options
1123
+ end
1124
+
1125
+ task :my_task do
1126
+ # Do something task work here...
1127
+ end
1128
+
1129
+ Rake::Task['my_task'].invoke
1130
+ ```
1131
+
1132
+ Where `options` is an optional `Hash` that accepts the following parameters:
1133
+
1134
+ | Key | Description | Default |
1135
+ | --- | ----------- | ------- |
1136
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to the global setting, `false` for off. | `false` |
1137
+ | `enabled` | Defines whether Rake tasks should be traced. Useful for temporarily disabling tracing. `true` or `false` | `true` |
1138
+ | `quantize` | Hash containing options for quantization of task arguments. See below for more details and examples. | `{}` |
1139
+ | `service_name` | Service name used for `rake` instrumentation | `'rake'` |
1140
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually, you don't need to set this. | `Datadog.tracer` |
1141
+
1142
+ **Configuring task quantization behavior**
1143
+
1144
+ ```ruby
1145
+ Datadog.configure do |c|
1146
+ # Given a task that accepts :one, :two, :three...
1147
+ # Invoked with 'foo', 'bar', 'baz'.
1148
+
1149
+ # Default behavior: all arguments are quantized.
1150
+ # `rake.invoke.args` tag --> ['?']
1151
+ # `rake.execute.args` tag --> { one: '?', two: '?', three: '?' }
1152
+ c.use :rake
1153
+
1154
+ # Show values for any argument matching :two exactly
1155
+ # `rake.invoke.args` tag --> ['?']
1156
+ # `rake.execute.args` tag --> { one: '?', two: 'bar', three: '?' }
1157
+ c.use :rake, quantize: { args: { show: [:two] } }
1158
+
1159
+ # Show all values for all arguments.
1160
+ # `rake.invoke.args` tag --> ['foo', 'bar', 'baz']
1161
+ # `rake.execute.args` tag --> { one: 'foo', two: 'bar', three: 'baz' }
1162
+ c.use :rake, quantize: { args: { show: :all } }
1163
+
1164
+ # Totally exclude any argument matching :three exactly
1165
+ # `rake.invoke.args` tag --> ['?']
1166
+ # `rake.execute.args` tag --> { one: '?', two: '?' }
1167
+ c.use :rake, quantize: { args: { exclude: [:three] } }
1168
+
1169
+ # Remove the arguments entirely
1170
+ # `rake.invoke.args` tag --> ['?']
1171
+ # `rake.execute.args` tag --> {}
1172
+ c.use :rake, quantize: { args: { exclude: :all } }
1173
+ end
1174
+ ```
1175
+
1176
+ ### Redis
1177
+
1178
+ The Redis integration will trace simple calls as well as pipelines.
1179
+
1180
+ ```ruby
1181
+ require 'redis'
1182
+ require 'ddtrace'
1183
+
1184
+ Datadog.configure do |c|
1185
+ c.use :redis, options
1186
+ end
1187
+
1188
+ # Perform Redis commands
1189
+ redis = Redis.new
1190
+ redis.set 'foo', 'bar'
1191
+ ```
1192
+
1193
+ Where `options` is an optional `Hash` that accepts the following parameters:
1194
+
1195
+ | Key | Description | Default |
1196
+ | --- | ----------- | ------- |
1197
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to global setting, `false` for off. | `false` |
1198
+ | `service_name` | Service name used for `redis` instrumentation | `'redis'` |
1199
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually you don't need to set this. | `Datadog.tracer` |
1200
+
1201
+ You can also set *per-instance* configuration as it follows:
1202
+
1203
+ ```ruby
1204
+ customer_cache = Redis.new
1205
+ invoice_cache = Redis.new
1206
+
1207
+ Datadog.configure(customer_cache, service_name: 'customer-cache')
1208
+ Datadog.configure(invoice_cache, service_name: 'invoice-cache')
1209
+
1210
+ # Traced call will belong to `customer-cache` service
1211
+ customer_cache.get(...)
1212
+ # Traced call will belong to `invoice-cache` service
1213
+ invoice_cache.get(...)
1214
+ ```
1215
+
1216
+ ### Resque
1217
+
1218
+ The Resque integration uses Resque hooks that wraps the `perform` method.
1219
+
1220
+ To add tracing to a Resque job:
1221
+
1222
+ ```ruby
1223
+ require 'ddtrace'
1224
+
1225
+ class MyJob
1226
+ def self.perform(*args)
1227
+ # do_something
1228
+ end
1229
+ end
1230
+
1231
+ Datadog.configure do |c|
1232
+ c.use :resque, options
1233
+ end
1234
+ ```
1235
+
1236
+ Where `options` is an optional `Hash` that accepts the following parameters:
1237
+
1238
+ | Key | Description | Default |
1239
+ | --- | ----------- | ------- |
1240
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to the global setting, `false` for off. | `false` |
1241
+ | `service_name` | Service name used for `resque` instrumentation | `'resque'` |
1242
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually, you don't need to set this. | `Datadog.tracer` |
1243
+ | `workers` | An array including all worker classes you want to trace (e.g. `[MyJob]`) | `[]` |
1244
+
1245
+ ### Rest Client
1246
+
1247
+ The `rest-client` integration is available through the `ddtrace` middleware:
1248
+
1249
+ ```ruby
1250
+ require 'rest_client'
1251
+ require 'ddtrace'
1252
+
1253
+ Datadog.configure do |c|
1254
+ c.use :rest_client, options
1255
+ end
1256
+ ```
1257
+
1258
+ Where `options` is an optional `Hash` that accepts the following parameters:
1259
+
1260
+ | Key | Description | Default |
1261
+ | --- | ----------- | ------- |
1262
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to global setting, `false` for off. | `false` |
1263
+ | `distributed_tracing` | Enables [distributed tracing](#distributed-tracing) | `true` |
1264
+ | `service_name` | Service name for `rest_client` instrumentation. | `'rest_client'` |
1265
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually you don't need to set this. | `Datadog.tracer` |
1266
+
1267
+ ### Sequel
1268
+
1269
+ The Sequel integration traces queries made to your database.
1270
+
1271
+ ```ruby
1272
+ require 'sequel'
1273
+ require 'ddtrace'
1274
+
1275
+ # Connect to database
1276
+ database = Sequel.sqlite
1277
+
1278
+ # Create a table
1279
+ database.create_table :articles do
1280
+ primary_key :id
1281
+ String :name
1282
+ end
1283
+
1284
+ Datadog.configure do |c|
1285
+ c.use :sequel, options
1286
+ end
1287
+
1288
+ # Perform a query
1289
+ articles = database[:articles]
1290
+ articles.all
1291
+ ```
1292
+
1293
+ Where `options` is an optional `Hash` that accepts the following parameters:
1294
+
1295
+ | Key | Description | Default |
1296
+ | --- | ----------- | ------- |
1297
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to global setting, `false` for off. | `false` |
1298
+ | `service_name` | Service name for `sequel` instrumentation | Name of database adapter (e.g. `'mysql2'`) |
1299
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually you don't need to set this. | `Datadog.tracer` |
1300
+
1301
+ Only Ruby 2.0+ is supported.
1302
+
1303
+ **Configuring databases to use different settings**
1304
+
1305
+ If you use multiple databases with Sequel, you can give each of them different settings by configuring their respective `Sequel::Database` objects:
1306
+
1307
+ ```ruby
1308
+ sqlite_database = Sequel.sqlite
1309
+ postgres_database = Sequel.connect('postgres://user:password@host:port/database_name')
1310
+
1311
+ # Configure each database with different service names
1312
+ Datadog.configure(sqlite_database, service_name: 'my-sqlite-db')
1313
+ Datadog.configure(postgres_database, service_name: 'my-postgres-db')
1314
+ ```
1315
+
1316
+ ### Shoryuken
1317
+
1318
+ The Shoryuken integration is a server-side middleware which will trace job executions.
1319
+
1320
+ You can enable it through `Datadog.configure`:
1321
+
1322
+ ```ruby
1323
+ require 'ddtrace'
1324
+
1325
+ Datadog.configure do |c|
1326
+ c.use :shoryuken, options
1327
+ end
1328
+ ```
1329
+
1330
+ Where `options` is an optional `Hash` that accepts the following parameters:
1331
+
1332
+ | Key | Description | Default |
1333
+ | --- | ----------- | ------- |
1334
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to global setting, `false` for off. | `false` |
1335
+ | `service_name` | Service name used for `shoryuken` instrumentation | `'shoryuken'` |
1336
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually you don't need to set this. | `Datadog.tracer` |
1337
+
1338
+ ### Sidekiq
1339
+
1340
+ The Sidekiq integration is a client-side & server-side middleware which will trace job queuing and executions respectively.
1341
+
1342
+ You can enable it through `Datadog.configure`:
1343
+
1344
+ ```ruby
1345
+ require 'ddtrace'
1346
+
1347
+ Datadog.configure do |c|
1348
+ c.use :sidekiq, options
1349
+ end
1350
+ ```
1351
+
1352
+ Where `options` is an optional `Hash` that accepts the following parameters:
1353
+
1354
+ | Key | Description | Default |
1355
+ | --- | ----------- | ------- |
1356
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to global setting, `false` for off. | `false` |
1357
+ | `client_service_name` | Service name used for client-side `sidekiq` instrumentation | `'sidekiq-client'` |
1358
+ | `service_name` | Service name used for server-side `sidekiq` instrumentation | `'sidekiq'` |
1359
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually you don't need to set this. | `Datadog.tracer` |
1360
+
1361
+ ### Sinatra
1362
+
1363
+ The Sinatra integration traces requests and template rendering.
1364
+
1365
+ To start using the tracing client, make sure you import `ddtrace` and `use :sinatra` after either `sinatra` or `sinatra/base`, and before you define your application/routes:
1366
+
1367
+ ```ruby
1368
+ require 'sinatra'
1369
+ require 'ddtrace'
1370
+
1371
+ Datadog.configure do |c|
1372
+ c.use :sinatra, options
1373
+ end
1374
+
1375
+ get '/' do
1376
+ 'Hello world!'
1377
+ end
1378
+ ```
1379
+
1380
+ Where `options` is an optional `Hash` that accepts the following parameters:
1381
+
1382
+ | Key | Description | Default |
1383
+ | --- | ----------- | ------- |
1384
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to global setting, `false` for off. | `nil` |
1385
+ | `distributed_tracing` | Enables [distributed tracing](#distributed-tracing) so that this service trace is connected with a trace of another service if tracing headers are received | `true` |
1386
+ | `headers` | Hash of HTTP request or response headers to add as tags to the `sinatra.request`. Accepts `request` and `response` keys with Array values e.g. `['Last-Modified']`. Adds `http.request.headers.*` and `http.response.headers.*` tags respectively. | `{ response: ['Content-Type', 'X-Request-ID'] }` |
1387
+ | `resource_script_names` | Prepend resource names with script name | `false` |
1388
+ | `service_name` | Service name used for `sinatra` instrumentation | `'sinatra'` |
1389
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually you don't need to set this. | `Datadog.tracer` |
1390
+
1391
+ ### Sucker Punch
1392
+
1393
+ The `sucker_punch` integration traces all scheduled jobs:
1394
+
1395
+ ```ruby
1396
+ require 'ddtrace'
1397
+
1398
+ Datadog.configure do |c|
1399
+ c.use :sucker_punch, options
1400
+ end
1401
+
1402
+ # Execution of this job is traced
1403
+ LogJob.perform_async('login')
1404
+ ```
1405
+
1406
+ Where `options` is an optional `Hash` that accepts the following parameters:
1407
+
1408
+ | Key | Description | Default |
1409
+ | --- | ----------- | ------- |
1410
+ | `analytics_enabled` | Enable analytics for spans produced by this integration. `true` for on, `nil` to defer to global setting, `false` for off. | `false` |
1411
+ | `service_name` | Service name used for `sucker_punch` instrumentation | `'sucker_punch'` |
1412
+ | `tracer` | `Datadog::Tracer` used to perform instrumentation. Usually you don't need to set this. | `Datadog.tracer` |
1413
+
1414
+ ## Advanced configuration
1415
+
1416
+ ### Tracer settings
1417
+
1418
+ To change the default behavior of the Datadog tracer, you can provide custom options inside the `Datadog.configure` block as in:
1419
+
1420
+ ```ruby
1421
+ # config/initializers/datadog-tracer.rb
1422
+
1423
+ Datadog.configure do |c|
1424
+ c.tracer option_name: option_value, ...
1425
+ end
1426
+ ```
1427
+
1428
+ Available options are:
1429
+
1430
+ - `enabled`: defines if the `tracer` is enabled or not. If set to `false` the code could be still instrumented because of other settings, but no spans are sent to the local trace agent.
1431
+ - `debug`: set to true to enable debug logging.
1432
+ - `hostname`: set the hostname of the trace agent.
1433
+ - `port`: set the port the trace agent is listening on.
1434
+ - `env`: set the environment. Rails users may set it to `Rails.env` to use their application settings.
1435
+ - `tags`: set global tags that should be applied to all spans. Defaults to an empty hash
1436
+ - `log`: defines a custom logger.
1437
+ - `partial_flush`: set to `true` to enable partial trace flushing (for long running traces.) Disabled by default. *Experimental.*
1438
+
1439
+ #### Custom logging
1440
+
1441
+ By default, all logs are processed by the default Ruby logger. When using Rails, you should see the messages in your application log file.
1442
+
1443
+ Datadog client log messages are marked with `[ddtrace]` so you should be able to isolate them from other messages.
1444
+
1445
+ Additionally, it is possible to override the default logger and replace it by a custom one. This is done using the `log` attribute of the tracer.
1446
+
1447
+ ```ruby
1448
+ f = File.new("my-custom.log", "w+") # Log messages should go there
1449
+ Datadog.configure do |c|
1450
+ c.tracer log: Logger.new(f) # Overriding the default tracer
1451
+ end
1452
+
1453
+ Datadog::Tracer.log.info { "this is typically called by tracing code" }
1454
+ ```
1455
+
1456
+ ### Environment and tags
1457
+
1458
+ By default, the trace agent (not this library, but the program running in the background collecting data from various clients) uses the tags set in the agent config file, see our [environments tutorial](https://app.datadoghq.com/apm/docs/tutorials/environments) for details.
1459
+
1460
+ These values can be overridden at the tracer level:
1461
+
1462
+ ```ruby
1463
+ Datadog.configure do |c|
1464
+ c.tracer tags: { 'env' => 'prod' }
1465
+ end
1466
+ ```
1467
+
1468
+ This enables you to set this value on a per tracer basis, so you can have for example several applications reporting for different environments on the same host.
1469
+
1470
+ Ultimately, tags can be set per span, but `env` should typically be the same for all spans belonging to a given trace.
1471
+
1472
+ ### Sampling
1473
+
1474
+ `ddtrace` can perform trace sampling. While the trace agent already samples traces to reduce bandwidth usage, client sampling reduces the performance overhead.
1475
+
1476
+ `Datadog::RateSampler` samples a ratio of the traces. For example:
1477
+
1478
+ ```ruby
1479
+ # Sample rate is between 0 (nothing sampled) to 1 (everything sampled).
1480
+ sampler = Datadog::RateSampler.new(0.5) # sample 50% of the traces
1481
+ Datadog.configure do |c|
1482
+ c.tracer sampler: sampler
1483
+ end
1484
+ ```
1485
+
1486
+ #### Priority sampling
1487
+
1488
+ Priority sampling decides whether to keep a trace by using a priority attribute propagated for distributed traces. Its value indicates to the Agent and the backend about how important the trace is.
1489
+
1490
+ The sampler can set the priority to the following values:
1491
+
1492
+ - `Datadog::Ext::Priority::AUTO_REJECT`: the sampler automatically decided to reject the trace.
1493
+ - `Datadog::Ext::Priority::AUTO_KEEP`: the sampler automatically decided to keep the trace.
1494
+
1495
+ Priority sampling is enabled by default. Enabling it ensures that your sampled distributed traces will be complete. Once enabled, the sampler will automatically assign a priority of 0 or 1 to traces, depending on their service and volume.
1496
+
1497
+ You can also set this priority manually to either drop a non-interesting trace or to keep an important one. For that, set the `context#sampling_priority` to:
1498
+
1499
+ - `Datadog::Ext::Priority::USER_REJECT`: the user asked to reject the trace.
1500
+ - `Datadog::Ext::Priority::USER_KEEP`: the user asked to keep the trace.
1501
+
1502
+ When not using [distributed tracing](#distributed-tracing), you may change the priority at any time, as long as the trace incomplete. But it has to be done before any context propagation (fork, RPC calls) to be useful in a distributed context. Changing the priority after the context has been propagated causes different parts of a distributed trace to use different priorities. Some parts might be kept, some parts might be rejected, and this can cause the trace to be partially stored and remain incomplete.
1503
+
1504
+ If you change the priority, we recommend you do it as soon as possible - when the root span has just been created.
1505
+
1506
+ ```ruby
1507
+ # First, grab the active span
1508
+ span = Datadog.tracer.active_span
1509
+
1510
+ # Indicate to reject the trace
1511
+ span.context.sampling_priority = Datadog::Ext::Priority::USER_REJECT
1512
+
1513
+ # Indicate to keep the trace
1514
+ span.context.sampling_priority = Datadog::Ext::Priority::USER_KEEP
1515
+ ```
1516
+
1517
+ ### Distributed Tracing
1518
+
1519
+ Distributed tracing allows traces to be propagated across multiple instrumented applications so that a request can be presented as a single trace, rather than a separate trace per service.
1520
+
1521
+ To trace requests across application boundaries, the following must be propagated between each application:
1522
+
1523
+ | Property | Type | Description |
1524
+ | --------------------- | ------- | --------------------------------------------------------------------------------------------------------------------------- |
1525
+ | **Trace ID** | Integer | ID of the trace. This value should be the same across all requests that belong to the same trace. |
1526
+ | **Parent Span ID** | Integer | ID of the span in the service originating the request. This value will always be different for each request within a trace. |
1527
+ | **Sampling Priority** | Integer | Sampling priority level for the trace. This value should be the same across all requests that belong to the same trace. |
1528
+
1529
+ Such propagation can be visualized as:
1530
+
1531
+ ```
1532
+ Service A:
1533
+ Trace ID: 100000000000000001
1534
+ Parent ID: 0
1535
+ Span ID: 100000000000000123
1536
+ Priority: 1
1537
+
1538
+ |
1539
+ | Service B Request:
1540
+ | Metadata:
1541
+ | Trace ID: 100000000000000001
1542
+ | Parent ID: 100000000000000123
1543
+ | Priority: 1
1544
+ |
1545
+ V
1546
+
1547
+ Service B:
1548
+ Trace ID: 100000000000000001
1549
+ Parent ID: 100000000000000123
1550
+ Span ID: 100000000000000456
1551
+ Priority: 1
1552
+
1553
+ |
1554
+ | Service C Request:
1555
+ | Metadata:
1556
+ | Trace ID: 100000000000000001
1557
+ | Parent ID: 100000000000000456
1558
+ | Priority: 1
1559
+ |
1560
+ V
1561
+
1562
+ Service C:
1563
+ Trace ID: 100000000000000001
1564
+ Parent ID: 100000000000000456
1565
+ Span ID: 100000000000000789
1566
+ Priority: 1
1567
+ ```
1568
+
1569
+ **Via HTTP**
1570
+
1571
+ For HTTP requests between instrumented applications, this trace metadata is propagated by use of HTTP Request headers:
1572
+
1573
+ | Property | Type | HTTP Header name |
1574
+ | --------------------- | ------- | ----------------------------- |
1575
+ | **Trace ID** | Integer | `x-datadog-trace-id` |
1576
+ | **Parent Span ID** | Integer | `x-datadog-parent-id` |
1577
+ | **Sampling Priority** | Integer | `x-datadog-sampling-priority` |
1578
+
1579
+ Such that:
1580
+
1581
+ ```
1582
+ Service A:
1583
+ Trace ID: 100000000000000001
1584
+ Parent ID: 0
1585
+ Span ID: 100000000000000123
1586
+ Priority: 1
1587
+
1588
+ |
1589
+ | Service B HTTP Request:
1590
+ | Headers:
1591
+ | x-datadog-trace-id: 100000000000000001
1592
+ | x-datadog-parent-id: 100000000000000123
1593
+ | x-datadog-sampling-priority: 1
1594
+ |
1595
+ V
1596
+
1597
+ Service B:
1598
+ Trace ID: 100000000000000001
1599
+ Parent ID: 100000000000000123
1600
+ Span ID: 100000000000000456
1601
+ Priority: 1
1602
+
1603
+ |
1604
+ | Service C HTTP Request:
1605
+ | Headers:
1606
+ | x-datadog-trace-id: 100000000000000001
1607
+ | x-datadog-parent-id: 100000000000000456
1608
+ | x-datadog-sampling-priority: 1
1609
+ |
1610
+ V
1611
+
1612
+ Service C:
1613
+ Trace ID: 100000000000000001
1614
+ Parent ID: 100000000000000456
1615
+ Span ID: 100000000000000789
1616
+ Priority: 1
1617
+ ```
1618
+
1619
+ **Activating distributed tracing for integrations**
1620
+
1621
+ Many integrations included in `ddtrace` support distributed tracing. Distributed tracing is enabled by default, but can be activated via configuration settings.
1622
+
1623
+ - If your application receives requests from services with distributed tracing activated, you must activate distributed tracing on the integrations that handle these requests (e.g. Rails)
1624
+ - If your application send requests to services with distributed tracing activated, you must activate distributed tracing on the integrations that send these requests (e.g. Faraday)
1625
+ - If your application both sends and receives requests implementing distributed tracing, it must activate all integrations that handle these requests.
1626
+
1627
+ For more details on how to activate distributed tracing for integrations, see their documentation:
1628
+
1629
+ - [Excon](#excon)
1630
+ - [Faraday](#faraday)
1631
+ - [Rest Client](#restclient)
1632
+ - [Net/HTTP](#nethttp)
1633
+ - [Rack](#rack)
1634
+ - [Rails](#rails)
1635
+ - [Sinatra](#sinatra)
1636
+
1637
+ **Using the HTTP propagator**
1638
+
1639
+ To make the process of propagating this metadata easier, you can use the `Datadog::HTTPPropagator` module.
1640
+
1641
+ On the client:
1642
+
1643
+ ```ruby
1644
+ Datadog.tracer.trace('web.call') do |span|
1645
+ # Inject span context into headers (`env` must be a Hash)
1646
+ Datadog::HTTPPropagator.inject!(span.context, env)
1647
+ end
1648
+ ```
1649
+
1650
+ On the server:
1651
+
1652
+ ```ruby
1653
+ Datadog.tracer.trace('web.work') do |span|
1654
+ # Build a context from headers (`env` must be a Hash)
1655
+ context = HTTPPropagator.extract(request.env)
1656
+ Datadog.tracer.provider.context = context if context.trace_id
1657
+ end
1658
+ ```
1659
+
1660
+ ### HTTP request queuing
1661
+
1662
+ Traces that originate from HTTP requests can be configured to include the time spent in a frontend web server or load balancer queue before the request reaches the Ruby application.
1663
+
1664
+ This functionality is **experimental** and deactivated by default.
1665
+
1666
+ To activate this feature, you must add an `X-Request-Start` or `X-Queue-Start` header from your web server (i.e., Nginx). The following is an Nginx configuration example:
1667
+
1668
+ ```
1669
+ # /etc/nginx/conf.d/ruby_service.conf
1670
+ server {
1671
+ listen 8080;
1672
+
1673
+ location / {
1674
+ proxy_set_header X-Request-Start "t=${msec}";
1675
+ proxy_pass http://web:3000;
1676
+ }
1677
+ }
1678
+ ```
1679
+
1680
+ Then you must enable the request queuing feature in the integration handling the request.
1681
+
1682
+ For Rack-based applications, see the [documentation](#rack) for details for enabling this feature.
1683
+
1684
+ ### Processing Pipeline
1685
+
1686
+ Some applications might require that traces be altered or filtered out before they are sent upstream. The processing pipeline allows users to create *processors* to define such behavior.
1687
+
1688
+ Processors can be any object that responds to `#call` accepting `trace` as an argument (which is an `Array` of `Datadog::Span`s.)
1689
+
1690
+ For example:
1691
+
1692
+ ```ruby
1693
+ lambda_processor = ->(trace) do
1694
+ # Processing logic...
1695
+ trace
1696
+ end
1697
+
1698
+ class MyCustomProcessor
1699
+ def call(trace)
1700
+ # Processing logic...
1701
+ trace
1702
+ end
1703
+ end
1704
+ custom_processor = MyFancyProcessor.new
1705
+ ```
1706
+
1707
+ `#call` blocks of processors *must* return the `trace` object; this return value will be passed to the next processor in the pipeline.
1708
+
1709
+ These processors must then be added to the pipeline via `Datadog::Pipeline.before_flush`:
1710
+
1711
+ ```ruby
1712
+ Datadog::Pipeline.before_flush(lambda_processor, custom_processor)
1713
+ ```
1714
+
1715
+ You can also define processors using the short-hand block syntax for `Datadog::Pipeline.before_flush`:
1716
+
1717
+ ```ruby
1718
+ Datadog::Pipeline.before_flush do |trace|
1719
+ trace.delete_if { |span| span.name =~ /forbidden/ }
1720
+ end
1721
+ ```
1722
+
1723
+ #### Filtering
1724
+
1725
+ You can use the `Datadog::Pipeline::SpanFilter` processor to remove spans, when the block evaluates as truthy:
1726
+
1727
+ ```ruby
1728
+ Datadog::Pipeline.before_flush(
1729
+ # Remove spans that match a particular resource
1730
+ Datadog::Pipeline::SpanFilter.new { |span| span.resource =~ /PingController/ },
1731
+ # Remove spans that are trafficked to localhost
1732
+ Datadog::Pipeline::SpanFilter.new { |span| span.get_tag('host') == 'localhost' }
1733
+ )
1734
+ ```
1735
+
1736
+ #### Processing
1737
+
1738
+ You can use the `Datadog::Pipeline::SpanProcessor` processor to modify spans:
1739
+
1740
+ ```ruby
1741
+ Datadog::Pipeline.before_flush(
1742
+ # Strip matching text from the resource field
1743
+ Datadog::Pipeline::SpanProcessor.new { |span| span.resource.gsub!(/password=.*/, '') }
1744
+ )
1745
+ ```
1746
+
1747
+ ### Trace correlation
1748
+
1749
+ In many cases, such as logging, it may be useful to correlate trace IDs to other events or data streams, for easier cross-referencing. The tracer can produce a correlation identifier for the currently active trace via `active_correlation`, which can be used to decorate these other data sources.
1750
+
1751
+ ```ruby
1752
+ # When a trace is active...
1753
+ Datadog.tracer.trace('correlation.example') do
1754
+ # Returns #<Datadog::Correlation::Identifier>
1755
+ correlation = Datadog.tracer.active_correlation
1756
+ correlation.trace_id # => 5963550561812073440
1757
+ correlation.span_id # => 2232727802607726424
1758
+ end
1759
+
1760
+ # When a trace isn't active...
1761
+ correlation = Datadog.tracer.active_correlation
1762
+ # Returns #<Datadog::Correlation::Identifier>
1763
+ correlation = Datadog.tracer.active_correlation
1764
+ correlation.trace_id # => 0
1765
+ correlation.span_id # => 0
1766
+ ```
1767
+
1768
+ #### For logging in Rails applications using Lograge (recommended)
1769
+
1770
+ After [setting up Lograge in a Rails application](https://docs.datadoghq.com/logs/log_collection/ruby/), modify the `custom_options` block in your environment configuration file (e.g. `config/environments/production.rb`) to add the trace IDs:
1771
+
1772
+ ```ruby
1773
+ config.lograge.custom_options = lambda do |event|
1774
+ # Retrieves trace information for current thread
1775
+ correlation = Datadog.tracer.active_correlation
1776
+
1777
+ {
1778
+ # Adds IDs as tags to log output
1779
+ :dd => {
1780
+ # To preserve precision during JSON serialization, use strings for large numbers
1781
+ :trace_id => correlation.trace_id.to_s,
1782
+ :span_id => correlation.span_id.to_s
1783
+ },
1784
+ :ddsource => ["ruby"],
1785
+ :params => event.payload[:params].reject { |k| %w(controller action).include? k }
1786
+ }
1787
+ end
1788
+ ```
1789
+
1790
+ #### For logging in Rails applications
1791
+
1792
+ Rails applications which are configured with an `ActiveSupport::TaggedLogging` logger can append correlation IDs as tags to log output. The default Rails logger implements this tagged logging, making it easier to add correlation tags.
1793
+
1794
+ In your Rails environment configuration file, add the following:
1795
+
1796
+ ```ruby
1797
+ Rails.application.configure do
1798
+ config.log_tags = [proc { Datadog.tracer.active_correlation.to_s }]
1799
+ end
1800
+
1801
+ # Web requests will produce:
1802
+ # [dd.trace_id=7110975754844687674 dd.span_id=7518426836986654206] Started GET "/articles" for 172.22.0.1 at 2019-01-16 18:50:57 +0000
1803
+ # [dd.trace_id=7110975754844687674 dd.span_id=7518426836986654206] Processing by ArticlesController#index as */*
1804
+ # [dd.trace_id=7110975754844687674 dd.span_id=7518426836986654206] Article Load (0.5ms) SELECT "articles".* FROM "articles"
1805
+ # [dd.trace_id=7110975754844687674 dd.span_id=7518426836986654206] Completed 200 OK in 7ms (Views: 5.5ms | ActiveRecord: 0.5ms)
1806
+ ```
1807
+
1808
+ #### For logging in Ruby applications
1809
+
1810
+ To add correlation IDs to your logger, add a log formatter which retrieves the correlation IDs with `Datadog.tracer.active_correlation`, then add them to the message.
1811
+
1812
+ To properly correlate with Datadog logging, be sure the following is present in the log message:
1813
+
1814
+ - `dd.trace_id=<TRACE_ID>`: Where `<TRACE_ID>` is equal to `Datadog.tracer.active_correlation.trace_id` or `0` if no trace is active during logging.
1815
+ - `dd.span_id=<SPAN_ID>`: Where `<SPAN_ID>` is equal to `Datadog.tracer.active_correlation.span_id` or `0` if no trace is active during logging.
1816
+
1817
+ By default, `Datadog::Correlation::Identifier#to_s` will return `dd.trace_id=<TRACE_ID> dd.span_id=<SPAN_ID>`.
1818
+
1819
+ An example of this in practice:
1820
+
1821
+ ```ruby
1822
+ require 'ddtrace'
1823
+ require 'logger'
1824
+
1825
+ logger = Logger.new(STDOUT)
1826
+ logger.progname = 'my_app'
1827
+ logger.formatter = proc do |severity, datetime, progname, msg|
1828
+ "[#{datetime}][#{progname}][#{severity}][#{Datadog.tracer.active_correlation}] #{msg}\n"
1829
+ end
1830
+
1831
+ # When no trace is active
1832
+ logger.warn('This is an untraced operation.')
1833
+ # [2019-01-16 18:38:41 +0000][my_app][WARN][dd.trace_id=0 dd.span_id=0] This is an untraced operation.
1834
+
1835
+ # When a trace is active
1836
+ Datadog.tracer.trace('my.operation') { logger.warn('This is a traced operation.') }
1837
+ # [2019-01-16 18:38:41 +0000][my_app][WARN][dd.trace_id=8545847825299552251 dd.span_id=3711755234730770098] This is a traced operation.
1838
+ ```
1839
+
1840
+ ### Configuring the transport layer
1841
+
1842
+ By default, the tracer submits trace data using `Net::HTTP` to `127.0.0.1:8126`, the default location for the Datadog trace agent process. However, the tracer can be configured to send its trace data to alternative destinations, or by alternative protocols.
1843
+
1844
+ Some basic settings, such as hostname and port, can be configured using [tracer settings](#tracer-settings).
1845
+
1846
+ #### Using the Net::HTTP adapter
1847
+
1848
+ The `Net` adapter submits traces using `Net::HTTP` over TCP. It is the default transport adapter.
1849
+
1850
+ ```ruby
1851
+ Datadog.configure do |c|
1852
+ c.tracer transport_options: proc do |t|
1853
+ # Hostname, port, and additional options. :timeout is in seconds.
1854
+ t.adapter :net_http, '127.0.0.1', 8126, { timeout: 1 }
1855
+ }
1856
+ end
1857
+ ```
1858
+
1859
+ #### Using the Unix socket adapter
1860
+
1861
+ The `UnixSocket` adapter submits traces using `Net::HTTP` over Unix socket.
1862
+
1863
+ To use, first configure your trace agent to listen by Unix socket, then configure the tracer with:
1864
+
1865
+ ```ruby
1866
+ Datadog.configure do |c|
1867
+ c.tracer transport_options: proc { |t|
1868
+ # Provide filepath to trace agent Unix socket
1869
+ t.adapter :unix, '/tmp/ddagent/trace.sock'
1870
+ }
1871
+ end
1872
+ ```
1873
+
1874
+ #### Using the transport test adapter
1875
+
1876
+ The `Test` adapter is a no-op transport that can optionally buffer requests. For use in test suites or other non-production environments.
1877
+
1878
+ ```ruby
1879
+ Datadog.configure do |c|
1880
+ c.tracer transport_options: proc { |t|
1881
+ # Set transport to no-op mode. Does not retain traces.
1882
+ t.adapter :test
1883
+
1884
+ # Alternatively, you can provide a buffer to examine trace output.
1885
+ # The buffer must respond to '<<'.
1886
+ t.adapter :test, []
1887
+ }
1888
+ end
1889
+ ```
1890
+
1891
+ #### Using a custom transport adapter
1892
+
1893
+ Custom adapters can be configured with:
1894
+
1895
+ ```ruby
1896
+ Datadog.configure do |c|
1897
+ c.tracer transport_options: proc { |t|
1898
+ # Initialize and pass an instance of the adapter
1899
+ custom_adapter = CustomAdapter.new
1900
+ t.adapter custom_adapter
1901
+ }
1902
+ end
1903
+ ```
1904
+
1905
+ ### Metrics
1906
+
1907
+ The tracer and its integrations can produce some additional metrics that can provide useful insight into the performance of your application. These metrics are collected with `dogstatsd-ruby`, and can be sent to the same Datadog agent to which you send your traces.
1908
+
1909
+ To configure your application for metrics collection:
1910
+
1911
+ 1. [Configure your Datadog agent for StatsD](https://docs.datadoghq.com/developers/dogstatsd/#setup)
1912
+ 2. Add `gem 'dogstatsd-ruby'` to your Gemfile
1913
+
1914
+ #### For application runtime
1915
+
1916
+ If runtime metrics are configured, the trace library will automatically collect and send metrics about the health of your application.
1917
+
1918
+ To configure runtime metrics, add the following configuration:
1919
+
1920
+ ```ruby
1921
+ # config/initializers/datadog.rb
1922
+ require 'datadog/statsd'
1923
+ require 'ddtrace'
1924
+
1925
+ Datadog.configure do |c|
1926
+ # To enable runtime metrics collection, set `true`. Defaults to `false`
1927
+ # You can also set DD_RUNTIME_METRICS_ENABLED=true to configure this.
1928
+ c.runtime_metrics_enabled = true
1929
+
1930
+ # Optionally, you can configure the Statsd instance used for sending runtime metrics.
1931
+ # Statsd is automatically configured with default settings if `dogstatsd-ruby` is available.
1932
+ # You can configure with host and port of Datadog agent; defaults to 'localhost:8125'.
1933
+ c.runtime_metrics statsd: Datadog::Statsd.new
1934
+ end
1935
+ ```
1936
+
1937
+ See the [Dogstatsd documentation](https://www.rubydoc.info/github/DataDog/dogstatsd-ruby/master/frames) for more details about configuring `Datadog::Statsd`.
1938
+
1939
+ The stats sent will include:
1940
+
1941
+ | Name | Type | Description |
1942
+ | -------------------------- | ------- | -------------------------------------------------------- |
1943
+ | `runtime.ruby.class_count` | `gauge` | Number of classes in memory space. |
1944
+ | `runtime.ruby.thread_count` | `gauge` | Number of threads. |
1945
+ | `runtime.ruby.gc.*`. | `gauge` | Garbage collection statistics (one per value in GC.stat) |
1946
+
1947
+ In addition, all metrics include the following tags:
1948
+
1949
+ | Name | Description |
1950
+ | ------------ | ------------------------------------------------------- |
1951
+ | `language` | Programming language traced. (e.g. `ruby`) |
1952
+ | `service` | List of services this associated with this metric. |
1953
+
1954
+ ### OpenTracing
1955
+
1956
+ For setting up Datadog with OpenTracing, see out [Quickstart for OpenTracing](#quickstart-for-opentracing) section for details.
1957
+
1958
+ **Configuring Datadog tracer settings**
1959
+
1960
+ The underlying Datadog tracer can be configured by passing options (which match `Datadog::Tracer`) when configuring the global tracer:
1961
+
1962
+ ```ruby
1963
+ # Where `options` is a Hash of options provided to Datadog::Tracer
1964
+ OpenTracing.global_tracer = Datadog::OpenTracer::Tracer.new(options)
1965
+ ```
1966
+
1967
+ It can also be configured by using `Datadog.configure` described in the [Tracer settings](#tracer-settings) section.
1968
+
1969
+ **Activating and configuring integrations**
1970
+
1971
+ By default, configuring OpenTracing with Datadog will not automatically activate any additional instrumentation provided by Datadog. You will only receive spans and traces from OpenTracing instrumentation you have in your application.
1972
+
1973
+ However, additional instrumentation provided by Datadog can be activated alongside OpenTracing using `Datadog.configure`, which can be used to enhance your tracing further. To activate this, see [Integration instrumentation](#integration-instrumentation) for more details.
1974
+
1975
+ **Supported serialization formats**
1976
+
1977
+ | Type | Supported? | Additional information |
1978
+ | ------------------------------ | ---------- | ---------------------- |
1979
+ | `OpenTracing::FORMAT_TEXT_MAP` | Yes | |
1980
+ | `OpenTracing::FORMAT_RACK` | Yes | Because of the loss of resolution in the Rack format, please note that baggage items with names containing either upper case characters or `-` will be converted to lower case and `_` in a round-trip respectively. We recommend avoiding these characters or accommodating accordingly on the receiving end. |
1981
+ | `OpenTracing::FORMAT_BINARY` | No | |