datadog 2.0.0.beta1 → 2.0.0.beta2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +125 -1
  3. data/ext/datadog_profiling_native_extension/collectors_cpu_and_wall_time_worker.c +32 -12
  4. data/ext/datadog_profiling_native_extension/collectors_thread_context.c +5 -2
  5. data/ext/datadog_profiling_native_extension/heap_recorder.c +45 -3
  6. data/ext/datadog_profiling_native_extension/heap_recorder.h +7 -1
  7. data/ext/datadog_profiling_native_extension/http_transport.c +5 -5
  8. data/ext/datadog_profiling_native_extension/native_extension_helpers.rb +1 -1
  9. data/ext/datadog_profiling_native_extension/stack_recorder.c +7 -9
  10. data/lib/datadog/appsec/contrib/rack/request_middleware.rb +43 -13
  11. data/lib/datadog/appsec/event.rb +2 -2
  12. data/lib/datadog/core/configuration/components.rb +2 -1
  13. data/lib/datadog/core/configuration/option.rb +7 -5
  14. data/lib/datadog/core/configuration/settings.rb +38 -14
  15. data/lib/datadog/core/configuration.rb +20 -4
  16. data/lib/datadog/core/environment/platform.rb +7 -1
  17. data/lib/datadog/core/remote/client/capabilities.rb +2 -1
  18. data/lib/datadog/core/remote/transport/http/config.rb +5 -5
  19. data/lib/datadog/core/telemetry/client.rb +18 -10
  20. data/lib/datadog/core/telemetry/emitter.rb +9 -13
  21. data/lib/datadog/core/telemetry/event.rb +247 -57
  22. data/lib/datadog/core/telemetry/ext.rb +1 -0
  23. data/lib/datadog/core/telemetry/heartbeat.rb +1 -3
  24. data/lib/datadog/core/telemetry/http/ext.rb +4 -1
  25. data/lib/datadog/core/telemetry/http/transport.rb +9 -4
  26. data/lib/datadog/core/telemetry/request.rb +59 -0
  27. data/lib/datadog/core/utils/base64.rb +22 -0
  28. data/lib/datadog/opentelemetry/sdk/span_processor.rb +1 -1
  29. data/lib/datadog/opentelemetry/sdk/trace/span.rb +3 -17
  30. data/lib/datadog/profiling/collectors/code_provenance.rb +10 -4
  31. data/lib/datadog/profiling/collectors/cpu_and_wall_time_worker.rb +25 -0
  32. data/lib/datadog/profiling/component.rb +23 -15
  33. data/lib/datadog/profiling/load_native_extension.rb +14 -1
  34. data/lib/datadog/profiling.rb +11 -0
  35. data/lib/datadog/tracing/contrib/action_pack/integration.rb +1 -1
  36. data/lib/datadog/tracing/contrib/action_view/integration.rb +1 -1
  37. data/lib/datadog/tracing/contrib/active_record/integration.rb +1 -1
  38. data/lib/datadog/tracing/contrib/active_support/integration.rb +1 -1
  39. data/lib/datadog/tracing/contrib/grape/endpoint.rb +43 -5
  40. data/lib/datadog/tracing/correlation.rb +3 -4
  41. data/lib/datadog/tracing/sampling/matcher.rb +23 -3
  42. data/lib/datadog/tracing/sampling/rule.rb +18 -2
  43. data/lib/datadog/tracing/sampling/rule_sampler.rb +2 -0
  44. data/lib/datadog/tracing/span.rb +7 -2
  45. data/lib/datadog/tracing/span_link.rb +86 -0
  46. data/lib/datadog/tracing/trace_operation.rb +12 -0
  47. data/lib/datadog/tracing/tracer.rb +4 -3
  48. data/lib/datadog/tracing/transport/serializable_trace.rb +3 -1
  49. data/lib/datadog/tracing/utils.rb +16 -0
  50. data/lib/datadog/version.rb +1 -1
  51. metadata +7 -30
  52. data/lib/datadog/core/telemetry/collector.rb +0 -248
  53. data/lib/datadog/core/telemetry/v1/app_event.rb +0 -59
  54. data/lib/datadog/core/telemetry/v1/application.rb +0 -94
  55. data/lib/datadog/core/telemetry/v1/configuration.rb +0 -27
  56. data/lib/datadog/core/telemetry/v1/dependency.rb +0 -45
  57. data/lib/datadog/core/telemetry/v1/host.rb +0 -59
  58. data/lib/datadog/core/telemetry/v1/install_signature.rb +0 -38
  59. data/lib/datadog/core/telemetry/v1/integration.rb +0 -66
  60. data/lib/datadog/core/telemetry/v1/product.rb +0 -36
  61. data/lib/datadog/core/telemetry/v1/telemetry_request.rb +0 -108
  62. data/lib/datadog/core/telemetry/v2/app_client_configuration_change.rb +0 -41
  63. data/lib/datadog/core/telemetry/v2/request.rb +0 -29
