ddtrace 0.29.1 → 0.30.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 (57) hide show
  1. checksums.yaml +5 -5
  2. data/.rubocop.yml +4 -0
  3. data/CHANGELOG.md +18 -1
  4. data/lib/ddtrace/context.rb +26 -10
  5. data/lib/ddtrace/contrib/action_pack/action_controller/patcher.rb +3 -15
  6. data/lib/ddtrace/contrib/action_pack/patcher.rb +3 -9
  7. data/lib/ddtrace/contrib/action_view/event.rb +39 -0
  8. data/lib/ddtrace/contrib/action_view/events.rb +30 -0
  9. data/lib/ddtrace/contrib/action_view/events/render_partial.rb +40 -0
  10. data/lib/ddtrace/contrib/action_view/events/render_template.rb +43 -0
  11. data/lib/ddtrace/contrib/action_view/instrumentation/partial_renderer.rb +4 -12
  12. data/lib/ddtrace/contrib/action_view/instrumentation/template_renderer.rb +6 -14
  13. data/lib/ddtrace/contrib/action_view/patcher.rb +19 -25
  14. data/lib/ddtrace/contrib/active_model_serializers/patcher.rb +3 -10
  15. data/lib/ddtrace/contrib/active_record/patcher.rb +3 -9
  16. data/lib/ddtrace/contrib/active_support/cache/patcher.rb +10 -24
  17. data/lib/ddtrace/contrib/active_support/patcher.rb +3 -9
  18. data/lib/ddtrace/contrib/aws/patcher.rb +7 -13
  19. data/lib/ddtrace/contrib/concurrent_ruby/patcher.rb +4 -11
  20. data/lib/ddtrace/contrib/dalli/patcher.rb +4 -10
  21. data/lib/ddtrace/contrib/delayed_job/patcher.rb +4 -10
  22. data/lib/ddtrace/contrib/elasticsearch/patcher.rb +8 -14
  23. data/lib/ddtrace/contrib/ethon/patcher.rb +7 -9
  24. data/lib/ddtrace/contrib/excon/patcher.rb +4 -11
  25. data/lib/ddtrace/contrib/faraday/patcher.rb +6 -12
  26. data/lib/ddtrace/contrib/grape/patcher.rb +7 -13
  27. data/lib/ddtrace/contrib/graphql/patcher.rb +5 -11
  28. data/lib/ddtrace/contrib/grpc/patcher.rb +7 -13
  29. data/lib/ddtrace/contrib/http/patcher.rb +3 -9
  30. data/lib/ddtrace/contrib/mongodb/patcher.rb +5 -11
  31. data/lib/ddtrace/contrib/mysql2/patcher.rb +3 -9
  32. data/lib/ddtrace/contrib/patcher.rb +38 -10
  33. data/lib/ddtrace/contrib/racecar/patcher.rb +4 -10
  34. data/lib/ddtrace/contrib/rack/patcher.rb +56 -21
  35. data/lib/ddtrace/contrib/rails/patcher.rb +4 -8
  36. data/lib/ddtrace/contrib/rake/patcher.rb +4 -10
  37. data/lib/ddtrace/contrib/redis/patcher.rb +8 -14
  38. data/lib/ddtrace/contrib/resque/patcher.rb +4 -10
  39. data/lib/ddtrace/contrib/rest_client/patcher.rb +5 -7
  40. data/lib/ddtrace/contrib/sequel/patcher.rb +4 -10
  41. data/lib/ddtrace/contrib/shoryuken/patcher.rb +4 -10
  42. data/lib/ddtrace/contrib/sidekiq/patcher.rb +12 -18
  43. data/lib/ddtrace/contrib/sinatra/patcher.rb +4 -10
  44. data/lib/ddtrace/contrib/sucker_punch/patcher.rb +7 -13
  45. data/lib/ddtrace/diagnostics/health.rb +9 -2
  46. data/lib/ddtrace/ext/diagnostics.rb +6 -0
  47. data/lib/ddtrace/ext/sampling.rb +13 -0
  48. data/lib/ddtrace/sampler.rb +49 -8
  49. data/lib/ddtrace/sampling.rb +2 -0
  50. data/lib/ddtrace/sampling/matcher.rb +57 -0
  51. data/lib/ddtrace/sampling/rate_limiter.rb +127 -0
  52. data/lib/ddtrace/sampling/rule.rb +61 -0
  53. data/lib/ddtrace/sampling/rule_sampler.rb +111 -0
  54. data/lib/ddtrace/span.rb +12 -0
  55. data/lib/ddtrace/tracer.rb +1 -0
  56. data/lib/ddtrace/version.rb +2 -2
  57. metadata +27 -4
@@ -11,20 +11,14 @@ module Datadog
11
11
 
