datadog 2.2.0 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (196) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +87 -2
  3. data/ext/datadog_profiling_loader/datadog_profiling_loader.c +9 -1
  4. data/ext/datadog_profiling_loader/extconf.rb +14 -26
  5. data/ext/datadog_profiling_native_extension/clock_id.h +1 -0
  6. data/ext/datadog_profiling_native_extension/clock_id_from_pthread.c +1 -2
  7. data/ext/datadog_profiling_native_extension/clock_id_noop.c +1 -2
  8. data/ext/datadog_profiling_native_extension/collectors_cpu_and_wall_time_worker.c +257 -69
  9. data/ext/datadog_profiling_native_extension/collectors_discrete_dynamic_sampler.c +53 -28
  10. data/ext/datadog_profiling_native_extension/collectors_discrete_dynamic_sampler.h +34 -4
  11. data/ext/datadog_profiling_native_extension/collectors_idle_sampling_helper.c +4 -0
  12. data/ext/datadog_profiling_native_extension/collectors_stack.c +136 -81
  13. data/ext/datadog_profiling_native_extension/collectors_stack.h +2 -2
  14. data/ext/datadog_profiling_native_extension/collectors_thread_context.c +661 -48
  15. data/ext/datadog_profiling_native_extension/collectors_thread_context.h +10 -1
  16. data/ext/datadog_profiling_native_extension/datadog_ruby_common.c +83 -0
  17. data/ext/datadog_profiling_native_extension/datadog_ruby_common.h +53 -0
  18. data/ext/datadog_profiling_native_extension/extconf.rb +91 -69
  19. data/ext/datadog_profiling_native_extension/gvl_profiling_helper.c +50 -0
  20. data/ext/datadog_profiling_native_extension/gvl_profiling_helper.h +75 -0
  21. data/ext/datadog_profiling_native_extension/heap_recorder.c +54 -12
  22. data/ext/datadog_profiling_native_extension/heap_recorder.h +3 -1
  23. data/ext/datadog_profiling_native_extension/helpers.h +6 -17
  24. data/ext/datadog_profiling_native_extension/http_transport.c +41 -9
  25. data/ext/datadog_profiling_native_extension/libdatadog_helpers.c +0 -86
  26. data/ext/datadog_profiling_native_extension/libdatadog_helpers.h +2 -23
  27. data/ext/datadog_profiling_native_extension/native_extension_helpers.rb +61 -172
  28. data/ext/datadog_profiling_native_extension/private_vm_api_access.c +116 -139
  29. data/ext/datadog_profiling_native_extension/private_vm_api_access.h +20 -11
  30. data/ext/datadog_profiling_native_extension/profiling.c +1 -3
  31. data/ext/datadog_profiling_native_extension/ruby_helpers.c +0 -33
  32. data/ext/datadog_profiling_native_extension/ruby_helpers.h +1 -26
  33. data/ext/datadog_profiling_native_extension/setup_signal_handler.h +1 -0
  34. data/ext/datadog_profiling_native_extension/stack_recorder.c +14 -2
  35. data/ext/datadog_profiling_native_extension/stack_recorder.h +2 -0
  36. data/ext/datadog_profiling_native_extension/time_helpers.c +0 -15
  37. data/ext/datadog_profiling_native_extension/time_helpers.h +36 -6
  38. data/ext/{datadog_profiling_native_extension → libdatadog_api}/crashtracker.c +37 -22
  39. data/ext/libdatadog_api/datadog_ruby_common.c +83 -0
  40. data/ext/libdatadog_api/datadog_ruby_common.h +53 -0
  41. data/ext/libdatadog_api/extconf.rb +108 -0
  42. data/ext/libdatadog_api/macos_development.md +26 -0
  43. data/ext/libdatadog_extconf_helpers.rb +130 -0
  44. data/lib/datadog/appsec/assets/waf_rules/recommended.json +2184 -108
  45. data/lib/datadog/appsec/assets/waf_rules/strict.json +1430 -2
  46. data/lib/datadog/appsec/component.rb +29 -8
  47. data/lib/datadog/appsec/configuration/settings.rb +2 -2
  48. data/lib/datadog/appsec/contrib/devise/patcher/authenticatable_patch.rb +1 -0
  49. data/lib/datadog/appsec/contrib/devise/patcher/rememberable_patch.rb +21 -0
  50. data/lib/datadog/appsec/contrib/devise/patcher.rb +12 -2
  51. data/lib/datadog/appsec/contrib/graphql/appsec_trace.rb +35 -0
  52. data/lib/datadog/appsec/contrib/graphql/gateway/multiplex.rb +109 -0
  53. data/lib/datadog/appsec/contrib/graphql/gateway/watcher.rb +71 -0
  54. data/lib/datadog/appsec/contrib/graphql/integration.rb +54 -0
  55. data/lib/datadog/appsec/contrib/graphql/patcher.rb +37 -0
  56. data/lib/datadog/appsec/contrib/graphql/reactive/multiplex.rb +59 -0
  57. data/lib/datadog/appsec/contrib/rack/gateway/request.rb +3 -6
  58. data/lib/datadog/appsec/event.rb +1 -1
  59. data/lib/datadog/appsec/processor/actions.rb +1 -1
  60. data/lib/datadog/appsec/processor/rule_loader.rb +3 -1
  61. data/lib/datadog/appsec/processor/rule_merger.rb +33 -15
  62. data/lib/datadog/appsec/processor.rb +36 -37
  63. data/lib/datadog/appsec/rate_limiter.rb +25 -40
  64. data/lib/datadog/appsec/remote.rb +7 -3
  65. data/lib/datadog/appsec/response.rb +15 -1
  66. data/lib/datadog/appsec.rb +3 -2
  67. data/lib/datadog/core/configuration/components.rb +18 -15
  68. data/lib/datadog/core/configuration/settings.rb +135 -9
  69. data/lib/datadog/core/crashtracking/agent_base_url.rb +21 -0
  70. data/lib/datadog/core/crashtracking/component.rb +111 -0
  71. data/lib/datadog/core/crashtracking/tag_builder.rb +39 -0
  72. data/lib/datadog/core/diagnostics/environment_logger.rb +8 -11
  73. data/lib/datadog/core/environment/execution.rb +5 -5
  74. data/lib/datadog/core/metrics/client.rb +7 -0
  75. data/lib/datadog/core/rate_limiter.rb +183 -0
  76. data/lib/datadog/core/remote/client/capabilities.rb +4 -3
  77. data/lib/datadog/core/remote/component.rb +4 -2
  78. data/lib/datadog/core/remote/negotiation.rb +4 -4
  79. data/lib/datadog/core/remote/tie.rb +2 -0
  80. data/lib/datadog/core/runtime/metrics.rb +1 -1
  81. data/lib/datadog/core/telemetry/component.rb +51 -2
  82. data/lib/datadog/core/telemetry/emitter.rb +9 -11
  83. data/lib/datadog/core/telemetry/event.rb +37 -1
  84. data/lib/datadog/core/telemetry/ext.rb +1 -0
  85. data/lib/datadog/core/telemetry/http/adapters/net.rb +10 -12
  86. data/lib/datadog/core/telemetry/http/ext.rb +3 -0
  87. data/lib/datadog/core/telemetry/http/transport.rb +38 -9
  88. data/lib/datadog/core/telemetry/logger.rb +51 -0
  89. data/lib/datadog/core/telemetry/logging.rb +71 -0
  90. data/lib/datadog/core/telemetry/request.rb +13 -1
  91. data/lib/datadog/core/utils/at_fork_monkey_patch.rb +102 -0
  92. data/lib/datadog/core/utils/time.rb +12 -0
  93. data/lib/datadog/di/code_tracker.rb +168 -0
  94. data/lib/datadog/di/configuration/settings.rb +163 -0
  95. data/lib/datadog/di/configuration.rb +11 -0
  96. data/lib/datadog/di/error.rb +31 -0
  97. data/lib/datadog/di/extensions.rb +16 -0
  98. data/lib/datadog/di/probe.rb +133 -0
  99. data/lib/datadog/di/probe_builder.rb +41 -0
  100. data/lib/datadog/di/redactor.rb +188 -0
  101. data/lib/datadog/di/serializer.rb +193 -0
  102. data/lib/datadog/di.rb +14 -0
  103. data/lib/datadog/kit/appsec/events.rb +2 -4
  104. data/lib/datadog/opentelemetry/sdk/propagator.rb +2 -0
  105. data/lib/datadog/opentelemetry/sdk/span_processor.rb +10 -0
  106. data/lib/datadog/opentelemetry/sdk/trace/span.rb +23 -0
  107. data/lib/datadog/profiling/collectors/code_provenance.rb +7 -7
  108. data/lib/datadog/profiling/collectors/cpu_and_wall_time_worker.rb +28 -26
  109. data/lib/datadog/profiling/collectors/idle_sampling_helper.rb +11 -13
  110. data/lib/datadog/profiling/collectors/info.rb +15 -6
  111. data/lib/datadog/profiling/collectors/thread_context.rb +30 -2
  112. data/lib/datadog/profiling/component.rb +89 -95
  113. data/lib/datadog/profiling/exporter.rb +3 -3
  114. data/lib/datadog/profiling/ext/dir_monkey_patches.rb +3 -3
  115. data/lib/datadog/profiling/ext.rb +21 -21
  116. data/lib/datadog/profiling/flush.rb +1 -1
  117. data/lib/datadog/profiling/http_transport.rb +14 -7
  118. data/lib/datadog/profiling/load_native_extension.rb +5 -5
  119. data/lib/datadog/profiling/preload.rb +1 -1
  120. data/lib/datadog/profiling/profiler.rb +5 -8
  121. data/lib/datadog/profiling/scheduler.rb +33 -25
  122. data/lib/datadog/profiling/stack_recorder.rb +3 -0
  123. data/lib/datadog/profiling/tag_builder.rb +2 -2
  124. data/lib/datadog/profiling/tasks/exec.rb +5 -5
  125. data/lib/datadog/profiling/tasks/setup.rb +16 -35
  126. data/lib/datadog/profiling.rb +4 -5
  127. data/lib/datadog/single_step_instrument.rb +12 -0
  128. data/lib/datadog/tracing/contrib/action_cable/instrumentation.rb +8 -12
  129. data/lib/datadog/tracing/contrib/action_pack/action_controller/instrumentation.rb +5 -0
  130. data/lib/datadog/tracing/contrib/action_pack/action_dispatch/instrumentation.rb +78 -0
  131. data/lib/datadog/tracing/contrib/action_pack/action_dispatch/patcher.rb +33 -0
  132. data/lib/datadog/tracing/contrib/action_pack/patcher.rb +2 -0
  133. data/lib/datadog/tracing/contrib/active_record/configuration/resolver.rb +4 -0
  134. data/lib/datadog/tracing/contrib/active_record/events/instantiation.rb +3 -1
  135. data/lib/datadog/tracing/contrib/active_record/events/sql.rb +4 -1
  136. data/lib/datadog/tracing/contrib/active_support/cache/events/cache.rb +5 -1
  137. data/lib/datadog/tracing/contrib/aws/instrumentation.rb +5 -0
  138. data/lib/datadog/tracing/contrib/elasticsearch/patcher.rb +6 -1
  139. data/lib/datadog/tracing/contrib/ext.rb +14 -0
  140. data/lib/datadog/tracing/contrib/faraday/middleware.rb +9 -0
  141. data/lib/datadog/tracing/contrib/grape/endpoint.rb +19 -0
  142. data/lib/datadog/tracing/contrib/graphql/patcher.rb +9 -12
  143. data/lib/datadog/tracing/contrib/graphql/trace_patcher.rb +3 -3
  144. data/lib/datadog/tracing/contrib/graphql/tracing_patcher.rb +3 -3
  145. data/lib/datadog/tracing/contrib/graphql/unified_trace.rb +14 -10
  146. data/lib/datadog/tracing/contrib/graphql/unified_trace_patcher.rb +10 -4
  147. data/lib/datadog/tracing/contrib/http/instrumentation.rb +18 -15
  148. data/lib/datadog/tracing/contrib/httpclient/instrumentation.rb +6 -5
  149. data/lib/datadog/tracing/contrib/httpclient/patcher.rb +1 -14
  150. data/lib/datadog/tracing/contrib/httprb/instrumentation.rb +5 -0
  151. data/lib/datadog/tracing/contrib/httprb/patcher.rb +1 -14
  152. data/lib/datadog/tracing/contrib/lograge/patcher.rb +15 -0
  153. data/lib/datadog/tracing/contrib/mongodb/subscribers.rb +2 -0
  154. data/lib/datadog/tracing/contrib/mysql2/configuration/settings.rb +5 -0
  155. data/lib/datadog/tracing/contrib/mysql2/instrumentation.rb +17 -13
  156. data/lib/datadog/tracing/contrib/opensearch/patcher.rb +13 -6
  157. data/lib/datadog/tracing/contrib/patcher.rb +2 -1
  158. data/lib/datadog/tracing/contrib/pg/configuration/settings.rb +5 -0
  159. data/lib/datadog/tracing/contrib/pg/instrumentation.rb +4 -1
  160. data/lib/datadog/tracing/contrib/presto/patcher.rb +1 -13
  161. data/lib/datadog/tracing/contrib/propagation/sql_comment/ext.rb +28 -0
  162. data/lib/datadog/tracing/contrib/propagation/sql_comment/mode.rb +5 -1
  163. data/lib/datadog/tracing/contrib/propagation/sql_comment.rb +22 -10
  164. data/lib/datadog/tracing/contrib/rack/middlewares.rb +27 -0
  165. data/lib/datadog/tracing/contrib/redis/tags.rb +4 -0
  166. data/lib/datadog/tracing/contrib/sinatra/tracer.rb +4 -0
  167. data/lib/datadog/tracing/contrib/stripe/request.rb +3 -2
  168. data/lib/datadog/tracing/contrib/trilogy/configuration/settings.rb +5 -0
  169. data/lib/datadog/tracing/contrib/trilogy/instrumentation.rb +4 -1
  170. data/lib/datadog/tracing/diagnostics/environment_logger.rb +14 -16
  171. data/lib/datadog/tracing/distributed/propagation.rb +7 -0
  172. data/lib/datadog/tracing/metadata/errors.rb +9 -1
  173. data/lib/datadog/tracing/metadata/ext.rb +6 -0
  174. data/lib/datadog/tracing/pipeline/span_filter.rb +2 -2
  175. data/lib/datadog/tracing/remote.rb +5 -2
  176. data/lib/datadog/tracing/sampling/matcher.rb +6 -1
  177. data/lib/datadog/tracing/sampling/rate_sampler.rb +1 -1
  178. data/lib/datadog/tracing/sampling/rule.rb +2 -0
  179. data/lib/datadog/tracing/sampling/rule_sampler.rb +9 -5
  180. data/lib/datadog/tracing/sampling/span/ext.rb +1 -1
  181. data/lib/datadog/tracing/sampling/span/rule.rb +2 -2
  182. data/lib/datadog/tracing/span.rb +9 -2
  183. data/lib/datadog/tracing/span_event.rb +41 -0
  184. data/lib/datadog/tracing/span_operation.rb +6 -2
  185. data/lib/datadog/tracing/trace_operation.rb +26 -2
  186. data/lib/datadog/tracing/tracer.rb +14 -12
  187. data/lib/datadog/tracing/transport/http/client.rb +1 -0
  188. data/lib/datadog/tracing/transport/io/client.rb +1 -0
  189. data/lib/datadog/tracing/transport/serializable_trace.rb +3 -0
  190. data/lib/datadog/tracing/workers/trace_writer.rb +1 -1
  191. data/lib/datadog/tracing/workers.rb +1 -1
  192. data/lib/datadog/version.rb +1 -1
  193. metadata +46 -11
  194. data/lib/datadog/profiling/crashtracker.rb +0 -91
  195. data/lib/datadog/profiling/ext/forking.rb +0 -98
  196. data/lib/datadog/tracing/sampling/rate_limiter.rb +0 -185
