datadog 2.15.0 → 2.17.0

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 (194) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +74 -2
  3. data/ext/datadog_profiling_native_extension/datadog_ruby_common.c +1 -4
  4. data/ext/datadog_profiling_native_extension/datadog_ruby_common.h +7 -0
  5. data/ext/datadog_profiling_native_extension/encoded_profile.c +22 -12
  6. data/ext/datadog_profiling_native_extension/encoded_profile.h +1 -0
  7. data/ext/datadog_profiling_native_extension/extconf.rb +3 -0
  8. data/ext/datadog_profiling_native_extension/heap_recorder.c +8 -1
  9. data/ext/datadog_profiling_native_extension/http_transport.c +45 -72
  10. data/ext/datadog_profiling_native_extension/stack_recorder.c +4 -5
  11. data/ext/libdatadog_api/crashtracker.c +11 -12
  12. data/ext/libdatadog_api/crashtracker.h +5 -0
  13. data/ext/libdatadog_api/datadog_ruby_common.c +1 -4
  14. data/ext/libdatadog_api/datadog_ruby_common.h +7 -0
  15. data/ext/libdatadog_api/init.c +15 -0
  16. data/ext/libdatadog_api/library_config.c +122 -0
  17. data/ext/libdatadog_api/library_config.h +19 -0
  18. data/ext/libdatadog_api/macos_development.md +3 -3
  19. data/ext/libdatadog_api/process_discovery.c +117 -0
  20. data/ext/libdatadog_api/process_discovery.h +5 -0
  21. data/ext/libdatadog_extconf_helpers.rb +1 -1
  22. data/lib/datadog/appsec/actions_handler.rb +3 -2
  23. data/lib/datadog/appsec/api_security/lru_cache.rb +49 -0
  24. data/lib/datadog/appsec/api_security.rb +9 -0
  25. data/lib/datadog/appsec/assets/waf_rules/recommended.json +1344 -0
  26. data/lib/datadog/appsec/assets/waf_rules/strict.json +1344 -0
  27. data/lib/datadog/appsec/autoload.rb +1 -1
  28. data/lib/datadog/appsec/component.rb +11 -4
  29. data/lib/datadog/appsec/configuration/settings.rb +31 -18
  30. data/lib/datadog/appsec/context.rb +1 -1
  31. data/lib/datadog/appsec/contrib/active_record/instrumentation.rb +10 -12
  32. data/lib/datadog/appsec/contrib/active_record/integration.rb +1 -1
  33. data/lib/datadog/appsec/contrib/active_record/patcher.rb +22 -22
  34. data/lib/datadog/appsec/contrib/devise/data_extractor.rb +2 -3
  35. data/lib/datadog/appsec/contrib/devise/ext.rb +1 -0
  36. data/lib/datadog/appsec/contrib/devise/integration.rb +1 -1
  37. data/lib/datadog/appsec/contrib/devise/patcher.rb +3 -5
  38. data/lib/datadog/appsec/contrib/devise/tracking_middleware.rb +17 -4
  39. data/lib/datadog/appsec/contrib/excon/integration.rb +1 -1
  40. data/lib/datadog/appsec/contrib/excon/ssrf_detection_middleware.rb +9 -10
  41. data/lib/datadog/appsec/contrib/faraday/integration.rb +1 -1
  42. data/lib/datadog/appsec/contrib/faraday/ssrf_detection_middleware.rb +8 -9
  43. data/lib/datadog/appsec/contrib/graphql/gateway/watcher.rb +8 -9
  44. data/lib/datadog/appsec/contrib/graphql/integration.rb +1 -1
  45. data/lib/datadog/appsec/contrib/rack/gateway/watcher.rb +22 -32
  46. data/lib/datadog/appsec/contrib/rack/integration.rb +1 -1
  47. data/lib/datadog/appsec/contrib/rack/request_middleware.rb +16 -16
  48. data/lib/datadog/appsec/contrib/rails/gateway/watcher.rb +11 -13
  49. data/lib/datadog/appsec/contrib/rails/integration.rb +1 -1
  50. data/lib/datadog/appsec/contrib/rails/patcher.rb +21 -21
  51. data/lib/datadog/appsec/contrib/rest_client/integration.rb +1 -1
  52. data/lib/datadog/appsec/contrib/rest_client/request_ssrf_detection_patch.rb +10 -11
  53. data/lib/datadog/appsec/contrib/sinatra/gateway/watcher.rb +17 -23
  54. data/lib/datadog/appsec/contrib/sinatra/integration.rb +1 -1
  55. data/lib/datadog/appsec/event.rb +85 -95
  56. data/lib/datadog/appsec/instrumentation/gateway/argument.rb +5 -2
  57. data/lib/datadog/appsec/metrics/telemetry.rb +1 -1
  58. data/lib/datadog/appsec/monitor/gateway/watcher.rb +42 -12
  59. data/lib/datadog/appsec/processor/rule_loader.rb +26 -28
  60. data/lib/datadog/appsec/processor/rule_merger.rb +5 -5
  61. data/lib/datadog/appsec/processor.rb +1 -1
  62. data/lib/datadog/appsec/remote.rb +14 -13
  63. data/lib/datadog/appsec/response.rb +6 -6
  64. data/lib/datadog/appsec/security_engine/runner.rb +1 -1
  65. data/lib/datadog/appsec/security_event.rb +39 -0
  66. data/lib/datadog/appsec.rb +1 -1
  67. data/lib/datadog/core/buffer/random.rb +18 -2
  68. data/lib/datadog/core/configuration/agent_settings_resolver.rb +5 -5
  69. data/lib/datadog/core/configuration/agentless_settings_resolver.rb +176 -0
  70. data/lib/datadog/core/configuration/components.rb +48 -30
  71. data/lib/datadog/core/configuration/components_state.rb +23 -0
  72. data/lib/datadog/core/configuration/option.rb +79 -43
  73. data/lib/datadog/core/configuration/option_definition.rb +4 -4
  74. data/lib/datadog/core/configuration/options.rb +1 -1
  75. data/lib/datadog/core/configuration/settings.rb +20 -10
  76. data/lib/datadog/core/configuration/stable_config.rb +23 -0
  77. data/lib/datadog/core/configuration.rb +40 -16
  78. data/lib/datadog/core/crashtracking/component.rb +3 -10
  79. data/lib/datadog/core/encoding.rb +1 -1
  80. data/lib/datadog/core/environment/cgroup.rb +10 -12
  81. data/lib/datadog/core/environment/container.rb +38 -40
  82. data/lib/datadog/core/environment/ext.rb +6 -6
  83. data/lib/datadog/core/environment/git.rb +1 -0
  84. data/lib/datadog/core/environment/identity.rb +3 -3
  85. data/lib/datadog/core/environment/platform.rb +3 -3
  86. data/lib/datadog/core/environment/variable_helpers.rb +1 -1
  87. data/lib/datadog/core/error.rb +11 -9
  88. data/lib/datadog/core/logger.rb +2 -2
  89. data/lib/datadog/core/metrics/client.rb +20 -21
  90. data/lib/datadog/core/metrics/logging.rb +5 -5
  91. data/lib/datadog/core/process_discovery.rb +32 -0
  92. data/lib/datadog/core/rate_limiter.rb +4 -2
  93. data/lib/datadog/core/remote/client.rb +39 -31
  94. data/lib/datadog/core/remote/component.rb +3 -3
  95. data/lib/datadog/core/remote/configuration/digest.rb +7 -7
  96. data/lib/datadog/core/remote/configuration/path.rb +1 -1
  97. data/lib/datadog/core/remote/transport/http/client.rb +1 -1
  98. data/lib/datadog/core/remote/transport/http/config.rb +21 -5
  99. data/lib/datadog/core/remote/transport/http/negotiation.rb +1 -1
  100. data/lib/datadog/core/runtime/metrics.rb +4 -4
  101. data/lib/datadog/core/telemetry/component.rb +78 -53
  102. data/lib/datadog/core/telemetry/emitter.rb +23 -11
  103. data/lib/datadog/core/telemetry/event/app_client_configuration_change.rb +65 -0
  104. data/lib/datadog/core/telemetry/event/app_closing.rb +18 -0
  105. data/lib/datadog/core/telemetry/event/app_dependencies_loaded.rb +33 -0
  106. data/lib/datadog/core/telemetry/event/app_heartbeat.rb +18 -0
  107. data/lib/datadog/core/telemetry/event/app_integrations_change.rb +58 -0
  108. data/lib/datadog/core/telemetry/event/app_started.rb +179 -0
  109. data/lib/datadog/core/telemetry/event/base.rb +40 -0
  110. data/lib/datadog/core/telemetry/event/distributions.rb +18 -0
  111. data/lib/datadog/core/telemetry/event/generate_metrics.rb +43 -0
  112. data/lib/datadog/core/telemetry/event/log.rb +76 -0
  113. data/lib/datadog/core/telemetry/event/message_batch.rb +42 -0
  114. data/lib/datadog/core/telemetry/event/synth_app_client_configuration_change.rb +43 -0
  115. data/lib/datadog/core/telemetry/event.rb +17 -472
  116. data/lib/datadog/core/telemetry/http/adapters/net.rb +12 -97
  117. data/lib/datadog/core/telemetry/logger.rb +1 -1
  118. data/lib/datadog/core/telemetry/metric.rb +3 -3
  119. data/lib/datadog/core/telemetry/request.rb +3 -3
  120. data/lib/datadog/core/telemetry/transport/http/api.rb +43 -0
  121. data/lib/datadog/core/telemetry/transport/http/client.rb +49 -0
  122. data/lib/datadog/core/telemetry/transport/http/telemetry.rb +92 -0
  123. data/lib/datadog/core/telemetry/transport/http.rb +63 -0
  124. data/lib/datadog/core/telemetry/transport/telemetry.rb +51 -0
  125. data/lib/datadog/core/telemetry/worker.rb +90 -24
  126. data/lib/datadog/core/transport/http/adapters/test.rb +2 -1
  127. data/lib/datadog/core/transport/http/builder.rb +13 -13
  128. data/lib/datadog/core/utils/at_fork_monkey_patch.rb +6 -6
  129. data/lib/datadog/core/utils/duration.rb +32 -32
  130. data/lib/datadog/core/utils/forking.rb +2 -2
  131. data/lib/datadog/core/utils/network.rb +6 -6
  132. data/lib/datadog/core/utils/only_once_successful.rb +16 -5
  133. data/lib/datadog/core/utils/time.rb +20 -0
  134. data/lib/datadog/core/utils/truncation.rb +21 -0
  135. data/lib/datadog/core/vendor/multipart-post/multipart/post/composite_read_io.rb +1 -1
  136. data/lib/datadog/core/vendor/multipart-post/multipart/post/multipartable.rb +8 -8
  137. data/lib/datadog/core/vendor/multipart-post/multipart/post/parts.rb +7 -7
  138. data/lib/datadog/core/worker.rb +1 -1
  139. data/lib/datadog/core/workers/async.rb +29 -12
  140. data/lib/datadog/core/workers/interval_loop.rb +12 -1
  141. data/lib/datadog/core/workers/runtime_metrics.rb +2 -2
  142. data/lib/datadog/core.rb +8 -0
  143. data/lib/datadog/di/boot.rb +34 -0
  144. data/lib/datadog/di/remote.rb +2 -0
  145. data/lib/datadog/di.rb +5 -32
  146. data/lib/datadog/error_tracking/collector.rb +87 -0
  147. data/lib/datadog/error_tracking/component.rb +167 -0
  148. data/lib/datadog/error_tracking/configuration/settings.rb +63 -0
  149. data/lib/datadog/error_tracking/configuration.rb +11 -0
  150. data/lib/datadog/error_tracking/ext.rb +18 -0
  151. data/lib/datadog/error_tracking/extensions.rb +16 -0
  152. data/lib/datadog/error_tracking/filters.rb +77 -0
  153. data/lib/datadog/error_tracking.rb +18 -0
  154. data/lib/datadog/kit/identity.rb +1 -1
  155. data/lib/datadog/profiling/collectors/code_provenance.rb +1 -1
  156. data/lib/datadog/profiling/exporter.rb +1 -1
  157. data/lib/datadog/profiling/ext.rb +0 -1
  158. data/lib/datadog/profiling/flush.rb +1 -1
  159. data/lib/datadog/profiling/http_transport.rb +1 -6
  160. data/lib/datadog/profiling/scheduler.rb +8 -1
  161. data/lib/datadog/profiling/tag_builder.rb +1 -5
  162. data/lib/datadog/tracing/analytics.rb +1 -1
  163. data/lib/datadog/tracing/contrib/active_support/cache/events/cache.rb +4 -1
  164. data/lib/datadog/tracing/contrib/active_support/cache/instrumentation.rb +33 -0
  165. data/lib/datadog/tracing/contrib/active_support/cache/patcher.rb +4 -0
  166. data/lib/datadog/tracing/contrib/active_support/cache/redis.rb +2 -4
  167. data/lib/datadog/tracing/contrib/aws/instrumentation.rb +10 -0
  168. data/lib/datadog/tracing/contrib/aws/parsed_context.rb +5 -1
  169. data/lib/datadog/tracing/contrib/http/instrumentation.rb +1 -5
  170. data/lib/datadog/tracing/contrib/httpclient/instrumentation.rb +1 -5
  171. data/lib/datadog/tracing/contrib/httprb/instrumentation.rb +1 -5
  172. data/lib/datadog/tracing/contrib/karafka/distributed/propagation.rb +2 -0
  173. data/lib/datadog/tracing/contrib/karafka/monitor.rb +1 -1
  174. data/lib/datadog/tracing/contrib/mongodb/configuration/settings.rb +8 -0
  175. data/lib/datadog/tracing/contrib/mongodb/ext.rb +1 -0
  176. data/lib/datadog/tracing/contrib/mongodb/subscribers.rb +18 -1
  177. data/lib/datadog/tracing/contrib/patcher.rb +5 -2
  178. data/lib/datadog/tracing/contrib/support.rb +28 -0
  179. data/lib/datadog/tracing/distributed/b3_multi.rb +1 -1
  180. data/lib/datadog/tracing/distributed/b3_single.rb +1 -1
  181. data/lib/datadog/tracing/distributed/datadog.rb +2 -2
  182. data/lib/datadog/tracing/metadata/errors.rb +4 -4
  183. data/lib/datadog/tracing/sampling/rate_sampler.rb +2 -1
  184. data/lib/datadog/tracing/span_operation.rb +38 -14
  185. data/lib/datadog/tracing/trace_operation.rb +15 -7
  186. data/lib/datadog/tracing/tracer.rb +7 -3
  187. data/lib/datadog/tracing/utils.rb +1 -1
  188. data/lib/datadog/version.rb +1 -1
  189. data/lib/datadog.rb +2 -3
  190. metadata +53 -10
  191. data/lib/datadog/core/telemetry/http/env.rb +0 -20
  192. data/lib/datadog/core/telemetry/http/ext.rb +0 -28
  193. data/lib/datadog/core/telemetry/http/response.rb +0 -70
  194. data/lib/datadog/core/telemetry/http/transport.rb +0 -90