12
12
  module_function
13
13
 
14
- def patched?
15
- done?(:mongo)
14
+ def target_version
15
+ Integration.version
16
16
  end
17
17
 
18
18
  def patch
19
- do_once(:mongo) do
20
- begin
21
- ::Mongo::Address.send(:include, Instrumentation::Address)
22
- ::Mongo::Client.send(:include, Instrumentation::Client)
23
- add_mongo_monitoring
24
- rescue StandardError => e
25
- Datadog::Tracer.log.error("Unable to apply MongoDB integration: #{e}")
26
- end
27
- end
19
+ ::Mongo::Address.send(:include, Instrumentation::Address)
20
+ ::Mongo::Client.send(:include, Instrumentation::Client)
21
+ add_mongo_monitoring
28
22
  end
29
23
 
30
24
  def add_mongo_monitoring
@@ -10,18 +10,12 @@ module Datadog
10
10
 
11
11
  module_function
12
12
 
13
- def patched?
14
- done?(:mysql2)
13
+ def target_version
14
+ Integration.version
15
15
  end
16
16
 
17
17
  def patch
18
- do_once(:mysql2) do
19
- begin
20
- patch_mysql2_client
21
- rescue StandardError => e
22
- Datadog::Tracer.log.error("Unable to apply mysql2 integration: #{e}")
23
- end
24
- end
18
+ patch_mysql2_client
25
19
  end
26
20
 
27
21
  def patch_mysql2_client
@@ -6,21 +6,49 @@ module Datadog
6
6
  module Patcher
7
7
  def self.included(base)
8
8
  base.send(:include, Datadog::Patcher)
9
- base.send(:extend, InstanceMethods)
10
- base.send(:include, InstanceMethods)
9
+
10
+ base.singleton_class.send(:prepend, CommonMethods)
11
+ base.send(:prepend, CommonMethods) if base.class == Class
11
12
  end
12
13
 
13
- # Class methods for patchers
14
- module ClassMethods
15
- def patch
16
- raise NotImplementedError, '#patch not implemented for Patcher!'
14
+ # Prepended instance methods for all patchers
15
+ module CommonMethods
16
+ def patch_name
17
+ self.class != Class && self.class != Module ? self.class.name : name
18
+ end
19
+
20
+ def patched?
21
+ done?(:patch)
17
22
  end
18
- end
19
23
 
20
- # Instance methods for patchers
21
- module InstanceMethods
22
24
  def patch
23
- raise NotImplementedError, '#patch not implemented for Patcher!'
25
+ return unless defined?(super)
26
+
27
+ do_once(:patch) do
28
+ begin
29
+ super.tap do
30
+ # Emit a metric
31
+ Diagnostics::Health.metrics.instrumentation_patched(1, tags: default_tags)
32
+ end
33
+ rescue StandardError => e
34
+ # Log the error
35
+ Datadog::Tracer.log.error("Failed to apply #{patch_name} patch. Cause: #{e} Location: #{e.backtrace.first}")
36
+
37
+ # Emit a metric
38
+ tags = default_tags
39
+ tags << "error:#{e.class.name}"
40
+
41
+ Diagnostics::Health.metrics.error_instrumentation_patch(1, tags: tags)
42
+ end
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ def default_tags
49
+ ["patcher:#{patch_name}"].tap do |tags|
50
+ tags << "target_version:#{target_version}" if respond_to?(:target_version) && !target_version.nil?
51
+ end
24
52
  end
25
53
  end
26
54
  end
@@ -12,19 +12,13 @@ module Datadog
12
12
 
13
13
  module_function
14
14
 
15
- def patched?
16
- done?(:racecar)
15
+ def target_version
16
+ Integration.version
17
17
  end
18
18
 
19
19
  def patch
20
- do_once(:racecar) do
21
- begin
22
- # Subscribe to Racecar events
23
- Events.subscribe!
24
- rescue StandardError => e
25
- Datadog::Tracer.log.error("Unable to apply Racecar integration: #{e}")
26
- end
27
- end
20
+ # Subscribe to Racecar events
21
+ Events.subscribe!
28
22
  end
29
23
  end
30
24
  end
@@ -2,36 +2,33 @@ module Datadog
2
2
  module Contrib
3
3
  module Rack
4
4
  # Provides instrumentation for `rack`
5
- module Patcher
5
+ module MiddlewarePatcher
6
6
  include Contrib::Patcher
7
7
 
8
8
  module_function
9
9
 
10
- def patched?
11
- done?(:rack)
10
+ def target_version
11
+ Integration.version
12
12
  end
13
13
 
14
14
  def patch
15
15
  # Patch middleware
16
- do_once(:rack) do
17
- require_relative 'middlewares'
18
- end
16
+ require_relative 'middlewares'
17
+ end
18
+ end
19
19
 
