ddtrace 1.21.1 → 1.22.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +39 -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 +1 -1
  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 -17
  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 +1 -1
  18. data/lib/datadog/core/remote/transport/http/config.rb +1 -1
  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/profiling/collectors/code_provenance.rb +10 -4
  28. data/lib/datadog/profiling/collectors/cpu_and_wall_time_worker.rb +25 -0
  29. data/lib/datadog/profiling/component.rb +23 -15
  30. data/lib/datadog/profiling/load_native_extension.rb +14 -1
  31. data/lib/datadog/profiling.rb +11 -0
  32. data/lib/datadog/tracing/sampling/matcher.rb +23 -3
  33. data/lib/datadog/tracing/sampling/rule.rb +7 -2
  34. data/lib/datadog/tracing/sampling/rule_sampler.rb +2 -0
  35. data/lib/ddtrace/version.rb +2 -2
  36. metadata +6 -17
  37. data/lib/datadog/core/telemetry/collector.rb +0 -250
  38. data/lib/datadog/core/telemetry/v1/app_event.rb +0 -59
  39. data/lib/datadog/core/telemetry/v1/application.rb +0 -92
  40. data/lib/datadog/core/telemetry/v1/configuration.rb +0 -25
  41. data/lib/datadog/core/telemetry/v1/dependency.rb +0 -43
  42. data/lib/datadog/core/telemetry/v1/host.rb +0 -59
  43. data/lib/datadog/core/telemetry/v1/install_signature.rb +0 -38
  44. data/lib/datadog/core/telemetry/v1/integration.rb +0 -64
  45. data/lib/datadog/core/telemetry/v1/product.rb +0 -36
  46. data/lib/datadog/core/telemetry/v1/telemetry_request.rb +0 -106
  47. data/lib/datadog/core/telemetry/v2/app_client_configuration_change.rb +0 -41
  48. data/lib/datadog/core/telemetry/v2/request.rb +0 -29
@@ -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 => DDTrace::VERSION::STRING,
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.tracer_version
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
@@ -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
@@ -22,6 +22,7 @@ module Datadog
22
22
  # **NOTE**: This should only be used for testing; disabling the dynamic sampling rate will increase the
23
23
  # profiler overhead!
24
24
  dynamic_sampling_rate_enabled: true,
25
+ skip_idle_samples_for_testing: false,
25
26
  idle_sampling_helper: IdleSamplingHelper.new
26
27
  )
27
28
  unless dynamic_sampling_rate_enabled
@@ -39,11 +40,14 @@ module Datadog
39
40
  dynamic_sampling_rate_enabled,
40
41
  dynamic_sampling_rate_overhead_target_percentage,
41
42
  allocation_profiling_enabled,
43
+ skip_idle_samples_for_testing,
42
44
  )
43
45
  @worker_thread = nil
44
46
  @failure_exception = nil
45
47
  @start_stop_mutex = Mutex.new
46
48
  @idle_sampling_helper = idle_sampling_helper
49
+ @wait_until_running_mutex = Mutex.new
50
+ @wait_until_running_condition = ConditionVariable.new
47
51
  end
48
52
 
49
53
  def start(on_failure_proc: nil)
@@ -106,6 +110,27 @@ module Datadog
106
110
  self.class._native_stats_reset_not_thread_safe(self)
107
111
  stats
108
112
  end
113
+
114
+ # Useful for testing, to e.g. make sure the profiler is running before we start running some code we want to observe
115
+ def wait_until_running(timeout_seconds: 5)
116
+ @wait_until_running_mutex.synchronize do
117
+ return true if self.class._native_is_running?(self)
118
+
119
+ @wait_until_running_condition.wait(@wait_until_running_mutex, timeout_seconds)
120
+
121
+ if self.class._native_is_running?(self)
122
+ true
123
+ else
124
+ raise "Timeout waiting for #{self.class.name} to start (waited for #{timeout_seconds} seconds)"
125
+ end
126
+ end
127
+ end
128
+
129
+ private
130
+
131
+ def signal_running
132
+ @wait_until_running_mutex.synchronize { @wait_until_running_condition.broadcast }
133
+ end
109
134
  end
110
135
  end
111
136
  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
 
@@ -18,7 +18,20 @@ rescue LoadError => e
18
18
  end
19
19
 
20
20
  extension_name = "datadog_profiling_native_extension.#{RUBY_VERSION}_#{RUBY_PLATFORM}"
