datadog 2.0.0.beta1 → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +181 -1
  3. data/ext/datadog_profiling_native_extension/NativeExtensionDesign.md +1 -1
  4. data/ext/datadog_profiling_native_extension/collectors_cpu_and_wall_time_worker.c +40 -32
  5. data/ext/datadog_profiling_native_extension/collectors_thread_context.c +23 -12
  6. data/ext/datadog_profiling_native_extension/crashtracker.c +108 -0
  7. data/ext/datadog_profiling_native_extension/extconf.rb +9 -23
  8. data/ext/datadog_profiling_native_extension/heap_recorder.c +81 -4
  9. data/ext/datadog_profiling_native_extension/heap_recorder.h +12 -1
  10. data/ext/datadog_profiling_native_extension/http_transport.c +1 -94
  11. data/ext/datadog_profiling_native_extension/libdatadog_helpers.c +86 -0
  12. data/ext/datadog_profiling_native_extension/libdatadog_helpers.h +4 -0
  13. data/ext/datadog_profiling_native_extension/native_extension_helpers.rb +2 -12
  14. data/ext/datadog_profiling_native_extension/private_vm_api_access.c +25 -86
  15. data/ext/datadog_profiling_native_extension/profiling.c +2 -0
  16. data/ext/datadog_profiling_native_extension/ruby_helpers.h +3 -5
  17. data/ext/datadog_profiling_native_extension/stack_recorder.c +161 -62
  18. data/lib/datadog/appsec/contrib/devise/tracking.rb +8 -0
  19. data/lib/datadog/appsec/contrib/rack/request_middleware.rb +43 -13
  20. data/lib/datadog/appsec/event.rb +2 -2
  21. data/lib/datadog/core/configuration/components.rb +2 -1
  22. data/lib/datadog/core/configuration/option.rb +7 -5
  23. data/lib/datadog/core/configuration/settings.rb +34 -79
  24. data/lib/datadog/core/configuration.rb +20 -4
  25. data/lib/datadog/core/environment/platform.rb +7 -1
  26. data/lib/datadog/core/remote/client/capabilities.rb +2 -1
  27. data/lib/datadog/core/remote/client.rb +1 -5
  28. data/lib/datadog/core/remote/configuration/repository.rb +1 -1
  29. data/lib/datadog/core/remote/dispatcher.rb +3 -3
  30. data/lib/datadog/core/remote/transport/http/config.rb +5 -5
  31. data/lib/datadog/core/telemetry/client.rb +18 -10
  32. data/lib/datadog/core/telemetry/emitter.rb +9 -13
  33. data/lib/datadog/core/telemetry/event.rb +247 -57
  34. data/lib/datadog/core/telemetry/ext.rb +1 -0
  35. data/lib/datadog/core/telemetry/heartbeat.rb +1 -3
  36. data/lib/datadog/core/telemetry/http/ext.rb +4 -1
  37. data/lib/datadog/core/telemetry/http/response.rb +4 -0
  38. data/lib/datadog/core/telemetry/http/transport.rb +9 -4
  39. data/lib/datadog/core/telemetry/request.rb +59 -0
  40. data/lib/datadog/core/utils/base64.rb +22 -0
  41. data/lib/datadog/opentelemetry/sdk/span_processor.rb +19 -2
  42. data/lib/datadog/opentelemetry/sdk/trace/span.rb +3 -17
  43. data/lib/datadog/profiling/collectors/code_provenance.rb +10 -4
  44. data/lib/datadog/profiling/collectors/cpu_and_wall_time_worker.rb +25 -0
  45. data/lib/datadog/profiling/component.rb +49 -17
  46. data/lib/datadog/profiling/crashtracker.rb +91 -0
  47. data/lib/datadog/profiling/exporter.rb +6 -3
  48. data/lib/datadog/profiling/http_transport.rb +7 -11
  49. data/lib/datadog/profiling/load_native_extension.rb +14 -1
  50. data/lib/datadog/profiling/profiler.rb +9 -2
  51. data/lib/datadog/profiling/stack_recorder.rb +6 -2
  52. data/lib/datadog/profiling.rb +12 -0
  53. data/lib/datadog/tracing/component.rb +5 -1
  54. data/lib/datadog/tracing/configuration/dynamic.rb +39 -1
  55. data/lib/datadog/tracing/configuration/settings.rb +1 -0
  56. data/lib/datadog/tracing/contrib/action_pack/integration.rb +1 -1
  57. data/lib/datadog/tracing/contrib/action_view/integration.rb +1 -1
  58. data/lib/datadog/tracing/contrib/active_record/configuration/resolver.rb +1 -0
  59. data/lib/datadog/tracing/contrib/active_record/integration.rb +11 -1
  60. data/lib/datadog/tracing/contrib/active_support/integration.rb +1 -1
  61. data/lib/datadog/tracing/contrib/configuration/resolver.rb +43 -0
  62. data/lib/datadog/tracing/contrib/grape/endpoint.rb +43 -5
  63. data/lib/datadog/tracing/contrib/trilogy/instrumentation.rb +1 -1
  64. data/lib/datadog/tracing/correlation.rb +3 -4
  65. data/lib/datadog/tracing/remote.rb +5 -1
  66. data/lib/datadog/tracing/sampling/ext.rb +5 -1
  67. data/lib/datadog/tracing/sampling/matcher.rb +75 -26
  68. data/lib/datadog/tracing/sampling/rule.rb +27 -4
  69. data/lib/datadog/tracing/sampling/rule_sampler.rb +19 -1
  70. data/lib/datadog/tracing/sampling/span/matcher.rb +13 -41
  71. data/lib/datadog/tracing/span.rb +7 -2
  72. data/lib/datadog/tracing/span_link.rb +92 -0
  73. data/lib/datadog/tracing/span_operation.rb +6 -4
  74. data/lib/datadog/tracing/trace_operation.rb +12 -0
  75. data/lib/datadog/tracing/tracer.rb +4 -3
  76. data/lib/datadog/tracing/transport/serializable_trace.rb +3 -1
  77. data/lib/datadog/tracing/utils.rb +16 -0
  78. data/lib/datadog/version.rb +1 -1
  79. metadata +10 -31
  80. data/lib/datadog/core/telemetry/collector.rb +0 -248
  81. data/lib/datadog/core/telemetry/v1/app_event.rb +0 -59
  82. data/lib/datadog/core/telemetry/v1/application.rb +0 -94
  83. data/lib/datadog/core/telemetry/v1/configuration.rb +0 -27
  84. data/lib/datadog/core/telemetry/v1/dependency.rb +0 -45
  85. data/lib/datadog/core/telemetry/v1/host.rb +0 -59
  86. data/lib/datadog/core/telemetry/v1/install_signature.rb +0 -38
  87. data/lib/datadog/core/telemetry/v1/integration.rb +0 -66
  88. data/lib/datadog/core/telemetry/v1/product.rb +0 -36
  89. data/lib/datadog/core/telemetry/v1/telemetry_request.rb +0 -108
  90. data/lib/datadog/core/telemetry/v2/app_client_configuration_change.rb +0 -41
  91. 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