@@ -1,81 +1,271 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'collector'
4
- require_relative 'v1/app_event'
5
- require_relative 'v1/telemetry_request'
6
- require_relative 'v2/app_client_configuration_change'
7
-
8
3
  module Datadog
9
4
  module Core
10
5
  module Telemetry
11
- # Class defining methods to construct a Telemetry event
12
6
  class Event
13
- include Telemetry::Collector
7
+ # Base class for all Telemetry V2 events.
8
+ class Base
9
+ # The type of the event.
10
+ # It must be one of the stings defined in the Telemetry V2
11
+ # specification for event names.
12
+ def type; end
13
+
14
+ # The JSON payload for the event.
15
+ # @param seq_id [Integer] The sequence ID for the event.
16
+ def payload(seq_id)
17
+ {}
18
+ end
19
+ end
20
+
21
+ # Telemetry class for the 'app-started' event
22
+ class AppStarted < Base
23
+ def type
24
+ 'app-started'
25
+ end
26
+
27
+ def payload(seq_id)
28
+ @seq_id = seq_id
29
+ {
30
+ products: products,
31
+ configuration: configuration,
32
+ install_signature: install_signature,
33
+ # DEV: Not implemented yet
34
+ # error: error, # Start-up errors
35
+ }
36
+ end
37
+
38
+ private
39
+
40
+ def products
41
+ products = {
42
+ appsec: {
43
+ enabled: Datadog::AppSec.enabled?,
44
+ },
45
+ profiler: {
46
+ enabled: Datadog::Profiling.enabled?,
47
+ },
48
+ # DEV: Not implemented yet
49
+ # dynamic_instrumentation: {
50
+ # enabled: true,
51
+ # }
52
+ }
53
+
54
+ if (unsupported_reason = Datadog::Profiling.unsupported_reason)
55
+ products[:profiler][:error] = {
56
+ code: 1, # Error code. 0 if no error.
57
+ message: unsupported_reason,
58
+ }
59
+ end
60
+
61
+ products
62
+ end
63
+
64
+ TARGET_OPTIONS = %w[
65
+ logger.level
66
+ profiling.advanced.code_provenance_enabled
67
+ profiling.advanced.endpoint.collection.enabled
68
+ profiling.enabled
69
+ runtime_metrics.enabled
70
+ tracing.analytics.enabled
71
+ tracing.propagation_style_extract
72
+ tracing.propagation_style_inject
73
+ tracing.enabled
74
+ tracing.log_injection
75
+ tracing.partial_flush.enabled
76
+ tracing.partial_flush.min_spans_threshold
77
+ tracing.report_hostname
78
+ tracing.sampling.rate_limit
79
+ ].freeze
80
+
81
+ # rubocop:disable Metrics/AbcSize
82
+ def configuration
83
+ config = Datadog.configuration
84
+
85
+ list = [
86
+ conf_value('DD_AGENT_HOST', config.agent.host),
87
+ conf_value('DD_AGENT_TRANSPORT', agent_transport(config)),
88
+ conf_value('DD_TRACE_SAMPLE_RATE', to_value(config.tracing.sampling.default_rate)),
89
+ conf_value(
90
+ 'DD_TRACE_REMOVE_INTEGRATION_SERVICE_NAMES_ENABLED',
91
+ config.tracing.contrib.global_default_service_name.enabled
92
+ ),
93
+ ]
14
94
 