20
- # Patch middleware names
21
- if !done?(:rack_middleware_names) && get_option(:middleware_names)
22
- if get_option(:application)
23
- do_once(:rack_middleware_names) do
24
- patch_middleware_names
25
- end
26
- else
27
- Datadog::Tracer.log.warn(%(
28
- Rack :middleware_names requires you to also pass :application.
29
- Middleware names have NOT been patched; please provide :application.
30
- e.g. use: :rack, middleware_names: true, application: my_rack_app).freeze)
31
- end
32
- end
33
- rescue StandardError => e
34
- Datadog::Tracer.log.error("Unable to apply Rack integration: #{e}")
20
+ # Provides instrumentation for Rack middleware names
21
+ module MiddlewareNamePatcher
22
+ include Contrib::Patcher
23
+
24
+ module_function
25
+
26
+ def target_version
27
+ Integration.version
28
+ end
29
+
30
+ def patch
31
+ patch_middleware_names
35
32
  end
36
33
 
37
34
  def patch_middleware_names
@@ -67,6 +64,44 @@ module Datadog
67
64
  Datadog.configuration[:rack].get_option(option)
68
65
  end
69
66
  end
67
+
68
+ # Applies multiple patches
69
+ module Patcher
70
+ PATCHERS = [
71
+ MiddlewarePatcher,
72
+ MiddlewareNamePatcher
73
+ ].freeze
74
+
75
+ module_function
76
+
77
+ def patched?
78
+ PATCHERS.all?(&:patched?)
79
+ end
80
+
81
+ def target_version
82
+ Integration.version
83
+ end
84
+
85
+ def patch
86
+ MiddlewarePatcher.patch unless MiddlewarePatcher.patched?
87
+
88
+ # Patch middleware names
89
+ if !MiddlewareNamePatcher.patched? && get_option(:middleware_names)
90
+ if get_option(:application)
91
+ MiddlewareNamePatcher.patch
92
+ else
93
+ Datadog::Tracer.log.warn(%(
94
+ Rack :middleware_names requires you to also pass :application.
95
+ Middleware names have NOT been patched; please provide :application.
96
+ e.g. use: :rack, middleware_names: true, application: my_rack_app).freeze)
97
+ end
98
+ end
99
+ end
100
+
101
+ def get_option(option)
102
+ Datadog.configuration[:rack].get_option(option)
103
+ end
104
+ end
70
105
  end
71
106
  end
72
107
  end
@@ -12,17 +12,13 @@ module Datadog
12
12
 
13
13
  module_function
14
14
 
15
- def patched?
16
- done?(:rails)
15
+ def target_version
16
+ Integration.version
17
17
  end
18
18
 
19
19
  def patch
20
- do_once(:rails) do
21
- patch_before_intialize
22
- patch_after_intialize
23
- end
24
- rescue => e
25
- Datadog::Tracer.log.error("Unable to apply Rails integration: #{e}")
20
+ patch_before_intialize
21
+ patch_after_intialize
26
22
  end
27
23
 
28
24
  def patch_before_intialize
@@ -12,19 +12,13 @@ module Datadog
12
12
 
13
13
  module_function
14
14
 
15
- def patched?
16
- done?(:rake)
15
+ def target_version
16
+ Integration.version
17
17
  end
18
18
 
19
19
  def patch
20
- do_once(:rake) do
21
- begin
22
- # Add instrumentation patch to Rake task
23
- ::Rake::Task.send(:include, Instrumentation)
24
- rescue StandardError => e
25
- Datadog::Tracer.log.error("Unable to apply Rake integration: #{e}")
26
- end
27
- end
20
+ # Add instrumentation patch to Rake task
21
+ ::Rake::Task.send(:include, Instrumentation)
28
22
  end
29
23
 
30
24
  def get_option(option)
@@ -10,25 +10,19 @@ module Datadog
10
10
 
11
11
  module_function
12
12
 
13
- def patched?
14
- done?(:redis)
13
+ def target_version
14
+ Integration.version
15
15
  end
16
16
 
17
17
  # patch applies our patch if needed
18
18
  def patch
19
- do_once(:redis) do
20
- begin
21
- # do not require these by default, but only when actually patching
22
- require 'redis'
23
- require 'ddtrace/ext/app_types'
24
- require 'ddtrace/contrib/redis/tags'
25
- require 'ddtrace/contrib/redis/quantize'
19
+ # do not require these by default, but only when actually patching
20
+ require 'redis'
21
+ require 'ddtrace/ext/app_types'
22
+ require 'ddtrace/contrib/redis/tags'
23
+ require 'ddtrace/contrib/redis/quantize'
26
24
 
