datadog 2.15.0 → 2.16.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 (121) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +46 -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/extconf.rb +3 -0
  6. data/ext/datadog_profiling_native_extension/heap_recorder.c +8 -1
  7. data/ext/libdatadog_api/crashtracker.c +1 -9
  8. data/ext/libdatadog_api/crashtracker.h +5 -0
  9. data/ext/libdatadog_api/datadog_ruby_common.c +1 -4
  10. data/ext/libdatadog_api/datadog_ruby_common.h +7 -0
  11. data/ext/libdatadog_api/init.c +15 -0
  12. data/ext/libdatadog_api/library_config.c +122 -0
  13. data/ext/libdatadog_api/library_config.h +19 -0
  14. data/ext/libdatadog_api/process_discovery.c +117 -0
  15. data/ext/libdatadog_api/process_discovery.h +5 -0
  16. data/lib/datadog/appsec/actions_handler.rb +3 -2
  17. data/lib/datadog/appsec/assets/waf_rules/recommended.json +1344 -0
  18. data/lib/datadog/appsec/assets/waf_rules/strict.json +1344 -0
  19. data/lib/datadog/appsec/autoload.rb +1 -1
  20. data/lib/datadog/appsec/component.rb +11 -4
  21. data/lib/datadog/appsec/configuration/settings.rb +31 -18
  22. data/lib/datadog/appsec/context.rb +1 -1
  23. data/lib/datadog/appsec/contrib/active_record/instrumentation.rb +10 -12
  24. data/lib/datadog/appsec/contrib/active_record/integration.rb +1 -1
  25. data/lib/datadog/appsec/contrib/active_record/patcher.rb +22 -22
  26. data/lib/datadog/appsec/contrib/devise/data_extractor.rb +2 -3
  27. data/lib/datadog/appsec/contrib/devise/ext.rb +1 -0
  28. data/lib/datadog/appsec/contrib/devise/integration.rb +1 -1
  29. data/lib/datadog/appsec/contrib/devise/patcher.rb +3 -5
  30. data/lib/datadog/appsec/contrib/devise/tracking_middleware.rb +17 -4
  31. data/lib/datadog/appsec/contrib/excon/integration.rb +1 -1
  32. data/lib/datadog/appsec/contrib/excon/ssrf_detection_middleware.rb +9 -10
  33. data/lib/datadog/appsec/contrib/faraday/integration.rb +1 -1
  34. data/lib/datadog/appsec/contrib/faraday/ssrf_detection_middleware.rb +8 -9
  35. data/lib/datadog/appsec/contrib/graphql/gateway/watcher.rb +8 -9
  36. data/lib/datadog/appsec/contrib/graphql/integration.rb +1 -1
  37. data/lib/datadog/appsec/contrib/rack/gateway/watcher.rb +22 -32
  38. data/lib/datadog/appsec/contrib/rack/integration.rb +1 -1
  39. data/lib/datadog/appsec/contrib/rack/request_middleware.rb +16 -16
  40. data/lib/datadog/appsec/contrib/rails/gateway/watcher.rb +11 -13
  41. data/lib/datadog/appsec/contrib/rails/integration.rb +1 -1
  42. data/lib/datadog/appsec/contrib/rails/patcher.rb +21 -21
  43. data/lib/datadog/appsec/contrib/rest_client/integration.rb +1 -1
  44. data/lib/datadog/appsec/contrib/rest_client/request_ssrf_detection_patch.rb +10 -11
  45. data/lib/datadog/appsec/contrib/sinatra/gateway/watcher.rb +17 -23
  46. data/lib/datadog/appsec/contrib/sinatra/integration.rb +1 -1
  47. data/lib/datadog/appsec/event.rb +85 -95
  48. data/lib/datadog/appsec/instrumentation/gateway/argument.rb +5 -2
  49. data/lib/datadog/appsec/metrics/telemetry.rb +1 -1
  50. data/lib/datadog/appsec/monitor/gateway/watcher.rb +42 -12
  51. data/lib/datadog/appsec/processor/rule_loader.rb +26 -28
  52. data/lib/datadog/appsec/processor/rule_merger.rb +5 -5
  53. data/lib/datadog/appsec/processor.rb +1 -1
  54. data/lib/datadog/appsec/remote.rb +14 -13
  55. data/lib/datadog/appsec/response.rb +6 -6
  56. data/lib/datadog/appsec/security_engine/runner.rb +1 -1
  57. data/lib/datadog/appsec/security_event.rb +39 -0
  58. data/lib/datadog/appsec.rb +1 -1
  59. data/lib/datadog/core/configuration/agentless_settings_resolver.rb +176 -0
  60. data/lib/datadog/core/configuration/components.rb +19 -10
  61. data/lib/datadog/core/configuration/option.rb +61 -25
  62. data/lib/datadog/core/configuration/settings.rb +10 -0
  63. data/lib/datadog/core/configuration/stable_config.rb +23 -0
  64. data/lib/datadog/core/configuration.rb +24 -0
  65. data/lib/datadog/core/crashtracking/component.rb +1 -9
  66. data/lib/datadog/core/environment/git.rb +1 -0
  67. data/lib/datadog/core/environment/variable_helpers.rb +1 -1
  68. data/lib/datadog/core/metrics/client.rb +8 -7
  69. data/lib/datadog/core/process_discovery.rb +32 -0
  70. data/lib/datadog/core/remote/client.rb +7 -0
  71. data/lib/datadog/core/runtime/metrics.rb +1 -1
  72. data/lib/datadog/core/telemetry/component.rb +60 -50
  73. data/lib/datadog/core/telemetry/emitter.rb +17 -11
  74. data/lib/datadog/core/telemetry/event.rb +7 -4
  75. data/lib/datadog/core/telemetry/http/adapters/net.rb +12 -97
  76. data/lib/datadog/core/telemetry/request.rb +3 -3
  77. data/lib/datadog/core/telemetry/transport/http/api.rb +43 -0
  78. data/lib/datadog/core/telemetry/transport/http/client.rb +49 -0
  79. data/lib/datadog/core/telemetry/transport/http/telemetry.rb +92 -0
  80. data/lib/datadog/core/telemetry/transport/http.rb +63 -0
  81. data/lib/datadog/core/telemetry/transport/telemetry.rb +52 -0
  82. data/lib/datadog/core/telemetry/worker.rb +45 -0
  83. data/lib/datadog/core/utils/time.rb +12 -0
  84. data/lib/datadog/core/workers/async.rb +20 -2
  85. data/lib/datadog/core/workers/interval_loop.rb +12 -1
  86. data/lib/datadog/core/workers/runtime_metrics.rb +2 -2
  87. data/lib/datadog/core.rb +8 -0
  88. data/lib/datadog/di/boot.rb +34 -0
  89. data/lib/datadog/di/remote.rb +2 -0
  90. data/lib/datadog/di.rb +5 -32
  91. data/lib/datadog/error_tracking/collector.rb +87 -0
  92. data/lib/datadog/error_tracking/component.rb +167 -0
  93. data/lib/datadog/error_tracking/configuration/settings.rb +63 -0
  94. data/lib/datadog/error_tracking/configuration.rb +11 -0
  95. data/lib/datadog/error_tracking/ext.rb +18 -0
  96. data/lib/datadog/error_tracking/extensions.rb +16 -0
  97. data/lib/datadog/error_tracking/filters.rb +77 -0
  98. data/lib/datadog/error_tracking.rb +18 -0
  99. data/lib/datadog/kit/identity.rb +1 -1
  100. data/lib/datadog/profiling/exporter.rb +1 -1
  101. data/lib/datadog/tracing/analytics.rb +1 -1
  102. data/lib/datadog/tracing/contrib/karafka/distributed/propagation.rb +2 -0
  103. data/lib/datadog/tracing/contrib/karafka/monitor.rb +1 -1
  104. data/lib/datadog/tracing/contrib/mongodb/configuration/settings.rb +8 -0
  105. data/lib/datadog/tracing/contrib/mongodb/ext.rb +1 -0
  106. data/lib/datadog/tracing/contrib/mongodb/subscribers.rb +18 -1
  107. data/lib/datadog/tracing/distributed/b3_multi.rb +1 -1
  108. data/lib/datadog/tracing/distributed/b3_single.rb +1 -1
  109. data/lib/datadog/tracing/distributed/datadog.rb +2 -2
  110. data/lib/datadog/tracing/sampling/rate_sampler.rb +2 -1
  111. data/lib/datadog/tracing/span_operation.rb +38 -14
  112. data/lib/datadog/tracing/trace_operation.rb +15 -7
  113. data/lib/datadog/tracing/tracer.rb +7 -3
  114. data/lib/datadog/tracing/utils.rb +1 -1
  115. data/lib/datadog/version.rb +1 -1
  116. data/lib/datadog.rb +2 -3
  117. metadata +34 -8
  118. data/lib/datadog/core/telemetry/http/env.rb +0 -20
  119. data/lib/datadog/core/telemetry/http/ext.rb +0 -28
  120. data/lib/datadog/core/telemetry/http/response.rb +0 -70
  121. data/lib/datadog/core/telemetry/http/transport.rb +0 -90
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'logger'
4
+ require_relative 'base'
5
+ require_relative 'error'
6
+ require_relative 'code_tracker'
7
+ require_relative 'component'
8
+ require_relative 'instrumenter'
9
+ require_relative 'probe'
10
+ require_relative 'probe_builder'
11
+ require_relative 'probe_manager'
12
+ require_relative 'probe_notification_builder'
13
+ require_relative 'probe_notifier_worker'
14
+ require_relative 'redactor'
15
+ require_relative 'serializer'
16
+ require_relative 'transport/http'
17
+ require_relative 'utils'
18
+
19
+ if %w[1 true yes].include?(ENV['DD_DYNAMIC_INSTRUMENTATION_ENABLED']) # steep:ignore
20
+ # For initial release of Dynamic Instrumentation, activate code tracking
21
+ # only if DI is explicitly requested in the environment.
22
+ # Code tracking is required for line probes to work; see the comments
23
+ # above for the implementation of the method.
24
+ #
25
+ # If DI is enabled programmatically, the application can (and must,
26
+ # for line probes to work) activate tracking in an initializer.
27
+ # We seem to have Datadog.logger here already
28
+ Datadog.logger.debug("di: activating code tracking")
29
+ Datadog::DI.activate_tracking
30
+ end
31
+
32
+ require_relative 'contrib'
33
+
34
+ Datadog::DI::Contrib.load_now_or_later
@@ -18,6 +18,8 @@ module Datadog
18
18
  PRODUCT = 'LIVE_DEBUGGING'
