ddtrace 1.7.0 → 1.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (182) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +100 -1
  3. data/README.md +2 -2
  4. data/ext/ddtrace_profiling_loader/extconf.rb +4 -1
  5. data/ext/ddtrace_profiling_native_extension/NativeExtensionDesign.md +1 -1
  6. data/ext/ddtrace_profiling_native_extension/clock_id_from_pthread.c +3 -2
  7. data/ext/ddtrace_profiling_native_extension/collectors_cpu_and_wall_time.c +24 -50
  8. data/ext/ddtrace_profiling_native_extension/collectors_cpu_and_wall_time.h +1 -1
  9. data/ext/ddtrace_profiling_native_extension/collectors_cpu_and_wall_time_worker.c +284 -74
  10. data/ext/ddtrace_profiling_native_extension/collectors_dynamic_sampling_rate.c +142 -0
  11. data/ext/ddtrace_profiling_native_extension/collectors_dynamic_sampling_rate.h +14 -0
  12. data/ext/ddtrace_profiling_native_extension/collectors_idle_sampling_helper.c +241 -0
  13. data/ext/ddtrace_profiling_native_extension/collectors_idle_sampling_helper.h +3 -0
  14. data/ext/ddtrace_profiling_native_extension/collectors_stack.c +32 -32
  15. data/ext/ddtrace_profiling_native_extension/collectors_stack.h +2 -2
  16. data/ext/ddtrace_profiling_native_extension/extconf.rb +21 -7
  17. data/ext/ddtrace_profiling_native_extension/helpers.h +5 -0
  18. data/ext/ddtrace_profiling_native_extension/http_transport.c +50 -49
  19. data/ext/ddtrace_profiling_native_extension/libdatadog_helpers.h +5 -1
  20. data/ext/ddtrace_profiling_native_extension/native_extension_helpers.rb +42 -12
  21. data/ext/ddtrace_profiling_native_extension/private_vm_api_access.c +116 -22
  22. data/ext/ddtrace_profiling_native_extension/private_vm_api_access.h +9 -0
  23. data/ext/ddtrace_profiling_native_extension/profiling.c +205 -0
  24. data/ext/ddtrace_profiling_native_extension/ruby_helpers.c +86 -0
  25. data/ext/ddtrace_profiling_native_extension/ruby_helpers.h +28 -6
  26. data/ext/ddtrace_profiling_native_extension/setup_signal_handler.c +23 -4
  27. data/ext/ddtrace_profiling_native_extension/setup_signal_handler.h +4 -0
  28. data/ext/ddtrace_profiling_native_extension/stack_recorder.c +47 -50
  29. data/ext/ddtrace_profiling_native_extension/stack_recorder.h +4 -4
  30. data/ext/ddtrace_profiling_native_extension/time_helpers.c +17 -0
  31. data/ext/ddtrace_profiling_native_extension/time_helpers.h +10 -0
  32. data/lib/datadog/appsec/assets/waf_rules/recommended.json +75 -8
  33. data/lib/datadog/appsec/assets/waf_rules/risky.json +1 -1
  34. data/lib/datadog/appsec/assets/waf_rules/strict.json +1 -1
  35. data/lib/datadog/appsec/assets.rb +1 -1
  36. data/lib/datadog/appsec/configuration/settings.rb +35 -22
  37. data/lib/datadog/appsec/configuration.rb +4 -2
  38. data/lib/datadog/appsec/contrib/auto_instrument.rb +1 -1
  39. data/lib/datadog/appsec/contrib/configuration/settings.rb +1 -1
  40. data/lib/datadog/appsec/contrib/integration.rb +1 -1
  41. data/lib/datadog/appsec/contrib/patcher.rb +1 -1
  42. data/lib/datadog/appsec/contrib/rack/configuration/settings.rb +1 -1
  43. data/lib/datadog/appsec/contrib/rack/ext.rb +1 -1
  44. data/lib/datadog/appsec/contrib/rack/gateway/watcher.rb +1 -1
  45. data/lib/datadog/appsec/contrib/rack/reactive/request.rb +1 -1
  46. data/lib/datadog/appsec/contrib/rack/reactive/request_body.rb +1 -1
  47. data/lib/datadog/appsec/contrib/rack/reactive/response.rb +1 -1
  48. data/lib/datadog/appsec/contrib/rack/request.rb +1 -1
  49. data/lib/datadog/appsec/contrib/rack/response.rb +1 -1
  50. data/lib/datadog/appsec/contrib/rails/configuration/settings.rb +1 -1
  51. data/lib/datadog/appsec/contrib/rails/ext.rb +1 -1
  52. data/lib/datadog/appsec/contrib/rails/framework.rb +1 -1
  53. data/lib/datadog/appsec/contrib/rails/gateway/watcher.rb +1 -1
  54. data/lib/datadog/appsec/contrib/rails/reactive/action.rb +1 -1
  55. data/lib/datadog/appsec/contrib/rails/request.rb +1 -1
  56. data/lib/datadog/appsec/contrib/rails/request_middleware.rb +1 -1
  57. data/lib/datadog/appsec/contrib/sinatra/configuration/settings.rb +1 -1
  58. data/lib/datadog/appsec/contrib/sinatra/ext.rb +1 -1
  59. data/lib/datadog/appsec/contrib/sinatra/framework.rb +1 -1
  60. data/lib/datadog/appsec/contrib/sinatra/gateway/watcher.rb +1 -1
  61. data/lib/datadog/appsec/contrib/sinatra/reactive/routed.rb +1 -1
  62. data/lib/datadog/appsec/contrib/sinatra/request_middleware.rb +1 -1
  63. data/lib/datadog/appsec/event.rb +1 -1
  64. data/lib/datadog/appsec/extensions.rb +36 -26
  65. data/lib/datadog/appsec/instrumentation/gateway.rb +3 -3
  66. data/lib/datadog/appsec/processor.rb +15 -19
  67. data/lib/datadog/appsec/rate_limiter.rb +1 -1
  68. data/lib/datadog/appsec/reactive/address_hash.rb +1 -1
  69. data/lib/datadog/appsec/reactive/engine.rb +1 -1
  70. data/lib/datadog/appsec/reactive/operation.rb +2 -2
  71. data/lib/datadog/appsec/reactive/subscriber.rb +1 -1
  72. data/lib/datadog/appsec/response.rb +18 -9
  73. data/lib/datadog/appsec/utils/http/media_range.rb +201 -0
  74. data/lib/datadog/appsec/utils/http/media_type.rb +87 -0
  75. data/lib/datadog/appsec/utils/http.rb +9 -0
  76. data/lib/datadog/appsec/utils.rb +7 -0
  77. data/lib/datadog/appsec.rb +1 -1
  78. data/lib/datadog/ci/ext/environment.rb +57 -13
  79. data/lib/datadog/core/configuration/agent_settings_resolver.rb +2 -2
  80. data/lib/datadog/core/configuration/base.rb +3 -0
  81. data/lib/datadog/core/configuration/components.rb +27 -6
  82. data/lib/datadog/core/configuration/ext.rb +26 -0
  83. data/lib/datadog/core/configuration/option_definition.rb +11 -2
  84. data/lib/datadog/core/configuration/settings.rb +16 -341
  85. data/lib/datadog/core/diagnostics/environment_logger.rb +4 -3
  86. data/lib/datadog/core/diagnostics/health.rb +4 -22
  87. data/lib/datadog/core/environment/variable_helpers.rb +58 -10
  88. data/lib/datadog/core/metrics/client.rb +3 -2
  89. data/lib/datadog/core/metrics/ext.rb +0 -2
  90. data/lib/datadog/core/telemetry/collector.rb +1 -0
  91. data/lib/datadog/core/utils.rb +0 -21
  92. data/lib/datadog/core.rb +21 -1
  93. data/lib/datadog/kit/appsec/events.rb +75 -0
  94. data/lib/datadog/kit/enable_core_dumps.rb +1 -0
  95. data/lib/datadog/kit/identity.rb +8 -7
  96. data/lib/datadog/opentelemetry/api/context.rb +187 -0
  97. data/lib/datadog/opentelemetry/api/trace/span.rb +15 -0
  98. data/lib/datadog/opentelemetry/sdk/configurator.rb +38 -0
  99. data/lib/datadog/opentelemetry/sdk/id_generator.rb +27 -0
  100. data/lib/datadog/opentelemetry/sdk/propagator.rb +91 -0
  101. data/lib/datadog/opentelemetry/sdk/span_processor.rb +92 -0
  102. data/lib/datadog/opentelemetry.rb +48 -0
  103. data/lib/datadog/opentracer/distributed_headers.rb +2 -2
  104. data/lib/datadog/profiling/collectors/cpu_and_wall_time_worker.rb +16 -5
  105. data/lib/datadog/profiling/collectors/dynamic_sampling_rate.rb +14 -0
  106. data/lib/datadog/profiling/collectors/idle_sampling_helper.rb +68 -0
  107. data/lib/datadog/profiling/stack_recorder.rb +14 -0
  108. data/lib/datadog/profiling.rb +2 -0
  109. data/lib/datadog/tracing/configuration/ext.rb +33 -4
  110. data/lib/datadog/tracing/configuration/settings.rb +433 -0
  111. data/lib/datadog/tracing/contrib/aws/configuration/settings.rb +4 -1
  112. data/lib/datadog/tracing/contrib/aws/ext.rb +1 -0
  113. data/lib/datadog/tracing/contrib/dalli/configuration/settings.rb +4 -1
  114. data/lib/datadog/tracing/contrib/dalli/ext.rb +1 -0
  115. data/lib/datadog/tracing/contrib/elasticsearch/configuration/settings.rb +5 -1
  116. data/lib/datadog/tracing/contrib/elasticsearch/ext.rb +1 -0
  117. data/lib/datadog/tracing/contrib/ethon/configuration/settings.rb +6 -1
  118. data/lib/datadog/tracing/contrib/ethon/ext.rb +1 -0
  119. data/lib/datadog/tracing/contrib/excon/configuration/settings.rb +5 -1
  120. data/lib/datadog/tracing/contrib/excon/ext.rb +1 -0
  121. data/lib/datadog/tracing/contrib/faraday/configuration/settings.rb +5 -1
  122. data/lib/datadog/tracing/contrib/faraday/ext.rb +1 -0
  123. data/lib/datadog/tracing/contrib/grpc/configuration/settings.rb +6 -1
  124. data/lib/datadog/tracing/contrib/grpc/distributed/propagation.rb +9 -4
  125. data/lib/datadog/tracing/contrib/grpc/ext.rb +1 -0
  126. data/lib/datadog/tracing/contrib/http/configuration/settings.rb +11 -1
  127. data/lib/datadog/tracing/contrib/http/distributed/fetcher.rb +10 -3
  128. data/lib/datadog/tracing/contrib/http/distributed/propagation.rb +9 -4
  129. data/lib/datadog/tracing/contrib/http/ext.rb +2 -0
  130. data/lib/datadog/tracing/contrib/http/instrumentation.rb +3 -6
  131. data/lib/datadog/tracing/contrib/httpclient/configuration/settings.rb +11 -1
  132. data/lib/datadog/tracing/contrib/httpclient/ext.rb +2 -0
  133. data/lib/datadog/tracing/contrib/httpclient/instrumentation.rb +3 -4
  134. data/lib/datadog/tracing/contrib/httprb/configuration/settings.rb +11 -1
  135. data/lib/datadog/tracing/contrib/httprb/ext.rb +2 -0
  136. data/lib/datadog/tracing/contrib/httprb/instrumentation.rb +3 -4
  137. data/lib/datadog/tracing/contrib/mongodb/configuration/settings.rb +5 -1
  138. data/lib/datadog/tracing/contrib/mongodb/ext.rb +1 -0
  139. data/lib/datadog/tracing/contrib/mysql2/configuration/settings.rb +4 -1
  140. data/lib/datadog/tracing/contrib/mysql2/ext.rb +1 -0
  141. data/lib/datadog/tracing/contrib/mysql2/instrumentation.rb +2 -2
  142. data/lib/datadog/tracing/contrib/patcher.rb +3 -2
  143. data/lib/datadog/tracing/contrib/pg/configuration/settings.rb +4 -1
  144. data/lib/datadog/tracing/contrib/pg/ext.rb +1 -0
  145. data/lib/datadog/tracing/contrib/pg/instrumentation.rb +56 -33
  146. data/lib/datadog/tracing/contrib/presto/configuration/settings.rb +4 -1
  147. data/lib/datadog/tracing/contrib/presto/ext.rb +1 -0
  148. data/lib/datadog/tracing/contrib/propagation/sql_comment/ext.rb +1 -0
  149. data/lib/datadog/tracing/contrib/propagation/sql_comment.rb +10 -12
  150. data/lib/datadog/tracing/contrib/redis/configuration/settings.rb +4 -1
  151. data/lib/datadog/tracing/contrib/redis/ext.rb +1 -0
  152. data/lib/datadog/tracing/contrib/redis/instrumentation.rb +30 -23
  153. data/lib/datadog/tracing/contrib/redis/integration.rb +34 -2
  154. data/lib/datadog/tracing/contrib/redis/patcher.rb +18 -14
  155. data/lib/datadog/tracing/contrib/redis/quantize.rb +12 -9
  156. data/lib/datadog/tracing/contrib/redis/tags.rb +4 -6
  157. data/lib/datadog/tracing/contrib/redis/trace_middleware.rb +72 -0
  158. data/lib/datadog/tracing/contrib/rest_client/configuration/settings.rb +6 -1
  159. data/lib/datadog/tracing/contrib/rest_client/ext.rb +1 -0
  160. data/lib/datadog/tracing/contrib/stripe/configuration/settings.rb +33 -0
  161. data/lib/datadog/tracing/contrib/stripe/ext.rb +26 -0
  162. data/lib/datadog/tracing/contrib/stripe/integration.rb +43 -0
  163. data/lib/datadog/tracing/contrib/stripe/patcher.rb +29 -0
  164. data/lib/datadog/tracing/contrib/stripe/request.rb +67 -0
  165. data/lib/datadog/tracing/contrib.rb +1 -0
  166. data/lib/datadog/{core → tracing}/diagnostics/ext.rb +1 -6
  167. data/lib/datadog/tracing/diagnostics/health.rb +40 -0
  168. data/lib/datadog/tracing/distributed/{b3.rb → b3_multi.rb} +2 -2
  169. data/lib/datadog/tracing/distributed/helpers.rb +2 -1
  170. data/lib/datadog/tracing/distributed/none.rb +19 -0
  171. data/lib/datadog/tracing/distributed/trace_context.rb +378 -0
  172. data/lib/datadog/tracing/metadata/ext.rb +1 -1
  173. data/lib/datadog/tracing/metadata/tagging.rb +6 -0
  174. data/lib/datadog/tracing/sampling/priority_sampler.rb +11 -0
  175. data/lib/datadog/tracing/sampling/rate_sampler.rb +3 -3
  176. data/lib/datadog/tracing/span.rb +3 -19
  177. data/lib/datadog/tracing/span_operation.rb +5 -4
  178. data/lib/datadog/tracing/trace_digest.rb +85 -2
  179. data/lib/datadog/tracing/trace_operation.rb +13 -4
  180. data/lib/datadog/tracing/utils.rb +50 -0
  181. data/lib/ddtrace/version.rb +1 -1
  182. metadata +41 -9