@@ -93,11 +93,16 @@ module Datadog
93
93
  span.resource = "#{method} #{quantized_url}"
94
94
  Contrib::SpanAttributeSchema.set_peer_service!(span, Ext::PEER_SERVICE_SOURCES)
95
95
  rescue StandardError => e
96
+ # TODO: Refactor the code to streamline the execution without ensure
96
97
  Datadog.logger.error(e.message)
98
+ Datadog::Core::Telemetry::Logger.report(e)
97
99
  ensure
98
100
  # the call is still executed
99
101
  response = super
100
- span.set_tag(Tracing::Metadata::Ext::HTTP::TAG_STATUS_CODE, response.status)
102
+
103
+ if response && response.respond_to?(:status)
104
+ span.set_tag(Tracing::Metadata::Ext::HTTP::TAG_STATUS_CODE, response.status)
105
+ end
101
106
  end
102
107
  end
103
108
  response
@@ -7,7 +7,21 @@ module Datadog
7
7
  module Ext
8
8
  # @public_api
9
9
  module DB
10
+ # Name of the database. This is *not* the database hostname.
11
+ #
12
+ # For databases which support such a concept, the default schema/database/namespace
13
+ # as configured in the connection string.
14
+ #
15
+ # If the tracer is already tracking changes to the default schema/database throughout the lifetime of
16
+ # the session (i.e. the client executes USE {NEW_SCHEMA} and now the default schema has changed from what
17
+ # was set upon connection initialization), then ideally this attribute reflects the “current” value.
18
+ # If the tracer is not already tracking changes then just leaving it to the default value set upon
19
+ # initialization is OK.
20
+ #
21
+ # This is the equivalent of OTel’s `db.namespace`
22
+ # @see https://opentelemetry.io/docs/specs/semconv/database/database-spans/#common-attributes
10
23
  TAG_INSTANCE = 'db.instance'