19
19
 
20
20
  def products
21
+ # TODO: do not send our product on unsupported runtimes
22
+ # (Ruby 2.5 / JRuby)
21
23
  [PRODUCT]
22
24
  end
23
25
 
data/lib/datadog/di.rb CHANGED
@@ -1,24 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'di/logger'
4
- require_relative 'di/base'
5
- require_relative 'di/error'
6
- require_relative 'di/code_tracker'
7
- require_relative 'di/component'
8
3
  require_relative 'di/configuration'
9
4
  require_relative 'di/extensions'
10
- require_relative 'di/instrumenter'
11
- require_relative 'di/probe'
12
- require_relative 'di/probe_builder'
13
- require_relative 'di/probe_manager'
14
- require_relative 'di/probe_notification_builder'
15
- require_relative 'di/probe_notifier_worker'
16
- require_relative 'di/redactor'
17
5
  require_relative 'di/remote'
18
- require_relative 'di/serializer'
19
- #require_relative 'di/transport'
20
- require_relative 'di/transport/http'
21
- require_relative 'di/utils'
22
6
 
23
7
  module Datadog
24
8
  # Namespace for Datadog dynamic instrumentation.
@@ -52,19 +36,8 @@ module Datadog
52
36
  end
53
37
  end
54
38
 