15
- API_VERSION = 'v1'
95
+ peer_service_mapping_str = ''
96
+ unless config.tracing.contrib.peer_service_mapping.empty?
97
+ peer_service_mapping = config.tracing.contrib.peer_service_mapping
98
+ peer_service_mapping_str = peer_service_mapping.map { |key, value| "#{key}:#{value}" }.join(',')
99
+ end
100
+ list << conf_value('DD_TRACE_PEER_SERVICE_MAPPING', peer_service_mapping_str)
16
101
 
17
- attr_reader \
18
- :api_version
102
+ # Whitelist of configuration options to send in additional payload object
103
+ TARGET_OPTIONS.each do |option|
104
+ split_option = option.split('.')
105
+ list << conf_value(option, to_value(config.dig(*split_option)))
106
+ end
107
+
108
+ # Add some more custom additional payload values here
109
+ list.push(
110
+ conf_value('tracing.auto_instrument.enabled', !defined?(Datadog::AutoInstrument::LOADED).nil?),
111
+ conf_value('tracing.writer_options.buffer_size', to_value(config.tracing.writer_options[:buffer_size])),
112
+ conf_value('tracing.writer_options.flush_interval', to_value(config.tracing.writer_options[:flush_interval])),
113
+ conf_value('tracing.opentelemetry.enabled', !defined?(Datadog::OpenTelemetry::LOADED).nil?),
114
+ )
115
+ list << conf_value('logger.instance', config.logger.instance.class.to_s) if config.logger.instance
116
+ list << conf_value('appsec.enabled', config.dig('appsec', 'enabled')) if config.respond_to?('appsec')
117
+ list << conf_value('ci.enabled', config.dig('ci', 'enabled')) if config.respond_to?('ci')
118
+
119
+ list.reject! { |entry| entry[:value].nil? }
120
+ list
121
+ end
122
+ # rubocop:enable Metrics/AbcSize
123
+
124
+ def agent_transport(config)
125
+ adapter = Core::Configuration::AgentSettingsResolver.call(config).adapter
126
+ if adapter == Datadog::Core::Transport::Ext::UnixSocket::ADAPTER
127
+ 'UDS'
128
+ else
129
+ 'TCP'
130
+ end
131
+ end
132
+
133
+ def conf_value(name, value, origin = 'code')
134
+ {
135
+ name: name,
136
+ value: value,
137
+ origin: origin,
138
+ seq_id: @seq_id,
139
+ }
140
+ end
141
+
142
+ def to_value(value)
143
+ # TODO: Add float if telemetry starts accepting it
144
+ case value
145
+ when Integer, String, true, false, nil
146
+ value
147
+ else
148
+ value.to_s
149
+ end
150
+ end
19
151
 
20
- def initialize
21
- @api_version = API_VERSION
152
+ def install_signature
153
+ config = Datadog.configuration
154
+ {
155
+ install_id: config.dig('telemetry', 'install_id'),
156
+ install_type: config.dig('telemetry', 'install_type'),
157
+ install_time: config.dig('telemetry', 'install_time'),
158
+ }
159
+ end
22
160
  end
23
161
 
24
- # Forms a TelemetryRequest object based on the event request_type
25
- # @param request_type [String] the type of telemetry request to collect data for
26
- # @param seq_id [Integer] the ID of the request; incremented each time a telemetry request is sent to the API
27
- # @param data [Object] arbitrary object to be passed to the respective `request_type` handler
28
- def telemetry_request(request_type:, seq_id:, data: nil)
29
- Telemetry::V1::TelemetryRequest.new(
30
- api_version: @api_version,
31
- application: application,
32
- host: host,
33
- payload: payload(request_type, data),
34
- request_type: request_type,
35
- runtime_id: runtime_id,
36
- seq_id: seq_id,
37
- tracer_time: tracer_time,
38
- )
162
+ # Telemetry class for the 'app-dependencies-loaded' event
163
+ class AppDependenciesLoaded < Base
164
+ def type
165
+ 'app-dependencies-loaded'
166
+ end
167
+
168
+ def payload(seq_id)
169
+ { dependencies: dependencies }
170
+ end
171
+
172
+ private
173
+
174
+ def dependencies
175
+ Gem.loaded_specs.collect do |name, gem|
176
+ {
177
+ name: name,
178
+ version: gem.version.to_s,
179
+ # hash: nil,
180
+ }
181
+ end
182
+ end
39
183
  end