@@ -18,14 +18,6 @@ module Datadog
18
18
  #
19
19
  # Methods prefixed with _native_ are implemented in `crashtracker.c`
20
20
  class Component
21
- LIBDATADOG_API_FAILURE =
22
- begin
23
- require "libdatadog_api.#{RUBY_VERSION[/\d+.\d+/]}_#{RUBY_PLATFORM}"
24
- nil
25
- rescue LoadError => e
26
- e.message
27
- end
28
-
29
21
  ONLY_ONCE = Core::Utils::OnlyOnce.new
30
22
 
31
23
  def self.build(settings, agent_settings, logger:)
@@ -71,7 +63,7 @@ module Datadog
71
63
  # Must NOT reference `self` here, as only the first instance will
72
64
  # be captured by the ONLY_ONCE and we want to pick the latest active one
73
65
  # (which may have different tags or agent config)
74
- Datadog.send(:components).crashtracker&.update_on_fork
66
+ Datadog.send(:components, allow_initialization: false)&.crashtracker&.update_on_fork
75
67
  end
76
68
  end
77
69
  end
@@ -100,7 +92,8 @@ module Datadog
100
92
  path_to_crashtracking_receiver_binary: path_to_crashtracking_receiver_binary,
101
93
  ld_library_path: ld_library_path,