55
- if %w(1 true).include?(ENV['DD_DYNAMIC_INSTRUMENTATION_ENABLED']) # steep:ignore
56
- # For initial release of Dynamic Instrumentation, activate code tracking
57
- # only if DI is explicitly requested in the environment.
58
- # Code tracking is required for line probes to work; see the comments
59
- # above for the implementation of the method.
60
- #
61
- # If DI is enabled programmatically, the application can (and must,
62
- # for line probes to work) activate tracking in an initializer.
63
- # We seem to have Datadog.logger here already
64
- Datadog.logger.debug("di: activating code tracking")
65
- Datadog::DI.activate_tracking
66
- end
67
-
68
- require_relative 'di/contrib'
69
-
70
- Datadog::DI::Contrib.load_now_or_later
39
+ # Line probes will not work on Ruby < 2.6 because of lack of :script_compiled
40
+ # trace point. Activate DI automatically on supported Ruby versions but
41
+ # always load its settings so that, for example, turning DI off when
42
+ # we are on Ruby 2.5 does not produce exceptions.
43
+ require_relative 'di/boot' if RUBY_VERSION >= '2.6' && RUBY_ENGINE != 'jruby'
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'ext'
4
+
5
+ module Datadog
6
+ module ErrorTracking
7
+ # The Collector is in charge, for a SpanOperation of storing the span events
8
+ # created when an error is handled. Each SpanOperation has a Collector as soon
9
+ # as a span event is created and the Collector has the same life time as the SpanOp.
10
+ #
11
+ # If an error is handled then rethrown, the SpanEvent corresponding to the error
12
+ # will be deleted. That is why we do not add directly the SpanEvent to the SpanOp.
13
+ #
14
+ # @api private
15
+ class Collector
16
+ SPAN_EVENTS_LIMIT = 100
17
+ LOCK = Mutex.new
18
+ # Proc called when the span_operation :after_stop event is published
19
+ def self.after_stop
20
+ @after_stop ||= proc do |span_op, error|
21
+ # if this proc is called, we are sure that span_op has a collector
22
+ collector = span_op.get_collector_or_initialize
23
+ # if an error exited the scope of the span, we delete the corresponding SpanEvent.
24
+ collector.on_error(span_op, error) if error
25
+
26
+ span_events = collector.span_events
27
+ span_op.span_events.concat(span_events)
28
+ end
29
+ end
30
+
31
+ def initialize
32
+ @span_event_per_error = {}
33
+ end
34
+
35
+ def add_span_event(span_op, span_event, error)
36
+ # When this is the first time we add a span event for a span,
37
+ # we suscribe to the :after_stop event
38
+ if @span_event_per_error.empty?
39
+ events = span_op.send(:events)
40
+ events.after_stop.subscribe(&self.class.after_stop)
41
+
42
+ # This tag is used by the Error Tracking product to report
43
+ # the error in Error Tracking
44
+ span_op.set_tag(Ext::SPAN_EVENTS_HAS_EXCEPTION, true)
45
+ end
46
+ # Set a limit to the number of span event we can store per SpanOp
47
+ # If an error has been handled several times in the same span we can still
48
+ # modify the event (even if the capacity is reached) in order to report
49
+ # the information of the last rescue
50
+ if @span_event_per_error.key?(error) || @span_event_per_error.length < SPAN_EVENTS_LIMIT
51
+ @span_event_per_error[error] = span_event
52
+ end
53
+ end
54
+
55
+ if RUBY_VERSION >= Ext::RUBY_VERSION_WITH_RESCUE_EVENT
56
+ # Starting from ruby3.3, as we are listening to :rescue event,
57
+ # we just want to remove the span event if the error was
58
+ # previously handled
59
+ def on_error(_span_op, error)
60
+ @span_event_per_error.delete(error)
61
+ end
62
+ else
63
+ # Up to ruby3.2, we are listening to :raise event. We need to ensure
64
+ # that an error exiting the scope of a span is not handled in a parent span.
65
+ # This function will propagate the span event to the parent span. If the
66
+ # error is not handled in the parent span, it will be deleted by design.
67
+ def on_error(span_op, error)
68
+ return unless @span_event_per_error.key?(error)
69
+
70
+ unless span_op.root?
71
+ parent = span_op.send(:parent)
72
+ LOCK.synchronize do
73
+ parent_collector = parent.get_collector_or_initialize { Collector.new }
74
+ parent_collector.add_span_event(parent, @span_event_per_error[error], error)
75
+ end
76
+ end
77
+
78
+ @span_event_per_error.delete(error)
79
+ end
80
+ end
81
+
82
+ def span_events
83
+ @span_event_per_error.values
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,167 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'set'
4
+ require_relative 'collector'
5
+ require_relative 'filters'
6
+
7
+ module Datadog
8
+ module ErrorTracking
9
+ # Component for Error Tracking.
10
+ #
11
+ # Only one instance of the Component should ever be active.
12
+ #
13
+ # The component instance records every handled exceptions from the configured scopes
14
+ # (user, third_party packages, specified files or everything).
15
+ class Component
16
+ LOCK = Mutex.new
17
+
18
+ class << self
19
+ def build(settings, tracer, logger)
20
+ return if !settings.respond_to?(:error_tracking) || (settings.error_tracking.handled_errors.nil? &&
21
+ settings.error_tracking.handled_errors_include.empty?)
22
+
23
+ return unless environment_supported?(logger)
24
+
25
+ new(
26
+ tracer: tracer,
27
+ handled_errors: settings.error_tracking.handled_errors,
28
+ handled_errors_include: settings.error_tracking.handled_errors_include,
29
+ ).tap(&:start)
30
+ end
31
+
32
+ def environment_supported?(logger)
33
+ if RUBY_ENGINE != 'ruby'
34
+ logger.warn("error tracking: cannot enable error tracking: MRI is required, but running on #{RUBY_ENGINE}")
35
+ false
36
+ elsif RUBY_VERSION < '2.6'
37
+ logger.warn(
38
+ "error tracking: cannot enable error tracking: Ruby 2.6+ is required, but running
39
+ on #{RUBY_VERSION}"
40
+ )
41
+ false
42
+ else
43
+ true
44
+ end
45
+ end
46
+ end
47
+
48
+ def initialize(tracer:, handled_errors:, handled_errors_include:)
49
+ @tracer = tracer
50
+
51
+ # Hash containing the paths to the instrumented files
52
+ @instrumented_files = Set.new unless handled_errors_include.empty?
53
+ # Array containing file paths, file names and gems names to instrument.
54
+ # This is coming from the DD_ERROR_TRACKING_HANDLED_ERRORS_INCLUDE env variable
55
+ @handled_errors_include = handled_errors_include
56
+
57
+ # Filter function is used to filter out the exception
58
+ # we do not want to report. For instance exception from gems.
59
+ @filter_function = Filters.generate_filter(handled_errors, @instrumented_files)
60
+
61
+ # :rescue event was added in Ruby 3.3
62
+ #
63
+ # Before Ruby3.3 the TracePoint listen for :raise events.
64
+ # If an error is not handled, we will delete the according
65
+ # span event in the collector.
66
+ event = (RUBY_VERSION >= '3.3') ? :rescue : :raise
67
+
68
+ # This TracePoint is in charge of capturing the handled exceptions
69
+ # and of adding the corresponding span events to the collector
70
+ @handled_exc_tracker = create_exc_tracker_trace_point(event)
71
+
72
+ if @instrumented_files
73
+ # The only thing we know about the handled errors is the path of the file
74
+ # in which the error was rescued. Therefore, we need to retrieve the path
75
+ # of the files the user want to instrument. This TracePoint is used for that
76
+ # purpose
77
+ @include_path_getter = create_script_compiled_trace_point
78
+ end
79
+ end
80
+
81
+ def create_exc_tracker_trace_point(event)
82
+ TracePoint.new(event) do |tp|
83
+ active_span = @tracer.active_span
84
+ if active_span
85
+ raised_exception = tp.raised_exception
86
+ # Note that in 3.2, this will give the path of where the error was raised
87
+ # which may cause the handled_error_include env variable to malfunction.
88
+ rescue_file_path = tp.path
89
+ if @filter_function.call(rescue_file_path)
90
+ span_event = generate_span_event(raised_exception)
91
+ LOCK.synchronize do
92
+ collector = active_span.get_collector_or_initialize { Collector.new }
93
+ collector.add_span_event(active_span, span_event, raised_exception)
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
99
+
100
+ def create_script_compiled_trace_point
101
+ TracePoint.new(:script_compiled) do |tp|
102
+ next if tp.eval_script
103
+
104
+ path = tp.instruction_sequence.path
105
+ next if path.nil?
106
+
107
+ @handled_errors_include.each do |file_to_instr|
108
+ # The user can provide either
109
+ # - absolute_path starting with '/'. In that case the path of the file
110
+ # should begin with file_to_instr
111
+ # - a relative_path starting with './'. In that case, we extend the path
112
+ # and it is the same as above
113
+ # - otherwise we just check if the name provided is in the path and is
114
+ # either the name of a folder or of a ruby file.
115
+ regex =
116
+ if file_to_instr.start_with?('/')
117
+ %r{\A#{Regexp.escape(file_to_instr)}(?:/|\.rb\z|\z)}
118
+ elsif file_to_instr.start_with?('./')
119
+ abs_path = File.expand_path(file_to_instr)
120
+ %r{\A#{Regexp.escape(abs_path)}(?:/|\.rb\z|\z)}
121
+ else
122
+ %r{/#{Regexp.escape(file_to_instr)}(?:/|\.rb\z|\z)}
123
+ end
124
+
125
+ add_instrumented_file(path) if path.match?(regex)
126
+ end
127
+ end
128
+ end
129
+
130
+ # Starts the TracePoints.
131
+ #
132
+ # Enables the script_compiled TracePoint if handled_errors_include is not empty.
133
+ def start
134
+ @handled_exc_tracker.enable
135
+ @include_path_getter&.enable
136
+ end
137
+
138
+ # Shuts down error tracker.
139
+ #
140
+ # Disables the TracePoints.
141
+ def shutdown!
142
+ @handled_exc_tracker.disable
143
+ @include_path_getter&.disable
144
+ end
145
+
146
+ private
147
+
148
+ # Generates a span event from the exception info.
149
+ #
150
+ # The event follows the otel semantics.
151
+ # https://opentelemetry.io/docs/specs/otel/trace/exceptions/
152
+ def generate_span_event(exception)
153
+ formatted_exception = Datadog::Core::Error.build_from(exception)
154
+ attributes = {
155
+ 'exception.type' => formatted_exception.type,
156
+ 'exception.message' => formatted_exception.message,
157
+ 'exception.stacktrace' => formatted_exception.backtrace
158
+ }
159
+ Datadog::Tracing::SpanEvent.new('exception', attributes: attributes)
160
+ end
161
+
162
+ def add_instrumented_file(file_path)
163
+ @instrumented_files&.add(file_path)
164
+ end
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../ext'
4
+
5
+ module Datadog
6
+ module ErrorTracking
7
+ module Configuration
8
+ # Settings
9
+ module Settings
10
+ def self.extended(base)
11
+ base = base.singleton_class unless base.is_a?(Class)
12
+ add_settings!(base)
13
+ end
14
+
15
+ def self.add_settings!(base)
16
+ base.class_eval do
17
+ # Error Tracking specific configurations.
18
+ # @public_api
19
+ settings :error_tracking do
20
+ # Enable automatic reporting of handled errors and defines the scope
21
+ # for which to report errors: user code, gems, or both. Possible
22
+ # values are: all | user | third_party.
23
+ #
24
+ # @default 'DD_ERROR_TRACKING_HANDLED_ERRORS' environment variable, otherwise `nil`
25
+ # @return [String, nil]
26
+ option :handled_errors do |o|
27
+ o.type :string, nilable: true
28
+ o.default Ext::DEFAULT_HANDLED_ERRORS
29
+ o.env Ext::ENV_HANDLED_ERRORS
30
+ o.setter do |value|
31
+ next value if Ext::VALID_HANDLED_ERRORS.include?(value)
32
+
33
+ unless value.nil?
34
+ Datadog.logger.warn(
35
+ "Invalid handled errors scope: #{value}. " \
36
+ "Supported values are: #{Ext::VALID_HANDLED_ERRORS.join(" | ")}. " \
37
+ 'Deactivating the feature.'
38
+ )
39
+ end
40
+
41
+ Ext::DEFAULT_HANDLED_ERRORS
42
+ end
43
+ end
44
+
45
+ # Enable automatic reporting of handled errors and specify what files should be
46
+ # instrumented. The value is a list of comma separated paths, filenames or gem names.
47
+ # The paths can be absolute, starting with '/' or relative to directory in which the program
48
+ # is launched, starting with './'.
49
+ #
50
+ # @default 'DD_ERROR_TRACKING_HANDLED_ERRORS_MODULES' environment variable, otherwise `nil`
51
+ # @return [String, nil]
52
+ option :handled_errors_include do |o|
53
+ o.type :array
54
+ o.default []
55
+ o.env Ext::ENV_HANDLED_ERRORS_INCLUDE
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datadog
4
+ module ErrorTracking
5
+ # Configuration for ErrorTracking
6
+ module Configuration
7
+ end
8
+ end
9
+ end
10
+
11
+ require_relative 'configuration/settings'
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datadog
4
+ module ErrorTracking
5
+ # Error Tracking constants
6
+ module Ext
7
+ ENV_HANDLED_ERRORS = 'DD_ERROR_TRACKING_HANDLED_ERRORS'
8
+ ENV_HANDLED_ERRORS_INCLUDE = 'DD_ERROR_TRACKING_HANDLED_ERRORS_INCLUDE'
9
+ HANDLED_ERRORS_ALL = 'all'
10
+ HANDLED_ERRORS_USER = 'user'
11
+ HANDLED_ERRORS_THIRD_PARTY = 'third_party'
12
+ DEFAULT_HANDLED_ERRORS = nil
13
+ VALID_HANDLED_ERRORS = [HANDLED_ERRORS_ALL, HANDLED_ERRORS_USER, HANDLED_ERRORS_THIRD_PARTY].freeze
14
+ SPAN_EVENTS_HAS_EXCEPTION = '_dd.span_events.has_exception'
15
+ RUBY_VERSION_WITH_RESCUE_EVENT = '3.3'
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'configuration'
4
+ require_relative '../core/configuration'
5
+
6
+ module Datadog
7
+ module ErrorTracking
8
+ # Extends Datadog tracing with ErrorTracking features
9
+ module Extensions
10
+ # Inject Error Tracking into global objects.
11
+ def self.activate!
12
+ Core::Configuration::Settings.extend(Configuration::Settings)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datadog
4
+ module ErrorTracking
5
+ # Based on configuration, the TracePoint listening to :rescue or :raise
6
+ # may report more handled errors than we want to report. Therefore we need
7
+ # a function to filter the events. As the filter function both depends
8
+ # on configuration and is called numerous time, we generate it during
9
+ # during the initialization of the feature to have the best performance
10
+ # possible.
11
+ #
12
+ # @api private
13
+ module Filters
14
+ module_function
15
+
16
+ def get_gem_name(file_path)
17
+ regex = %r{gems/([^/]+)-\d}
18
+ regex_match = regex.match(file_path)
19
+ return unless regex_match
20
+
21
+ gem_name = regex_match[1]
22
+
23
+ begin
24
+ Gem::Specification.find_by_name(gem_name) # steep:ignore
25
+ rescue Gem::MissingSpecError
26
+ nil
27
+ end
28
+ end
29
+
30
+ def user_code?(file_path)
31
+ !get_gem_name(file_path)
32
+ end
33
+
34
+ def datadog_code?(file_path)
35
+ file_path.include?('lib/datadog/')
36
+ end
37
+
38
+ def third_party_code?(file_path)
39
+ gem_name = get_gem_name(file_path)
40
+ gem_name && gem_name != "datadog"
41
+ end
42
+
43
+ def file_included?(file_path, instrumented_files)
44
+ instrumented_files.include?(file_path)
45
+ end
46
+
47
+ # Generate the proc used in the TracePoint
48
+ def generate_filter(to_instrument_scope, handled_errors_include = nil)
49
+ case to_instrument_scope
50
+ # If DD_ERROR_TRACKING_HANDLED_ERRORS is set
51
+ when 'all'
52
+ proc { |file_path| !datadog_code?(file_path) }
53
+ when 'user'
54
+ # If DD_ERROR_TRACKING_HANDLED_ERRORS_INCLUDE is set
55
+ if handled_errors_include
56
+ proc { |file_path|
57
+ user_code?(file_path) || file_included?(file_path, handled_errors_include)
58
+ }
59
+ else
60
+ proc { |file_path| user_code?(file_path) }
61
+ end
62
+ when 'third_party'
63
+ if handled_errors_include
64
+ proc { |file_path|
65
+ third_party_code?(file_path) || file_included?(file_path, handled_errors_include)
66
+ }
67
+ else
68
+ proc { |file_path| third_party_code?(file_path) }
69
+ end
70
+ else
71
+ # If only DD_ERROR_TRACKING_HANDLED_ERRORS_INCLUDE is set
72
+ proc { |file_path| file_included?(file_path, handled_errors_include) }
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'error_tracking/collector'
4
+ require_relative 'error_tracking/component'
5
+ require_relative 'error_tracking/configuration'
6
+ require_relative 'error_tracking/ext'
7
+ require_relative 'error_tracking/extensions'
8
+ require_relative 'error_tracking/filters'
9
+
10
+ module Datadog
11
+ # Namespace for Datadog ErrorTracking.
12
+ #
13
+ # @api private
14
+ module ErrorTracking
15
+ # Expose ErrorTracking to global shared objects
16
+ Extensions.activate!
17
+ end
18
+ end
@@ -70,7 +70,7 @@ module Datadog
70
70
  if Datadog::AppSec.active_context
71
71
  active_span.set_tag('_dd.appsec.user.collection_mode', 'sdk')
72
72
 
73
- user = ::Datadog::AppSec::Instrumentation::Gateway::User.new(id, others[:login])
73
+ user = ::Datadog::AppSec::Instrumentation::Gateway::User.new(id, others[:login], session_id)
74
74
  ::Datadog::AppSec::Instrumentation.gateway.push('identity.set_user', user)
75
75
  end
76
76
  end
@@ -49,7 +49,7 @@ module Datadog
49
49
  @internal_metadata = internal_metadata
50
50
  # NOTE: At the time of this comment collected info does not change over time so we'll hardcode
51
51
  # it on startup to prevent serializing the same info on every flush.
52
- @info_json = JSON.fast_generate(info_collector.info).freeze
52
+ @info_json = JSON.generate(info_collector.info).freeze
53
53
  end
54
54
 
55
55
  def flush
@@ -16,7 +16,7 @@ module Datadog
16
16
  def set_measured(span_op, value = true)
17
17
  return if span_op.nil?
18
18
 
19
- value = value == true || value == 1 ? 1 : 0 # rubocop:disable Style/MultipleComparison
19
+ value = value == true || value == 1 ? 1 : 0
20
20
  span_op.set_metric(Metadata::Ext::Analytics::TAG_MEASURED, value)
21
21
  end
22
22
  end
@@ -31,6 +31,8 @@ module Datadog
31
31
  Tracing::Distributed::Datadog.new(fetcher: Tracing::Distributed::Fetcher),
32
32
  Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_TRACE_CONTEXT =>
33
33
  Tracing::Distributed::TraceContext.new(fetcher: Tracing::Distributed::Fetcher),
34
+ Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_BAGGAGE =>
35
+ Tracing::Distributed::Baggage.new(fetcher: Tracing::Distributed::Fetcher),
34
36
  Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_NONE => Tracing::Distributed::None.new
35
37
  },
36
38
  propagation_style_inject: propagation_style_inject,
@@ -15,7 +15,7 @@ module Datadog
15
15
  worker.processed
16
16
  ].freeze
17
17
 
18
- def instrument(event_id, payload = EMPTY_HASH, &block)
18
+ def instrument(event_id, payload = {}, &block)
19
19
  return super unless TRACEABLE_EVENTS.include?(event_id)
20
20
 
21
21
  Datadog::Tracing.trace(Ext::SPAN_WORKER_PROCESS) do |span|
@@ -48,6 +48,14 @@ module Datadog
48
48
  o.type :string, nilable: true
49
49
  o.env Ext::ENV_PEER_SERVICE
50
50
  end
51
+
52
+ # Serializes the command to JSON format, which is the desired format for the agent and Datadog UI.
53
+ # Setting this to false is deprecated and does not have any advantages.
54
+ option :json_command do |o|
55
+ o.type :bool
56
+ o.env Ext::ENV_JSON_COMMAND
57
+ o.default false
58
+ end
51
59
  end
52
60
  end
53
61
  end
@@ -13,6 +13,7 @@ module Datadog
13
13
  # @!visibility private
14
14
  ENV_ANALYTICS_ENABLED = 'DD_TRACE_MONGO_ANALYTICS_ENABLED'
15
15
  ENV_ANALYTICS_SAMPLE_RATE = 'DD_TRACE_MONGO_ANALYTICS_SAMPLE_RATE'
16
+ ENV_JSON_COMMAND = 'DD_TRACE_MONGO_JSON_COMMAND'
16
17
  DEFAULT_PEER_SERVICE_NAME = 'mongodb'
17
18
  SPAN_COMMAND = 'mongo.cmd'
18
19
  SPAN_TYPE_COMMAND = 'mongodb'
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'json'
4
+
3
5
  require_relative '../analytics'
4
6
  require_relative 'ext'
5
7
  require_relative '../ext'
@@ -30,7 +32,7 @@ module Datadog
30
32
 
31
33
  # build a quantized Query using the Parser module
32
34
  query = MongoDB.query_builder(event.command_name, event.database_name, event.command)
33
- serialized_query = query.to_s
35
+ serialized_query = serialize_query(query)
34
36
 
35
37
  if datadog_configuration[:peer_service]
36
38
  span.set_tag(
@@ -109,6 +111,21 @@ module Datadog
109
111
 
110
112
  private
111
113
 
114
+ def serialize_query(query)
115
+ if datadog_configuration[:json_command]
116
+ query.to_json
117
+ else
118
+ # Incorrect Hash#to_s serialization. The Mongo command should only be encoded as JSON.
119
+ # This code path should be removed, and is only kept to avoid a breaking change.
120
+ Datadog::Core.log_deprecation(key: :mongo_json_command) do
121
+ 'MongoDB integration: `json_command: false` causes invalid command serialization. '\
122
+ 'Use `json_command: true` or `DD_TRACE_MONGO_JSON_COMMAND=1` instead.'
123
+ end
124
+
125
+ query.to_s
126
+ end
127
+ end
128
+
112
129
  def get_span(event)
113
130
  Thread.current[:datadog_mongo_span] \
114
131
  && Thread.current[:datadog_mongo_span][event.request_id]