40
184
 
41
- private
185
+ # Telemetry class for the 'app-integrations-change' event
186
+ class AppIntegrationsChange < Base
187
+ def type
188
+ 'app-integrations-change'
189
+ end
42
190
 
43
- def payload(request_type, data)
44
- case request_type
45
- when :'app-started'
46
- app_started
47
- when :'app-closing', :'app-heartbeat'
48
- {}
49
- when :'app-integrations-change'
50
- app_integrations_change
51
- when 'app-client-configuration-change'
52
- app_client_configuration_change(data)
53
- else
54
- raise ArgumentError, "Request type invalid, received request_type: #{@request_type}"
191
+ def payload(seq_id)
192
+ { integrations: integrations }
193
+ end
194
+
195
+ private
196
+
197
+ def integrations
198
+ instrumented_integrations = Datadog.configuration.tracing.instrumented_integrations
199
+ Datadog.registry.map do |integration|
200
+ is_instrumented = instrumented_integrations.include?(integration.name)
201
+
202
+ is_enabled = is_instrumented ? !!integration.klass.patcher.patch_successful : false
203
+
204
+ version = integration.klass.class.version ? integration.klass.class.version.to_s : nil
205
+
206
+ res = {
207
+ name: integration.name.to_s,
208
+ enabled: is_enabled,
209
+ version: version,
210
+ compatible: integration.klass.class.compatible?,
211
+ error: (patch_error(integration) if is_instrumented && !is_enabled),
212
+ # TODO: Track if integration is instrumented by manual configuration or by auto instrumentation
213
+ # auto_enabled: is_enabled && ???,
214
+ }
215
+ res.reject! { |_, v| v.nil? }
216
+ res
217
+ end
218
+ end
219
+
220
+ def patch_error(integration)
221
+ patch_error_result = integration.klass.patcher.patch_error_result
222
+ return patch_error_result.compact.to_s if patch_error_result
223
+
224
+ # If no error occurred during patching, but integration is still not instrumented
225
+ "Available?: #{integration.klass.class.available?}" \
226
+ ", Loaded? #{integration.klass.class.loaded?}" \
227
+ ", Compatible? #{integration.klass.class.compatible?}" \
228
+ ", Patchable? #{integration.klass.class.patchable?}"
55
229
  end
56
230
  end
57
231
 
58
- def app_started
59
- Telemetry::V1::AppEvent.new(
60
- dependencies: dependencies,
61
- integrations: integrations,
62
- configuration: configurations,
63
- additional_payload: additional_payload,
64
- install_signature: install_signature
65
- )
232
+ # Telemetry class for the 'app-client-configuration-change' event
233
+ class AppClientConfigurationChange < Base
234
+ def type
235
+ 'app-client-configuration-change'
236
+ end
237
+
238
+ def initialize(changes, origin)
239
+ super()
240
+ @changes = changes
241
+ @origin = origin
242
+ end
243
+
244
+ def payload(seq_id)
245
+ {
246
+ configuration: @changes.map do |name, value|
247
+ {
248
+ name: name,
249
+ value: value,
250
+ origin: @origin,
251
+ }
252
+ end
253
+ }
254
+ end
66
255
  end
67
256
 
68
- def app_integrations_change
69
- Telemetry::V1::AppEvent.new(integrations: integrations)
257
+ # Telemetry class for the 'app-heartbeat' event
258
+ class AppHeartbeat < Base
259
+ def type
260
+ 'app-heartbeat'
261
+ end
70
262
  end