102
94
  tags_as_array: tags.to_a,
103
- upload_timeout_seconds: 1
95
+ # @ivoanjo: On my machine this needs to be > 5 seconds, and seems to work with 10; the extra 15 is extra margin
96
+ upload_timeout_seconds: 15,
104
97
  )
105
98
  logger.debug("Crash tracking action: #{action} successful")
106
99
  rescue => e
@@ -54,7 +54,7 @@ module Datadog
54
54
  end
55
55
 
56
56
  def join(encoded_data)
57
- "[#{encoded_data.join(',')}]"
57
+ "[#{encoded_data.join(",")}]"
58
58
  end
59
59
  end
60
60
 
@@ -23,20 +23,18 @@ module Datadog
23
23
 
24
24
  def descriptors(process = 'self')
25
25
  [].tap do |descriptors|
26
- begin
27
- filepath = "/proc/#{process}/cgroup"
28
-
29
- if File.exist?(filepath)
30
- File.foreach("/proc/#{process}/cgroup") do |line|
31
- line = line.strip
32
- descriptors << parse(line) unless line.empty?
33
- end
26
+ filepath = "/proc/#{process}/cgroup"
27
+
28
+ if File.exist?(filepath)
29
+ File.foreach("/proc/#{process}/cgroup") do |line|
30
+ line = line.strip
31
+ descriptors << parse(line) unless line.empty?
34
32
  end
