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
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../../../core/transport/response'
4
+
5
+ module Datadog
6
+ module Core
7
+ module Transport
8
+ module HTTP
9
+ module Adapters
10
+ # Adapter for testing
11
+ class Test
12
+ attr_reader \
13
+ :buffer,
14
+ :status
15
+
16
+ # @param buffer [Array] an optional array that will capture all spans sent to this adapter, defaults to +nil+
17
+ # @deprecated Positional parameters are deprecated. Use named parameters instead.
18
+ def initialize(buffer = nil, **options)
19
+ @buffer = buffer || options[:buffer]
20
+ @mutex = Mutex.new
21
+ @status = 200
22
+ end
23
+
24
+ def call(env)
25
+ add_request(env)
26
+ Response.new(status)
27
+ end
28
+
29
+ def buffer?
30
+ !@buffer.nil?
31
+ end
32
+
33
+ def add_request(env)
34
+ @mutex.synchronize { buffer << env } if buffer?
35
+ end
36
+
37
+ def set_status!(status)
38
+ @status = status
39
+ end
40
+
41
+ def url; end
42
+
43
+ # Response for test adapter
44
+ class Response
45
+ include Datadog::Core::Transport::Response
46
+
47
+ attr_reader \
48
+ :body,
49
+ :code
50
+
51
+ def initialize(code, body = nil)
52
+ @code = code
53
+ @body = body
54
+ end
55
+
56
+ def payload
57
+ @body
58
+ end
59
+
60
+ def ok?
61
+ code.between?(200, 299)
62
+ end
63
+
64
+ def unsupported?
65
+ code == 415
66
+ end
67
+
68
+ def not_found?
69
+ code == 404
70
+ end
71
+
72
+ def client_error?
73
+ code.between?(400, 499)
74
+ end
75
+
76
+ def server_error?
77
+ code.between?(500, 599)
78
+ end
79
+
80
+ def inspect
81
+ "#{super}, code:#{code}"
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'net/http'
4
+ require_relative '../../../transport/ext'
5
+ require_relative 'net'
6
+
7
+ module Datadog
8
+ module Core
9
+ module Transport
10
+ module HTTP
11
+ module Adapters
12
+ # Adapter for Unix sockets
13
+ class UnixSocket < Adapters::Net
14
+ attr_reader \
15
+ :filepath, # DEV(1.0): Rename to `uds_path`
16
+ :timeout
17
+
18
+ alias_method :uds_path, :filepath
19
+
20
+ # @deprecated Positional parameters are deprecated. Use named parameters instead.
21
+ # rubocop:disable Lint/MissingSuper
22
+ def initialize(uds_path = nil, **options)
23
+ @filepath = uds_path || options.fetch(:uds_path)
24
+ @timeout = options[:timeout] || Datadog::Core::Transport::Ext::UnixSocket::DEFAULT_TIMEOUT_SECONDS
25
+ end
26
+ # rubocop:enable Lint/MissingSuper
27
+
28
+ def self.build(agent_settings)
29
+ new(
30
+ uds_path: agent_settings.uds_path,
31
+ timeout: agent_settings.timeout_seconds,
32
+ )
33
+ end
34
+
35
+ def open(&block)
36
+ # Open connection
37
+ connection = HTTP.new(
38
+ uds_path,
39
+ read_timeout: timeout,
40
+ continue_timeout: timeout
41
+ )
42
+
43
+ connection.start(&block)
44
+ end
45
+
46
+ def url
47
+ "http+unix://#{uds_path}?timeout=#{timeout}"
48
+ end
49
+
50
+ # Re-implements Net:HTTP with underlying Unix socket
51
+ class HTTP < ::Net::HTTP
52
+ DEFAULT_TIMEOUT = 1
53
+
54
+ attr_reader \
55
+ :filepath, # DEV(1.0): Rename to `uds_path`
56
+ :unix_socket
57
+
58
+ alias_method :uds_path, :filepath
59
+
60
+ def initialize(uds_path, options = {})
61
+ super('localhost', 80)
62
+ @filepath = uds_path
63
+ @read_timeout = options.fetch(:read_timeout, DEFAULT_TIMEOUT)
64
+ @continue_timeout = options.fetch(:continue_timeout, DEFAULT_TIMEOUT)
65
+ @debug_output = options[:debug_output] if options.key?(:debug_output)
66
+ end
67
+
68
+ def connect
69
+ @unix_socket = UNIXSocket.open(uds_path)
70
+ @socket = ::Net::BufferedIO.new(@unix_socket).tap do |socket|
71
+ socket.read_timeout = @read_timeout
72
+ socket.continue_timeout = @continue_timeout
73
+ socket.debug_output = @debug_output
74
+ end
75
+ on_connect
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module Datadog
6
+ module Core
7
+ module Transport
8
+ module HTTP
9
+ module API
10
+ # Endpoint
11
+ class Endpoint
12
+ attr_reader \
13
+ :verb,
14
+ :path
15
+
16
+ def initialize(verb, path)
17
+ @verb = verb
18
+ @path = path
19
+ end
20
+
21
+ def call(env)
22
+ env.verb = verb
23
+ env.path = path
24
+ yield(env)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datadog
4
+ module Core
5
+ module Transport
6
+ module HTTP
7
+ module API
8
+ # Extension for Map with adds fallback versions.
9
+ module Fallbacks
10
+ def fallbacks
11
+ @fallbacks ||= {}
12
+ end
13
+
14
+ def with_fallbacks(fallbacks)
15
+ tap { add_fallbacks!(fallbacks) }
16
+ end
17
+
18
+ def add_fallbacks!(fallbacks)
19
+ self.fallbacks.merge!(fallbacks)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'fallbacks'
4
+
5
+ module Datadog
6
+ module Core
7
+ module Transport
8
+ module HTTP
9
+ module API
10
+ # A mapping of API version => API Routes/Instance
11
+ class Map < Hash
12
+ include Fallbacks
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datadog
4
+ module Core
5
+ module Transport
6
+ module HTTP
7
+ # Data structure for an HTTP request
8
+ class Env < Hash
9
+ attr_reader \
10
+ :request
11
+
12
+ # rubocop:disable Lint/MissingSuper
13
+ def initialize(request, options = nil)
14
+ @request = request
15
+ merge!(options) unless options.nil?
16
+ end
17
+ # rubocop:enable Lint/MissingSuper
18
+
19
+ def verb
20
+ self[:verb]
21
+ end
22
+
23
+ def verb=(value)
24
+ self[:verb] = value
25
+ end
26
+
27
+ def path
28
+ self[:path]
29
+ end
30
+
31
+ def path=(value)
32
+ self[:path] = value
33
+ end
34
+
35
+ def body
36
+ self[:body]
37
+ end
38
+
39
+ def body=(value)
40
+ self[:body] = value
41
+ end
42
+
43
+ def headers
44
+ self[:headers] ||= {}
45
+ end
46
+
47
+ def headers=(value)
48
+ self[:headers] = value
49
+ end
50
+
51
+ def form
52
+ self[:form] ||= {}
53
+ end
54
+
55
+ def form=(value)
56
+ self[:form] = value
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../../core/transport/response'
4
+
5
+ module Datadog
6
+ module Core
7
+ module Transport
8
+ module HTTP
9
+ # Wraps an HTTP response from an adapter.
10
+ #
11
+ # Used by endpoints to wrap responses from adapters with
12
+ # fields or behavior that's specific to that endpoint.
13
+ module Response
14
+ def initialize(http_response)
15
+ @http_response = http_response
16
+ end
17
+
18
+ # (see Datadog::Core::Transport::Response#payload)
19
+ def payload
20
+ @http_response.payload
21
+ end
22
+
23
+ # (see Datadog::Core::Transport::Response#internal_error?)
24
+ def internal_error?
25
+ @http_response.internal_error?
26
+ end
27
+
28
+ # (see Datadog::Core::Transport::Response#unsupported?)
29
+ def unsupported?
30
+ @http_response.unsupported?
31
+ end
32
+
33
+ # (see Datadog::Core::Transport::Response#ok?)
34
+ def ok?
35
+ @http_response.ok?
36
+ end
37
+
38
+ # (see Datadog::Core::Transport::Response#not_found?)
39
+ def not_found?
40
+ @http_response.not_found?
41
+ end
42
+
43
+ # (see Datadog::Core::Transport::Response#client_error?)
44
+ def client_error?
45
+ @http_response.client_error?
46
+ end
47
+
48
+ # (see Datadog::Core::Transport::Response#server_error?)
49
+ def server_error?
50
+ @http_response.server_error?
51
+ end
52
+
53
+ def code
54
+ @http_response.respond_to?(:code) ? @http_response.code : nil
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datadog
4
+ module Core
5
+ module Transport
6
+ # Data transfer object for generic data
7
+ # @abstract
8
+ module Parcel
9
+ attr_reader \
10
+ :data
11
+
12
+ def initialize(data)
13
+ @data = data
14
+ end
15
+
16
+ def encode_with(encoder)
17
+ raise NotImplementedError
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datadog
4
+ module Core
5
+ module Transport
6
+ # Defines request for transport operations
7
+ class Request
8
+ attr_reader \
9
+ :parcel
10
+
11
+ def initialize(parcel = nil)
12
+ @parcel = parcel
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datadog
4
+ module Core
5
+ module Transport
6
+ # Defines abstract response for transport operations
7
+ module Response
8
+ def payload
9
+ nil
10
+ end
11
+
12
+ def ok?
13
+ nil
14
+ end
15
+
16
+ def unsupported?
17
+ nil
18
+ end
19
+
20
+ def not_found?
21
+ nil
22
+ end
23
+
24
+ def client_error?
25
+ nil
26
+ end
27
+
28
+ def server_error?
29
+ nil
30
+ end
31
+
32
+ def internal_error?
33
+ nil
34
+ end
35
+
36
+ def inspect
37
+ "#{self.class} ok?:#{ok?} unsupported?:#{unsupported?}, " \
38
+ "not_found?:#{not_found?}, client_error?:#{client_error?}, " \
39
+ "server_error?:#{server_error?}, internal_error?:#{internal_error?}, " \
40
+ "payload:#{payload}"
41
+ end
42
+ end
43
+
44
+ # A generic error response for internal errors
45
+ class InternalErrorResponse
46
+ include Response
47
+
48
+ attr_reader :error
49
+
50
+ def initialize(error)
51
+ @error = error
52
+ end
53
+
54
+ def internal_error?
55
+ true
56
+ end
57
+
58
+ def inspect
59
+ "#{super}, error_type:#{error.class} error:#{error}"
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -6,7 +6,7 @@ module Datadog
6
6
  module Workers