71
263
 
72
- # DEV: During the transition from V1 to V2, the backend accepts many V2
73
- # DEV: payloads through the V1 transport protocol.
74
- # DEV: The `app-client-configuration-change` payload is one of them.
75
- # DEV: Once V2 is fully implemented, `Telemetry::V2::AppClientConfigurationChange`
76
- # DEV: should be reusable without major modifications.
77
- def app_client_configuration_change(data)
78
- Telemetry::V2::AppClientConfigurationChange.new(data[:changes], origin: data[:origin])
264
+ # Telemetry class for the 'app-closing' event
265
+ class AppClosing < Base
266
+ def type
267
+ 'app-closing'
268
+ end
79
269
  end
80
270
  end
81
271
  end
@@ -6,6 +6,7 @@ module Datadog
6
6
  module Ext
7
7
  ENV_ENABLED = 'DD_INSTRUMENTATION_TELEMETRY_ENABLED'
8
8
  ENV_HEARTBEAT_INTERVAL = 'DD_TELEMETRY_HEARTBEAT_INTERVAL'
9
+ ENV_DEPENDENCY_COLLECTION = 'DD_TELEMETRY_DEPENDENCY_COLLECTION_ENABLED'
9
10
  ENV_INSTALL_ID = 'DD_INSTRUMENTATION_INSTALL_ID'
10
11
  ENV_INSTALL_TYPE = 'DD_INSTRUMENTATION_INSTALL_TYPE'
11
12
  ENV_INSTALL_TIME = 'DD_INSTRUMENTATION_INSTALL_TIME'
@@ -20,9 +20,7 @@ module Datadog
20
20
  start
21
21
  end
22
22
 
23
- def loop_wait_before_first_iteration?
24
- true
25
- end
23
+ def loop_wait_before_first_iteration?; end
26
24
 
27
25
  private
28
26
 
@@ -10,9 +10,12 @@ module Datadog
10
10
  HEADER_CONTENT_LENGTH = 'Content-Length'
11
11
  HEADER_DD_TELEMETRY_API_VERSION = 'DD-Telemetry-API-Version'
12
12
  HEADER_DD_TELEMETRY_REQUEST_TYPE = 'DD-Telemetry-Request-Type'
13
+ HEADER_TELEMETRY_DEBUG_ENABLED = 'DD-Telemetry-Debug-Enabled'
14
+ HEADER_CLIENT_LIBRARY_LANGUAGE = 'DD-Client-Library-Language'
15
+ HEADER_CLIENT_LIBRARY_VERSION = 'DD-Client-Library-Version'
13
16
 
14
17
  CONTENT_TYPE_APPLICATION_JSON = 'application/json'
15
- API_VERSION = 'v1'
18
+ API_VERSION = 'v2'
16
19
 
17
20
  AGENT_ENDPOINT = '/telemetry/proxy/api/v2/apmtelemetry'
18
21
  end
@@ -39,10 +39,15 @@ module Datadog
39
39
 
40
40
  def headers(request_type:, api_version: Http::Ext::API_VERSION)
41
41
  {
42
- Datadog::Core::Transport::Ext::HTTP::HEADER_DD_INTERNAL_UNTRACED_REQUEST => '1',
43
- Http::Ext::HEADER_CONTENT_TYPE => Http::Ext::CONTENT_TYPE_APPLICATION_JSON,
44
- Http::Ext::HEADER_DD_TELEMETRY_API_VERSION => api_version,
45
- Http::Ext::HEADER_DD_TELEMETRY_REQUEST_TYPE => request_type,
42
+ Core::Transport::Ext::HTTP::HEADER_DD_INTERNAL_UNTRACED_REQUEST => '1',
43
+ Ext::HEADER_CONTENT_TYPE => Http::Ext::CONTENT_TYPE_APPLICATION_JSON,
44
+ Ext::HEADER_DD_TELEMETRY_API_VERSION => api_version,
45
+ Ext::HEADER_DD_TELEMETRY_REQUEST_TYPE => request_type,
46
+ Ext::HEADER_CLIENT_LIBRARY_LANGUAGE => Core::Environment::Ext::LANG,
47
+ Ext::HEADER_CLIENT_LIBRARY_VERSION => Core::Environment::Identity.gem_datadog_version_semver2,
48
+
49
+ # Enable debug mode for telemetry
50
+ # HEADER_TELEMETRY_DEBUG_ENABLED => 'true',
46
51
  }