35
- rescue StandardError => e
36
- Datadog.logger.error(
37
- "Error while parsing cgroup. Cause: #{e.class.name} #{e.message} Location: #{Array(e.backtrace).first}"
38
- )
39
33
  end
34
+ rescue => e
35
+ Datadog.logger.error(
36
+ "Error while parsing cgroup. Cause: #{e.class.name} #{e.message} Location: #{Array(e.backtrace).first}"
37
+ )
40
38
  end
41
39
  end
42
40
 
@@ -37,52 +37,50 @@ module Datadog
37
37
 
38
38
  def descriptor
39
39
  @descriptor ||= Descriptor.new.tap do |descriptor|
40
- begin
41
- Cgroup.descriptors.each do |cgroup_descriptor|
42
- # Parse container data from cgroup descriptor
43
- path = cgroup_descriptor.path
44
- next if path.nil?
45
-
46
- # Split path into parts
47
- parts = path.split('/')
48
- parts.shift # Remove leading empty part
49
-
50
- # Read info from path
51
- next if parts.empty?
52
-
53
- platform = parts[0][PLATFORM_REGEX, :platform]
54
- container_id, task_uid = nil
55
-
56
- case parts.length
57
- when 0..1
58
- next
59
- when 2
60
- container_id = parts[-1][CONTAINER_REGEX, :container] \
61
- || parts[-1][FARGATE_14_CONTAINER_REGEX, :container]
40
+ Cgroup.descriptors.each do |cgroup_descriptor|
41
+ # Parse container data from cgroup descriptor
42
+ path = cgroup_descriptor.path
43
+ next if path.nil?
44
+
45
+ # Split path into parts
46
+ parts = path.split('/')
47
+ parts.shift # Remove leading empty part
48
+
49
+ # Read info from path
50
+ next if parts.empty?
51
+
52
+ platform = parts[0][PLATFORM_REGEX, :platform]
53
+ container_id, task_uid = nil
54
+
55
+ case parts.length
56
+ when 0..1
57
+ next
58
+ when 2
59
+ container_id = parts[-1][CONTAINER_REGEX, :container] \
60
+ || parts[-1][FARGATE_14_CONTAINER_REGEX, :container]
61
+ else
62
+ if (container_id = parts[-1][CONTAINER_REGEX, :container])
63
+ task_uid = parts[-2][POD_REGEX, :pod] || parts[1][POD_REGEX, :pod]
62
64
  else
63
- if (container_id = parts[-1][CONTAINER_REGEX, :container])
64
- task_uid = parts[-2][POD_REGEX, :pod] || parts[1][POD_REGEX, :pod]
65
- else
66
- container_id = parts[-1][FARGATE_14_CONTAINER_REGEX, :container]
67
- end
65
+ container_id = parts[-1][FARGATE_14_CONTAINER_REGEX, :container]
68
66
  end
67
+ end
69
68
 
70
- # If container ID wasn't found, ignore.
71
- # Path might describe a non-container environment.
72
- next if container_id.nil?
69
+ # If container ID wasn't found, ignore.
70
+ # Path might describe a non-container environment.
71
+ next if container_id.nil?
73
72
 