7
7
  # Adds polling (async looping) behavior to workers
8
8
  module Polling
9
- SHUTDOWN_TIMEOUT = 1
9
+ DEFAULT_SHUTDOWN_TIMEOUT = 1
10
10
 
11
11
  def self.included(base)
12
12
  base.include(Workers::IntervalLoop)
@@ -21,7 +21,7 @@ module Datadog
21
21
  end
22
22
  end
23
23
 
24
- def stop(force_stop = false, timeout = SHUTDOWN_TIMEOUT)
24
+ def stop(force_stop = false, timeout = DEFAULT_SHUTDOWN_TIMEOUT)
25
25
  if running?
26
26
  # Attempt graceful stop and wait
27
27
  stop_loop
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative 'trace/span'
4
4
  require_relative '../../tracing/trace_operation'
5
+ require_relative '../trace'
5
6
 
6
7
  module Datadog
7
8
  module OpenTelemetry
@@ -66,9 +67,15 @@ module Datadog
66
67
  # @param [Object] value Object to be stored under key
67
68
  # @return [Context]
68
69
  def set_values(values)
69
- trace = nil
70
- if (current_span = values[CURRENT_SPAN_KEY])
71
- trace = current_span.datadog_trace
70
+ if (current_span = values[CURRENT_SPAN_KEY]) && current_span.datadog_trace
71
+ # Because `#set_value` returns new context, we have to create
72
+ # a new copy of the active trace to ensure there's no conflict with
73
+ # other users of the active trace.
74
+ # It effectively becomes an internal trace propagation.
75
+ trace = Datadog::OpenTelemetry::Trace.start_trace_copy(
76
+ current_span.datadog_trace,
77
+ parent_span: current_span.datadog_span
78
+ )
72
79
  end