47
52
  end
48
53
 
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../environment/platform'
4
+ require_relative '../utils/hash'
5
+
6
+ module Datadog
7
+ module Core
8
+ module Telemetry
9
+ # Module defining methods for collecting metadata for telemetry
10
+ module Request
11
+ class << self
12
+ using Core::Utils::Hash::Refinement
13
+
14
+ def build_payload(event, seq_id)
15
+ hash = {
16
+ api_version: Http::Ext::API_VERSION,
17
+ application: application,
18
+ debug: false,
19
+ host: host,
20
+ payload: event.payload(seq_id),
21
+ request_type: event.type,
22
+ runtime_id: Core::Environment::Identity.id,
23
+ seq_id: seq_id,
24
+ tracer_time: Time.now.to_i,
25
+ }
26
+ hash.compact!
27
+ hash
28
+ end
29
+
30
+ private
31
+
32
+ def application
33
+ config = Datadog.configuration
34
+ {
35
+ env: config.env,
36
+ language_name: Core::Environment::Ext::LANG,
37
+ language_version: Core::Environment::Ext::LANG_VERSION,
38
+ runtime_name: Core::Environment::Ext::RUBY_ENGINE,
39
+ runtime_version: Core::Environment::Ext::ENGINE_VERSION,
40
+ service_name: config.service,
41
+ service_version: config.version,
42
+ tracer_version: Core::Environment::Identity.gem_datadog_version_semver2
43
+ }
44
+ end
45
+
46
+ def host
47
+ {
48
+ architecture: Core::Environment::Platform.architecture,
49
+ hostname: Core::Environment::Platform.hostname,
50
+ kernel_name: Core::Environment::Platform.kernel_name,
51
+ kernel_release: Core::Environment::Platform.kernel_release,
52
+ kernel_version: Core::Environment::Platform.kernel_version
53
+ }
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datadog
4
+ module Core
5
+ module Utils
6
+ # Helper methods for encoding and decoding base64
7
+ module Base64
8
+ def self.encode64(bin)
9
+ [bin].pack('m')
10
+ end
11
+
12
+ def self.strict_encode64(bin)
13
+ [bin].pack('m0')
14
+ end
15
+
16
+ def self.strict_decode64(str)
17
+ str.unpack1('m0')
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -115,7 +115,7 @@ module Datadog
115
115
 
116
116
  # DEV: There's no `flat_map!`; we have to split it into two operations
117
117
  attributes = attributes.map do |key, value|
118
- Datadog::OpenTelemetry::Trace::Span.serialize_attribute(key, value)
118
+ Datadog::Tracing::Utils.serialize_attribute(key, value)
119
119
  end
120
120
  attributes.flatten!(1)
121
121
 
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'datadog/tracing/utils'
4
+
3
5
  module Datadog
4
6
  module OpenTelemetry
5
7
  module Trace
@@ -36,22 +38,6 @@ module Datadog
36
38
  span.set_error(status.description) if status && status.code == ::OpenTelemetry::Trace::Status::ERROR
37
39
  end
38
40
 
39
- # Serialize values into Datadog span tags and metrics.
40
- # Notably, arrays are exploded into many keys, each with
41
- # a numeric suffix representing the array index, for example:
42
- # `'foo' => ['a','b']` becomes `'foo.0' => 'a', 'foo.1' => 'b'`
43
- def self.serialize_attribute(key, value)
44
- if value.is_a?(Array)
45
- value.flat_map.with_index do |v, idx|
46
- serialize_attribute("#{key}.#{idx}", v)
47
- end
48
- elsif value.is_a?(TrueClass) || value.is_a?(FalseClass)
49
- [[key, value.to_s]]
50
- else
51
- [[key, value]]
52
- end
53
- end
54
-
55
41
  # Create a meaningful Datadog operation name from the OpenTelemetry