74
- descriptor.platform = platform
75
- descriptor.container_id = container_id
76
- descriptor.task_uid = task_uid
73
+ descriptor.platform = platform
74
+ descriptor.container_id = container_id
75
+ descriptor.task_uid = task_uid
77
76
 
78
- break
79
- end
80
- rescue StandardError => e
81
- Datadog.logger.error(
82
- "Error while parsing container info. Cause: #{e.class.name} #{e.message} " \
83
- "Location: #{Array(e.backtrace).first}"
84
- )
77
+ break
85
78
  end
79
+ rescue => e
80
+ Datadog.logger.error(
81
+ "Error while parsing container info. Cause: #{e.class.name} #{e.message} " \
82
+ "Location: #{Array(e.backtrace).first}"
83
+ )
86
84
  end
87
85
  end
88
86
  end
@@ -9,11 +9,11 @@ module Datadog
9
9
  module Ext
10
10
  # e.g for CRuby '3.0.1', for JRuby '9.2.19.0', for TruffleRuby '21.1.0'
11
11
  ENGINE_VERSION = if defined?(RUBY_ENGINE_VERSION)
12
- RUBY_ENGINE_VERSION
13
- else
14
- # CRuby < 2.3 doesn't support RUBY_ENGINE_VERSION
15
- RUBY_VERSION
16
- end
12
+ RUBY_ENGINE_VERSION
13
+ else
14
+ # CRuby < 2.3 doesn't support RUBY_ENGINE_VERSION
15
+ RUBY_VERSION
16
+ end
17
17
 
18
18
  ENV_API_KEY = 'DD_API_KEY'
19
19
  ENV_ENVIRONMENT = 'DD_ENV'
@@ -26,7 +26,7 @@ module Datadog
26
26
  FALLBACK_SERVICE_NAME =
27
27
  begin
28
28
  File.basename($PROGRAM_NAME, '.*')
29
- rescue StandardError
29
+ rescue
30
30
  'ruby'
31
31
  end.freeze
32
32
 
@@ -5,6 +5,7 @@ require_relative '../utils/url'
5
5
 
6
6
  module Datadog
7
7
  module Core
8
+ # Environment
8
9
  module Environment
9
10
  # Retrieves git repository information from environment variables
10
11
  module Git
@@ -67,12 +67,12 @@ module Datadog
67
67
 
68
68
  rest.split('.').tap do |segments|
69
69
  if segments.length >= 4
70
- pre = "-#{segments.shift}"
71
- build = "+#{segments.join('.')}"
70
+ pre = "-#{segments.shift}"
71
+ build = "+#{segments.join(".")}"
72
72
  elsif segments.length == 1
73
73
  pre = "-#{segments.shift}"
74
74
  else
75
- build = "+#{segments.join('.')}"
75
+ build = "+#{segments.join(".")}"
76
76
  end
77
77
  end
78
78
 
@@ -13,18 +13,18 @@ module Datadog
13
13
 
14
14
  # @return [String] ISA of host; `uname -m`
15
15
  def architecture
16
- Identity.lang_version >= '2.2' ? Etc.uname[:machine] : Gem::Platform.local.cpu
16
+ (Identity.lang_version >= '2.2') ? Etc.uname[:machine] : Gem::Platform.local.cpu
17
17
  end
18
18
 
19
19
  # @return [String] name of host; `uname -n`
20
20
  def hostname
21
- Identity.lang_version >= '2.2' ? Etc.uname[:nodename] : nil
21
+ (Identity.lang_version >= '2.2') ? Etc.uname[:nodename] : nil
22
22
  end
23
23
 
24
24
  # System name, normally `Linux` or `Darwin` (but 'Mac OS X' on JRuby);
25
25
  # @return [String] name of kernel; `uname -s`.
26
26
  def kernel_name
27
- Identity.lang_version >= '2.2' ? Etc.uname[:sysname] : Gem::Platform.local.os.capitalize
27
+ (Identity.lang_version >= '2.2') ? Etc.uname[:sysname] : Gem::Platform.local.os.capitalize
28
28
  end
29
29
 
30
30
  # @return [String] kernel release; `uname -r`
@@ -23,7 +23,7 @@ module Datadog
23
23
  if var && ENV.key?(var)
24
24
  value = ENV[var].to_s.strip
25
25
  value.downcase!
26
- value == 'true' || value == '1' # rubocop:disable Style/MultipleComparison
26
+ value == 'true' || value == '1'
27
27
  else
28
28
  default
29
29
  end
@@ -11,12 +11,17 @@ module Datadog
11
11
  class << self
12
12
  def build_from(value)
13
13
  case value
14
- when Error then value
15
- when Array then new(*value)
14
+ # A Ruby {Exception} is the most common parameter type.
16
15
  when Exception then new(value.class, value.message, full_backtrace(value))
17
- when ContainsMessage then new(value.class, value.message)
16
+ # steep:ignore:start
17
+ # Steep doesn't like an array with up to 3 elements to be passed here: it thinks the array is unbounded.
18
+ when Array then new(*value)
19
+ # Steep can not follow the logic inside the lambda, thus it doesn't know `value` responds to `:message`.
20
+ when ->(v) { v.respond_to?(:message) } then new(value.class, value.message)
21
+ # steep:ignore:end
18
22
  when String then new(nil, value)
19
- else BlankError
23
+ when Error then value
24
+ else Error.new # Blank error
20
25
  end