73
80
 
74
81
  existing_values = @trace && @trace.otel_values || {}
@@ -81,8 +81,9 @@ module Datadog
81
81
  private
82
82
 
83
83
  # Converts the {Numeric} Datadog id object to OpenTelemetry's byte array format.
84
+ # This method currently converts an unsigned 64-bit Integer to a binary String.
84
85
  def to_otel_id(dd_id)
85
- Array(dd_id).pack('S')
86
+ Array(dd_id).pack('Q')
86
87
  end
87
88
  end
88
89
  end
@@ -27,7 +27,7 @@ module Datadog
27
27
  #
28
28
  # @param [Span] span the {Span} that just ended.
29
29
  def on_finish(span)
30
- span.datadog_span.finish
30
+ span.datadog_span.finish(ns_to_time(span.end_timestamp))
31
31
  end
32
32
 
33
33
  # Export all ended spans to the configured `Exporter` that have not yet
@@ -81,10 +81,22 @@ module Datadog
81
81
  kind = span.kind || 'internal'
82
82
  tags[Tracing::Metadata::Ext::TAG_KIND] = kind
83
83
 
84
- datadog_span = Tracing.trace(span.name, tags: tags)
84
+ datadog_span = Tracing.trace(
85
+ span.name,
86
+ tags: tags,
87
+ start_time: ns_to_time(span.start_timestamp)
88
+ )
89
+
85
90
  datadog_span.set_error([nil, span.status.description]) unless span.status.ok?
91
+ datadog_span.set_tags(span.attributes)
92
+
86
93
  datadog_span
87
94
  end
