ddtrace 1.14.0 → 1.15.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (270) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +178 -2
  3. data/ext/ddtrace_profiling_native_extension/NativeExtensionDesign.md +3 -5
  4. data/ext/ddtrace_profiling_native_extension/clock_id.h +0 -3
  5. data/ext/ddtrace_profiling_native_extension/clock_id_from_pthread.c +0 -22
  6. data/ext/ddtrace_profiling_native_extension/clock_id_noop.c +0 -1
  7. data/ext/ddtrace_profiling_native_extension/collectors_cpu_and_wall_time_worker.c +41 -6
  8. data/ext/ddtrace_profiling_native_extension/collectors_idle_sampling_helper.c +3 -0
  9. data/ext/ddtrace_profiling_native_extension/collectors_stack.c +76 -24
  10. data/ext/ddtrace_profiling_native_extension/collectors_stack.h +1 -1
  11. data/ext/ddtrace_profiling_native_extension/collectors_thread_context.c +207 -32
  12. data/ext/ddtrace_profiling_native_extension/collectors_thread_context.h +1 -1
  13. data/ext/ddtrace_profiling_native_extension/extconf.rb +8 -2
  14. data/ext/ddtrace_profiling_native_extension/http_transport.c +26 -10
  15. data/ext/ddtrace_profiling_native_extension/libdatadog_helpers.c +42 -0
  16. data/ext/ddtrace_profiling_native_extension/libdatadog_helpers.h +6 -0
  17. data/ext/ddtrace_profiling_native_extension/native_extension_helpers.rb +1 -16
  18. data/ext/ddtrace_profiling_native_extension/pid_controller.c +57 -0
  19. data/ext/ddtrace_profiling_native_extension/pid_controller.h +45 -0
  20. data/ext/ddtrace_profiling_native_extension/private_vm_api_access.c +17 -12
  21. data/ext/ddtrace_profiling_native_extension/profiling.c +0 -2
  22. data/ext/ddtrace_profiling_native_extension/stack_recorder.c +74 -37
  23. data/ext/ddtrace_profiling_native_extension/stack_recorder.h +13 -3
  24. data/lib/datadog/appsec/assets/waf_rules/processors.json +92 -0
  25. data/lib/datadog/appsec/assets/waf_rules/recommended.json +698 -75
  26. data/lib/datadog/appsec/assets/waf_rules/scanners.json +114 -0
  27. data/lib/datadog/appsec/assets/waf_rules/strict.json +98 -8
  28. data/lib/datadog/appsec/assets.rb +8 -0
  29. data/lib/datadog/appsec/component.rb +9 -2
  30. data/lib/datadog/appsec/configuration/settings.rb +61 -2
  31. data/lib/datadog/appsec/contrib/rack/gateway/watcher.rb +8 -6
  32. data/lib/datadog/appsec/contrib/rack/reactive/request.rb +2 -7
  33. data/lib/datadog/appsec/contrib/rack/reactive/request_body.rb +2 -5
  34. data/lib/datadog/appsec/contrib/rack/reactive/response.rb +2 -5
  35. data/lib/datadog/appsec/contrib/rack/request_body_middleware.rb +3 -2
  36. data/lib/datadog/appsec/contrib/rack/request_middleware.rb +23 -9
  37. data/lib/datadog/appsec/contrib/rails/gateway/watcher.rb +3 -2
  38. data/lib/datadog/appsec/contrib/rails/patcher.rb +9 -3
  39. data/lib/datadog/appsec/contrib/rails/reactive/action.rb +2 -5
  40. data/lib/datadog/appsec/contrib/sinatra/gateway/watcher.rb +6 -4
  41. data/lib/datadog/appsec/contrib/sinatra/patcher.rb +13 -7
  42. data/lib/datadog/appsec/contrib/sinatra/reactive/routed.rb +2 -5
  43. data/lib/datadog/appsec/event.rb +106 -50
  44. data/lib/datadog/appsec/monitor/gateway/watcher.rb +3 -3
  45. data/lib/datadog/appsec/monitor/reactive/set_user.rb +2 -5
  46. data/lib/datadog/appsec/processor/actions.rb +49 -0
  47. data/lib/datadog/appsec/processor/rule_merger.rb +22 -2
  48. data/lib/datadog/appsec/processor.rb +34 -6
  49. data/lib/datadog/appsec/remote.rb +4 -1
  50. data/lib/datadog/appsec/response.rb +82 -4
  51. data/lib/datadog/appsec/sample_rate.rb +21 -0
  52. data/lib/datadog/appsec.rb +2 -2
  53. data/lib/datadog/core/configuration/agent_settings_resolver.rb +29 -24
  54. data/lib/datadog/core/configuration/base.rb +1 -11
  55. data/lib/datadog/core/configuration/components.rb +7 -2
  56. data/lib/datadog/core/configuration/ext.rb +21 -0
  57. data/lib/datadog/core/configuration/option.rb +2 -4
  58. data/lib/datadog/core/configuration/option_definition.rb +17 -41
  59. data/lib/datadog/core/configuration/options.rb +5 -5
  60. data/lib/datadog/core/configuration/settings.rb +47 -45
  61. data/lib/datadog/core/environment/execution.rb +47 -9
  62. data/lib/datadog/core/environment/variable_helpers.rb +0 -69
  63. data/lib/datadog/core/error.rb +1 -0
  64. data/lib/datadog/core/git/ext.rb +2 -0
  65. data/lib/datadog/core/remote/client/capabilities.rb +1 -1
  66. data/lib/datadog/core/remote/component.rb +2 -2
  67. data/lib/datadog/core/remote/negotiation.rb +2 -2
  68. data/lib/datadog/core/remote/transport/config.rb +60 -0
  69. data/lib/datadog/core/remote/transport/http/api/instance.rb +39 -0
  70. data/lib/datadog/core/remote/transport/http/api/spec.rb +21 -0
  71. data/lib/datadog/core/remote/transport/http/api.rb +58 -0
  72. data/lib/datadog/core/remote/transport/http/builder.rb +219 -0
  73. data/lib/datadog/core/remote/transport/http/client.rb +48 -0
  74. data/lib/datadog/core/remote/transport/http/config.rb +280 -0
  75. data/lib/datadog/core/remote/transport/http/negotiation.rb +146 -0
  76. data/lib/datadog/core/remote/transport/http.rb +179 -0
  77. data/lib/datadog/core/{transport → remote/transport}/negotiation.rb +25 -23
  78. data/lib/datadog/core/telemetry/collector.rb +3 -2
  79. data/lib/datadog/core/telemetry/http/transport.rb +2 -1
  80. data/lib/datadog/core/transport/ext.rb +47 -0
  81. data/lib/datadog/core/transport/http/adapters/net.rb +168 -0
  82. data/lib/datadog/core/transport/http/adapters/registry.rb +29 -0
  83. data/lib/datadog/core/transport/http/adapters/test.rb +89 -0
  84. data/lib/datadog/core/transport/http/adapters/unix_socket.rb +83 -0
  85. data/lib/datadog/core/transport/http/api/endpoint.rb +31 -0
  86. data/lib/datadog/core/transport/http/api/fallbacks.rb +26 -0
  87. data/lib/datadog/core/transport/http/api/map.rb +18 -0
  88. data/lib/datadog/core/transport/http/env.rb +62 -0
  89. data/lib/datadog/core/transport/http/response.rb +60 -0
  90. data/lib/datadog/core/transport/parcel.rb +22 -0
  91. data/lib/datadog/core/transport/request.rb +17 -0
  92. data/lib/datadog/core/transport/response.rb +64 -0
  93. data/lib/datadog/core/workers/polling.rb +2 -2
  94. data/lib/datadog/opentelemetry/api/context.rb +10 -3
  95. data/lib/datadog/opentelemetry/sdk/propagator.rb +2 -1
  96. data/lib/datadog/opentelemetry/sdk/span_processor.rb +14 -2
  97. data/lib/datadog/opentelemetry/sdk/trace/span.rb +68 -0
  98. data/lib/datadog/opentelemetry/trace.rb +58 -0
  99. data/lib/datadog/opentelemetry.rb +1 -0
  100. data/lib/datadog/opentracer.rb +9 -0
  101. data/lib/datadog/profiling/collectors/cpu_and_wall_time_worker.rb +12 -18
  102. data/lib/datadog/profiling/collectors/idle_sampling_helper.rb +1 -1
  103. data/lib/datadog/profiling/collectors/thread_context.rb +9 -1
  104. data/lib/datadog/profiling/component.rb +24 -99
  105. data/lib/datadog/profiling/ext.rb +0 -12
  106. data/lib/datadog/profiling/flush.rb +0 -3
  107. data/lib/datadog/profiling/http_transport.rb +6 -3
  108. data/lib/datadog/profiling/native_extension.rb +0 -21
  109. data/lib/datadog/profiling/profiler.rb +11 -12
  110. data/lib/datadog/profiling.rb +8 -81
  111. data/lib/datadog/tracing/component.rb +10 -4
  112. data/lib/datadog/tracing/configuration/agent_settings_resolver.rb +13 -0
  113. data/lib/datadog/tracing/configuration/ext.rb +4 -2
  114. data/lib/datadog/tracing/configuration/settings.rb +14 -7
  115. data/lib/datadog/tracing/contrib/action_pack/configuration/settings.rb +1 -1
  116. data/lib/datadog/tracing/contrib/active_job/configuration/settings.rb +1 -1
  117. data/lib/datadog/tracing/contrib/active_record/events/sql.rb +4 -0
  118. data/lib/datadog/tracing/contrib/active_support/cache/instrumentation.rb +104 -197
  119. data/lib/datadog/tracing/contrib/active_support/cache/patcher.rb +3 -0
  120. data/lib/datadog/tracing/contrib/aws/instrumentation.rb +7 -0
  121. data/lib/datadog/tracing/contrib/configuration/settings.rb +1 -1
  122. data/lib/datadog/tracing/contrib/dalli/configuration/settings.rb +6 -0
  123. data/lib/datadog/tracing/contrib/dalli/ext.rb +7 -0
  124. data/lib/datadog/tracing/contrib/dalli/instrumentation.rb +9 -2
  125. data/lib/datadog/tracing/contrib/delayed_job/configuration/settings.rb +1 -1
  126. data/lib/datadog/tracing/contrib/elasticsearch/patcher.rb +5 -0
  127. data/lib/datadog/tracing/contrib/ethon/easy_patch.rb +5 -0
  128. data/lib/datadog/tracing/contrib/ethon/multi_patch.rb +8 -0
  129. data/lib/datadog/tracing/contrib/excon/middleware.rb +5 -0
  130. data/lib/datadog/tracing/contrib/ext.rb +3 -0
  131. data/lib/datadog/tracing/contrib/faraday/configuration/settings.rb +1 -1
  132. data/lib/datadog/tracing/contrib/faraday/middleware.rb +5 -0
  133. data/lib/datadog/tracing/contrib/grpc/configuration/settings.rb +21 -1
  134. data/lib/datadog/tracing/contrib/grpc/datadog_interceptor/client.rb +11 -1
  135. data/lib/datadog/tracing/contrib/grpc/datadog_interceptor/server.rb +18 -0
  136. data/lib/datadog/tracing/contrib/grpc/datadog_interceptor.rb +0 -4
  137. data/lib/datadog/tracing/contrib/http/circuit_breaker.rb +3 -3
  138. data/lib/datadog/tracing/contrib/http/instrumentation.rb +5 -0
  139. data/lib/datadog/tracing/contrib/httpclient/instrumentation.rb +5 -0
  140. data/lib/datadog/tracing/contrib/httprb/instrumentation.rb +5 -0
  141. data/lib/datadog/tracing/contrib/mongodb/subscribers.rb +7 -0
  142. data/lib/datadog/tracing/contrib/mysql2/instrumentation.rb +13 -3
  143. data/lib/datadog/tracing/contrib/opensearch/integration.rb +2 -2
  144. data/lib/datadog/tracing/contrib/opensearch/patcher.rb +7 -0
  145. data/lib/datadog/tracing/contrib/pg/instrumentation.rb +5 -0
  146. data/lib/datadog/tracing/contrib/presto/instrumentation.rb +5 -0
  147. data/lib/datadog/tracing/contrib/propagation/sql_comment.rb +1 -1
  148. data/lib/datadog/tracing/contrib/que/configuration/settings.rb +1 -1
  149. data/lib/datadog/tracing/contrib/racecar/event.rb +5 -0
  150. data/lib/datadog/tracing/contrib/rack/header_tagging.rb +14 -4
  151. data/lib/datadog/tracing/contrib/rails/configuration/settings.rb +4 -4
  152. data/lib/datadog/tracing/contrib/rake/configuration/settings.rb +1 -1
  153. data/lib/datadog/tracing/contrib/redis/tags.rb +5 -0
  154. data/lib/datadog/tracing/contrib/resque/configuration/settings.rb +1 -1
  155. data/lib/datadog/tracing/contrib/rest_client/request_patch.rb +5 -0
  156. data/lib/datadog/tracing/contrib/sequel/utils.rb +5 -0
  157. data/lib/datadog/tracing/contrib/shoryuken/configuration/settings.rb +1 -1
  158. data/lib/datadog/tracing/contrib/sidekiq/configuration/settings.rb +1 -1
  159. data/lib/datadog/tracing/contrib/sneakers/configuration/settings.rb +1 -1
  160. data/lib/datadog/tracing/contrib/utils/quantization/http.rb +2 -2
  161. data/lib/datadog/tracing/distributed/propagation.rb +13 -33
  162. data/lib/datadog/tracing/metadata/tagging.rb +3 -3
  163. data/lib/datadog/tracing/sync_writer.rb +3 -3
  164. data/lib/datadog/tracing/tracer.rb +2 -0
  165. data/lib/datadog/{core → tracing}/transport/http/api/instance.rb +1 -1
  166. data/lib/datadog/{core → tracing}/transport/http/api/spec.rb +1 -1
  167. data/lib/datadog/tracing/transport/http/api.rb +43 -0
  168. data/lib/datadog/{core → tracing}/transport/http/builder.rb +13 -68
  169. data/lib/datadog/tracing/transport/http/client.rb +57 -0
  170. data/lib/datadog/tracing/transport/http/statistics.rb +47 -0
  171. data/lib/datadog/tracing/transport/http/traces.rb +152 -0
  172. data/lib/datadog/tracing/transport/http.rb +124 -0
  173. data/lib/datadog/tracing/transport/io/client.rb +89 -0
  174. data/lib/datadog/tracing/transport/io/response.rb +27 -0
  175. data/lib/datadog/tracing/transport/io/traces.rb +101 -0
  176. data/lib/datadog/tracing/transport/io.rb +30 -0
  177. data/lib/datadog/tracing/transport/serializable_trace.rb +126 -0
  178. data/lib/datadog/tracing/transport/statistics.rb +77 -0
  179. data/lib/datadog/tracing/transport/trace_formatter.rb +209 -0
  180. data/lib/datadog/tracing/transport/traces.rb +224 -0
  181. data/lib/datadog/tracing/workers/trace_writer.rb +5 -3
  182. data/lib/datadog/tracing/workers.rb +3 -2
  183. data/lib/datadog/tracing/writer.rb +5 -2
  184. data/lib/ddtrace/transport/ext.rb +17 -15
  185. data/lib/ddtrace/version.rb +1 -1
  186. data/lib/ddtrace.rb +1 -1
  187. metadata +72 -96
  188. data/lib/datadog/ci/configuration/components.rb +0 -32
  189. data/lib/datadog/ci/configuration/settings.rb +0 -51
  190. data/lib/datadog/ci/contrib/cucumber/configuration/settings.rb +0 -35
  191. data/lib/datadog/ci/contrib/cucumber/ext.rb +0 -22
  192. data/lib/datadog/ci/contrib/cucumber/formatter.rb +0 -94
  193. data/lib/datadog/ci/contrib/cucumber/instrumentation.rb +0 -28
  194. data/lib/datadog/ci/contrib/cucumber/integration.rb +0 -47
  195. data/lib/datadog/ci/contrib/cucumber/patcher.rb +0 -27
  196. data/lib/datadog/ci/contrib/minitest/configuration/settings.rb +0 -35
  197. data/lib/datadog/ci/contrib/minitest/ext.rb +0 -21
  198. data/lib/datadog/ci/contrib/minitest/integration.rb +0 -49
  199. data/lib/datadog/ci/contrib/minitest/patcher.rb +0 -27
  200. data/lib/datadog/ci/contrib/minitest/test_helper.rb +0 -68
  201. data/lib/datadog/ci/contrib/rspec/configuration/settings.rb +0 -35
  202. data/lib/datadog/ci/contrib/rspec/example.rb +0 -68
  203. data/lib/datadog/ci/contrib/rspec/ext.rb +0 -21
  204. data/lib/datadog/ci/contrib/rspec/integration.rb +0 -48
  205. data/lib/datadog/ci/contrib/rspec/patcher.rb +0 -27
  206. data/lib/datadog/ci/ext/app_types.rb +0 -9
  207. data/lib/datadog/ci/ext/environment.rb +0 -575
  208. data/lib/datadog/ci/ext/settings.rb +0 -10
  209. data/lib/datadog/ci/ext/test.rb +0 -35
  210. data/lib/datadog/ci/extensions.rb +0 -19
  211. data/lib/datadog/ci/flush.rb +0 -38
  212. data/lib/datadog/ci/test.rb +0 -81
  213. data/lib/datadog/ci.rb +0 -21
  214. data/lib/datadog/core/configuration/dependency_resolver.rb +0 -28
  215. data/lib/datadog/core/configuration/option_definition_set.rb +0 -22
  216. data/lib/datadog/core/configuration/option_set.rb +0 -10
  217. data/lib/datadog/core/transport/config.rb +0 -58
  218. data/lib/datadog/core/transport/http/api.rb +0 -57
  219. data/lib/datadog/core/transport/http/client.rb +0 -45
  220. data/lib/datadog/core/transport/http/config.rb +0 -278
  221. data/lib/datadog/core/transport/http/negotiation.rb +0 -144
  222. data/lib/datadog/core/transport/http.rb +0 -169
  223. data/lib/datadog/core/utils/object_set.rb +0 -43
  224. data/lib/datadog/core/utils/string_table.rb +0 -47
  225. data/lib/datadog/profiling/backtrace_location.rb +0 -34
  226. data/lib/datadog/profiling/buffer.rb +0 -43
  227. data/lib/datadog/profiling/collectors/old_stack.rb +0 -301
  228. data/lib/datadog/profiling/encoding/profile.rb +0 -41
  229. data/lib/datadog/profiling/event.rb +0 -15
  230. data/lib/datadog/profiling/events/stack.rb +0 -82
  231. data/lib/datadog/profiling/old_recorder.rb +0 -107
  232. data/lib/datadog/profiling/pprof/builder.rb +0 -125
  233. data/lib/datadog/profiling/pprof/converter.rb +0 -102
  234. data/lib/datadog/profiling/pprof/message_set.rb +0 -16
  235. data/lib/datadog/profiling/pprof/payload.rb +0 -20
  236. data/lib/datadog/profiling/pprof/pprof.proto +0 -212
  237. data/lib/datadog/profiling/pprof/pprof_pb.rb +0 -81
  238. data/lib/datadog/profiling/pprof/stack_sample.rb +0 -139
  239. data/lib/datadog/profiling/pprof/string_table.rb +0 -12
  240. data/lib/datadog/profiling/pprof/template.rb +0 -118
  241. data/lib/datadog/profiling/trace_identifiers/ddtrace.rb +0 -43
  242. data/lib/datadog/profiling/trace_identifiers/helper.rb +0 -45
  243. data/lib/ddtrace/transport/http/adapters/net.rb +0 -168
  244. data/lib/ddtrace/transport/http/adapters/registry.rb +0 -27
  245. data/lib/ddtrace/transport/http/adapters/test.rb +0 -85
  246. data/lib/ddtrace/transport/http/adapters/unix_socket.rb +0 -77
  247. data/lib/ddtrace/transport/http/api/endpoint.rb +0 -29
  248. data/lib/ddtrace/transport/http/api/fallbacks.rb +0 -24
  249. data/lib/ddtrace/transport/http/api/instance.rb +0 -35
  250. data/lib/ddtrace/transport/http/api/map.rb +0 -16
  251. data/lib/ddtrace/transport/http/api/spec.rb +0 -17
  252. data/lib/ddtrace/transport/http/api.rb +0 -39
  253. data/lib/ddtrace/transport/http/builder.rb +0 -176
  254. data/lib/ddtrace/transport/http/client.rb +0 -52
  255. data/lib/ddtrace/transport/http/env.rb +0 -58
  256. data/lib/ddtrace/transport/http/response.rb +0 -58
  257. data/lib/ddtrace/transport/http/statistics.rb +0 -43
  258. data/lib/ddtrace/transport/http/traces.rb +0 -144
  259. data/lib/ddtrace/transport/http.rb +0 -117
  260. data/lib/ddtrace/transport/io/client.rb +0 -85
  261. data/lib/ddtrace/transport/io/response.rb +0 -25
  262. data/lib/ddtrace/transport/io/traces.rb +0 -99
  263. data/lib/ddtrace/transport/io.rb +0 -28
  264. data/lib/ddtrace/transport/parcel.rb +0 -20
  265. data/lib/ddtrace/transport/request.rb +0 -15
  266. data/lib/ddtrace/transport/response.rb +0 -60
  267. data/lib/ddtrace/transport/serializable_trace.rb +0 -122
  268. data/lib/ddtrace/transport/statistics.rb +0 -75
  269. data/lib/ddtrace/transport/trace_formatter.rb +0 -207
  270. data/lib/ddtrace/transport/traces.rb +0 -216
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Datadog
4
- module Core
4
+ module Tracing
5
5
  module Transport