data/lib/datadog/core.rb CHANGED
@@ -1,4 +1,5 @@
1
- # typed: strict
1
+ # frozen_string_literal: true
2
+ # typed: true
2
3
 
3
4
  require_relative 'core/extensions'
4
5
 
@@ -9,6 +10,25 @@ module Datadog
9
10
  # products. It is a dependency of each product. Contrast with Datadog::Kit
10
11
  # for higher-level features.
11
12
  module Core
13
+ class << self
14
+ # Records the occurrence of a deprecated operation in this library.
15
+ #
16
+ # Currently, these operations are logged to `Datadog.logger` at `warn` level.
17
+ #
18
+ # `disallowed_next_major` adds a message informing that the deprecated operation
19
+ # won't be allowed in the next major release.
20
+ #
21
+ # @yieldreturn [String] a String with the lazily evaluated deprecation message.
22
+ # @param [Boolean] disallowed_next_major whether this deprecation will be enforced in the next major release.
23
+ def log_deprecation(disallowed_next_major: true)
24
+ Datadog.logger.warn do
25
+ message = yield
26
+ message += ' This will be enforced in the next major release.' if disallowed_next_major
27
+ message
28
+ end
29
+ nil
30
+ end
31
+ end
12
32
  end
13
33
 
14
34
  extend Core::Extensions