21
- full_file_path = "#{__dir__}/../../#{extension_name}.#{RbConfig::CONFIG['DLEXT']}"
21
+ file_name = "#{extension_name}.#{RbConfig::CONFIG['DLEXT']}"
22
+ full_file_path = "#{__dir__}/../../#{file_name}"
23
+
24
+ unless File.exist?(full_file_path)
25
+ extension_dir = Gem.loaded_specs['ddtrace'].extension_dir
26
+ candidate_path = "#{extension_dir}/#{file_name}"
27
+ if File.exist?(candidate_path)
28
+ full_file_path = candidate_path
29
+ else # rubocop:disable Style/EmptyElse
30
+ # We found none of the files. This is unexpected. Let's go ahead anyway, the error is going to be reported further
31
+ # down anyway.
32
+ end
33
+ end
34
+
22
35
  init_function_name = "Init_#{extension_name.split('.').first}"
23
36
 
24
37
  status, result = Datadog::Profiling::Loader._native_load(full_file_path, init_function_name)
@@ -63,6 +63,17 @@ module Datadog
63
63
  !!(profiler.send(:scheduler).running? if profiler)
64
64
  end
65
65
 
66
+ def self.wait_until_running(timeout_seconds: 5)
67
+ profiler = Datadog.send(:components).profiler
68
+ if profiler
69
+ # Use .send(...) to avoid exposing the attr_reader as an API to the outside
70
+ worker = profiler.send(:worker)
71
+ worker.wait_until_running(timeout_seconds: timeout_seconds)
72
+ else
73
+ raise 'Profiler not enabled or available'
74
+ end
75
+ end
76
+
66
77
  private_class_method def self.replace_noop_allocation_count
67
78
  def self.allocation_count # rubocop:disable Lint/NestedMethodDefinition (On purpose!)
68
79
  Datadog::Profiling::Collectors::CpuAndWallTimeWorker._native_allocation_count
@@ -29,20 +29,40 @@ module Datadog
29
29
  end
30
30
  end.new
31
31
 
32
- attr_reader :name, :service
32
+ attr_reader :name, :service, :resource, :tags
33
33
 
34
34
  # @param name [String,Regexp,Proc] Matcher for case equality (===) with the trace name,
35
35
  # defaults to always match
36
36
  # @param service [String,Regexp,Proc] Matcher for case equality (===) with the service name,
37
37
  # defaults to always match
38
- def initialize(name: MATCH_ALL, service: MATCH_ALL)
38
+ # @param resource [String,Regexp,Proc] Matcher for case equality (===) with the resource name,
39
+ # defaults to always match
40
+ def initialize(name: MATCH_ALL, service: MATCH_ALL, resource: MATCH_ALL, tags: {})
39
41
  super()
40
42
  @name = name
41
43
  @service = service
44
+ @resource = resource
45
+ @tags = tags
42
46
  end
43
47
 
44
48
  def match?(trace)
45
- name === trace.name && service === trace.service
49
+ name === trace.name && service === trace.service && resource === trace.resource && tags_match?(trace)
50
+ end
51
+
52
+ private
53
+
54
+ # Match against the trace tags and metrics.
55
+ def tags_match?(trace)
56
+ @tags.all? do |name, matcher|
57
+ tag = trace.get_tag(name)
58
+
59
+ # Format metrics as strings, to allow for partial number matching (/4.*/ matching '400', '404', etc.).
60
+ # Because metrics are floats, we use the '%g' format specifier to avoid trailing zeros, which
61
+ # can affect exact string matching (e.g. '400' matching '400.0').
62
+ tag = format('%g', tag) if tag.is_a?(Numeric)
63
+
64
+ matcher === tag
65
+ end
46
66
  end
47
67
  end
48
68
 
@@ -51,8 +51,13 @@ module Datadog
51
51
  # @param name [String,Regexp,Proc] Matcher for case equality (===) with the trace name, defaults to always match
52
52
  # @param service [String,Regexp,Proc] Matcher for case equality (===) with the service name,
53
53
  # defaults to always match
54
+ # @param resource [String,Regexp,Proc] Matcher for case equality (===) with the resource name,
55
+ # defaults to always match
54
56
  # @param sample_rate [Float] Sampling rate between +[0,1]+