27
- patch_redis_client
28
- rescue StandardError => e
29
- Datadog::Tracer.log.error("Unable to apply Redis integration: #{e}")
30
- end
31
- end
25
+ patch_redis_client
32
26
  end
33
27
 
34
28
  # rubocop:disable Metrics/MethodLength
@@ -11,19 +11,13 @@ module Datadog
11
11
 
12
12
  module_function
13
13
 
14
- def patched?
15
- done?(:resque)
14
+ def target_version
15
+ Integration.version
16
16
  end
17
17
 
18
18
  def patch
19
- do_once(:resque) do
20
- begin
21
- require_relative 'resque_job'
22
- get_option(:workers).each { |worker| worker.extend(ResqueJob) }
23
- rescue StandardError => e
24
- Datadog::Tracer.log.error("Unable to apply Resque integration: #{e}")
25
- end
26
- end
19
+ require_relative 'resque_job'
20
+ get_option(:workers).each { |worker| worker.extend(ResqueJob) }
27
21
  end
28
22
 
29
23
  def get_option(option)
@@ -7,17 +7,15 @@ module Datadog
7
7
 
8
8
  module_function
9
9
 
10
- def patched?
11
- done?(:rest_client)
10
+ def target_version
11
+ Integration.version
12
12
  end
13
13
 
14
14
  def patch
15
- do_once(:rest_client) do
16
- require 'ddtrace/ext/app_types'
17
- require 'ddtrace/contrib/rest_client/request_patch'
15
+ require 'ddtrace/ext/app_types'
16
+ require 'ddtrace/contrib/rest_client/request_patch'
18
17
 
19
- ::RestClient::Request.send(:include, RequestPatch)
20
- end
18
+ ::RestClient::Request.send(:include, RequestPatch)
21
19
  end
22
20
  end
23
21
  end
@@ -11,19 +11,13 @@ module Datadog
11
11
 
12
12
  module_function
13
13
 
14
- def patched?
15
- done?(:sequel)
14
+ def target_version
15
+ Integration.version
16
16
  end
17
17
 
18
18
  def patch
19
- do_once(:sequel) do
20
- begin
21
- patch_sequel_database
22
- patch_sequel_dataset
23
- rescue StandardError => e
24
- Datadog::Tracer.log.error("Unable to apply Sequel integration: #{e}")
25
- end
26
- end
19
+ patch_sequel_database
20
+ patch_sequel_dataset
27
21
  end
28
22
 
29
23
  def patch_sequel_database
@@ -9,19 +9,13 @@ module Datadog
9
9
 
10
10
  module_function
11
11
 
12
- def patched?
13
- done?(:shoryuken)
12
+ def target_version
13
+ Integration.version
14
14
  end
15
15
 
16
16
  def patch
17
- do_once(:shoryuken) do
18
- begin
19
- ::Shoryuken.server_middleware do |chain|
20
- chain.add Shoryuken::Tracer
21
- end
22
- rescue StandardError => e
23
- Datadog::Tracer.log.error("Unable to apply Shoryuken integration: #{e}")
24
- end
17
+ ::Shoryuken.server_middleware do |chain|
18
+ chain.add Shoryuken::Tracer
25
19
  end
26
20
  end
27
21
  end
@@ -9,28 +9,22 @@ module Datadog
9
9
 
10
10
  module_function
11
11
 
12
- def patched?
13
- done?(:sidekiq)
12
+ def target_version
13
+ Integration.version
14
14
  end
15
15
 
16
16
  def patch
17
- do_once(:sidekiq) do
18
- begin
19
- require 'ddtrace/contrib/sidekiq/client_tracer'
20
- ::Sidekiq.configure_client do |config|
21
- config.client_middleware do |chain|
22
- chain.add(Sidekiq::ClientTracer)
23
- end
24
- end
17
+ require 'ddtrace/contrib/sidekiq/client_tracer'
18
+ ::Sidekiq.configure_client do |config|
19
+ config.client_middleware do |chain|
20
+ chain.add(Sidekiq::ClientTracer)
21
+ end
22
+ end
25
23
 
26
- require 'ddtrace/contrib/sidekiq/server_tracer'
27
- ::Sidekiq.configure_server do |config|
28
- config.server_middleware do |chain|
29
- chain.add(Sidekiq::ServerTracer)
30
- end
31
- end
32
- rescue StandardError => e
33
- Datadog::Tracer.log.error("Unable to apply Sidekiq integration: #{e}")
24
+ require 'ddtrace/contrib/sidekiq/server_tracer'
25
+ ::Sidekiq.configure_server do |config|
26
+ config.server_middleware do |chain|
27
+ chain.add(Sidekiq::ServerTracer)
34
28
  end
35
29
  end
36
30
  end