6
6
  module HTTP
7
7
  module API
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Datadog
4
- module Core
4
+ module Tracing
5
5
  module Transport
6
6
  module HTTP
7
7
  module API
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../../core/encoding'
4
+
5
+ require_relative '../../../core/transport/http/api/map'
6
+ require_relative 'api/spec'
7
+
8
+ require_relative 'traces'
9
+
10
+ module Datadog
11
+ module Tracing
12
+ module Transport
13
+ module HTTP
14
+ # Namespace for API components
15
+ module API
16
+ # Default API versions
17
+ V4 = 'v0.4'
18
+ V3 = 'v0.3'
19
+
20
+ module_function
21
+
22
+ def defaults
23
+ Datadog::Core::Transport::HTTP::API::Map[
24
+ V4 => Spec.new do |s|
25
+ s.traces = Traces::API::Endpoint.new(
26
+ '/v0.4/traces',
27
+ Core::Encoding::MsgpackEncoder,
28
+ service_rates: true
29
+ )
30
+ end,
31
+ V3 => Spec.new do |s|
32
+ s.traces = Traces::API::Endpoint.new(
33
+ '/v0.3/traces',
34
+ Core::Encoding::MsgpackEncoder
35
+ )
36
+ end,
37
+ ].with_fallbacks(V4 => V3)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -1,50 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative '../../configuration/agent_settings_resolver'
4
- require_relative '../../../../ddtrace/transport/http/adapters/registry'
5
- require_relative '../../../../ddtrace/transport/http/api/map'
6
-
7
- # TODO: Decouple standard transport/http/api/instance
8
- #
9
- # Separate classes are needed because transport/http/trace includes
10
- # Trace::API::Instance which closes over and uses a single spec, which is
11
- # negotiated as either /v3 or /v4 for the whole API at the spec level, but we
12
- # need an independent toplevel path at the endpoint level.
13
- #
14
- # Separate classes are needed because of `include Trace::API::Instance`.
15
- #
16
- # Below should be:
17
- # require_relative '../../../../ddtrace/transport/http/api/instance'
3
+ require_relative '../../../core/configuration/agent_settings_resolver'
4
+ require_relative '../../../core/transport/http/adapters/registry'
5
+ require_relative '../../../core/transport/http/api/map'
18
6
  require_relative 'api/instance'