24
+
11
25
  TAG_USER = 'db.user'
12
26
  TAG_SYSTEM = 'db.system'
13
27
  TAG_STATEMENT = 'db.statement'
@@ -7,6 +7,7 @@ require_relative '../http'
7
7
  require_relative '../analytics'
8
8
  require_relative 'ext'
9
9
  require_relative '../http_annotation_helper'
10
+ require_relative '../../../core/telemetry/logger'
10
11
 
11
12
  module Datadog
12
13
  module Tracing
@@ -37,6 +38,7 @@ module Datadog
37
38
 
38
39
  attr_reader :app
39
40
 
41
+ # rubocop:disable Metrics/AbcSize
40
42
  def annotate!(span, env, options)
41
43
  span.resource = resource_name(env)
42
44
  span.service = service_name(env[:url].host, options)
@@ -75,7 +77,11 @@ module Datadog
75
77
  )
76
78
 
77
79
  Contrib::SpanAttributeSchema.set_peer_service!(span, Ext::PEER_SERVICE_SOURCES)
80
+ rescue StandardError => e
81
+ Datadog.logger.error(e.message)
82
+ Datadog::Core::Telemetry::Logger.report(e)
78
83
  end
84
+ # rubocop:enable Metrics/AbcSize
79
85
 
80
86
  def handle_response(span, env, options)