56
42
  # semantic convention for span kind and span attributes.
57
43
  # @see https://opentelemetry.io/docs/specs/semconv/general/trace/
@@ -123,7 +109,7 @@ module Datadog
123
109
  span.name = rich_name.downcase
124
110
  end
125
111
 
126
- Span.serialize_attribute(key, @attributes[key]).each do |new_key, value|
112
+ Tracing::Utils.serialize_attribute(key, @attributes[key]).each do |new_key, value|
127
113
  override_datadog_values(span, new_key, value)
128
114
 
129
115
  # When an attribute is used to override a Datadog Span property,
@@ -97,14 +97,20 @@ module Datadog
97
97
  end
98
98
  end
99
99
 
100
- Library = Struct.new(:kind, :name, :version, :path) do
100
+ # Represents metadata we have for a ruby gem
101
+ class Library
102
+ attr_reader :kind, :name, :version, :path
103
+
101
104
  def initialize(kind:, name:, version:, path:)
102
- super(kind.freeze, name.dup.freeze, version.to_s.dup.freeze, path.dup.freeze)
105
+ @kind = kind.freeze
106
+ @name = name.dup.freeze
107
+ @version = version.to_s.dup.freeze
108
+ @path = path.dup.freeze
103
109
  freeze
104
110
  end
105
111
 
106
- def to_json(*args)
107
- { kind: kind, name: name, version: version, paths: [path] }.to_json(*args)
112
+ def to_json(arg = nil)
113
+ { kind: @kind, name: @name, version: @version, paths: [@path] }.to_json(arg)
108
114
  end
109
115
  end
110
116
  end
@@ -24,6 +24,7 @@ module Datadog
24
24
  # **NOTE**: This should only be used for testing; disabling the dynamic sampling rate will increase the
25
25
  # profiler overhead!
26
26
  dynamic_sampling_rate_enabled: true,
27
+ skip_idle_samples_for_testing: false,
27
28
  idle_sampling_helper: IdleSamplingHelper.new
28
29
  )
29
30
  unless dynamic_sampling_rate_enabled
@@ -41,11 +42,14 @@ module Datadog
41
42
  dynamic_sampling_rate_enabled,
42
43
  dynamic_sampling_rate_overhead_target_percentage,
43
44
  allocation_profiling_enabled,
45
+ skip_idle_samples_for_testing,
44
46
  )
45
47
  @worker_thread = nil
46
48
  @failure_exception = nil
47
49
  @start_stop_mutex = Mutex.new
48
50
  @idle_sampling_helper = idle_sampling_helper
51
+ @wait_until_running_mutex = Mutex.new
52
+ @wait_until_running_condition = ConditionVariable.new
49
53
  end
50
54
 
51
55
  def start(on_failure_proc: nil)
@@ -108,6 +112,27 @@ module Datadog
108
112
  self.class._native_stats_reset_not_thread_safe(self)
109
113
  stats
110
114
  end
115
+
116
+ # Useful for testing, to e.g. make sure the profiler is running before we start running some code we want to observe
117
+ def wait_until_running(timeout_seconds: 5)
118
+ @wait_until_running_mutex.synchronize do
119
+ return true if self.class._native_is_running?(self)
120
+
121
+ @wait_until_running_condition.wait(@wait_until_running_mutex, timeout_seconds)
122
+
123
+ if self.class._native_is_running?(self)
124
+ true
125
+ else
126
+ raise "Timeout waiting for #{self.class.name} to start (waited for #{timeout_seconds} seconds)"
127
+ end
128
+ end
129
+ end
130
+
131
+ private
132
+
133
+ def signal_running
134
+ @wait_until_running_mutex.synchronize { @wait_until_running_condition.broadcast }
135
+ end
111
136
  end
112
137
  end
113
138
  end