19
-
20
- # TODO: Decouple transport/http/client
21
- #
22
- # The standard one does `include Transport::HTTP::Statistics` and performs
23
- # stats updates, which may or may not be desirable in general.
24
- #
25
- # Below should be:
26
- # require_relative '../../../../ddtrace/transport/http/client'
27
7
  require_relative 'client'
28
8
 
29
- # TODO: Decouple transport/http/builder
30
- #
31
- # This class is duplicated even though it is tantalisingly close to the
32
- # one in ddtrace/transport mostly because it refers to a different
33
- # `API::Instance` in `#api_instance_class` but also because it operates on a
34
- # different `HTTP::Client`, as well as de-hardcoding the transport class
35
- # `Transport::Traces::Transport` in `#to_transport`
36
- #
37
- # Additionally, its design makes it so there can be only one (API, Client,
38
- # Transport) triplet, requiring a new transport instance for any varying
39
- # element of the triplet.
40
-
41
9
  module Datadog
42
- module Core
10
+ module Tracing
43
11
  module Transport
44
12
  module HTTP
45
13
  # Builds new instances of Transport::HTTP::Client
46
14
  class Builder
47
- REGISTRY = Datadog::Transport::HTTP::Adapters::Registry.new
15
+ REGISTRY = Datadog::Core::Transport::HTTP::Adapters::Registry.new
48
16
 