81
87
  span.set_error(["Error #{env[:status]}", env[:body]]) if options[:error_status_codes].include? env[:status]
@@ -85,6 +91,9 @@ module Datadog
85
91
  span.set_tags(
86
92
  Datadog.configuration.tracing.header_tags.response_tags(env[:response_headers])
87
93
  )
94
+ rescue StandardError => e
95
+ Datadog.logger.error(e.message)
96
+ Datadog::Core::Telemetry::Logger.report(e)
88
97
  end
89
98
 
90
99
  def propagate!(trace, span, env)
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative '../../../core'
4
+ require_relative '../../../core/telemetry/logger'
4
5
  require_relative '../../metadata/ext'
5
6
  require_relative '../analytics'
6
7
  require_relative '../rack/ext'
@@ -41,6 +42,7 @@ module Datadog
41
42
 
42
43
  # collect endpoint details
43
44
  endpoint = payload.fetch(:endpoint)
45
+ env = payload.fetch(:env)
44
46
  api_view = api_view(endpoint.options[:for])
45
47
  request_method = endpoint.options.fetch(:method).first
46
48
  path = endpoint_expand_path(endpoint)
@@ -61,9 +63,22 @@ module Datadog
61
63
  span.set_tag(Tracing::Metadata::Ext::TAG_COMPONENT, Ext::TAG_COMPONENT)
62
64
  span.set_tag(Tracing::Metadata::Ext::TAG_OPERATION, Ext::TAG_OPERATION_ENDPOINT_RUN)
63
65
 
66
+ if (grape_route = env['grape.routing_args']) && grape_route[:route_info]
67
+ trace.set_tag(
68
+ Tracing::Metadata::Ext::HTTP::TAG_ROUTE,
69
+ # here we are removing the format from the path:
70
+ # e.g. /path/to/resource(.json) => /path/to/resource
71
+ # e.g. /path/to/resource(.:format) => /path/to/resource
72
+ grape_route[:route_info].path&.gsub(/\(\.:?\w+\)\z/, '')
73
+ )
74
+
75
+ trace.set_tag(Tracing::Metadata::Ext::HTTP::TAG_ROUTE_PATH, env['SCRIPT_NAME'])
76
+ end
77
+
64
78
  Thread.current[KEY_RUN] = true
65
79
  rescue StandardError => e
66
80
  Datadog.logger.error(e.message)
81
+ Datadog::Core::Telemetry::Logger.report(e)
67
82
  end
68
83
 
69
84
  def endpoint_run(name, start, finish, id, payload)
@@ -107,6 +122,7 @@ module Datadog
107
122
  end
108
123
  rescue StandardError => e
109
124
  Datadog.logger.error(e.message)
125
+ Datadog::Core::Telemetry::Logger.report(e)
110
126
  end
111
127
 
112
128
  # Status code resolution is tied to the exception handling
@@ -150,6 +166,7 @@ module Datadog
150
166
  Thread.current[KEY_RENDER] = true
151
167
  rescue StandardError => e
152
168
  Datadog.logger.error(e.message)
169
+ Datadog::Core::Telemetry::Logger.report(e)
153
170
  end