55
- def initialize(name: SimpleMatcher::MATCH_ALL, service: SimpleMatcher::MATCH_ALL, sample_rate: 1.0)
57
+ def initialize(
58
+ name: SimpleMatcher::MATCH_ALL, service: SimpleMatcher::MATCH_ALL,
59
+ resource: SimpleMatcher::MATCH_ALL, tags: {}, sample_rate: 1.0
60
+ )
56
61
  # We want to allow 0.0 to drop all traces, but {Datadog::Tracing::Sampling::RateSampler}
57
62
  # considers 0.0 an invalid rate and falls back to 100% sampling.
58
63
  #
@@ -64,7 +69,7 @@ module Datadog
64
69
  sampler = RateSampler.new
65
70
  sampler.sample_rate = sample_rate
66
71
 
67
- super(SimpleMatcher.new(name: name, service: service), sampler)
72
+ super(SimpleMatcher.new(name: name, service: service, resource: resource, tags: tags), sampler)
68
73
  end
69
74
  end
70
75
  end
@@ -61,6 +61,8 @@ module Datadog
61
61
  kwargs = {
62
62
  name: rule['name'],
63
63
  service: rule['service'],
64
+ resource: rule['resource'],
65
+ tags: rule['tags'],
64
66
  sample_rate: sample_rate,
65
67
  }
66
68
 
@@ -3,8 +3,8 @@
3
3
  module DDTrace
4
4
  module VERSION
5
5
  MAJOR = 1
6
- MINOR = 21
7
- PATCH = 1
6
+ MINOR = 22
7
+ PATCH = 0
8
8
  PRE = nil
9
9
  BUILD = nil
10
10
  # PRE and BUILD above are modified for dev gems during gem build GHA workflow
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ddtrace
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.21.1
4
+ version: 1.22.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Datadog, Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-03-20 00:00:00.000000000 Z
11
+ date: 2024-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: msgpack
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: 6.0.0.2.0
61
+ version: 7.0.0.1.0
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: 6.0.0.2.0
68
+ version: 7.0.0.1.0
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: datadog-ci
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -296,7 +296,6 @@ files:
296
296
  - lib/datadog/core/runtime/ext.rb
297
297
  - lib/datadog/core/runtime/metrics.rb
298
298
  - lib/datadog/core/telemetry/client.rb
299
- - lib/datadog/core/telemetry/collector.rb
300
299
  - lib/datadog/core/telemetry/emitter.rb
301
300
  - lib/datadog/core/telemetry/event.rb
302
301
  - lib/datadog/core/telemetry/ext.rb
@@ -306,17 +305,7 @@ files:
306
305
  - lib/datadog/core/telemetry/http/ext.rb
307
306
  - lib/datadog/core/telemetry/http/response.rb
308
307
  - lib/datadog/core/telemetry/http/transport.rb
309
- - lib/datadog/core/telemetry/v1/app_event.rb
310
- - lib/datadog/core/telemetry/v1/application.rb
311
- - lib/datadog/core/telemetry/v1/configuration.rb
312
- - lib/datadog/core/telemetry/v1/dependency.rb
313
- - lib/datadog/core/telemetry/v1/host.rb
314
- - lib/datadog/core/telemetry/v1/install_signature.rb
315
- - lib/datadog/core/telemetry/v1/integration.rb
316
- - lib/datadog/core/telemetry/v1/product.rb
317
- - lib/datadog/core/telemetry/v1/telemetry_request.rb
318
- - lib/datadog/core/telemetry/v2/app_client_configuration_change.rb
319
- - lib/datadog/core/telemetry/v2/request.rb
308
+ - lib/datadog/core/telemetry/request.rb
320
309
  - lib/datadog/core/transport/ext.rb
321
310
  - lib/datadog/core/transport/http/adapters/net.rb
322
311
  - lib/datadog/core/transport/http/adapters/registry.rb
@@ -908,7 +897,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
908
897
  - !ruby/object:Gem::Version
909
898
  version: 2.0.0
910
899
  requirements: []
911
- rubygems_version: 3.4.21
900
+ rubygems_version: 3.5.3
912
901
  signing_key:
913
902
  specification_version: 4
914
903
  summary: Datadog tracing code for your Ruby applications