49
17
  attr_reader \
50
18
  :apis,
@@ -59,7 +27,7 @@ module Datadog
59
27
  @default_headers = {}
60
28
 
61
29
  # Client settings
62
- @apis = Datadog::Transport::HTTP::API::Map.new
30
+ @apis = Datadog::Core::Transport::HTTP::API::Map.new
63
31
  @default_api = nil
64
32
 
65
33
  # API settings
@@ -114,16 +82,17 @@ module Datadog
114
82
  @default_api = key
115
83
  end
116
84
 
117
- def to_transport(klass)
85
+ def to_transport
118
86
  raise NoDefaultApiError if @default_api.nil?
119
87
 
120
- klass.new(to_api_instances, @default_api)
88
+ # DEV: Should not be specific to traces
89
+ Transport::Traces::Transport.new(to_api_instances, @default_api)
121
90
  end
122
91
 
123
92
  def to_api_instances
124
93
  raise NoApisError if @apis.empty?
125
94
 
126
- @apis.inject(Datadog::Transport::HTTP::API::Map.new) do |instances, (key, spec)|
95
+ @apis.inject(Datadog::Core::Transport::HTTP::API::Map.new) do |instances, (key, spec)|
127
96
  instances.tap do
128
97
  api_options = @api_options[key].dup