21
26
  end
22
27
 
@@ -34,7 +39,7 @@ module Datadog
34
39
  # but it's around 3x faster in our benchmark test
35
40
  # at `error_spec.rb`.
36
41
  def full_backtrace(ex)
37
- backtrace = String.new
42
+ backtrace = +''
38
43
  backtrace_for(ex, backtrace)
39
44
 
40
45
  # Avoid circular causes
@@ -75,7 +80,7 @@ module Datadog
75
80
  if trace[1]
76
81
  # Ident stack trace for caller lines, to separate
77
82
  # them from the main error lines.
78
- trace[1..-1].each do |line|
83
+ trace[1..-1]&.each do |line|
79
84
  backtrace << "\n\tfrom "
80
85
  backtrace << line
81
86
  end
@@ -92,9 +97,6 @@ module Datadog
92
97
  @message = Utils.utf8_encode(message)
93
98
  @backtrace = Utils.utf8_encode(backtrace)
94
99
  end
95
-
96
- BlankError = Error.new
97
- ContainsMessage = ->(v) { v.respond_to?(:message) }
98
100
  end
99
101
  end
100
102
  end
@@ -28,7 +28,7 @@ module Datadog
28
28
 
29
29
  if message.nil?
30
30
  if block
31
- super(severity, message, progname) do
31
+ super do
32
32
  "[#{self.progname}] #{where}#{yield}"
33
33
  end
34
34
  else
@@ -39,7 +39,7 @@ module Datadog
39
39
  end
40
40
  end
41
41
 
42
- alias log add
42
+ alias_method :log, :add
43
43
  end
44
44
  end
45
45
  end
@@ -21,9 +21,10 @@ module Datadog
21
21
  extend Options
22
22
  extend Helpers
23
23
 
24
- attr_reader :statsd, :logger
24
+ attr_reader :statsd, :logger, :telemetry
25
25
 
26
- def initialize(logger: Datadog.logger, statsd: nil, enabled: true, **_)
26
+ def initialize(telemetry:, logger: Datadog.logger, statsd: nil, enabled: true, **_)
27
+ @telemetry = telemetry
27
28
  @logger = logger
28
29
  @statsd =
29
30
  if supported?
@@ -74,10 +75,10 @@ module Datadog
74
75
  #
75
76
  # Versions < 5.0 are always single-threaded, but do not have the kwarg option.
76
77
  options = if dogstatsd_version >= Gem::Version.new('5.2')
77
- { single_thread: true }
78
- else
79
- {}
80
- end
78
+ {single_thread: true}
79
+ else
80
+ {}
81
+ end
81
82
 
82
83
  Datadog::Statsd.new(default_hostname, default_port, **options)
83
84
  end
@@ -98,11 +99,11 @@ module Datadog
98
99
  raise ArgumentError if value.nil?
99
100
 
100
101
  statsd.count(stat, value, metric_options(options))
101
- rescue StandardError => e
102
+ rescue => e
102
103
  logger.error(
103
104
  "Failed to send count stat. Cause: #{e.class.name} #{e.message} Source: #{Array(e.backtrace).first}"
104
105
  )
105
- Telemetry::Logger.report(e, description: 'Failed to send count stat')
106
+ telemetry.report(e, description: 'Failed to send count stat')
106
107
  end
107
108
 
108
109
  def distribution(stat, value = nil, options = nil, &block)
@@ -112,11 +113,11 @@ module Datadog
112
113
  raise ArgumentError if value.nil?
113
114
 
114
115
  statsd.distribution(stat, value, metric_options(options))
115
- rescue StandardError => e
116
+ rescue => e
116
117
  logger.error(
117
118
  "Failed to send distribution stat. Cause: #{e.class.name} #{e.message} Source: #{Array(e.backtrace).first}"
118
119
  )
119
- Telemetry::Logger.report(e, description: 'Failed to send distribution stat')
120
+ telemetry.report(e, description: 'Failed to send distribution stat')
120
121
  end
121
122
 
122
123
  def increment(stat, options = nil)
@@ -125,11 +126,11 @@ module Datadog
125
126
  options = yield if block_given?
126
127
 
127
128
  statsd.increment(stat, metric_options(options))
128
- rescue StandardError => e
129
+ rescue => e
129
130
  logger.error(
130
131
  "Failed to send increment stat. Cause: #{e.class.name} #{e.message} Source: #{Array(e.backtrace).first}"
131
132
  )
132
- Telemetry::Logger.report(e, description: 'Failed to send increment stat')
133
+ telemetry.report(e, description: 'Failed to send increment stat')
133
134
  end
134
135
 
135
136
  def gauge(stat, value = nil, options = nil, &block)
@@ -139,11 +140,11 @@ module Datadog
139
140
  raise ArgumentError if value.nil?
140
141
 
141
142
  statsd.gauge(stat, value, metric_options(options))
142
- rescue StandardError => e
143
+ rescue => e
143
144
  logger.error(
144
145
  "Failed to send gauge stat. Cause: #{e.class.name} #{e.message} Source: #{Array(e.backtrace).first}"
145
146
  )
146
- Telemetry::Logger.report(e, description: 'Failed to send gauge stat')
147
+ telemetry.report(e, description: 'Failed to send gauge stat')
147
148
  end
148
149
 
149
150
  def time(stat, options = nil)