@@ -0,0 +1,75 @@
1
+ # typed: false
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../identity'
5
+
6
+ module Datadog
7
+ module Kit
8
+ module AppSec
9
+ # Tracking events
10
+ module Events
11
+ LOGIN_SUCCESS_EVENT = 'users.login.success'
12
+ LOGIN_FAILURE_EVENT = 'users.login.failure'
13
+
14
+ # Attach login success event information to the trace
15
+ #
16
+ # This method is experimental and may change in the future.
17
+ #
18
+ # @param trace [TraceOperation] Trace to attach data to.
19
+ # @param user [Hash<Symbol, String>] User information to pass to
20
+ # Datadog::Kit::Identity.set_user. Must contain at least :id as key.
21
+ # @param others [Hash<String || Symbol, String>] Additional free-form
22
+ # event information to attach to the trace.
23
+ def self.track_login_success(trace, user:, **others)
24
+ track(LOGIN_SUCCESS_EVENT, trace, **others)
25
+
26
+ user_options = user.dup
27
+ user_id = user.delete(:id)
28
+
29
+ raise ArgumentError, 'missing required key: :user => { :id }' if user_id.nil?
30
+
31
+ Kit::Identity.set_user(trace, id: user_id, **user_options)
32
+ end
33
+
34
+ # Attach login failure event information to the trace
35
+ #
36
+ # This method is experimental and may change in the future.
37
+ #
38
+ # @param trace [TraceOperation] Trace to attach data to.
39
+ # @param user_id [String] User id that attempted login
40
+ # @param user_exists [bool] Whether the user id that did a login attempt exists.
41
+ # @param others [Hash<String || Symbol, String>] Additional free-form
42
+ # event information to attach to the trace.
43
+ def self.track_login_failure(trace, user_id:, user_exists:, **others)
44
+ track(LOGIN_FAILURE_EVENT, trace, **others)
45
+
46
+ raise ArgumentError, 'user_id cannot be nil' if user_id.nil?
47
+
48
+ trace.set_tag('appsec.events.users.login.failure.usr.id', user_id)
49
+ trace.set_tag('appsec.events.users.login.failure.usr.exists', user_exists)
50
+ end
51
+
52
+ # Attach custom event information to the trace
53
+ #
54
+ # This method is experimental and may change in the future.
55
+ #
56
+ # @param event [String] Mandatory. Event code.
57
+ # @param trace [TraceOperation] Trace to attach data to.
58
+ # @param others [Hash<Symbol, String>] Additional free-form
59
+ # event information to attach to the trace. Key must not
60
+ # be :track.
61
+ def self.track(event, trace, **others)
62
+ trace.set_tag("appsec.events.#{event}.track", 'true')
63
+
64
+ others.each do |k, v|
65
+ raise ArgumentError, 'key cannot be :track' if k.to_sym == :track
66
+
67
+ trace.set_tag("appsec.events.#{event}.#{k}", v) unless v.nil?
68
+ end
69
+
70
+ trace.keep!
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -1,4 +1,5 @@
1
1
  # typed: ignore