154
171
 
155
172
  def endpoint_render(name, start, finish, id, payload)
@@ -174,6 +191,7 @@ module Datadog
174
191
  end
175
192
  rescue StandardError => e
176
193
  Datadog.logger.error(e.message)
194
+ Datadog::Core::Telemetry::Logger.report(e)
177
195
  end
178
196
 
179
197
  def endpoint_run_filters(name, start, finish, id, payload)
@@ -212,6 +230,7 @@ module Datadog
212
230
  end
213
231
  rescue StandardError => e
214
232
  Datadog.logger.error(e.message)
233
+ Datadog::Core::Telemetry::Logger.report(e)
215
234
  end
216
235
 
217
236
  private
@@ -21,13 +21,18 @@ module Datadog
21
21
  end
22
22
 
23
23
  def patch
24
+ # DEV-3.0: We should remove as many patching options as possible, given the alternatives do not
25
+ # DEV-3.0: provide any benefit to the recommended `with_unified_tracer` patching method.
26
+ # DEV-3.0: `with_deprecated_tracer` is likely safe to remove.
27
+ # DEV-3.0: `with_unified_tracer: false` should be removed if possible.
28
+ # DEV-3.0: `with_unified_tracer: true` should be the default and hopefully not even necessary as an option.
24
29
  if configuration[:with_deprecated_tracer]
25
- TracingPatcher.patch!(schemas, trace_options)
30
+ TracingPatcher.patch!(schemas)
26
31
  elsif Integration.trace_supported?
27
32
  if configuration[:with_unified_tracer]
28
- UnifiedTracePatcher.patch!(schemas, trace_options)
33
+ UnifiedTracePatcher.patch!(schemas)
29
34
  else
30
- TracePatcher.patch!(schemas, trace_options)
35
+ TracePatcher.patch!(schemas)
31
36
  end
32
37
  else
33
38
  Datadog.logger.warn(
@@ -35,18 +40,10 @@ module Datadog
35
40
  'or Datadog::Tracing::Contrib::GraphQL::UnifiedTrace.'\
36
41
  'Falling back to GraphQL::Tracing::DataDogTracing.'
37
42
  )
38
- TracingPatcher.patch!(schemas, trace_options)
43
+ TracingPatcher.patch!(schemas)
39
44
  end
40
45
  end
41
46
 
42
- def trace_options
43
- {
44
- service: configuration[:service_name],
45
- analytics_enabled: Contrib::Analytics.enabled?(configuration[:analytics_enabled]),
46
- analytics_sample_rate: configuration[:analytics_sample_rate]
47
- }
48
- end
49
-
50
47
  def configuration
51
48
  Datadog.configuration.tracing[:graphql]
52
49
  end
@@ -8,12 +8,12 @@ module Datadog
8
8
  module TracePatcher
9
9
  module_function
10
10
 
11
- def patch!(schemas, options)
11
+ def patch!(schemas)
12
12
  if schemas.empty?
13
- ::GraphQL::Schema.trace_with(::GraphQL::Tracing::DataDogTrace, **options)
13
+ ::GraphQL::Schema.trace_with(::GraphQL::Tracing::DataDogTrace)
14
14
  else
15
15
  schemas.each do |schema|
16
- schema.trace_with(::GraphQL::Tracing::DataDogTrace, **options)
16
+ schema.trace_with(::GraphQL::Tracing::DataDogTrace)
17
17
  end
18
18
  end
19
19
  end
@@ -8,13 +8,13 @@ module Datadog
8
8
  module TracingPatcher
9
9
  module_function
10
10
 
11
- def patch!(schemas, options)
11
+ def patch!(schemas)
12
12
  if schemas.empty?
13
- ::GraphQL::Schema.tracer(::GraphQL::Tracing::DataDogTracing.new(**options))
13
+ ::GraphQL::Schema.tracer(::GraphQL::Tracing::DataDogTracing.new)
14
14
  else
15
15
  schemas.each do |schema|
16
16
  if schema.respond_to? :use
17
- schema.use(::GraphQL::Tracing::DataDogTracing, **options)
17
+ schema.use(::GraphQL::Tracing::DataDogTracing)
18
18
  else
19
19
  Datadog.logger.warn("Unable to patch #{schema}: Please migrate to class-based schema.")
20
20
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'graphql/tracing'
3
+ require 'graphql'
4
4
 
5
5
  module Datadog
6
6
  module Tracing
@@ -11,14 +11,7 @@ module Datadog
11
11
  # which is required to use features such as API Catalog.
12
12
  # DEV-3.0: This tracer should be the default one in the next major version.
13
13
  module UnifiedTrace
14
- # @param analytics_enabled [Boolean] Deprecated
15
- # @param analytics_sample_rate [Float] Deprecated
16
- # @param service [String|nil] The service name to be set on the spans
17
- def initialize(*args, analytics_enabled: false, analytics_sample_rate: 1.0, service: nil, **kwargs)
18
- @analytics_enabled = analytics_enabled
19
- @analytics_sample_rate = analytics_sample_rate
20
-
21
- @service_name = service
14
+ def initialize(*args, **kwargs)
22
15
  @has_prepare_span = respond_to?(:prepare_span)
23
16
  super
24
17
  end
@@ -139,7 +132,18 @@ module Datadog
139
132
  private
140
133
 
141
134
  def trace(callable, trace_key, resource, **kwargs)
