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,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../transport/traces'
4
+ require_relative '../../../core/transport/parcel'
5
+ require_relative 'response'
6
+ require_relative 'client'
7
+
8
+ module Datadog
9
+ module Tracing
10
+ module Transport
11
+ module IO
12
+ # IO transport behavior for traces
13
+ module Traces
14
+ # Response from HTTP transport for traces
15
+ class Response < IO::Response
16
+ include Transport::Traces::Response
17
+
18
+ def initialize(result, trace_count = 1)
19
+ super(result)
20
+ @trace_count = trace_count
21
+ end
22
+ end
23
+
24
+ # Extensions for HTTP client
25
+ module Client
26
+ def send_traces(traces)
27
+ # Build a request
28
+ req = Transport::Traces::Request.new(Parcel.new(traces))
29
+
30
+ [send_request(req) do |out, request|
31
+ # Encode trace data
32
+ data = encode_data(encoder, request)
33
+
34
+ # Write to IO
35
+ result = if block_given?
36
+ yield(out, data)
37
+ else
38
+ write_data(out, data)
39
+ end
40
+
41
+ # Generate response
42
+ Traces::Response.new(result)
43
+ end]
44
+ end
45
+ end
46
+
47
+ # Encoder for IO-specific trace encoding
48
+ # API compliant when used with {JSONEncoder}.
49
+ module Encoder
50
+ ENCODED_IDS = [
51
+ :trace_id,
52
+ :span_id,
53
+ :parent_id
54
+ ].freeze
55
+
56
+ # Encodes a list of traces
57
+ def encode_traces(encoder, traces)
58
+ trace_hashes = traces.map do |trace|
59
+ encode_trace(trace)
60
+ end
61
+
62
+ # Wrap traces & encode them
63
+ encoder.encode(traces: trace_hashes)
64
+ end
65
+
66
+ private
67
+
68
+ def encode_trace(trace)
69
+ # Convert each trace to hash
70
+ trace.spans.map(&:to_hash).tap do |spans|
71
+ # Convert IDs to hexadecimal
72
+ spans.each do |span|
73
+ ENCODED_IDS.each do |id|
74
+ span[id] = span[id].to_s(16) if span.key?(id)
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ # Transfer object for list of traces
82
+ class Parcel
83
+ include Datadog::Core::Transport::Parcel
84
+ include Encoder
85
+
86
+ def count
87
+ data.length
88
+ end
89
+
90
+ def encode_with(encoder)
91
+ encode_traces(encoder, data)
92
+ end
93
+ end
94
+
95
+ # Add traces behavior to transport components
96
+ IO::Client.include(Traces::Client)
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../core/encoding'
4
+ require_relative 'io/client'
5
+ require_relative 'io/traces'
6
+
7
+ module Datadog
8
+ module Tracing
9
+ module Transport
10
+ # Namespace for IO transport components
11
+ module IO
12
+ module_function
13
+
14
+ # Builds a new Transport::IO::Client
15
+ def new(out, encoder)
16
+ Client.new(out, encoder)
17
+ end
18
+
19
+ # Builds a new Transport::IO::Client with default settings
20
+ # Pass options to override any settings.
21
+ def default(options = {})
22
+ new(
23
+ options.fetch(:out, $stdout),
24
+ options.fetch(:encoder, Core::Encoding::JSONEncoder)
25
+ )
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,126 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'msgpack'
5
+ require 'datadog/tracing/utils'
6
+
7
+ module Datadog
8
+ module Tracing
9
+ module Transport
10
+ # Adds serialization functions to a {Datadog::TraceSegment}
11
+ class SerializableTrace
12
+ attr_reader \
13
+ :trace
14
+
15
+ def initialize(trace)
16
+ @trace = trace
17
+ end
18
+
19
+ # MessagePack serializer interface. Making this object
20
+ # respond to `#to_msgpack` allows it to be automatically
21
+ # serialized by MessagePack.
22
+ #
23
+ # This is more efficient than doing +MessagePack.pack(span.to_hash)+
24
+ # as we don't have to create an intermediate Hash.
25
+ #
26
+ # @param packer [MessagePack::Packer] serialization buffer, can be +nil+ with JRuby
27
+ def to_msgpack(packer = nil)
28
+ # As of 1.3.3, JRuby implementation doesn't pass an existing packer
29
+ trace.spans.map { |s| SerializableSpan.new(s) }.to_msgpack(packer)
30
+ end
31
+
32
+ # JSON serializer interface.
33
+ # Used by older version of the transport.
34
+ def to_json(*args)
35
+ trace.spans.map { |s| SerializableSpan.new(s).to_hash }.to_json(*args)
36
+ end
37
+ end
38
+
39
+ # Adds serialization functions to a {Datadog::Span}
40
+ class SerializableSpan
41
+ attr_reader \
42
+ :span
43
+
44
+ def initialize(span)
45
+ @span = span
46
+ @trace_id = Tracing::Utils::TraceId.to_low_order(span.trace_id)
47
+ end
48
+
49
+ # MessagePack serializer interface. Making this object
50
+ # respond to `#to_msgpack` allows it to be automatically
51
+ # serialized by MessagePack.
52
+ #
53
+ # This is more efficient than doing +MessagePack.pack(span.to_hash)+
54
+ # as we don't have to create an intermediate Hash.
55
+ #
56
+ # @param packer [MessagePack::Packer] serialization buffer, can be +nil+ with JRuby
57
+ # rubocop:disable Metrics/AbcSize
58
+ def to_msgpack(packer = nil)
59
+ packer ||= MessagePack::Packer.new
60
+
61
+ number_of_elements_to_write = 10
62
+
63
+ if span.stopped?
64
+ packer.write_map_header(number_of_elements_to_write + 2) # Set header with how many elements in the map
65
+
66
+ packer.write('start')
67
+ packer.write(time_nano(span.start_time))
68
+
69
+ packer.write('duration')
70
+ packer.write(duration_nano(span.duration))
71
+ else
72
+ packer.write_map_header(number_of_elements_to_write) # Set header with how many elements in the map
73
+ end
74
+
75
+ # DEV: We use strings as keys here, instead of symbols, as
76
+ # DEV: MessagePack will ultimately convert them to strings.
77
+ # DEV: By providing strings directly, we skip this indirection operation.
78
+ packer.write('span_id')
79
+ packer.write(span.id)
80
+ packer.write('parent_id')
81
+ packer.write(span.parent_id)
82
+ packer.write('trace_id')
83
+ packer.write(@trace_id)
84
+ packer.write('name')
85
+ packer.write(span.name)
86
+ packer.write('service')
87
+ packer.write(span.service)
88
+ packer.write('resource')
89
+ packer.write(span.resource)
90
+ packer.write('type')
91
+ packer.write(span.type)
92
+ packer.write('meta')
93
+ packer.write(span.meta)
94
+ packer.write('metrics')
95
+ packer.write(span.metrics)
96
+ packer.write('error')
97
+ packer.write(span.status)
98
+ packer
99
+ end
100
+ # rubocop:enable Metrics/AbcSize
101
+
102
+ # JSON serializer interface.
103
+ # Used by older version of the transport.
104
+ def to_json(*args)
105
+ to_hash.to_json(*args)
106
+ end
107
+
108
+ # Used for serialization
109
+ # @return [Integer] in nanoseconds since Epoch
110
+ def time_nano(time)
111
+ time.to_i * 1000000000 + time.nsec
112
+ end
113
+
114
+ def to_hash
115
+ span.to_hash.merge(trace_id: @trace_id)
116
+ end
117
+
118
+ # Used for serialization
119
+ # @return [Integer] in nanoseconds since Epoch
120
+ def duration_nano(duration)
121
+ (duration * 1e9).to_i
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../core/metrics/metric'
4
+ require_relative '../../core/diagnostics/health'
5
+
6
+ module Datadog
7
+ module Tracing
8
+ module Transport
9
+ # Tracks statistics for transports
10
+ module Statistics
11
+ def stats
12
+ @stats ||= Counts.new
13
+ end
14
+
15
+ def update_stats_from_response!(response)
16
+ if response.ok?
17
+ stats.success += 1
18
+ stats.consecutive_errors = 0
19
+ else
20
+ stats.client_error += 1 if response.client_error?
21
+ stats.server_error += 1 if response.server_error?
22
+ stats.internal_error += 1 if response.internal_error?
23
+ stats.consecutive_errors += 1
24
+ end
25
+
26
+ # Send health metrics
27
+ Datadog.health_metrics.send_metrics(
28
+ metrics_for_response(response).values
29
+ )
30
+ end
31
+
32
+ def metrics_for_response(response)
33
+ {}.tap do |metrics|
34
+ metrics[:api_errors] = Core::Metrics::Metric.new(:api_errors, nil, 1) if response.internal_error?
35
+ metrics[:api_responses] = Core::Metrics::Metric.new(:api_responses, nil, 1) unless response.internal_error?
36
+ end
37
+ end
38
+
39
+ def update_stats_from_exception!(exception)
40
+ stats.internal_error += 1
41
+ stats.consecutive_errors += 1
42
+
43
+ # Send health metrics
44
+ Datadog.health_metrics.send_metrics(
45
+ metrics_for_exception(exception).values
46
+ )
47
+ end
48
+
49
+ def metrics_for_exception(_exception)
50
+ { api_errors: Core::Metrics::Metric.new(:api_errors, nil, 1) }
51
+ end
52
+
53
+ # Stat counts
54
+ class Counts
55
+ attr_accessor \
56
+ :success,
57
+ :client_error,
58
+ :server_error,
59
+ :internal_error,
60
+ :consecutive_errors
61
+
62
+ def initialize
63
+ reset!
64
+ end
65
+
66
+ def reset!
67
+ @success = 0
68
+ @client_error = 0
69
+ @server_error = 0
70
+ @internal_error = 0
71
+ @consecutive_errors = 0
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,209 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../core/environment/identity'
4
+ require_relative '../../core/environment/socket'
5
+ require_relative '../../core/runtime/ext'
6
+ require_relative '../metadata/ext'
7
+ require_relative '../trace_segment'
8
+
9
+ module Datadog
10
+ module Tracing
11
+ module Transport
12
+ # Prepares traces for transport
13
+ class TraceFormatter
14
+ attr_reader \
15
+ :root_span,
16
+ :trace
17
+
18
+ def self.format!(trace)
19
+ new(trace).format!
20
+ end
21
+
22
+ def initialize(trace)
23
+ @trace = trace
24
+ @root_span = find_root_span(trace)
25
+ end
26
+
27
+ # Modifies a trace so suitable for transport
28
+ def format!
29
+ return unless trace
30
+ return trace unless root_span
31
+
32
+ # Because the trace API does not support
33
+ # trace metadata, we must put our trace
34
+ # metadata on the root span. This "root span"
35
+ # is needed by the agent/API to ingest the trace.
36
+
37
+ # Apply generic trace tags. Any more specific value will be overridden
38
+ # by the subsequent calls below.
39
+ set_trace_tags!
40
+
41
+ set_resource!
42
+
43
+ tag_agent_sample_rate!
44
+ tag_hostname!
45
+ tag_lang!
46
+ tag_origin!
47
+ tag_process_id!
48
+ tag_rule_sample_rate!
49
+ tag_runtime_id!
50
+ tag_rate_limiter_rate!
51
+ tag_sample_rate!
52
+ tag_sampling_decision_maker!
53
+ tag_high_order_trace_id!
54
+ tag_sampling_priority!
55
+ tag_profiling_enabled!
56
+
57
+ trace
58
+ end
59
+
60
+ protected
61
+
62
+ def set_resource!
63
+ # If the trace resource is undefined, or the root span wasn't
64
+ # specified, don't set this. We don't want to overwrite the
65
+ # resource of a span that is in the middle of the trace.
66
+ return if trace.resource.nil? || partial?
67
+
68
+ root_span.resource = trace.resource
69
+ end
70
+
71
+ def set_trace_tags!
72
+ # If the root span wasn't specified, don't set this. We don't want to
73
+ # misset or overwrite the tags of a span that is in the middle of the
74
+ # trace.
75
+ return if partial?
76
+
77
+ root_span.set_tags(trace.send(:meta))
78
+ root_span.set_tags(trace.send(:metrics))
79
+ end
80
+
81
+ def tag_agent_sample_rate!
82
+ return unless trace.agent_sample_rate
83
+
84
+ root_span.set_tag(
85
+ Tracing::Metadata::Ext::Sampling::TAG_AGENT_RATE,
86
+ trace.agent_sample_rate
87
+ )
88
+ end
89
+
90
+ def tag_hostname!
91
+ return unless trace.hostname
92
+
93
+ root_span.set_tag(
94
+ Tracing::Metadata::Ext::NET::TAG_HOSTNAME,
95
+ trace.hostname
96
+ )
97
+ end
98
+
99
+ def tag_lang!
100
+ return if trace.lang.nil?
101
+
102
+ root_span.set_tag(
103
+ Core::Runtime::Ext::TAG_LANG,
104
+ trace.lang
105
+ )
106
+ end
107
+
108
+ def tag_origin!
109
+ return unless trace.origin
110
+
111
+ root_span.set_tag(
112
+ Tracing::Metadata::Ext::Distributed::TAG_ORIGIN,
113
+ trace.origin
114
+ )
115
+ end
116
+
117
+ def tag_process_id!
118
+ return unless trace.process_id
119
+
120
+ root_span.set_tag(Core::Runtime::Ext::TAG_PROCESS_ID, trace.process_id)
121
+ end
122
+
123
+ def tag_rate_limiter_rate!
124
+ return unless trace.rate_limiter_rate
125
+
126
+ root_span.set_tag(
127
+ Tracing::Metadata::Ext::Sampling::TAG_RATE_LIMITER_RATE,
128
+ trace.rate_limiter_rate
129
+ )
130
+ end
131
+
132
+ def tag_rule_sample_rate!
133
+ return unless trace.rule_sample_rate
134
+
135
+ root_span.set_tag(
136
+ Tracing::Metadata::Ext::Sampling::TAG_RULE_SAMPLE_RATE,
137
+ trace.rule_sample_rate
138
+ )
139
+ end
140
+
141
+ def tag_runtime_id!
142
+ return unless trace.runtime_id
143
+
144
+ root_span.set_tag(
145
+ Core::Runtime::Ext::TAG_ID,
146
+ trace.runtime_id
147
+ )
148
+ end
149
+
150
+ def tag_sample_rate!
151
+ return unless trace.sample_rate
152
+
153
+ root_span.set_tag(
154
+ Tracing::Metadata::Ext::Sampling::TAG_SAMPLE_RATE,
155
+ trace.sample_rate
156
+ )
157
+ end
158
+
159
+ def tag_sampling_decision_maker!
160
+ return unless (decision = trace.sampling_decision_maker)
161
+
162
+ root_span.set_tag(Tracing::Metadata::Ext::Distributed::TAG_DECISION_MAKER, decision)
163
+ end
164
+
165
+ def tag_sampling_priority!
166
+ return unless trace.sampling_priority
167
+
168
+ root_span.set_metric(
169
+ Tracing::Metadata::Ext::Distributed::TAG_SAMPLING_PRIORITY,
170
+ trace.sampling_priority
171
+ )
172
+ end
173
+
174
+ def tag_high_order_trace_id!
175
+ return unless (high_order_tid = trace.high_order_tid)
176
+
177
+ root_span.set_tag(Tracing::Metadata::Ext::Distributed::TAG_TID, high_order_tid)
178
+ end
179
+
180
+ def tag_profiling_enabled!
181
+ return if trace.profiling_enabled.nil?
182
+
183
+ root_span.set_tag(
184
+ Tracing::Metadata::Ext::TAG_PROFILING_ENABLED, trace.profiling_enabled ? 1 : 0
185
+ )
186
+ end
187
+
188
+ private
189
+
190
+ def partial?
191
+ !@found_root_span
192
+ end
193
+
194
+ def find_root_span(trace)
195
+ # TODO: Should we memoize this?
196
+ # Would be safe, but `spans` is mutable, so if
197
+ # the root span were removed, it would be a stale reference.
198
+ # Figure out a better way to deal with this.
199
+ root_span_id = trace.send(:root_span_id)
200
+ root_span = trace.spans.find { |s| s.id == root_span_id } if root_span_id
201
+ @found_root_span = !root_span.nil?
202
+
203
+ # when root span is not found, fall back to last span (partial flush)
204
+ root_span || trace.spans.last
205
+ end
206
+ end
207
+ end
208
+ end
209
+ end