2
+ # frozen_string_literal: true
2
3
 
3
4
  module Datadog
4
5
  module Kit
@@ -1,4 +1,5 @@
1
1
  # typed: false
2
+ # frozen_string_literal: true
2
3
 
3
4
  module Datadog
4
5
  module Kit
@@ -22,7 +23,7 @@ module Datadog
22
23
  # currently possesses extracted from token or application security
23
24
  # context. The value would come from the scope associated with an OAuth
24
25
  # 2.0 Access Token or an attribute value in a SAML 2.0 Assertion.
25
- # @param others [Hash<String || Symbol, String>] Additional free-form
26
+ # @param others [Hash<Symbol, String>] Additional free-form
26
27
  # user information to attach to the trace.
27
28
  #
28
29
  # rubocop:disable Metrics/CyclomaticComplexity
@@ -32,12 +33,12 @@ module Datadog
32
33
 
33
34
  # enforce types
34
35
 
35
- raise TypeError, 'id must be a String' unless id.is_a?(String)
36
- raise TypeError, 'email must be a String' unless email.nil? || email.is_a?(String)
37
- raise TypeError, 'name must be a String' unless name.nil? || name.is_a?(String)
38
- raise TypeError, 'session_id must be a String' unless session_id.nil? || session_id.is_a?(String)
39
- raise TypeError, 'role must be a String' unless role.nil? || role.is_a?(String)
40
- raise TypeError, 'scope must be a String' unless scope.nil? || scope.is_a?(String)
36
+ raise TypeError, ':id must be a String' unless id.is_a?(String)
37
+ raise TypeError, ':email must be a String' unless email.nil? || email.is_a?(String)
38
+ raise TypeError, ':name must be a String' unless name.nil? || name.is_a?(String)
39
+ raise TypeError, ':session_id must be a String' unless session_id.nil? || session_id.is_a?(String)
40
+ raise TypeError, ':role must be a String' unless role.nil? || role.is_a?(String)
41
+ raise TypeError, ':scope must be a String' unless scope.nil? || scope.is_a?(String)
41
42
 