142
- Tracing.trace("graphql.#{trace_key}", resource: resource, service: @service_name, type: 'graphql') do |span|
135
+ config = Datadog.configuration.tracing[:graphql]
136
+
137
+ Tracing.trace(
138
+ "graphql.#{trace_key}",
139
+ type: 'graphql',
140
+ resource: resource,
141
+ service: config[:service_name]
142
+ ) do |span|
143
+ if Contrib::Analytics.enabled?(config[:analytics_enabled])
144
+ Contrib::Analytics.set_sample_rate(span, config[:analytics_sample_rate])
145
+ end
146
+
143
147
  yield(span) if block_given?
144
148
 
145
149
  prepare_span(trace_key, kwargs, span) if @has_prepare_span
@@ -1,5 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ if Gem.loaded_specs['graphql'] && Gem.loaded_specs['graphql'].version >= Gem::Version.new('2.0.19')
4
+ require_relative 'unified_trace'
5
+ end
6
+
3
7
  module Datadog
4
8
  module Tracing
5
9
  module Contrib
@@ -8,13 +12,15 @@ module Datadog
8
12
  module UnifiedTracePatcher
9
13
  module_function
10
14
 
11
- def patch!(schemas, options)
12
- require_relative 'unified_trace'
15
+ # TODO: `GraphQL::Schema.trace_with` and `YOUR_SCHEMA.trace_with` don't mix.
16
+ # TODO: They create duplicate spans when combined.
17
+ # TODO: We should measure how frequently users use `YOUR_SCHEMA.trace_with`, and hopefully we can remove it.
18
+ def patch!(schemas)
13
19
  if schemas.empty?
14
- ::GraphQL::Schema.trace_with(UnifiedTrace, **options)
20
+ ::GraphQL::Schema.trace_with(UnifiedTrace)
15
21
  else
16
22
  schemas.each do |schema|
17
- schema.trace_with(UnifiedTrace, **options)
23
+ schema.trace_with(UnifiedTrace)
18
24
  end
19
25
  end
20
26
  end
@@ -6,6 +6,7 @@ require_relative '../../metadata/ext'
6
6
  require_relative '../analytics'
7
7
  require_relative '../http_annotation_helper'
8
8
  require_relative '../utils/quantization/http'
9
+ require_relative '../../../core/telemetry/logger'
9
10
 
10
11
  module Datadog
11
12
  module Tracing
@@ -30,23 +31,19 @@ module Datadog
30
31
  return super(req, body, &block) if Contrib::HTTP.should_skip_tracing?(req)
31
32
 
32
33
  Tracing.trace(Ext::SPAN_REQUEST, on_error: method(:annotate_span_with_error!)) do |span, trace|
33
- begin
34
- span.service = service_name(host, request_options, client_config)
35
- span.type = Tracing::Metadata::Ext::HTTP::TYPE_OUTBOUND
36
- span.resource = req.method
37
-
38
- if Tracing.enabled? && !Contrib::HTTP.should_skip_distributed_tracing?(client_config)
39
- Contrib::HTTP.inject(trace, req)
40
- end
41
-
42
- # Add additional request specific tags to the span.
43
- annotate_span_with_request!(span, req, request_options)
44
- rescue StandardError => e
45
- Datadog.logger.error("error preparing span for http request: #{e}")
46
- ensure
47
- response = super(req, body, &block)
34
+ span.service = service_name(host, request_options, client_config)
35
+ span.type = Tracing::Metadata::Ext::HTTP::TYPE_OUTBOUND
36
+ span.resource = req.method
37
+
38
+ if Tracing.enabled? && !Contrib::HTTP.should_skip_distributed_tracing?(client_config)
39
+ Contrib::HTTP.inject(trace, req)
48
40
  end
49
41
 
42
+ # Add additional request specific tags to the span.
43
+ annotate_span_with_request!(span, req, request_options)
44
+
45
+ response = super(req, body, &block)
46
+
50
47
  # Add additional response specific tags to the span.
51
48
  annotate_span_with_response!(span, response, request_options)
52
49
 
@@ -91,6 +88,9 @@ module Datadog
91
88
  )
92
89
 
93
90
  Contrib::SpanAttributeSchema.set_peer_service!(span, Ext::PEER_SERVICE_SOURCES)
91
+ rescue StandardError => e
92
+ Datadog.logger.error("error preparing span from http request: #{e}")
93
+ Datadog::Core::Telemetry::Logger.report(e)
94
94
  end
95
95
 
96
96
  def annotate_span_with_response!(span, response, request_options)
@@ -103,6 +103,9 @@ module Datadog
103
103
  span.set_tags(
104
104
  Datadog.configuration.tracing.header_tags.response_tags(response)
105
105
  )
106
+ rescue StandardError => e
107
+ Datadog.logger.error("error preparing span from http response: #{e}")
108
+ Datadog::Core::Telemetry::Logger.report(e)
106
109
  end
107
110
 
108
111
  def annotate_span_with_error!(span, error)
@@ -4,6 +4,7 @@ require_relative '../../metadata/ext'
4
4
  require_relative '../http'
5
5
  require_relative '../analytics'
6
6
  require_relative '../http_annotation_helper'
7
+ require_relative '../../../core/telemetry/logger'
7
8
 
8
9
  module Datadog
9
10
  module Tracing
@@ -36,7 +37,8 @@ module Datadog
36
37
  # Add additional request specific tags to the span.
37
38
  annotate_span_with_request!(span, req, request_options)
38
39
  rescue StandardError => e
39
- logger.error("error preparing span for httpclient request: #{e}, Source: #{e.backtrace}")
40
+ Datadog.logger.error("error preparing span for httpclient request: #{e}, Source: #{e.backtrace}")
41
+ Datadog::Core::Telemetry::Logger.report(e)
40
42
  ensure
41
43
  res = super
42
44
  end