@@ -158,12 +159,12 @@ module Datadog
158
159
  finished = Utils::Time.get_time
159
160
  distribution(stat, ((finished - start) * 1000), options)
160
161
  end
161
- rescue StandardError => e
162
+ rescue => e
162
163
  # TODO: Likely to be redundant, since `distribution` handles its own errors.
163
164
  logger.error(
164
165
  "Failed to send time stat. Cause: #{e.class.name} #{e.message} Source: #{Array(e.backtrace).first}"
165
166
  )
166
- Telemetry::Logger.report(e, description: 'Failed to send time stat')
167
+ telemetry.report(e, description: 'Failed to send time stat')
167
168
  end
168
169
  end
169
170
 
@@ -172,7 +173,7 @@ module Datadog
172
173
  end
173
174
 
174
175
  def close
175
- @statsd.close if @statsd && @statsd.respond_to?(:close)
176
+ @statsd.close if @statsd&.respond_to?(:close)
176
177
  end
177
178
 
178
179
  private
@@ -184,10 +185,8 @@ module Datadog
184
185
  defined?(Datadog::Statsd::VERSION) &&
185
186
  Datadog::Statsd::VERSION &&
186
187
  Gem::Version.new(Datadog::Statsd::VERSION)
187
- ) || (
188
- Gem.loaded_specs['dogstatsd-ruby'] &&
189
- Gem.loaded_specs['dogstatsd-ruby'].version
190
- )
188
+ ) ||
189
+ Gem.loaded_specs['dogstatsd-ruby']&.version
191
190
  end
192
191
 
193
192
  IGNORED_STATSD_ONLY_ONCE = Utils::OnlyOnce.new
@@ -17,25 +17,25 @@ module Datadog
17
17
  l.progname = nil
18
18
  l.formatter = proc do |_severity, datetime, _progname, msg|
19
19
  stat = JSON.parse(msg[3..-1]) # Trim off leading progname...
20
- "#{JSON.dump(timestamp: datetime.to_i, message: 'Metric sent.', metric: stat)}\n"
20
+ "#{JSON.dump(timestamp: datetime.to_i, message: "Metric sent.", metric: stat)}\n"
21
21
  end
22
22
  end
23
23
  end
24
24
 
25
25
  def count(stat, value, options = nil)
26
- logger.info({ stat: stat, type: :count, value: value, options: options }.to_json)
26
+ logger.info({stat: stat, type: :count, value: value, options: options}.to_json)
27
27
  end
28
28
 
29
29
  def distribution(stat, value, options = nil)
30
- logger.info({ stat: stat, type: :distribution, value: value, options: options }.to_json)
30
+ logger.info({stat: stat, type: :distribution, value: value, options: options}.to_json)
31
31
  end
32
32
 
33
33
  def increment(stat, options = nil)
34
- logger.info({ stat: stat, type: :increment, options: options }.to_json)
34
+ logger.info({stat: stat, type: :increment, options: options}.to_json)
35
35
  end
36
36
 
37
37
  def gauge(stat, value, options = nil)
38
- logger.info({ stat: stat, type: :gauge, value: value, options: options }.to_json)
38
+ logger.info({stat: stat, type: :gauge, value: value, options: options}.to_json)
39
39
  end
40
40
  end
41
41
  end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datadog
4
+ module Core
5
+ # Class used to store tracer metadata in a native file descriptor.
6
+ class ProcessDiscovery
7
+ def self.get_and_store_metadata(settings, logger)
8
+ if (libdatadog_api_failure = Datadog::Core::LIBDATADOG_API_FAILURE)
9
+ logger.debug("Cannot enable process discovery: #{libdatadog_api_failure}")
10
+ return
11
+ end
12
+ metadata = get_metadata(settings)
13
+ _native_store_tracer_metadata(logger, **metadata)
14
+ end
15
+
16
+ # According to the RFC, runtime_id, service_name, service_env, service_version are optional.
17
+ # In the C method exposed by ddcommon, memfd_create replaces empty strings by None for these fields.
18
+ private_class_method def self.get_metadata(settings)
19
+ {
20
+ schema_version: 1,
21
+ runtime_id: Core::Environment::Identity.id,
22
+ tracer_language: Core::Environment::Identity.lang,
23
+ tracer_version: Core::Environment::Identity.gem_datadog_version_semver2,
24
+ hostname: Core::Environment::Socket.hostname,
25
+ service_name: settings.service || '',
26
+ service_env: settings.env || '',
27
+ service_version: settings.version || ''
28
+ }
29
+ end
30
+ end
31
+ end
32
+ end
@@ -13,13 +13,15 @@ module Datadog
13
13
  # to be side-effect free.
14
14
  #
15
15
  # @return [Boolean] whether a resource conforms with the current limit
16
- def allow?(size = 1); end
16
+ def allow?(size = 1)
17
+ end
17
18
 
18
19
  # The effective rate limiting ratio based on
19
20
  # recent calls to `allow?`.
20
21
  #
21
22
  # @return [Float] recent allowance ratio
22
- def effective_rate; end
23
+ def effective_rate
24
+ end
23
25
  end
24
26
 
25
27
  # Implementation of the Token Bucket metering algorithm
@@ -11,6 +11,7 @@ module Datadog
11
11
  # Client communicates with the agent and sync remote configuration
12
12
  class Client
