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,207 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../../datadog/core/environment/identity'
4
- require_relative '../../datadog/core/environment/socket'
5
- require_relative '../../datadog/core/runtime/ext'
6
- require_relative '../../datadog/tracing/metadata/ext'
7
- require_relative '../../datadog/tracing/trace_segment'
8
-
9
- module Datadog
10
- module Transport
11
- # Prepares traces for transport
12
- class TraceFormatter
13
- attr_reader \
14
- :root_span,
15
- :trace
16
-
17
- def self.format!(trace)
18
- new(trace).format!
19
- end
20
-
21
- def initialize(trace)
22
- @trace = trace
23
- @root_span = find_root_span(trace)
24
- end
25
-
26
- # Modifies a trace so suitable for transport
27
- def format!
28
- return unless trace
29
- return trace unless root_span
30
-
31
- # Because the trace API does not support
32
- # trace metadata, we must put our trace
33
- # metadata on the root span. This "root span"
34
- # is needed by the agent/API to ingest the trace.
35
-
36
- # Apply generic trace tags. Any more specific value will be overridden
37
- # by the subsequent calls below.
38
- set_trace_tags!
39
-
40
- set_resource!
41
-
42
- tag_agent_sample_rate!
43
- tag_hostname!
44
- tag_lang!
45
- tag_origin!
46
- tag_process_id!
47
- tag_rule_sample_rate!
48
- tag_runtime_id!
49
- tag_rate_limiter_rate!
50
- tag_sample_rate!
51
- tag_sampling_decision_maker!
52
- tag_high_order_trace_id!
53
- tag_sampling_priority!
54
- tag_profiling_enabled!
55
-
56
- trace
57
- end
58
-
59
- protected
60
-
61
- def set_resource!
62
- # If the trace resource is undefined, or the root span wasn't
63
- # specified, don't set this. We don't want to overwrite the
64
- # resource of a span that is in the middle of the trace.
65
- return if trace.resource.nil? || partial?
66
-
67
- root_span.resource = trace.resource
68
- end
69
-
70
- def set_trace_tags!
71
- # If the root span wasn't specified, don't set this. We don't want to
72
- # misset or overwrite the tags of a span that is in the middle of the
73
- # trace.
74
- return if partial?
75
-
76
- root_span.set_tags(trace.send(:meta))
77
- root_span.set_tags(trace.send(:metrics))
78
- end
79
-
80
- def tag_agent_sample_rate!
81
- return unless trace.agent_sample_rate
82
-
83
- root_span.set_tag(
84
- Tracing::Metadata::Ext::Sampling::TAG_AGENT_RATE,
85
- trace.agent_sample_rate
86
- )
87
- end
88
-
89
- def tag_hostname!
90
- return unless trace.hostname
91
-
92
- root_span.set_tag(
93
- Tracing::Metadata::Ext::NET::TAG_HOSTNAME,
94
- trace.hostname
95
- )
96
- end
97
-
98
- def tag_lang!
99
- return if trace.lang.nil?
100
-
101
- root_span.set_tag(
102
- Core::Runtime::Ext::TAG_LANG,
103
- trace.lang
104
- )
105
- end
106
-
107
- def tag_origin!
108
- return unless trace.origin
109
-
110
- root_span.set_tag(
111
- Tracing::Metadata::Ext::Distributed::TAG_ORIGIN,
112
- trace.origin
113
- )
114
- end
115
-
116
- def tag_process_id!
117
- return unless trace.process_id
118
-
119
- root_span.set_tag(Core::Runtime::Ext::TAG_PROCESS_ID, trace.process_id)
120
- end
121
-
122
- def tag_rate_limiter_rate!
123
- return unless trace.rate_limiter_rate
124
-
125
- root_span.set_tag(
126
- Tracing::Metadata::Ext::Sampling::TAG_RATE_LIMITER_RATE,
127
- trace.rate_limiter_rate
128
- )
129
- end
130
-
131
- def tag_rule_sample_rate!
132
- return unless trace.rule_sample_rate
133
-
134
- root_span.set_tag(
135
- Tracing::Metadata::Ext::Sampling::TAG_RULE_SAMPLE_RATE,
136
- trace.rule_sample_rate
137
- )
138
- end
139
-
140
- def tag_runtime_id!
141
- return unless trace.runtime_id
142
-
143
- root_span.set_tag(
144
- Core::Runtime::Ext::TAG_ID,
145
- trace.runtime_id
146
- )
147
- end
148
-
149
- def tag_sample_rate!
150
- return unless trace.sample_rate
151
-
152
- root_span.set_tag(
153
- Tracing::Metadata::Ext::Sampling::TAG_SAMPLE_RATE,
154
- trace.sample_rate
155
- )
156
- end
157
-
158
- def tag_sampling_decision_maker!
159
- return unless (decision = trace.sampling_decision_maker)
160
-
161
- root_span.set_tag(Tracing::Metadata::Ext::Distributed::TAG_DECISION_MAKER, decision)
162
- end
163
-
164
- def tag_sampling_priority!
165
- return unless trace.sampling_priority
166
-
167
- root_span.set_metric(
168
- Tracing::Metadata::Ext::Distributed::TAG_SAMPLING_PRIORITY,
169
- trace.sampling_priority
170
- )
171
- end
172
-
173
- def tag_high_order_trace_id!
174
- return unless (high_order_tid = trace.high_order_tid)
175
-
176
- root_span.set_tag(Tracing::Metadata::Ext::Distributed::TAG_TID, high_order_tid)
177
- end
178
-
179
- def tag_profiling_enabled!
180
- return if trace.profiling_enabled.nil?
181
-
182
- root_span.set_tag(
183
- Tracing::Metadata::Ext::TAG_PROFILING_ENABLED, trace.profiling_enabled ? 1 : 0
184
- )
185
- end
186
-
187
- private
188
-
189
- def partial?
190
- !@found_root_span
191
- end
192
-
193
- def find_root_span(trace)
194
- # TODO: Should we memoize this?
195
- # Would be safe, but `spans` is mutable, so if
196
- # the root span were removed, it would be a stale reference.
197
- # Figure out a better way to deal with this.
198
- root_span_id = trace.send(:root_span_id)
199
- root_span = trace.spans.find { |s| s.id == root_span_id } if root_span_id
200
- @found_root_span = !root_span.nil?
201
-
202
- # when root span is not found, fall back to last span (partial flush)
203
- root_span || trace.spans.last
204
- end
205
- end
206
- end
207
- end
@@ -1,216 +0,0 @@
1
- require_relative '../../datadog/core/chunker'
2
- require_relative 'parcel'
3
- require_relative 'request'
4
- require_relative 'serializable_trace'
5
- require_relative 'trace_formatter'
6
-
7
- module Datadog
8
- module Transport
9
- module Traces
10
- # Data transfer object for encoded traces
11
- class EncodedParcel
12
- include Datadog::Transport::Parcel
13
-
14
- attr_reader :trace_count
15
-
16
- def initialize(data, trace_count)
17
- super(data)
18
- @trace_count = trace_count
19
- end
20
-
21
- def count
22
- data.length
23
- end
24
- end
25
-
26
- # Traces request
27
- class Request < Datadog::Transport::Request
28
- end
29
-
30
- # Traces response
31
- module Response
32
- attr_reader :service_rates, :trace_count
33
- end
34
-
35
- # Traces chunker
36
- class Chunker
37
- # Trace agent limit payload size of 10 MiB (since agent v5.11.0):
38
- # https://github.com/DataDog/datadog-agent/blob/6.14.1/pkg/trace/api/api.go#L46
39
- #
40
- # We set the value to a conservative 5 MiB, in case network speed is slow.
41
- DEFAULT_MAX_PAYLOAD_SIZE = 5 * 1024 * 1024
42
-
43
- attr_reader :encoder, :max_size
44
-
45
- #
46
- # Single traces larger than +max_size+ will be discarded.
47
- #
48
- # @param encoder [Datadog::Core::Encoding::Encoder]
49
- # @param max_size [String] maximum acceptable payload size
50
- def initialize(encoder, max_size: DEFAULT_MAX_PAYLOAD_SIZE)
51
- @encoder = encoder
52
- @max_size = max_size
53
- end
54
-
55
- # Encodes a list of traces in chunks.
56
- # Before serializing, all traces are normalized. Trace nesting is not changed.
57
- #
58
- # @param traces [Enumerable<Trace>] list of traces
59
- # @return [Enumerable[Array[Bytes,Integer]]] list of encoded chunks: each containing a byte array and
60
- # number of traces
61
- def encode_in_chunks(traces)
62
- encoded_traces = if traces.respond_to?(:filter_map)
63
- # DEV Supported since Ruby 2.7, saves an intermediate object creation
64
- traces.filter_map { |t| encode_one(t) }
65
- else
66
- traces.map { |t| encode_one(t) }.reject(&:nil?)
67
- end
68
-
69
- Datadog::Core::Chunker.chunk_by_size(encoded_traces, max_size).map do |chunk|
70
- [encoder.join(chunk), chunk.size]
71
- end
72
- end
73
-
74
- private
75
-
76
- def encode_one(trace)
77
- encoded = Encoder.encode_trace(encoder, trace)
78
-
79
- if encoded.size > max_size
80
- # This single trace is too large, we can't flush it
81
- Datadog.logger.debug { "Dropping trace. Payload too large: '#{trace.inspect}'" }
82
- Datadog.health_metrics.transport_trace_too_large(1)
83
-
84
- return nil
85
- end
86
-
87
- encoded
88
- end
89
- end
90
-
91
- # Encodes traces using {Datadog::Core::Encoding::Encoder} instances.
92
- module Encoder
93
- module_function
94
-
95
- def encode_trace(encoder, trace)
96
- # Format the trace for transport
97
- TraceFormatter.format!(trace)
98
-
99
- # Make the trace serializable
100
- serializable_trace = SerializableTrace.new(trace)
101
-
102
- Datadog.logger.debug { "Flushing trace: #{JSON.dump(serializable_trace)}" }
103
-
104
- # Encode the trace
105
- encoder.encode(serializable_trace)
106
- end
107
- end
108
-
109
- # Sends traces based on transport API configuration.
110
- #
111
- # This class initializes the HTTP client, breaks down large
112
- # batches of traces into smaller chunks and handles
113
- # API version downgrade handshake.
114
- class Transport
115
- attr_reader :client, :apis, :default_api, :current_api_id
116
-
117
- def initialize(apis, default_api)
118
- @apis = apis
119
- @default_api = default_api
120
-
121
- change_api!(default_api)
122
- end
123
-
124
- def send_traces(traces)
125
- encoder = current_api.encoder
126
- chunker = Datadog::Transport::Traces::Chunker.new(encoder)
127
-
128
- responses = chunker.encode_in_chunks(traces.lazy).map do |encoded_traces, trace_count|
129
- request = Request.new(EncodedParcel.new(encoded_traces, trace_count))
130
-
131
- client.send_traces_payload(request).tap do |response|
132
- if downgrade?(response)
133
- downgrade!
134
- return send_traces(traces)
135
- end
136
- end
137
- end
138
-
139
- # Force resolution of lazy enumerator.
140
- #
141
- # The "correct" method to call here would be `#force`,
142
- # as this method was created to force the eager loading
143
- # of a lazy enumerator.
144
- #
145
- # Unfortunately, JRuby < 9.2.9.0 erroneously eagerly loads
146
- # the lazy Enumerator during intermediate steps.
147
- # This forces us to use `#to_a`, as this method works for both
148
- # lazy and regular Enumerators.
149
- # Using `#to_a` can mask the fact that we expect a lazy
150
- # Enumerator.
151
- responses = responses.to_a
152
-
153
- Datadog.health_metrics.transport_chunked(responses.size)
154
-
155
- responses
156
- end
157
-
158
- def stats
159
- @client.stats
160
- end
161
-
162
- def current_api
163
- apis[@current_api_id]
164
- end
165
-
166
- private
167
-
168
- def downgrade?(response)
169
- return false unless apis.fallbacks.key?(@current_api_id)
170
-
171
- response.not_found? || response.unsupported?
172
- end
173
-
174
- def downgrade!
175
- downgrade_api_id = apis.fallbacks[@current_api_id]
176
- raise NoDowngradeAvailableError, @current_api_id if downgrade_api_id.nil?
177
-
178
- change_api!(downgrade_api_id)
179
- end
180
-
181
- def change_api!(api_id)
182
- raise UnknownApiVersionError, api_id unless apis.key?(api_id)
183
-
184
- @current_api_id = api_id
185
- @client = HTTP::Client.new(current_api)
186
- end
187
-
188
- # Raised when configured with an unknown API version
189
- class UnknownApiVersionError < StandardError
190
- attr_reader :version
191
-
192
- def initialize(version)
193
- @version = version
194
- end
195
-
196
- def message
197
- "No matching transport API for version #{version}!"
198
- end
199
- end
200
-
201
- # Raised when configured with an unknown API version
202
- class NoDowngradeAvailableError < StandardError
203
- attr_reader :version
204
-
205
- def initialize(version)
206
- @version = version
207
- end
208
-
209
- def message
210
- "No downgrade from transport API version #{version} is available!"
211
- end
212
- end
213
- end
214
- end
215
- end
216
- end