@@ -100,6 +102,9 @@ module Datadog
100
102
  span.set_tags(
101
103
  Datadog.configuration.tracing.header_tags.response_tags(response.header)
102
104
  )
105
+ rescue StandardError => e
106
+ Datadog.logger.error("error preparing span from httpclient response: #{e}, Source: #{e.backtrace}")
107
+ Datadog::Core::Telemetry::Logger.report(e)
103
108
  end
104
109
 
105
110
  def annotate_span_with_error!(span, error)
@@ -114,10 +119,6 @@ module Datadog
114
119
  Contrib::Analytics.enabled?(request_options[:analytics_enabled])
115
120
  end
116
121
 
117
- def logger
118
- Datadog.logger
119
- end
120
-
121
122
  def should_skip_distributed_tracing?(client_config)
122
123
  return !client_config[:distributed_tracing] if client_config && client_config.key?(:distributed_tracing)
123
124
 
@@ -13,27 +13,14 @@ module Datadog
13
13
  module Patcher
14
14
  include Contrib::Patcher
15
15
 
16
- PATCH_ONLY_ONCE = Core::Utils::OnlyOnce.new
17
-
18
16
  module_function
19
17
 
20
- def patched?
21
- PATCH_ONLY_ONCE.ran?
22
- end
23
-
24
18
  def target_version
25
19
  Integration.version
26
20
  end
27
21
 
28
- # patch applies our patch
29
22
  def patch
30
- PATCH_ONLY_ONCE.run do
31
- begin
32
- ::HTTPClient.include(Instrumentation)
33
- rescue StandardError => e
34
- Datadog.logger.error("Unable to apply httpclient integration: #{e}")
35
- end
36
- end
23
+ ::HTTPClient.include(Instrumentation)
37
24
  end
38
25
  end
39
26
  end
@@ -4,6 +4,7 @@ require_relative '../../metadata/ext'
4
4
  require_relative '../http'
5
5
  require_relative '../analytics'
6
6
  require_relative '../http_annotation_helper'
7
+ require_relative '../../../core/telemetry/logger'
7
8
 
8
9
  module Datadog
9
10
  module Tracing
@@ -35,6 +36,7 @@ module Datadog
35
36
  annotate_span_with_request!(span, req, request_options)
36
37
  rescue StandardError => e
37
38
  logger.error("error preparing span for http.rb request: #{e}, Source: #{e.backtrace}")
39
+ Datadog::Core::Telemetry::Logger.report(e)
38
40
  ensure
39
41
  res = super(req, options)
40
42
  end
@@ -108,6 +110,9 @@ module Datadog
108
110
  span.set_tags(
109
111
  Datadog.configuration.tracing.header_tags.response_tags(response.headers)
110
112
  )
113
+ rescue StandardError => e
114
+ logger.error("error preparing span from http.rb response: #{e}, Source: #{e.backtrace}")
115
+ Datadog::Core::Telemetry::Logger.report(e)
111
116
  end
112
117
 
113
118
  def annotate_span_with_error!(span, error)
@@ -13,27 +13,14 @@ module Datadog
13
13
  module Patcher
14
14
  include Contrib::Patcher
15
15
 
16
- PATCH_ONLY_ONCE = Core::Utils::OnlyOnce.new
17
-
18
16
  module_function
19
17
 
20
- def patched?
21
- PATCH_ONLY_ONCE.ran?
22
- end
23
-
24
18
  def target_version
25
19
  Integration.version
26
20
  end
27
21
 
28
- # patch applies our patch
29
22
  def patch
30
- PATCH_ONLY_ONCE.run do
31
- begin
32
- ::HTTP::Client.include(Instrumentation)
33
- rescue StandardError => e
34
- Datadog.logger.error("Unable to apply httprb integration: #{e}")
35
- end
36
- end
23
+ ::HTTP::Client.include(Instrumentation)
37
24
  end
38
25
  end
39
26
  end
@@ -20,6 +20,21 @@ module Datadog
20
20
 
21
21
  # patch applies our patch
22
22
  def patch
23
+ # ActiveSupport::TaggedLogging is the default Rails logger since Rails 5
24
+ if defined?(::ActiveSupport::TaggedLogging::Formatter) &&
25
+ ::Lograge::LogSubscribers::ActionController
26
+ .logger&.formatter.is_a?(::ActiveSupport::TaggedLogging::Formatter)
27
+ Datadog.logger.warn(
28
+ 'Lograge and ActiveSupport::TaggedLogging (the default Rails log formatter) are not compatible: ' \
29
+ 'Lograge does not account for Rails log tags, creating polluted logs and breaking log formatting. ' \
30
+ 'Traces and Logs correlation may not work. ' \
31
+ 'Either: 1. Disable tagged logging in your Rails configuration ' \
32
+ '`config.logger = ActiveSupport::Logger.new(STDOUT); ' \
33
+ 'config.active_job.logger = ActiveSupport::Logger.new(STDOUT)` ' \
34
+ 'or 2. Use the `semantic_logger` gem instead of `lograge`.'
35
+ )
36
+ end
37
+
23
38
  ::Lograge::LogSubscribers::Base.include(Instrumentation)
24
39
  end
25
40
  end
@@ -70,6 +70,8 @@ module Datadog
70
70
 
71
71
  # set the resource with the quantized query
72
72
  span.resource = serialized_query
73
+ rescue StandardError => e
74
+ Datadog.logger.debug("error when handling MongoDB 'started' event: #{e}")
73
75
  end
74
76
  # rubocop:enable Metrics/AbcSize
75
77
 
@@ -48,6 +48,11 @@ module Datadog
48
48
  o.default Contrib::Propagation::SqlComment::Ext::DISABLED