@@ -34,6 +34,10 @@ module Datadog
34
34
  nil
35
35
  end
36
36
 
37
+ def code
38
+ nil
39
+ end
40
+
37
41
  def inspect
38
42
  "#{self.class} ok?:#{ok?} unsupported?:#{unsupported?}, " \
39
43
  "not_found?:#{not_found?}, client_error?:#{client_error?}, " \
@@ -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
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'trace/span'
4
+ require_relative '../../tracing/span_link'
5
+ require_relative '../../tracing/trace_digest'
4
6
 
5
7
  module Datadog
6
8
  module OpenTelemetry
@@ -87,6 +89,21 @@ module Datadog
87
89
  datadog_span.set_error([nil, span.status.description]) unless span.status.ok?
88
90
  datadog_span.set_tags(span.attributes)
89
91
 
92
+ unless span.links.nil?
93
+ datadog_span.links = span.links.map do |link|
94
+ Datadog::Tracing::SpanLink.new(
95
+ Datadog::Tracing::TraceDigest.new(
96
+ trace_id: link.span_context.hex_trace_id.to_i(16),
97
+ span_id: link.span_context.hex_span_id.to_i(16),
98
+ trace_sampling_priority: (link.span_context.trace_flags&.sampled? ? 1 : 0),
99
+ trace_state: link.span_context.tracestate&.to_s,
100
+ span_remote: link.span_context.remote?,
101
+ ),
102
+ attributes: link.attributes
103
+ )
104
+ end
105
+ end
106
+
90
107
  datadog_span
91
108
  end
92
109
 
@@ -115,11 +132,11 @@ module Datadog
115
132
 
116
133
  # DEV: There's no `flat_map!`; we have to split it into two operations
117
134
  attributes = attributes.map do |key, value|
118
- Datadog::OpenTelemetry::Trace::Span.serialize_attribute(key, value)
135
+ Datadog::Tracing::Utils.serialize_attribute(key, value)
119
136
  end
120
137
  attributes.flatten!(1)
121
138
 
122
- kwargs[:tags] = attributes
139
+ kwargs[:tags] = attributes.to_h
123
140
 
124
141
  [name, kwargs]
125
142
  end
@@ -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