@@ -111,19 +111,30 @@ module Datadog
111
111
  end
112
112
 
113
113
  private_class_method def self.enable_gc_profiling?(settings)
114
- # See comments on the setting definition for more context on why it exists.
115
- if settings.profiling.advanced.force_enable_gc_profiling
116
- if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3')
117
- Datadog.logger.debug(
118
- 'Profiling time/resources spent in Garbage Collection force enabled. Do not use Ractors in combination ' \
119
- 'with this option as profiles will be incomplete.'
120
- )
121
- end
114
+ return false unless settings.profiling.advanced.gc_enabled
122
115
 
123
- true
124
- else
125
- false
116
+ # SEVERE - Only with Ractors
117
+ # On Ruby versions 3.0 (all), 3.1.0 to 3.1.3, and 3.2.0 to 3.2.2 gc profiling can trigger a VM bug
118
+ # that causes a segmentation fault during garbage collection of Ractors
119
+ # (https://bugs.ruby-lang.org/issues/18464). We don't allow enabling gc profiling on such Rubies.
120
+ # This bug is fixed on Ruby versions 3.1.4, 3.2.3 and 3.3.0.
121
+ if RUBY_VERSION.start_with?('3.0.') ||
122
+ (RUBY_VERSION.start_with?('3.1.') && RUBY_VERSION < '3.1.4') ||
123
+ (RUBY_VERSION.start_with?('3.2.') && RUBY_VERSION < '3.2.3')
124
+ Datadog.logger.warn(
125
+ "Current Ruby version (#{RUBY_VERSION}) has a VM bug where enabling GC profiling would cause "\
126
+ 'crashes (https://bugs.ruby-lang.org/issues/18464). GC profiling has been disabled.'
127
+ )
128
+ return false
129
+ elsif RUBY_VERSION.start_with?('3.')
130
+ Datadog.logger.debug(
131
+ 'In all known versions of Ruby 3.x, using Ractors may result in GC profiling unexpectedly ' \
132
+ 'stopping (https://bugs.ruby-lang.org/issues/19112). Note that this stop has no impact in your ' \
133
+ 'application stability or performance. This does not happen if Ractors are not used.'
134
+ )
126
135
  end
136
+
137
+ true
127
138
  end
128
139
 
129
140
  private_class_method def self.get_heap_sample_every(settings)
@@ -135,10 +146,7 @@ module Datadog
135
146
  end
136
147
 
137
148
  private_class_method def self.enable_allocation_profiling?(settings)
138
- unless settings.profiling.allocation_enabled
139
- # Allocation profiling disabled, short-circuit out
140
- return false
141
- end
149
+ return false unless settings.profiling.allocation_enabled
142
150
 
143
151
  # Allocation sampling is safe and supported on Ruby 2.x, but has a few caveats on Ruby 3.x.
144
152
 
@@ -20,7 +20,20 @@ rescue LoadError => e
20
20
  end
21
21
 
22
22
  extension_name = "datadog_profiling_native_extension.#{RUBY_VERSION}_#{RUBY_PLATFORM}"
23
- full_file_path = "#{__dir__}/../../#{extension_name}.#{RbConfig::CONFIG['DLEXT']}"
23
+ file_name = "#{extension_name}.#{RbConfig::CONFIG['DLEXT']}"
24
+ full_file_path = "#{__dir__}/../../#{file_name}"
25
+
26
+ unless File.exist?(full_file_path)
27
+ extension_dir = Gem.loaded_specs['datadog'].extension_dir
28
+ candidate_path = "#{extension_dir}/#{file_name}"
29
+ if File.exist?(candidate_path)
30
+ full_file_path = candidate_path
31
+ else # rubocop:disable Style/EmptyElse
32
+ # We found none of the files. This is unexpected. Let's go ahead anyway, the error is going to be reported further
33
+ # down anyway.
34
+ end
35
+ end
36
+
24
37
  init_function_name = "Init_#{extension_name.split('.').first}"
25
38
 
26
39
  status, result = Datadog::Profiling::Loader._native_load(full_file_path, init_function_name)