129
98
 
@@ -154,46 +123,22 @@ module Datadog
154
123
 
155
124
  # Raised when the API key does not match known APIs.
156
125
  class UnknownApiError < StandardError
157
- attr_reader :key
158
-
159
126
  def initialize(key)
160
- super()
161
-
162
- @key = key
163
- end
164
-
165
- def message
166
- "Unknown transport API '#{key}'!"
127
+ super("Unknown transport API '#{key}'!")
167
128
  end
168
129
  end
169
130
 
170
131
  # Raised when the identifier cannot be matched to an adapter.
171
132
  class UnknownAdapterError < StandardError
172
- attr_reader :type
173
-
174
133
  def initialize(type)
175
- super()
176
-
177
- @type = type
178
- end
179
-
180
- def message
181
- "Unknown transport adapter '#{type}'!"
134
+ super("Unknown transport adapter '#{type}'!")
182
135
  end
183
136
  end
184
137
 
185
138
  # Raised when an adapter cannot be resolved for an API instance.
186
139
  class NoAdapterForApiError < StandardError
187
- attr_reader :key
188
-
189
140
  def initialize(key)
190
- super()
191
-
192
- @key = key
193
- end
194
-
195
- def message
196
- "No adapter resolved for transport API '#{key}'!"
141
+ super("No adapter resolved for transport API '#{key}'!")
197
142
  end