42
43
  others.each do |k, v|
43
44
  raise TypeError, "#{k.inspect} must be a String" unless v.nil? || v.is_a?(String)
@@ -0,0 +1,187 @@
1
+ # frozen_string_literal: true
2
+ # typed: ignore
3
+
4
+ require_relative 'trace/span'
5
+ require_relative '../../tracing/trace_operation'
6
+
7
+ module Datadog
8
+ module OpenTelemetry
9
+ module API
10
+ # The OpenTelemetry Context contains a key-value store that can be attached
11
+ # to a trace.
12
+ #
13
+ # It loosely matches our `TraceOperations#tags`, except for the following:
14
+ # * Context can store arbitrary objects as values. One example is for the key
15
+ # `Context::Key.new('current-span')`, which is associated with a `Span` object.
16
+ # In contrast, `TraceOperations#tags` only stores string values.
17
+ # * Context is how spans know who their parent span is. The parenting operation happens on every
18
+ # span created. Parenting is not directly tied to the active Fiber or Thread.
19
+ # * Context is immutable: changing a value creates a copy of Context.
20
+ # * Context is not bound to a specific trace: it can be reused an arbitrary number of times.
21
+ module Context
22
+ CURRENT_SPAN_KEY = ::OpenTelemetry::Trace.const_get(:CURRENT_SPAN_KEY)
23
+ private_constant :CURRENT_SPAN_KEY
24
+
25
+ def initialize(entries, trace: nil)
26
+ @trace = trace || ::Datadog::Tracing.send(:tracer).send(:start_trace)
27
+ @trace.otel_values.merge!(entries) if entries
28
+ @trace.otel_context ||= self
29
+ end
30
+
31
+ # Because Context can be reused, we have to make sure we have
32
+ # a valid `TraceOperation` on every invocation.
33
+ def ensure_trace
34
+ return nil unless @trace
35
+
36
+ # The Context can be reused after the root span has finished.
37
+ @trace.send(:reset) if @trace.finished?
38
+ @trace
39
+ end
40
+
41
+ # Returns the corresponding value (or nil) for key
42
+ #
43
+ # @param [Key] key The lookup key
44
+ # @return [Object]
45
+ def value(key)
46
+ return nil unless @trace
47
+
48
+ @trace.otel_value(key)
49
+ end
50
+
51
+ alias [] value
52
+
53
+ # Returns a new Context where entries contains the newly added key and value
54
+ #
55
+ # @param [Key] key The key to store this value under
56
+ # @param [Object] value Object to be stored under key
57
+ # @return [Context]
58
+ def set_value(key, value)
59
+ set_values(key => value)
60
+ end
61
+
62
+ # Returns a new Context with the current context's entries merged with the
63
+ # new entries
64
+ #
65
+ # @param [Hash] values The values to be merged with the current context's
66
+ # entries.
67
+ # @param [Object] value Object to be stored under key
68
+ # @return [Context]
69
+ def set_values(values)
70
+ trace = nil
71
+ if (current_span = values[CURRENT_SPAN_KEY])
72
+ trace = current_span.datadog_trace
73
+ end
74
+
75
+ existing_values = @trace && @trace.otel_values || {}
76
+
77
+ ::OpenTelemetry::Context.new(existing_values.merge(values), trace: trace)
78
+ end
79
+
80
+ # The Datadog {TraceOperation} associated with this {Context}.
81
+ def trace
82
+ @trace
83
+ end
84
+
85
+ # Singleton class methods for {Context}
86
+ module SingletonClass
87
+ # Returns current context, which is never nil
88
+ #
89
+ # @return [Context]
90
+ def current
91
+ trace = Tracing.active_trace
92
+ return ::OpenTelemetry::Context::ROOT unless trace
93
+
94
+ trace.otel_context ||= ::OpenTelemetry::Context.from_trace(trace)
95
+ end
96
+
97
+ # Associates a Context with the caller's current Fiber. Every call to
98
+ # this operation should be paired with a corresponding call to detach.
99
+ #
100
+ # Returns a token to be used with the matching call to detach
101
+ #
102
+ # @param [Context] context The new context
103
+ # @return [Object] A token to be used when detaching
104
+ def attach(context)
105
+ previous_trace = Tracing.active_trace
106
+ continue_trace!(context)
107
+
108
+ stack.push(previous_trace && previous_trace.otel_context || ::OpenTelemetry::Context::ROOT)
109
+ stack.size
110
+ end
111
+
112
+ # Restores the previous Context associated with the current Fiber.
113
+ # The supplied token is used to check if the call to detach is balanced
114
+ # with a corresponding attach call. A warning is logged if the
115
+ # calls are unbalanced.
116
+ #
117
+ # @param [Object] token The token provided by the matching call to attach
118
+ # @return [Boolean] True if the calls matched, false otherwise
119
+ def detach(token)
120
+ s = stack
121
+ calls_matched = (token == s.size)
122
+ unless calls_matched
123
+ ::OpenTelemetry.handle_error(
124
+ exception: ::OpenTelemetry::Context::DetachError.new(
125
+ 'calls to detach should match corresponding calls to attach.'
126
+ )
127
+ )
128
+ end
129
+
130
+ previous_context = s.pop
131
+ continue_trace!(previous_context)
132
+ calls_matched
133
+ end
134
+
135
+ # Part of the OpenTelemetry public API for {Context}.
136
+ def clear
137
+ super
138
+ tracer = Tracing.send(:tracer)
139
+ tracer.send(:call_context).activate!(nil)
140
+ end
141
+
142
+ # Creates a new {Context} associated with a {TraceOperation}.
143
+ def from_trace(trace)
144
+ new({}, trace: trace)
145
+ end
146
+
147
+ private
148
+
149
+ def continue_trace!(context, &block)
150
+ call_context = Tracing.send(:tracer).send(:call_context)
151
+ if context && context.trace
152
+ call_context.activate!(context.ensure_trace, &block)
153
+ else
154
+ call_context.activate!(nil)
155
+ end
156
+ end
157
+ end
158
+
159
+ def self.prepended(base)
160
+ base.singleton_class.prepend(SingletonClass)
161
+ end
162
+
163
+ ::OpenTelemetry::Context.prepend(self)
164
+ end
165
+
166
+ # OpenTelemetry-specific {TraceOperation} features.
167
+ #
168
+ # These extensions providing matching between {TraceOperation}
169
+ # and OpenTelemetry {Context}.
170
+ module TraceOperation
171
+ attr_accessor :otel_context
172
+
173
+ # Stores values from Context#entries
174
+ def otel_value(key)
175
+ otel_values[key]
176
+ end
177
+
178
+ # Retrieves values for Context#entries
179
+ def otel_values
180
+ @otel_values ||= {}
181
+ end
182
+
183
+ Tracing::TraceOperation.include(self)
184
+ end
185
+ end
186
+ end
187
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+ # typed: ignore
3
+
4
+ module Datadog
5
+ module OpenTelemetry
6
+ module Trace
7
+ # Stores associated Datadog entities to the OpenTelemetry Span.
8
+ module Span
9
+ attr_accessor :datadog_trace, :datadog_span
10
+
11
+ ::OpenTelemetry::Trace::Span.prepend(self)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+ # typed: ignore
3
+
4
+ require_relative 'span_processor'
5
+ require_relative 'id_generator'
6
+ require_relative 'propagator'
7
+
8
+ module Datadog
9
+ module OpenTelemetry
10
+ module SDK
11
+ # The Configurator is responsible for setting wiring up
12
+ # different OpenTelemetry requirements together.
13
+ # Some of the requirements will be changed to Datadog versions.
14
+ module Configurator
15
+ def initialize
16
+ super
17
+ @id_generator = IdGenerator
18
+ end
19
+
20
+ # Ensure Datadog-configure propagation styles have are applied when configured.
21
+ #
22
+ # DEV: Support configuring propagation through the environment variable
23
+ # DEV: `OTEL_PROPAGATORS`, similar to `DD_TRACE_PROPAGATION*`?
24
+ def configure_propagation
25
+ @propagators = [Propagator.new(Tracing::Contrib::HTTP::Distributed::Propagation.new)]
26
+ super
27
+ end
28
+
29
+ # Ensure Datadog-configure trace writer is configured.
30
+ def wrapped_exporters_from_env
31
+ [SpanProcessor.new]
32
+ end
33
+
34
+ ::OpenTelemetry::SDK::Configurator.prepend(self)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+ # typed: ignore
3
+
4
+ module Datadog
5
+ module OpenTelemetry
6
+ module SDK
7
+ # Generates Datadog-compatible IDs for OpenTelemetry traces.
8
+ # OpenTelemetry traces already produce Datadog-compatible IDs.
9
+ class IdGenerator
10
+ class << self
11
+ include ::OpenTelemetry::Trace
12
+
13
+ # Generates a valid trace identifier, a 16-byte string with at least one
14
+ # non-zero byte.
15
+ #
16
+ # @return [String] a valid trace ID.
17
+ def generate_trace_id
18
+ loop do
19
+ id = Random.bytes(8) # DEV: Change to 16 (16*8-byte) when 128-bit trace_id is supported.
20
+ return id unless id == ::OpenTelemetry::Trace::INVALID_SPAN_ID
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+ # typed: ignore
3
+
4
+ module Datadog
5
+ module OpenTelemetry
6
+ module SDK
7
+ # Compatibility wrapper to allow Datadog propagators to fulfill the
8
+ # OpenTelemetry propagator API.
9
+ class Propagator
10
+ def initialize(datadog_propagator)
11
+ @datadog_propagator = datadog_propagator
12
+ end
13
+
14
+ def inject(
15
+ carrier, context: ::OpenTelemetry::Context.current,
16
+ setter: ::OpenTelemetry::Context::Propagation.text_map_setter
17
+ )
18
+ unless setter == ::OpenTelemetry::Context::Propagation.text_map_setter
19
+ Datadog.logger.error(
20
+ 'Custom setter is not supported. Please inform the `ddtrace` team at ' \
21
+ ' https://github.com/DataDog/dd-trace-rb of your use case so we can best support you. Using the default ' \
22
+ 'OpenTelemetry::Context::Propagation.text_map_setter as a fallback setter.'
23
+ )
24
+ end
25
+
26
+ @datadog_propagator.inject!(context.trace.to_digest, carrier)
27
+ end
28
+
29
+ def extract(
30
+ carrier, context: ::OpenTelemetry::Context.current,
31
+ getter: ::OpenTelemetry::Context::Propagation.text_map_getter
32
+ )
33
+ unless getter == ::OpenTelemetry::Context::Propagation.text_map_getter
34
+ Datadog.logger.error(
35
+ 'Custom getter is not supported. Please inform the `ddtrace` team at ' \
36
+ ' https://github.com/DataDog/dd-trace-rb of your use case so we can best support you. Using the default ' \
37
+ 'OpenTelemetry::Context::Propagation.text_map_getter as a fallback getter.'
38
+ )
39
+ end
40
+
41
+ digest = @datadog_propagator.extract(carrier)
42
+ return context unless digest
43
+
44
+ trace_id = to_otel_id(digest.trace_id)
45
+ span_id = to_otel_id(digest.span_id)
46
+
47
+ if digest.trace_state || digest.trace_flags
48
+ trace_flags = ::OpenTelemetry::Trace::TraceFlags.from_byte(digest.trace_flags)
49
+ tracestate = Tracing::Distributed::TraceContext.new(fetcher: nil).send(:build_tracestate, digest)
50
+ else
51
+ trace_flags = if Tracing::Sampling::PrioritySampler.sampled?(digest.trace_sampling_priority)
52
+ ::OpenTelemetry::Trace::TraceFlags::SAMPLED
53
+ else
54
+ ::OpenTelemetry::Trace::TraceFlags::DEFAULT
55
+ end
56
+ tracestate = ::OpenTelemetry::Trace::Tracestate::DEFAULT
57
+ end
58
+
59
+ span_context = ::OpenTelemetry::Trace::SpanContext.new(
60
+ trace_id: trace_id,
61
+ span_id: span_id,
62
+ trace_flags: trace_flags,
63
+ tracestate: tracestate,
64
+ remote: true
65
+ )
66
+
67
+ span = ::OpenTelemetry::Trace.non_recording_span(span_context)
68
+
69
+ trace = Tracing.continue_trace!(digest)
70
+
71
+ span.datadog_trace = trace
72
+
73
+ ::OpenTelemetry::Trace.context_with_span(span, parent_context: context)
74
+ end
75
+
76
+ # Returns fields set by this propagator.
77
+ # DEV: Doesn't seem like it's used in production Otel code paths.
78
+ def fields
79
+ []
80
+ end
81
+
82
+ private
83
+
84
+ # Converts the {Numeric} Datadog id object to OpenTelemetry's byte array format.
85
+ def to_otel_id(dd_id)
86
+ Array(dd_id).pack('S')
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+ # typed: ignore
3
+
4
+ module Datadog
5
+ module OpenTelemetry
6
+ module SDK
7
+ # Keeps OpenTelemetry spans in sync with the Datadog execution context.
8
+ # Also responsible for flushing spans when their are finished.
9
+ class SpanProcessor
10
+ # Called when a {Span} is started, if the {Span#recording?}
11
+ # returns true.
12
+ #
13
+ # This method is called synchronously on the execution thread, should
14
+ # not throw or block the execution thread.
15
+ #
16
+ # @param [Span] span the {Span} that just started.
17
+ # @param [Context] parent_context the parent {Context} of the newly
18
+ # started span.
19
+ def on_start(span, parent_context)
20
+ create_matching_datadog_span(span, parent_context)
21
+ end
22
+
23
+ # Called when a {Span} is ended, if the {Span#recording?}
24
+ # returns true.
25
+ #
26
+ # This method is called synchronously on the execution thread, should
27
+ # not throw or block the execution thread.
28
+ #
29
+ # @param [Span] span the {Span} that just ended.
30
+ def on_finish(span)
31
+ span.datadog_span.finish
32
+ end
33
+
34
+ # Export all ended spans to the configured `Exporter` that have not yet
35
+ # been exported.
36
+ #
37
+ # This method should only be called in cases where it is absolutely
38
+ # necessary, such as when using some FaaS providers that may suspend
39
+ # the process after an invocation, but before the `Processor` exports
40
+ # the completed spans.
41
+ #
42
+ # @param [optional Numeric] timeout An optional timeout in seconds.
43
+ # @return [Integer] Export::SUCCESS if no error occurred, Export::FAILURE if
44
+ # a non-specific failure occurred, Export::TIMEOUT if a timeout occurred.
45
+ def force_flush(timeout: nil)
46
+ writer.force_flush(timeout: timeout) if writer.respond_to? :force_flush
47
+ Export::SUCCESS
48
+ end
49
+
50
+ # Called when {TracerProvider#shutdown} is called.
51
+ #
52
+ # @param [optional Numeric] timeout An optional timeout in seconds.
53
+ # @return [Integer] Export::SUCCESS if no error occurred, Export::FAILURE if
54
+ # a non-specific failure occurred, Export::TIMEOUT if a timeout occurred.
55
+ def shutdown(timeout: nil)
56
+ writer.stop
57
+ Export::SUCCESS
58
+ end
59
+
60
+ private
61
+
62
+ def writer
63
+ Datadog.configuration.tracing.writer
64
+ end
65
+
66
+ def create_matching_datadog_span(span, parent_context)
67
+ if parent_context.trace
68
+ Tracing.send(:tracer).send(:call_context).activate!(parent_context.ensure_trace)
69
+ else
70
+ Tracing.continue_trace!(nil)
71
+ end
72
+
73
+ datadog_span = start_datadog_span(span)
74
+
75
+ span.datadog_span = datadog_span
76
+ span.datadog_trace = Tracing.active_trace
77
+ end
78
+
79
+ def start_datadog_span(span)
80
+ tags = span.resource.attribute_enumerator.to_h
81
+
82
+ kind = span.kind || 'internal'
83
+ tags[Tracing::Metadata::Ext::TAG_KIND] = kind
84
+
85
+ datadog_span = Tracing.trace(span.name, tags: tags)
86
+ datadog_span.set_error([nil, span.status.description]) unless span.status.ok?
87
+ datadog_span
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+ # typed: ignore
3
+
4
+ # Entrypoint file for OpenTelemetry integration.
5
+ #
6
+ # This file's path is part of the @public_api.
7
+ #
8
+ # OpenTelemetry includes metrics, tracing, logs, and profiling.
9
+ # This file activates the integrations of all OpenTelemetry
10
+ # components supported by Datadog.
11
+
12
+ require_relative 'tracing'
13
+ require_relative 'opentelemetry/api/context'
14
+
15
+ # DEV: Should this be a Contrib integration, that depends on the `opentelemetry-sdk`
16
+ # DEV: and checks for compatibility?
17
+ # DEV: This is different from our existing OpenTracer API, but there are many safety
18
+ # DEV: features built into Contrib instrumentation today.
19
+ require_relative 'opentelemetry/sdk/configurator' if defined?(OpenTelemetry::SDK)
20
+
21
+ module Datadog
22
+ # Datadog OpenTelemetry integration.
23
+ module OpenTelemetry
24
+ # Used by Telemetry to decide if OpenTelemetry instrumentation is enabled
25
+ LOADED = true
26
+
27
+ # Use `Datadog.logger` as the default logger
28
+ def logger
29
+ @logger ||= ::Datadog.logger
30
+ end
31
+
32
+ ::OpenTelemetry.singleton_class.prepend(self)
33
+ end
34
+ end
35
+
36
+ # OpenTelemetry does not wait until the "root" span is finished to flush:
37
+ # the "root" span does not have special influence on flushing order.
38
+ #
39
+ # The "root" OpenTelemetry span might be a span that is never finished, but
40
+ # instead a placeholder for distributed tracing information, and ultimately gets discarded.
41
+ # Consumers of the OpenTelemetry SpanProcessor pipeline are free to flush spans whenever
42
+ # an individual span is finished.
43
+ # Currently, this closely translates to Datadog's partial flushing.
44
+ #
45
+ # @see OpenTelemetry::SDK::Trace::SpanProcessor#on_finish
46
+ Datadog.configure do |c|
47
+ c.tracing.partial_flush.enabled = true
48
+ end
@@ -1,7 +1,7 @@
1
1
  # typed: true
2
2
 
3
- require_relative '../tracing/span'
4
3
  require_relative '../tracing/distributed/datadog'
4
+ require_relative '../tracing/utils'
5
5
 
6
6
  module Datadog
7
7
  module OpenTracer
@@ -47,7 +47,7 @@ module Datadog
47
47
 
48
48
  def id(header)
49
49
  value = @carrier[header].to_i
50
- return if value.zero? || value >= Datadog::Tracing::Span::EXTERNAL_MAX_ID
50
+ return if value.zero? || value >= Datadog::Tracing::Utils::EXTERNAL_MAX_ID
51
51
 
52
52
  value < 0 ? value + 0x1_0000_0000_0000_0000 : value
53
53
  end