@@ -1,250 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'etc'
4
-
5
- require_relative '../configuration/agent_settings_resolver'
6
- require_relative '../environment/ext'
7
- require_relative '../environment/platform'
8
- require_relative '../utils/hash'
9
- require_relative 'v1/application'
10
- require_relative 'v1/dependency'
11
- require_relative 'v1/host'
12
- require_relative 'v1/install_signature'
13
- require_relative 'v1/integration'
14
- require_relative 'v1/product'
15
- require_relative '../transport/ext'
16
-
17
- module Datadog
18
- module Core
19
- module Telemetry
20
- # Module defining methods for collecting metadata for telemetry
21
- module Collector
22
- include Datadog::Core::Configuration
23
- using Core::Utils::Hash::Refinement
24
-
25
- # Forms a hash of configuration key value pairs to be sent in the additional payload
26
- def additional_payload
27
- additional_payload_variables
28
- end
29
-
30
- # Forms a telemetry application object
31
- def application
32
- Telemetry::V1::Application.new(
33
- env: env,
34
- language_name: Datadog::Core::Environment::Ext::LANG,
35
- language_version: Datadog::Core::Environment::Ext::LANG_VERSION,
36
- products: products,
37
- runtime_name: Datadog::Core::Environment::Ext::RUBY_ENGINE,
38
- runtime_version: Datadog::Core::Environment::Ext::ENGINE_VERSION,
39
- service_name: service_name,
40
- service_version: service_version,
41
- tracer_version: library_version
42
- )
43
- end
44
-
45
- # Forms a hash of standard key value pairs to be sent in the app-started event configuration
46
- def configurations
47
- config = Datadog.configuration
48
- hash = {
49
- DD_AGENT_HOST: config.agent.host,
50
- DD_AGENT_TRANSPORT: agent_transport,
51
- DD_TRACE_SAMPLE_RATE: format_configuration_value(config.tracing.sampling.default_rate),
52
- DD_TRACE_REMOVE_INTEGRATION_SERVICE_NAMES_ENABLED: config.tracing.contrib.global_default_service_name.enabled
53
- }
54
- peer_service_mapping_str = ''
55
- unless config.tracing.contrib.peer_service_mapping.empty?
56
- peer_service_mapping = config.tracing.contrib.peer_service_mapping
57
- peer_service_mapping_str = peer_service_mapping.map { |key, value| "#{key}:#{value}" }.join(',')
58
- end
59
- hash[:DD_TRACE_PEER_SERVICE_MAPPING] = peer_service_mapping_str
60
- hash.compact!
61
- hash
62
- end
63
-
64
- # Forms a telemetry app-started dependencies object
65
- def dependencies
66
- Gem.loaded_specs.collect do |name, loaded_gem|
67
- Datadog::Core::Telemetry::V1::Dependency.new(
68
- # `hash` should be used when `version` is not available
69
- name: name, version: loaded_gem.version.to_s, hash: nil
70
- )
71
- end
72
- end
73
-
74
- # Forms a telemetry host object
75
- def host
76
- Telemetry::V1::Host.new(
77
- container_id: Core::Environment::Container.container_id,
78
- hostname: Core::Environment::Platform.hostname,
79
- kernel_name: Core::Environment::Platform.kernel_name,
80
- kernel_release: Core::Environment::Platform.kernel_release,
81
- kernel_version: Core::Environment::Platform.kernel_version
82
- )
83
- end
84
-
85
- # Forms a telemetry app-started install_signature object
86
- def install_signature
87
- Telemetry::V1::InstallSignature.new(
88
- install_id: Datadog.configuration.dig('telemetry', 'install_id'),
89
- install_type: Datadog.configuration.dig('telemetry', 'install_type'),
90
- install_time: Datadog.configuration.dig('telemetry', 'install_time'),
91
- )
92
- end
93
-
94
- # Forms a telemetry app-started integrations object
95
- def integrations
96
- Datadog.registry.map do |integration|
97
- is_instrumented = instrumented?(integration)
98
- is_enabled = is_instrumented && patched?(integration)
99
- Telemetry::V1::Integration.new(
100
- name: integration.name.to_s,
101
- enabled: is_enabled,
102
- version: integration_version(integration),
103
- compatible: integration_compatible?(integration),
104
- error: (patch_error(integration) if is_instrumented && !is_enabled),
105
- auto_enabled: is_enabled ? integration_auto_instrument?(integration) : nil
106
- )
107
- end
108
- end
109
-
110
- # Returns the runtime ID of the current process
111
- def runtime_id
112
- Datadog::Core::Environment::Identity.id
113
- end
114
-
115
- # Returns the current as a UNIX timestamp in seconds
116
- def tracer_time
117
- Time.now.to_i
118
- end
119
-
120
- private
121
-
122
- TARGET_OPTIONS = [
123
- 'ci.enabled',
124
- 'logger.level',
125
- 'profiling.advanced.code_provenance_enabled',
126
- 'profiling.advanced.endpoint.collection.enabled',
127
- 'profiling.enabled',
128
- 'runtime_metrics.enabled',
129
- 'tracing.analytics.enabled',
130
- 'tracing.distributed_tracing.propagation_inject_style',
131
- 'tracing.distributed_tracing.propagation_extract_style',
132
- 'tracing.enabled',
133
- 'tracing.log_injection',
134
- 'tracing.partial_flush.enabled',
135
- 'tracing.partial_flush.min_spans_threshold',
136
- 'tracing.priority_sampling',
137
- 'tracing.report_hostname',
138
- 'tracing.sampling.default_rate',
139
- 'tracing.sampling.rate_limit'
140
- ].freeze
141
-
142
- def additional_payload_variables
143
- # Whitelist of configuration options to send in additional payload object
144
- configuration = Datadog.configuration
145
- options = TARGET_OPTIONS.each_with_object({}) do |option, hash|
146
- split_option = option.split('.')
147
- hash[option] = format_configuration_value(configuration.dig(*split_option))
148
- end
149
-
150
- # Add some more custom additional payload values here
151
- options['tracing.auto_instrument.enabled'] = !defined?(Datadog::AutoInstrument::LOADED).nil?
152
- options['tracing.writer_options.buffer_size'] =
153
- format_configuration_value(configuration.tracing.writer_options[:buffer_size])
154
- options['tracing.writer_options.flush_interval'] =
155
- format_configuration_value(configuration.tracing.writer_options[:flush_interval])
156
- options['logger.instance'] = configuration.logger.instance.class.to_s
157
- options['appsec.enabled'] = configuration.dig('appsec', 'enabled') if configuration.respond_to?('appsec')
158
- options['tracing.opentelemetry.enabled'] = !defined?(Datadog::OpenTelemetry::LOADED).nil?
159
- options['tracing.opentracing.enabled'] = !defined?(Datadog::OpenTracer::LOADED).nil?
160
- options.compact!
161
- options
162
- end
163
-
164
- def format_configuration_value(value)
165
- # TODO: Add float if telemetry starts accepting it
166
- case value
167
- when Integer, String, true, false, nil
168
- value
169
- else
170
- value.to_s
171
- end
172
- end
173
-
174
- def env
175
- Datadog.configuration.env
176
- end
177
-
178
- def service_name
179
- Datadog.configuration.service
180
- end
181
-
182
- def service_version
183
- Datadog.configuration.version
184
- end
185
-
186
- def library_version
187
- Core::Environment::Identity.tracer_version
188
- end
189
-
190
- def products
191
- Telemetry::V1::Product.new(profiler: profiler, appsec: appsec)
192
- end
193
-
194
- def profiler
195
- { version: library_version }
196
- end
197
-
198
- def appsec
199
- { version: library_version }
200
- end
201
-
202
- def agent_transport
203
- adapter = Core::Configuration::AgentSettingsResolver.call(Datadog.configuration).adapter
204
- if adapter == Datadog::Core::Transport::Ext::UnixSocket::ADAPTER
205
- 'UDS'
206
- else
207
- 'TCP'
208
- end
209
- end
210
-
211
- def instrumented_integrations
212
- Datadog.configuration.tracing.instrumented_integrations
213
- end
214
-
215
- def instrumented?(integration)
216
- instrumented_integrations.include?(integration.name)
217
- end
218
-
219
- def patched?(integration)
220
- !!integration.klass.patcher.patch_successful
221
- end
222
-
223
- def integration_auto_instrument?(integration)
224
- integration.klass.auto_instrument?
225
- end
226
-
227
- def integration_compatible?(integration)
228
- integration.klass.class.compatible?
229
- end
230
-
231
- def integration_version(integration)
232
- integration.klass.class.version ? integration.klass.class.version.to_s : nil
233
- end
234
-
235
- def patch_error(integration)
236
- patch_error_result = integration.klass.patcher.patch_error_result
237
- if patch_error_result.nil? # if no error occurred during patching, but integration is still not instrumented
238
- desc = "Available?: #{integration.klass.class.available?}"
239
- desc += ", Loaded? #{integration.klass.class.loaded?}"
240
- desc += ", Compatible? #{integration.klass.class.compatible?}"
241
- desc += ", Patchable? #{integration.klass.class.patchable?}"
242
- desc
243
- else
244
- patch_error_result.compact.to_s
245
- end
246
- end
247
- end
248
- end
249
- end
250
- end