13
13
  class TransportError < StandardError; end
14
+
14
15
  class SyncError < StandardError; end
15
16
 
16
17
  attr_reader :transport, :repository, :id, :dispatcher, :logger
@@ -119,7 +120,7 @@ module Datadog
119
120
  end
120
121
  end
121
122
 
122
- def payload # rubocop:disable Metrics/MethodLength
123
+ def payload # standard:disable Metrics/MethodLength
123
124
  state = repository.state
124
125
 
125
126
  client_tracer_tags = [
@@ -134,12 +135,19 @@ module Datadog
134
135
  "ruby.runtime.engine.name:#{RUBY_ENGINE}",
135
136
  "ruby.runtime.engine.version:#{ruby_engine_version}",
136
137
  "ruby.rubygems.platform.local:#{Gem::Platform.local}",
137
- "ruby.gem.libddwaf.version:#{gem_spec('libddwaf').version}",
138
- "ruby.gem.libddwaf.platform:#{gem_spec('libddwaf').platform}",
139
- "ruby.gem.libdatadog.version:#{gem_spec('libdatadog').version}",
140
- "ruby.gem.libdatadog.platform:#{gem_spec('libdatadog').platform}",
138
+ "ruby.gem.libddwaf.version:#{gem_spec("libddwaf").version}",
139
+ "ruby.gem.libddwaf.platform:#{gem_spec("libddwaf").platform}",
140
+ "ruby.gem.libdatadog.version:#{gem_spec("libdatadog").version}",
141
+ "ruby.gem.libdatadog.platform:#{gem_spec("libdatadog").platform}",
141
142
  ]
142
143
 
144
+ if (git_repository_url = Core::Environment::Git.git_repository_url)
145
+ client_tracer_tags << "git.repository_url:#{git_repository_url}"
146
+ end
147
+ if (git_commit_sha = Core::Environment::Git.git_commit_sha)
148
+ client_tracer_tags << "git.commit.sha:#{git_commit_sha}"
149
+ end
150
+
143
151
  client_tracer = {
144
152
  runtime_id: Core::Environment::Identity.id,
145
153
  language: Core::Environment::Identity.lang,
@@ -195,37 +203,37 @@ module Datadog
195
203
  return @native_platform unless @native_platform.nil?
196
204
 
197
205
  os = if RUBY_ENGINE == 'jruby'
198
- os_name = java.lang.System.get_property('os.name')
206
+ os_name = java.lang.System.get_property('os.name')
199
207
 
200
- case os_name
201
- when /linux/i then 'linux'
202
- when /mac/i then 'darwin'
203
- else os_name
204
- end
205
- else
206
- Gem::Platform.local.os
207
- end
208
+ case os_name
209
+ when /linux/i then 'linux'
210
+ when /mac/i then 'darwin'
211
+ else os_name
212
+ end
213
+ else
214
+ Gem::Platform.local.os
215
+ end
208
216
 
209
217
  version = if os != 'linux'
210
- nil
211
- elsif RUBY_PLATFORM =~ /linux-(.+)$/
212
- # Old rubygems don't handle non-gnu linux correctly
213
- Regexp.last_match(1)
214
- else
215
- 'gnu'
216
- end
218
+ nil
219
+ elsif RUBY_PLATFORM =~ /linux-(.+)$/
220
+ # Old rubygems don't handle non-gnu linux correctly
221
+ Regexp.last_match(1)
222
+ else
223
+ 'gnu'
224
+ end
217
225
 
218
226
  cpu = if RUBY_ENGINE == 'jruby'
219
- os_arch = java.lang.System.get_property('os.arch')
220
-
221
- case os_arch
222
- when 'amd64' then 'x86_64'
223
- when 'aarch64' then os == 'darwin' ? 'arm64' : 'aarch64'
224
- else os_arch
225
- end
226
- else
227
- Gem::Platform.local.cpu
228
- end
227
+ os_arch = java.lang.System.get_property('os.arch')
228
+
229
+ case os_arch
230
+ when 'amd64' then 'x86_64'
231
+ when 'aarch64' then (os == 'darwin') ? 'arm64' : 'aarch64'
232
+ else os_arch
233
+ end
234
+ else
235
+ Gem::Platform.local.cpu
236
+ end
229
237
 
230
238
  @native_platform = [cpu, os, version].compact.join('-')
231
239
  end
@@ -42,7 +42,7 @@ module Datadog
42
42
  logger.error do
43
43
  "remote worker client sync error: #{e.message} location: #{Array(e.backtrace).first}. skipping sync"
44
44
  end
45
- rescue StandardError => e
45
+ rescue => e
46
46
  # In case of unexpected errors, reset the negotiation object
47
47
  # given external conditions have changed and the negotiation
48
48
  # negotiation object stores error logging state that should be reset.
@@ -50,8 +50,8 @@ module Datadog
50
50
 
51
51
  # Transient errors due to network or agent. Logged the error but not via telemetry
52
52
  logger.error do
53
- "remote worker error: #{e.class.name} #{e.message} location: #{Array(e.backtrace).first}. "\
54
- 'reseting client state'
53
+ "remote worker error: #{e.class.name} #{e.message} location: #{Array(e.backtrace).first}. " \
54
+ 'resetting client state'
55
55
  end
56
56
 
57
57
  # client state is unknown, state might be corrupted