198
143
  end
199
144
 
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'statistics'
4
+ require_relative '../../../core/transport/http/env'
5
+ require_relative '../../../core/transport/http/response'
6
+
7
+ module Datadog
8
+ module Tracing
9
+ module Transport
10
+ module HTTP
11
+ # Routes, encodes, and sends tracer data to the trace agent via HTTP.
12
+ class Client
13
+ include Datadog::Tracing::Transport::HTTP::Statistics
14
+
15
+ attr_reader :api
16
+
17
+ def initialize(api)
18
+ @api = api
19
+ end
20
+
21
+ def send_request(request, &block)
22
+ # Build request into env
23
+ env = build_env(request)
24
+
25
+ # Get responses from API
26
+ response = yield(api, env)
27
+
28
+ # Update statistics
29
+ update_stats_from_response!(response)
30
+
31
+ response
32
+ rescue StandardError => e
33
+ message =
34
+ "Internal error during #{self.class.name} request. Cause: #{e.class.name} #{e.message} " \
35
+ "Location: #{Array(e.backtrace).first}"
36
+
37
+ # Log error
38
+ if stats.consecutive_errors > 0
39
+ Datadog.logger.debug(message)
40
+ else
41
+ Datadog.logger.error(message)
42
+ end
43
+
44
+ # Update statistics
45
+ update_stats_from_exception!(e)
46
+
47
+ Datadog::Core::Transport::InternalErrorResponse.new(e)
48
+ end
49
+
50
+ def build_env(request)
51
+ Datadog::Core::Transport::HTTP::Env.new(request)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../statistics'
4
+
5
+ module Datadog
6
+ module Tracing
7
+ module Transport
8
+ module HTTP
9
+ # Tracks statistics for HTTP transports
10
+ module Statistics
11
+ def self.included(base)
12
+ base.include(Datadog::Tracing::Transport::Statistics)
13
+ base.include(InstanceMethods)
14
+ end
15
+
16
+ # Instance methods for HTTP statistics
17
+ module InstanceMethods
18
+ # Decorate metrics for HTTP responses
19
+ def metrics_for_response(response)
20
+ super.tap do |metrics|
21
+ # Add status code tag to api.responses metric
22
+ if metrics.key?(:api_responses)
23
+ (metrics[:api_responses].options[:tags] ||= []).tap do |tags|
24
+ tags << metrics_tag_value(response.code)
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ # The most common status code on a healthy tracer
33
+ STATUS_CODE_200 = 'status_code:200'
34
+
35
+ def metrics_tag_value(status_code)
36
+ if status_code == 200
37
+ STATUS_CODE_200 # DEV Saves string concatenation/creation for common case
38
+ else
39
+ "status_code:#{status_code}"
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,152 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ require_relative '../traces'
6
+ require_relative 'client'
7
+ require_relative '../../../core/transport/http/response'
8
+ require_relative '../../../core/transport/http/api/endpoint'
9
+ require_relative 'api/instance'
10
+
11
+ module Datadog
12
+ module Tracing
13
+ module Transport
14
+ module HTTP
15
+ # HTTP transport behavior for traces
16
+ module Traces
17
+ # Response from HTTP transport for traces
18
+ class Response
19
+ include Datadog::Core::Transport::HTTP::Response
20
+ include Datadog::Tracing::Transport::Traces::Response
21
+
22
+ def initialize(http_response, options = {})
23
+ super(http_response)
24
+ @service_rates = options.fetch(:service_rates, nil)
25
+ @trace_count = options.fetch(:trace_count, 0)
26
+ end
27
+ end
28
+
29
+ # Extensions for HTTP client
30
+ module Client
31
+ def send_traces_payload(request)
32
+ send_request(request) do |api, env|
33
+ api.send_traces(env)
34
+ end
35
+ end
36
+ end
37
+
38
+ module API
39
+ # Extensions for HTTP API Spec
40
+ module Spec
41
+ attr_reader :traces
42
+
43
+ def traces=(endpoint)
44
+ @traces = endpoint
45
+ end
46
+
47
+ def send_traces(env, &block)
48
+ raise NoTraceEndpointDefinedError, self if traces.nil?
49
+
50
+ traces.call(env, &block)
51
+ end
52
+
53
+ def encoder
54
+ traces.encoder
55
+ end
56
+
57
+ # Raised when traces sent but no traces endpoint is defined
58
+ class NoTraceEndpointDefinedError < StandardError
59
+ attr_reader :spec
60
+
61
+ def initialize(spec)
62
+ super
63
+
64
+ @spec = spec
65
+ end
66
+
67
+ def message
68
+ 'No trace endpoint is defined for API specification!'
69
+ end
70
+ end
71
+ end
72
+
73
+ # Extensions for HTTP API Instance
74
+ module Instance
75
+ def send_traces(env)
76
+ raise TracesNotSupportedError, spec unless spec.is_a?(Traces::API::Spec)
77
+
78
+ spec.send_traces(env) do |request_env|
79
+ call(request_env)
80
+ end
81
+ end
82
+
83
+ # Raised when traces sent to API that does not support traces
84
+ class TracesNotSupportedError < StandardError
85
+ attr_reader :spec
86
+
87
+ def initialize(spec)
88
+ super
89
+
90
+ @spec = spec
91
+ end
92
+
93
+ def message
94
+ 'Traces not supported for this API!'
95
+ end
96
+ end
97
+ end
98
+
99
+ # Endpoint for submitting trace data
100
+ class Endpoint < Datadog::Core::Transport::HTTP::API::Endpoint
101
+ HEADER_CONTENT_TYPE = 'Content-Type'
102
+ HEADER_TRACE_COUNT = 'X-Datadog-Trace-Count'
103
+ SERVICE_RATE_KEY = 'rate_by_service'
104
+
105
+ attr_reader \
106
+ :encoder
107
+
108
+ def initialize(path, encoder, options = {})
109
+ super(:post, path)
110
+ @encoder = encoder
111
+ @service_rates = options.fetch(:service_rates, false)
112
+ end
113
+
114
+ def service_rates?
115
+ @service_rates == true
116
+ end
117
+
118
+ def call(env, &block)
119
+ # Add trace count header
120
+ env.headers[HEADER_TRACE_COUNT] = env.request.parcel.trace_count.to_s
121
+
122
+ # Encode body & type
123
+ env.headers[HEADER_CONTENT_TYPE] = encoder.content_type
124
+ env.body = env.request.parcel.data
125
+
126
+ # Query for response
127
+ http_response = super(env, &block)
128
+
129
+ # Process the response
130
+ response_options = { trace_count: env.request.parcel.trace_count }.tap do |options|
131
+ # Parse service rates, if configured to do so.
132
+ if service_rates? && !http_response.payload.to_s.empty?
133
+ body = JSON.parse(http_response.payload)
134
+ options[:service_rates] = body[SERVICE_RATE_KEY] if body.is_a?(Hash) && body.key?(SERVICE_RATE_KEY)
135
+ end
136
+ end
137
+
138
+ # Build and return a trace response
139
+ Traces::Response.new(http_response, response_options)
140
+ end
141
+ end
142
+ end
143
+
144
+ # Add traces behavior to transport components
145
+ HTTP::Client.include(Traces::Client)
146
+ HTTP::API::Spec.include(Traces::API::Spec)
147
+ HTTP::API::Instance.include(Traces::API::Instance)
148
+ end
149
+ end
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,124 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uri'
4
+
5
+ require_relative '../../core/environment/container'
6
+ require_relative '../../core/environment/ext'
7
+ require_relative '../../core/transport/ext'
8
+ require_relative '../../core/transport/http/adapters/net'
9
+ require_relative '../../core/transport/http/adapters/test'
10
+ require_relative '../../core/transport/http/adapters/unix_socket'
11
+ require_relative 'http/api'
12
+ require_relative 'http/builder'
13
+ require_relative '../../../ddtrace/version'
14
+
15
+ module Datadog
16
+ module Tracing
17
+ module Transport
18
+ # Namespace for HTTP transport components
19
+ module HTTP
20
+ # NOTE: Due to... legacy reasons... This class likes having a default `AgentSettings` instance to fall back to.
21
+ # Because we generate this instance with an empty instance of `Settings`, the resulting `AgentSettings` below
22
+ # represents only settings specified via environment variables + the usual defaults.
23
+ #
24
+ # DO NOT USE THIS IN NEW CODE, as it ignores any settings specified by users via `Datadog.configure`.
25
+ DO_NOT_USE_ENVIRONMENT_AGENT_SETTINGS = Datadog::Core::Configuration::AgentSettingsResolver.call(
26
+ Datadog::Core::Configuration::Settings.new,
27
+ logger: nil,
28
+ )
29
+
30
+ module_function
31
+
32
+ # Builds a new Transport::HTTP::Client
33
+ def new(&block)
34
+ Builder.new(&block).to_transport
35
+ end
36
+
37
+ # Builds a new Transport::HTTP::Client with default settings
38
+ # Pass a block to override any settings.
39
+ def default(
40
+ agent_settings: DO_NOT_USE_ENVIRONMENT_AGENT_SETTINGS,
41
+ **options
42
+ )
43
+ new do |transport|
44
+ transport.adapter(agent_settings)
45
+ transport.headers default_headers
46
+
47
+ apis = API.defaults
48
+
49
+ transport.api API::V4, apis[API::V4], fallback: API::V3, default: true
50
+ transport.api API::V3, apis[API::V3]
51
+
52
+ # Apply any settings given by options
53
+ unless options.empty?
54
+ transport.default_api = options[:api_version] if options.key?(:api_version)
55
+ transport.headers options[:headers] if options.key?(:headers)
56
+ end
57
+
58
+ if agent_settings.deprecated_for_removal_transport_configuration_proc
59
+ agent_settings.deprecated_for_removal_transport_configuration_proc.call(transport)
60
+ end
61
+
62
+ # Call block to apply any customization, if provided
63
+ yield(transport) if block_given?
64
+ end
65
+ end
66
+
67
+ def default_headers
68
+ {
69
+ Datadog::Core::Transport::Ext::HTTP::HEADER_CLIENT_COMPUTED_TOP_LEVEL => '1',
70
+ Datadog::Core::Transport::Ext::HTTP::HEADER_META_LANG => Datadog::Core::Environment::Ext::LANG,
71
+ Datadog::Core::Transport::Ext::HTTP::HEADER_META_LANG_VERSION => Datadog::Core::Environment::Ext::LANG_VERSION,
72
+ Datadog::Core::Transport::Ext::HTTP::HEADER_META_LANG_INTERPRETER =>
73
+ Datadog::Core::Environment::Ext::LANG_INTERPRETER,
74
+ Datadog::Core::Transport::Ext::HTTP::HEADER_META_TRACER_VERSION =>
75
+ Datadog::Core::Environment::Ext::TRACER_VERSION
76
+ }.tap do |headers|
77
+ # Add container ID, if present.
78
+ container_id = Datadog::Core::Environment::Container.container_id
79
+ headers[Datadog::Core::Transport::Ext::HTTP::HEADER_CONTAINER_ID] = container_id unless container_id.nil?
80
+ end
81
+ end
82
+
83
+ def default_adapter
84
+ Datadog::Core::Transport::Ext::HTTP::ADAPTER
85
+ end
86
+
87
+ def default_hostname(logger: Datadog.logger)
88
+ logger.warn(
89
+ 'Deprecated for removal: Using #default_hostname for configuration is deprecated and will ' \
90
+ 'be removed on a future ddtrace release.'
91
+ )
92
+
93
+ DO_NOT_USE_ENVIRONMENT_AGENT_SETTINGS.hostname
94
+ end
95
+
96
+ def default_port(logger: Datadog.logger)
97
+ logger.warn(
98
+ 'Deprecated for removal: Using #default_hostname for configuration is deprecated and will ' \
99
+ 'be removed on a future ddtrace release.'
100
+ )
101
+
102
+ DO_NOT_USE_ENVIRONMENT_AGENT_SETTINGS.port
103
+ end
104
+
105
+ def default_url(logger: Datadog.logger)
106
+ logger.warn(
107
+ 'Deprecated for removal: Using #default_url for configuration is deprecated and will ' \
108
+ 'be removed on a future ddtrace release.'
109
+ )
110
+
111
+ nil
112
+ end
113
+
114
+ # Add adapters to registry
115
+ Builder::REGISTRY.set(Datadog::Core::Transport::HTTP::Adapters::Net, Datadog::Core::Transport::Ext::HTTP::ADAPTER)
116
+ Builder::REGISTRY.set(Datadog::Core::Transport::HTTP::Adapters::Test, Datadog::Core::Transport::Ext::Test::ADAPTER)
117
+ Builder::REGISTRY.set(
118
+ Datadog::Core::Transport::HTTP::Adapters::UnixSocket,
119
+ Datadog::Core::Transport::Ext::UnixSocket::ADAPTER
120
+ )
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../statistics'
4
+ require_relative 'response'
5
+
6
+ module Datadog
7
+ module Tracing
8
+ module Transport
9
+ module IO
10
+ # Encodes and writes tracer data to IO
11
+ class Client
12
+ include Transport::Statistics
13
+
14
+ attr_reader \
15
+ :encoder,
16
+ :out
17
+
18
+ def initialize(out, encoder, options = {})
19
+ @out = out
20
+ @encoder = encoder
21
+
22
+ @request_block = options.fetch(:request, method(:send_default_request))
23
+ @encode_block = options.fetch(:encode, method(:encode_data))
24
+ @write_block = options.fetch(:write, method(:write_data))
25
+ @response_block = options.fetch(:response, method(:build_response))
26
+ end
27
+
28
+ def send_request(request)
29
+ # Write data to IO
30
+ # If block is given, allow it to handle writing
31
+ # Otherwise do a standard encode/write/response.
32
+ response = if block_given?
33
+ yield(out, request)
34
+ else
35
+ @request_block.call(out, request)
36
+ end
37
+
38
+ # Update statistics
39
+ update_stats_from_response!(response)
40
+
41
+ # Return response
42
+ response
43
+ rescue StandardError => e
44
+ message =
45
+ "Internal error during IO transport request. Cause: #{e.class.name} #{e.message} " \
46
+ "Location: #{Array(e.backtrace).first}"
47
+
48
+ # Log error
49
+ if stats.consecutive_errors > 0
50
+ Datadog.logger.debug(message)
51
+ else
52
+ Datadog.logger.error(message)
53
+ end
54
+
55
+ # Update statistics
56
+ update_stats_from_exception!(e)
57
+
58
+ InternalErrorResponse.new(e)
59
+ end
60
+
61
+ def encode_data(encoder, request)
62
+ request.parcel.encode_with(encoder)
63
+ end
64
+
65
+ def write_data(out, data)
66
+ out.puts(data)
67
+ end
68
+
69
+ def build_response(_request, _data, result)
70
+ IO::Response.new(result)
71
+ end
72
+
73
+ private
74
+
75
+ def send_default_request(out, request)
76
+ # Encode data
77
+ data = @encode_block.call(encoder, request)
78
+
79
+ # Write to IO
80
+ result = @write_block.call(out, data)
81
+
82
+ # Generate a response
83
+ @response_block.call(request, data, result)
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../../core/transport/response'
4
+
5
+ module Datadog
6
+ module Tracing
7
+ module Transport
8
+ module IO
9
+ # Response from HTTP transport for traces
10
+ class Response
11
+ include Datadog::Core::Transport::Response
12
+
13
+ attr_reader \
14
+ :result
15
+
16
+ def initialize(result)
17
+ @result = result
18
+ end
19
+
20
+ def ok?
21
+ true
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end