95
+
96
+ # From nanoseconds, used by OpenTelemetry, to a {Time} object, used by the Datadog Tracer.
97
+ def ns_to_time(timestamp_ns)
98
+ Time.at(timestamp_ns / 1000000000.0)
99
+ end
88
100
  end
89
101
  end
90
102
  end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datadog
4
+ module OpenTelemetry
5
+ module Trace
6
+ # Stores associated Datadog entities to the OpenTelemetry Span.
7
+ module Span
8
+ # Attributes are equivalent to span tags and metrics.
9
+ def set_attribute(key, value)
10
+ res = super
11
+ # Attributes can get dropped or their values truncated by `super`
12
+ datadog_set_attribute(key)
13
+ res
14
+ end
15
+
16
+ # `alias` performed to match {OpenTelemetry::SDK::Trace::Span} aliasing upstream
17
+ alias []= set_attribute
18
+
19
+ # Attributes are equivalent to span tags and metrics.
20
+ def add_attributes(attributes)
21
+ res = super
22
+ # Attributes can get dropped or their values truncated by `super`
23
+ attributes.each { |key, _| datadog_set_attribute(key) }
24
+ res
25
+ end
26
+
27
+ # Captures changes to span error state.
28
+ def status=(s)
29
+ super
30
+
31
+ return unless status # Return if status are currently disabled by OpenTelemetry.
32
+ return unless (span = datadog_span)
33
+
34
+ # Status code can only change into an error state.
35
+ # Other change operations should be ignored.
36
+ span.set_error(status.description) if status && status.code == ::OpenTelemetry::Trace::Status::ERROR
37
+ end
38
+
39
+ private
40
+
41
+ def datadog_set_attribute(key)
42
+ # Return if attributes are currently disabled by OpenTelemetry.
43
+ return unless defined?(@attributes) && @attributes
44
+ return unless (span = datadog_span)
45
+
46
+ # DEV: Accesses `@attributes` directly, since using `#attributes`
47
+ # DEV: clones the hash, causing unnecessary overhead.
48
+ if @attributes.key?(key)
49
+ value = @attributes[key]
50
+ span.set_tag(key, value)
51
+
52
+ span.service = value if key == 'service.name'
53
+ else
54
+ span.clear_tag(key)
55
+
56
+ if key == 'service.name'
57
+ # By removing the service name, we set it to the fallback default,
58
+ # effectively removing the `service` attribute from OpenTelemetry's perspective.
59
+ span.service = Datadog.send(:components).tracer.default_service
60
+ end
61
+ end
62
+ end
63
+
64
+ ::OpenTelemetry::SDK::Trace::Span.prepend(self)
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../tracing/trace_digest'
4
+
5
+ module Datadog
6
+ module OpenTelemetry
7
+ # OpenTelemetry utilities related to the respective Datadog trace.
8
+ module Trace
9
+ class << self
10
+ # Creates a new TraceOperation object that can be attached to a new
11
+ # OpenTelemetry span.
12
+ # If `parent_span` is provided, then that span is set as the currently
13
+ # active parent span.
14
+ #
15
+ # @return [TraceOperation]
16
+ def start_trace_copy(trace, parent_span: nil)
17
+ digest = if parent_span
18
+ digest_with_parent_span(trace, parent_span)
19
+ else
20
+ trace.to_digest
21
+ end
22
+
23
+ # Create a new TraceOperation, attached to the current Datadog Tracer.
24
+ Datadog::Tracing.continue_trace!(digest)
25
+ end
26
+
27
+ private
28
+
29
+ # Creates a TraceDigest with the active span modified.
30
+ # This supports the implementation of `OpenTelemetry::Trace.context_with_span`,
31
+ # which allows you to specific any span as the arbitrary parent of a new span.
32
+ def digest_with_parent_span(trace, parent_span)
33
+ digest = trace.to_digest
34
+
35
+ Tracing::TraceDigest.new(
36
+ span_id: parent_span.id,
37
+ span_name: parent_span.name,
38
+ span_resource: parent_span.resource,
39
+ span_service: parent_span.service,
40
+ span_type: parent_span.type,
41
+ trace_distributed_tags: digest.trace_distributed_tags,
42
+ trace_hostname: digest.trace_hostname,
43
+ trace_id: digest.trace_id,
44
+ trace_name: digest.trace_name,
45
+ trace_origin: digest.trace_origin,
46
+ trace_process_id: digest.trace_process_id,
47
+ trace_resource: digest.trace_resource,
48
+ trace_runtime_id: digest.trace_runtime_id,
49
+ trace_sampling_priority: digest.trace_sampling_priority,
50
+ trace_service: digest.trace_service,
51
+ trace_state: digest.trace_state,
52
+ trace_state_unknown_fields: digest.trace_state_unknown_fields,
53
+ ).freeze
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end