49
49
  end
50
50
 
51
+ option :append_comment do |o|
52
+ o.type :bool
53
+ o.default false
54
+ end
55
+
51
56
  option :peer_service do |o|
52
57
  o.type :string, nilable: true
53
58
  o.env Ext::ENV_PEER_SERVICE
@@ -50,21 +50,14 @@ module Datadog
50
50
  # Set analytics sample rate
51
51
  Contrib::Analytics.set_sample_rate(span, analytics_sample_rate) if analytics_enabled?
52
52
 
53
+ span.set_tag(Contrib::Ext::DB::TAG_INSTANCE, query_options[:database])
53
54
  span.set_tag(Ext::TAG_DB_NAME, query_options[:database])
54
55
  span.set_tag(Tracing::Metadata::Ext::NET::TAG_TARGET_HOST, query_options[:host])
55
56
  span.set_tag(Tracing::Metadata::Ext::NET::TAG_TARGET_PORT, query_options[:port])
56
57
 
57
58
  Contrib::SpanAttributeSchema.set_peer_service!(span, Ext::PEER_SERVICE_SOURCES)
58
59
 
59
- propagation_mode = Contrib::Propagation::SqlComment::Mode.new(comment_propagation)
60
-
61
- Contrib::Propagation::SqlComment.annotate!(span, propagation_mode)
62
- sql = Contrib::Propagation::SqlComment.prepend_comment(
63
- sql,
64
- span,
65
- trace_op,
66
- propagation_mode
67
- )
60
+ sql = inject_propagation(span, sql, trace_op)
68
61
 
69
62
  super(sql, options)
70
63
  end
@@ -72,6 +65,21 @@ module Datadog
72
65
 
73
66
  private
74
67
 
68
+ def inject_propagation(span, sql, trace_op)
69
+ propagation_mode = Contrib::Propagation::SqlComment::Mode.new(
70
+ datadog_configuration[:comment_propagation],
71
+ datadog_configuration[:append_comment]
72
+ )
73
+
74
+ Contrib::Propagation::SqlComment.annotate!(span, propagation_mode)
75
+ Contrib::Propagation::SqlComment.prepend_comment(
76
+ sql,
77
+ span,
78
+ trace_op,
79
+ propagation_mode
80
+ )
81
+ end
82
+
75
83
  def datadog_configuration
76
84
  Datadog.configuration.tracing[:mysql2]
77
85
  end
@@ -83,10 +91,6 @@ module Datadog
83
91
  def analytics_sample_rate
84
92
  datadog_configuration[:analytics_sample_rate]
85
93
  end
86
-
87
- def comment_propagation
88
- datadog_configuration[:comment_propagation]
89
- end
90
94
  end
91
95
  end
92
96
  end
@@ -6,6 +6,7 @@ require_relative 'ext'
6
6
  require_relative '../ext'
7
7
  require_relative '../integration'
8
8
  require_relative '../patcher'
9
+ require_relative '../../../core/telemetry/logger'
9
10
 
10
11
  module Datadog
11
12
  module Tracing
@@ -81,6 +82,8 @@ module Datadog
81
82
  Contrib::SpanAttributeSchema.set_peer_service!(span, Ext::PEER_SERVICE_SOURCES)
82
83
  rescue StandardError => e
83
84
  Datadog.logger.error(e.message)
85
+ Datadog::Core::Telemetry::Logger.report(e)
86
+ # TODO: Refactor the code to streamline the execution without ensure
84
87
  ensure
85
88
  begin
86
89
  response = super
@@ -90,12 +93,16 @@ module Datadog
90
93
  raise
91
94
  end
92
95
  # Set post-response tags
93
- span.set_tag(Tracing::Metadata::Ext::HTTP::TAG_STATUS_CODE, response.status)
94
- if response.headers['content-length']
95
- span.set_tag(
96
- OpenSearch::Ext::TAG_RESPONSE_CONTENT_LENGTH,
97
- response.headers['content-length'].to_i
98
- )
96
+ if response
97
+ if response.respond_to?(:status)
98
+ span.set_tag(Tracing::Metadata::Ext::HTTP::TAG_STATUS_CODE, response.status)
99
+ end
100
+ if response.respond_to?(:headers) && (response.headers || {})['content-length']
101
+ span.set_tag(
102
+ OpenSearch::Ext::TAG_RESPONSE_CONTENT_LENGTH,
103
+ response.headers['content-length'].to_i
104
+ )
105
+ end
99
106
  end
100
107
  end
101
108
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative '../../core/utils/only_once'
4
+ require_relative '../../core/telemetry/logger'
4
5
 
5
6
  module Datadog
6
7
  module Tracing
@@ -49,8 +50,8 @@ module Datadog
49
50
  # Processes patching errors. This default implementation logs the error and reports relevant metrics.
50
51
  # @param e [Exception]
51
52
  def on_patch_error(e)
52
- # Log the error
53
53
  Datadog.logger.error("Failed to apply #{patch_name} patch. Cause: #{e} Location: #{Array(e.backtrace).first}")
54
+ Datadog::Core::Telemetry::Logger.report(e, description: "Failed to apply #{patch_name} patch")
54
55
 
55
56
  @patch_error_result = {
56
57
  type: e.class.name,
@@ -52,6 +52,11 @@ module Datadog
52
52
  o.default Contrib::Propagation::SqlComment::Ext::DISABLED
53
53
  end
54
54
 
55
+ option :append_comment do |o|
56
+ o.type :bool
57
+ o.default false
58
+ end
59
+
55
60
  option :peer_service do |o|
56
61
  o.type :string, nilable: true
57
62
  o.env Ext::ENV_PEER_SERVICE