ddtrace 1.7.0 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +50 -1
  3. data/README.md +2 -2
  4. data/ext/ddtrace_profiling_loader/extconf.rb +4 -1
  5. data/ext/ddtrace_profiling_native_extension/NativeExtensionDesign.md +1 -1
  6. data/ext/ddtrace_profiling_native_extension/clock_id_from_pthread.c +3 -2
  7. data/ext/ddtrace_profiling_native_extension/collectors_cpu_and_wall_time.c +15 -41
  8. data/ext/ddtrace_profiling_native_extension/collectors_cpu_and_wall_time.h +1 -1
  9. data/ext/ddtrace_profiling_native_extension/collectors_cpu_and_wall_time_worker.c +284 -74
  10. data/ext/ddtrace_profiling_native_extension/collectors_dynamic_sampling_rate.c +142 -0
  11. data/ext/ddtrace_profiling_native_extension/collectors_dynamic_sampling_rate.h +14 -0
  12. data/ext/ddtrace_profiling_native_extension/collectors_idle_sampling_helper.c +241 -0
  13. data/ext/ddtrace_profiling_native_extension/collectors_idle_sampling_helper.h +3 -0
  14. data/ext/ddtrace_profiling_native_extension/extconf.rb +21 -7
  15. data/ext/ddtrace_profiling_native_extension/helpers.h +5 -0
  16. data/ext/ddtrace_profiling_native_extension/native_extension_helpers.rb +8 -0
  17. data/ext/ddtrace_profiling_native_extension/private_vm_api_access.c +108 -24
  18. data/ext/ddtrace_profiling_native_extension/private_vm_api_access.h +9 -0
  19. data/ext/ddtrace_profiling_native_extension/profiling.c +205 -0
  20. data/ext/ddtrace_profiling_native_extension/ruby_helpers.c +86 -0
  21. data/ext/ddtrace_profiling_native_extension/ruby_helpers.h +28 -6
  22. data/ext/ddtrace_profiling_native_extension/setup_signal_handler.c +23 -4
  23. data/ext/ddtrace_profiling_native_extension/setup_signal_handler.h +4 -0
  24. data/ext/ddtrace_profiling_native_extension/stack_recorder.c +15 -18
  25. data/ext/ddtrace_profiling_native_extension/time_helpers.c +17 -0
  26. data/ext/ddtrace_profiling_native_extension/time_helpers.h +10 -0
  27. data/lib/datadog/core/configuration/components.rb +27 -6
  28. data/lib/datadog/core/configuration/ext.rb +18 -0
  29. data/lib/datadog/core/configuration/settings.rb +14 -341
  30. data/lib/datadog/core/diagnostics/health.rb +4 -22
  31. data/lib/datadog/core/environment/variable_helpers.rb +58 -10
  32. data/lib/datadog/core/utils.rb +0 -21
  33. data/lib/datadog/core.rb +21 -1
  34. data/lib/datadog/opentracer/distributed_headers.rb +2 -2
  35. data/lib/datadog/profiling/collectors/cpu_and_wall_time_worker.rb +16 -5
  36. data/lib/datadog/profiling/collectors/dynamic_sampling_rate.rb +14 -0
  37. data/lib/datadog/profiling/collectors/idle_sampling_helper.rb +68 -0
  38. data/lib/datadog/profiling/stack_recorder.rb +14 -0
  39. data/lib/datadog/profiling.rb +2 -0
  40. data/lib/datadog/tracing/configuration/ext.rb +33 -3
  41. data/lib/datadog/tracing/configuration/settings.rb +433 -0
  42. data/lib/datadog/tracing/contrib/aws/configuration/settings.rb +4 -1
  43. data/lib/datadog/tracing/contrib/aws/ext.rb +1 -0
  44. data/lib/datadog/tracing/contrib/dalli/configuration/settings.rb +4 -1
  45. data/lib/datadog/tracing/contrib/dalli/ext.rb +1 -0
  46. data/lib/datadog/tracing/contrib/elasticsearch/configuration/settings.rb +5 -1
  47. data/lib/datadog/tracing/contrib/elasticsearch/ext.rb +1 -0
  48. data/lib/datadog/tracing/contrib/ethon/configuration/settings.rb +6 -1
  49. data/lib/datadog/tracing/contrib/ethon/ext.rb +1 -0
  50. data/lib/datadog/tracing/contrib/excon/configuration/settings.rb +5 -1
  51. data/lib/datadog/tracing/contrib/excon/ext.rb +1 -0
  52. data/lib/datadog/tracing/contrib/faraday/configuration/settings.rb +5 -1
  53. data/lib/datadog/tracing/contrib/faraday/ext.rb +1 -0
  54. data/lib/datadog/tracing/contrib/grpc/configuration/settings.rb +6 -1
  55. data/lib/datadog/tracing/contrib/grpc/distributed/propagation.rb +9 -4
  56. data/lib/datadog/tracing/contrib/grpc/ext.rb +1 -0
  57. data/lib/datadog/tracing/contrib/http/configuration/settings.rb +6 -1
  58. data/lib/datadog/tracing/contrib/http/distributed/propagation.rb +9 -4
  59. data/lib/datadog/tracing/contrib/http/ext.rb +1 -0
  60. data/lib/datadog/tracing/contrib/httpclient/configuration/settings.rb +6 -1
  61. data/lib/datadog/tracing/contrib/httpclient/ext.rb +1 -0
  62. data/lib/datadog/tracing/contrib/httprb/configuration/settings.rb +6 -1
  63. data/lib/datadog/tracing/contrib/httprb/ext.rb +1 -0
  64. data/lib/datadog/tracing/contrib/mongodb/configuration/settings.rb +5 -1
  65. data/lib/datadog/tracing/contrib/mongodb/ext.rb +1 -0
  66. data/lib/datadog/tracing/contrib/mysql2/configuration/settings.rb +4 -1
  67. data/lib/datadog/tracing/contrib/mysql2/ext.rb +1 -0
  68. data/lib/datadog/tracing/contrib/mysql2/instrumentation.rb +2 -2
  69. data/lib/datadog/tracing/contrib/patcher.rb +3 -2
  70. data/lib/datadog/tracing/contrib/pg/configuration/settings.rb +4 -1
  71. data/lib/datadog/tracing/contrib/pg/ext.rb +1 -0
  72. data/lib/datadog/tracing/contrib/pg/instrumentation.rb +12 -2
  73. data/lib/datadog/tracing/contrib/presto/configuration/settings.rb +4 -1
  74. data/lib/datadog/tracing/contrib/presto/ext.rb +1 -0
  75. data/lib/datadog/tracing/contrib/propagation/sql_comment/ext.rb +1 -0
  76. data/lib/datadog/tracing/contrib/propagation/sql_comment.rb +10 -12
  77. data/lib/datadog/tracing/contrib/redis/configuration/settings.rb +4 -1
  78. data/lib/datadog/tracing/contrib/redis/ext.rb +1 -0
  79. data/lib/datadog/tracing/contrib/redis/instrumentation.rb +30 -23
  80. data/lib/datadog/tracing/contrib/redis/integration.rb +34 -2
  81. data/lib/datadog/tracing/contrib/redis/patcher.rb +18 -14
  82. data/lib/datadog/tracing/contrib/redis/quantize.rb +12 -9
  83. data/lib/datadog/tracing/contrib/redis/tags.rb +4 -6
  84. data/lib/datadog/tracing/contrib/redis/trace_middleware.rb +72 -0
  85. data/lib/datadog/tracing/contrib/rest_client/configuration/settings.rb +6 -1
  86. data/lib/datadog/tracing/contrib/rest_client/ext.rb +1 -0
  87. data/lib/datadog/{core → tracing}/diagnostics/ext.rb +1 -6
  88. data/lib/datadog/tracing/diagnostics/health.rb +40 -0
  89. data/lib/datadog/tracing/distributed/{b3.rb → b3_multi.rb} +2 -2
  90. data/lib/datadog/tracing/distributed/helpers.rb +2 -1
  91. data/lib/datadog/tracing/distributed/none.rb +19 -0
  92. data/lib/datadog/tracing/distributed/trace_context.rb +369 -0
  93. data/lib/datadog/tracing/metadata/ext.rb +1 -1
  94. data/lib/datadog/tracing/sampling/priority_sampler.rb +11 -0
  95. data/lib/datadog/tracing/sampling/rate_sampler.rb +3 -3
  96. data/lib/datadog/tracing/span.rb +3 -19
  97. data/lib/datadog/tracing/span_operation.rb +5 -4
  98. data/lib/datadog/tracing/trace_digest.rb +75 -2
  99. data/lib/datadog/tracing/trace_operation.rb +5 -4
  100. data/lib/datadog/tracing/utils.rb +50 -0
  101. data/lib/ddtrace/version.rb +1 -1
  102. metadata +20 -5
@@ -3,9 +3,11 @@
3
3
 
4
4
  require_relative 'fetcher'
5
5
  require_relative '../../../distributed/propagation'
6
- require_relative '../../../distributed/b3'
6
+ require_relative '../../../distributed/b3_multi'
7
7
  require_relative '../../../distributed/b3_single'
8
8
  require_relative '../../../distributed/datadog'
9
+ require_relative '../../../distributed/none'
10
+ require_relative '../../../distributed/trace_context'
9
11
 
10
12
  module Datadog
11
13
  module Tracing
@@ -17,12 +19,15 @@ module Datadog
17
19
  def initialize
18
20
  super(
19
21
  propagation_styles: {
20
- Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_B3 =>
21
- Tracing::Distributed::B3.new(fetcher: Fetcher),
22
+ Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_B3_MULTI_HEADER =>
23
+ Tracing::Distributed::B3Multi.new(fetcher: Fetcher),
22
24
  Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_B3_SINGLE_HEADER =>
23
25
  Tracing::Distributed::B3Single.new(fetcher: Fetcher),
24
26
  Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_DATADOG =>
25
- Tracing::Distributed::Datadog.new(fetcher: Fetcher)
27
+ Tracing::Distributed::Datadog.new(fetcher: Fetcher),
28
+ Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_TRACE_CONTEXT =>
29
+ Tracing::Distributed::TraceContext.new(fetcher: Fetcher),
30
+ Tracing::Configuration::Ext::Distributed::PROPAGATION_STYLE_NONE => Tracing::Distributed::None.new
26
31
  })
27
32
  end
28
33
  end
@@ -8,6 +8,7 @@ module Datadog
8
8
  # @public_api Changing resource names, tag names, or environment variables creates breaking changes.
9
9
  module Ext
10
10
  ENV_ENABLED = 'DD_TRACE_HTTP_ENABLED'.freeze
11
+ ENV_SERVICE_NAME = 'DD_TRACE_NET_HTTP_SERVICE_NAME'.freeze
11
12
  ENV_ANALYTICS_ENABLED = 'DD_TRACE_HTTP_ANALYTICS_ENABLED'.freeze
12
13
  ENV_ANALYTICS_SAMPLE_RATE = 'DD_TRACE_HTTP_ANALYTICS_SAMPLE_RATE'.freeze
13
14
  DEFAULT_PEER_SERVICE_NAME = 'net/http'.freeze
@@ -27,7 +27,12 @@ module Datadog
27
27
  end
28
28
 
29
29
  option :distributed_tracing, default: true
30
- option :service_name, default: Ext::DEFAULT_PEER_SERVICE_NAME
30
+
31
+ option :service_name do |o|
32
+ o.default { ENV.fetch(Ext::ENV_SERVICE_NAME, Ext::DEFAULT_PEER_SERVICE_NAME) }
33
+ o.lazy
34
+ end
35
+
31
36
  option :split_by_domain, default: false
32
37
  end
33
38
  end
@@ -8,6 +8,7 @@ module Datadog
8
8
  # @public_api Changing resource names, tag names, or environment variables creates breaking changes.
9
9
  module Ext
10
10
  ENV_ENABLED = 'DD_TRACE_HTTPCLIENT_ENABLED'.freeze
11
+ ENV_SERVICE_NAME = 'DD_TRACE_HTTPCLIENT_SERVICE_NAME'.freeze
11
12
  ENV_ANALYTICS_ENABLED = 'DD_TRACE_HTTPCLIENT_ANALYTICS_ENABLED'.freeze
12
13
  ENV_ANALYTICS_SAMPLE_RATE = 'DD_TRACE_HTTPCLIENT_ANALYTICS_SAMPLE_RATE'.freeze
13
14
  DEFAULT_PEER_SERVICE_NAME = 'httpclient'.freeze
@@ -27,7 +27,12 @@ module Datadog
27
27
  end
28
28
 
29
29
  option :distributed_tracing, default: true
30
- option :service_name, default: Ext::DEFAULT_PEER_SERVICE_NAME
30
+
31
+ option :service_name do |o|
32
+ o.default { ENV.fetch(Ext::ENV_SERVICE_NAME, Ext::DEFAULT_PEER_SERVICE_NAME) }
33
+ o.lazy
34
+ end
35
+
31
36
  option :split_by_domain, default: false
32
37
  end
33
38
  end
@@ -8,6 +8,7 @@ module Datadog
8
8
  # @public_api Changing resource names, tag names, or environment variables creates breaking changes.
9
9
  module Ext
10
10
  ENV_ENABLED = 'DD_TRACE_HTTPRB_ENABLED'.freeze
11
+ ENV_SERVICE_NAME = 'DD_TRACE_HTTPRB_SERVICE_NAME'.freeze
11
12
  ENV_ANALYTICS_ENABLED = 'DD_TRACE_HTTPRB_ANALYTICS_ENABLED'.freeze
12
13
  ENV_ANALYTICS_SAMPLE_RATE = 'DD_TRACE_EHTTPRB_ANALYTICS_SAMPLE_RATE'.freeze
13
14
  DEFAULT_PEER_SERVICE_NAME = 'httprb'.freeze
@@ -29,7 +29,11 @@ module Datadog
29
29
  end
30
30
 
31
31
  option :quantize, default: DEFAULT_QUANTIZE
32
- option :service_name, default: Ext::DEFAULT_PEER_SERVICE_NAME
32
+
33
+ option :service_name do |o|
34
+ o.default { ENV.fetch(Ext::ENV_SERVICE_NAME, Ext::DEFAULT_PEER_SERVICE_NAME) }
35
+ o.lazy
36
+ end
33
37
  end
34
38
  end
35
39
  end
@@ -8,6 +8,7 @@ module Datadog
8
8
  # @public_api Changing resource names, tag names, or environment variables creates breaking changes.
9
9
  module Ext
10
10
  ENV_ENABLED = 'DD_TRACE_MONGO_ENABLED'.freeze
11
+ ENV_SERVICE_NAME = 'DD_TRACE_MONGO_SERVICE_NAME'.freeze
11
12
  ENV_ANALYTICS_ENABLED = 'DD_TRACE_MONGO_ANALYTICS_ENABLED'.freeze
12
13
  ENV_ANALYTICS_SAMPLE_RATE = 'DD_TRACE_MONGO_ANALYTICS_SAMPLE_RATE'.freeze
13
14
  DEFAULT_PEER_SERVICE_NAME = 'mongodb'.freeze
@@ -28,7 +28,10 @@ module Datadog
28
28
  o.lazy
29
29
  end
30
30
 
31
- option :service_name, default: Ext::DEFAULT_PEER_SERVICE_NAME
31
+ option :service_name do |o|
32
+ o.default { ENV.fetch(Ext::ENV_SERVICE_NAME, Ext::DEFAULT_PEER_SERVICE_NAME) }
33
+ o.lazy
34
+ end
32
35
 
33
36
  option :comment_propagation do |o|
34
37
  o.default do
@@ -8,6 +8,7 @@ module Datadog
8
8
  # @public_api Changing resource names, tag names, or environment variables creates breaking changes.
9
9
  module Ext
10
10
  ENV_ENABLED = 'DD_TRACE_MYSQL2_ENABLED'.freeze
11
+ ENV_SERVICE_NAME = 'DD_TRACE_MYSQL2_SERVICE_NAME'.freeze
11
12
  ENV_ANALYTICS_ENABLED = 'DD_TRACE_MYSQL2_ANALYTICS_ENABLED'.freeze
12
13
  ENV_ANALYTICS_SAMPLE_RATE = 'DD_TRACE_MYSQL2_ANALYTICS_SAMPLE_RATE'.freeze
13
14
  DEFAULT_PEER_SERVICE_NAME = 'mysql2'.freeze
@@ -22,7 +22,7 @@ module Datadog
22
22
  def query(sql, options = {})
23
23
  service = Datadog.configuration_for(self, :service_name) || datadog_configuration[:service_name]
24
24
 
25
- Tracing.trace(Ext::SPAN_QUERY, service: service) do |span|
25
+ Tracing.trace(Ext::SPAN_QUERY, service: service) do |span, trace_op|
26
26
  span.resource = sql
27
27
  span.span_type = Tracing::Metadata::Ext::SQL::TYPE
28
28
 
@@ -46,7 +46,7 @@ module Datadog
46
46
  propagation_mode = Contrib::Propagation::SqlComment::Mode.new(comment_propagation)
47
47
 
48
48
  Contrib::Propagation::SqlComment.annotate!(span, propagation_mode)
49
- sql = Contrib::Propagation::SqlComment.prepend_comment(sql, span, propagation_mode)
49
+ sql = Contrib::Propagation::SqlComment.prepend_comment(sql, span, trace_op, propagation_mode)
50
50
 
51
51
  super(sql, options)
52
52
  end
@@ -66,14 +66,15 @@ module Datadog
66
66
  Datadog.health_metrics.error_instrumentation_patch(1, tags: tags)
67
67
  end
68
68
 
69
- private
70
-
71
69
  def default_tags
72
70
  ["patcher:#{patch_name}"].tap do |tags|
73
71
  tags << "target_version:#{target_version}" if respond_to?(:target_version) && !target_version.nil?
72
+ super.each { |t| tags << t } if defined?(super)
74
73
  end
75
74
  end
76
75
 
76
+ private
77
+
77
78
  def patch_only_once
78
79
  # NOTE: This is not thread-safe
79
80
  @patch_only_once ||= Core::Utils::OnlyOnce.new
@@ -28,7 +28,10 @@ module Datadog
28
28
  o.lazy
29
29
  end
30
30
 
31
- option :service_name, default: Ext::DEFAULT_PEER_SERVICE_NAME
31
+ option :service_name do |o|
32
+ o.default { ENV.fetch(Ext::ENV_SERVICE_NAME, Ext::DEFAULT_PEER_SERVICE_NAME) }
33
+ o.lazy
34
+ end
32
35
 
33
36
  option :comment_propagation do |o|
34
37
  o.default do
@@ -8,6 +8,7 @@ module Datadog
8
8
  # @public_api Changing resource names, tag names, or environment variables creates breaking changes.
9
9
  module Ext
10
10
  ENV_ENABLED = 'DD_TRACE_PG_ENABLED'.freeze
11
+ ENV_SERVICE_NAME = 'DD_TRACE_PG_SERVICE_NAME'.freeze
11
12
  ENV_ANALYTICS_ENABLED = 'DD_TRACE_PG_ANALYTICS_ENABLED'.freeze
12
13
  ENV_ANALYTICS_SAMPLE_RATE = 'DD_TRACE_PG_ANALYTICS_SAMPLE_RATE'.freeze
13
14
  DEFAULT_PEER_SERVICE_NAME = 'pg'.freeze
@@ -80,7 +80,12 @@ module Datadog
80
80
  service = Datadog.configuration_for(self, :service_name) || datadog_configuration[:service_name]
81
81
  resource = statement_name || sql
82
82
 
83
- Tracing.trace(name, service: service, resource: resource, type: Tracing::Metadata::Ext::SQL::TYPE) do |span|
83
+ Tracing.trace(
84
+ name,
85
+ service: service,
86
+ resource: resource,
87
+ type: Tracing::Metadata::Ext::SQL::TYPE
88
+ ) do |span, trace_op|
84
89
  annotate_span_with_query!(span, service)
85
90
  # Set analytics sample rate
86
91
  Contrib::Analytics.set_sample_rate(span, analytics_sample_rate) if analytics_enabled?
@@ -88,7 +93,12 @@ module Datadog
88
93
  if sql
89
94
  propagation_mode = Contrib::Propagation::SqlComment::Mode.new(comment_propagation)
90
95
  Contrib::Propagation::SqlComment.annotate!(span, propagation_mode)
91
- propagated_sql_statement = Contrib::Propagation::SqlComment.prepend_comment(sql, span, propagation_mode)
96
+ propagated_sql_statement = Contrib::Propagation::SqlComment.prepend_comment(
97
+ sql,
98
+ span,
99
+ trace_op,
100
+ propagation_mode
101
+ )
92
102
  end
93
103
 
94
104
  result = yield(propagated_sql_statement)
@@ -26,7 +26,10 @@ module Datadog
26
26
  o.lazy
27
27
  end
28
28
 
29
- option :service_name, default: Ext::DEFAULT_PEER_SERVICE_NAME
29
+ option :service_name do |o|
30
+ o.default { ENV.fetch(Ext::ENV_SERVICE_NAME, Ext::DEFAULT_PEER_SERVICE_NAME) }
31
+ o.lazy
32
+ end
30
33
  end
31
34
  end
32
35
  end
@@ -8,6 +8,7 @@ module Datadog
8
8
  # @public_api Changing resource names, tag names, or environment variables creates breaking changes.
9
9
  module Ext
10
10
  ENV_ENABLED = 'DD_TRACE_PRESTO_ENABLED'.freeze
11
+ ENV_SERVICE_NAME = 'DD_TRACE_PRESTO_SERVICE_NAME'.freeze
11
12
  ENV_ANALYTICS_ENABLED = 'DD_TRACE_PRESTO_ANALYTICS_ENABLED'.freeze
12
13
  ENV_ANALYTICS_SAMPLE_RATE = 'DD_TRACE_PRESTO_ANALYTICS_SAMPLE_RATE'.freeze
13
14
  DEFAULT_PEER_SERVICE_NAME = 'presto'.freeze
@@ -24,6 +24,7 @@ module Datadog
24
24
  KEY_ENVIRONMENT = 'dde'.freeze
25
25
  KEY_PARENT_SERVICE = 'ddps'.freeze
26
26
  KEY_VERSION = 'ddpv'.freeze
27
+ KEY_TRACEPARENT = 'traceparent'.freeze
27
28
  end
28
29
  end
29
30
  end
@@ -3,6 +3,8 @@
3
3
  require_relative 'sql_comment/comment'
4
4
  require_relative 'sql_comment/ext'
5
5
 
6
+ require_relative '../../distributed/trace_context'
7
+
6
8
  module Datadog
7
9
  module Tracing
8
10
  module Contrib
@@ -12,11 +14,12 @@ module Datadog
12
14
  def self.annotate!(span_op, mode)
13
15
  return unless mode.enabled?
14
16
 
15
- # PENDING: Until `traceparent`` implementation in `full` mode
16
- # span_op.set_tag(Ext::TAG_DBM_TRACE_INJECTED, true) if mode.full?
17
+ span_op.set_tag(Ext::TAG_DBM_TRACE_INJECTED, true) if mode.full?
17
18
  end
18
19
 
19
- def self.prepend_comment(sql, span_op, mode)
20
+ # Inject span_op and trace_op instead of TraceDigest to improve memory usage
21
+ # for `disabled` and `service` mode
22
+ def self.prepend_comment(sql, span_op, trace_op, mode)
20
23
  return sql unless mode.enabled?
21
24
 
22
25
  tags = {
@@ -26,8 +29,10 @@ module Datadog
26
29
  Ext::KEY_VERSION => datadog_configuration.version
27
30
  }
28
31
 
29
- # PENDING: Until `traceparent`` implementation in `full` mode
30
- # tags.merge!(trace_context(span_op)) if mode.full?
32
+ if mode.full?
33
+ tags[Ext::KEY_TRACEPARENT] =
34
+ Tracing::Distributed::TraceContext.new(fetcher: nil).send(:build_traceparent, trace_op.to_digest)
35
+ end
31
36
 
32
37
  "#{Comment.new(tags)} #{sql}"
33
38
  end
@@ -35,13 +40,6 @@ module Datadog
35
40
  def self.datadog_configuration
36
41
  Datadog.configuration
37
42
  end
38
-
39
- # TODO: Derive from trace
40
- def self.trace_context(_)
41
- {
42
- # traceparent: '00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01'
43
- }.freeze
44
- end
45
43
  end
46
44
  end
47
45
  end
@@ -31,7 +31,10 @@ module Datadog
31
31
  o.lazy
32
32
  end
33
33
 
34
- option :service_name, default: Ext::DEFAULT_PEER_SERVICE_NAME
34
+ option :service_name do |o|
35
+ o.default { ENV.fetch(Ext::ENV_SERVICE_NAME, Ext::DEFAULT_PEER_SERVICE_NAME) }
36
+ o.lazy
37
+ end
35
38
  end
36
39
  end
37
40
  end
@@ -8,6 +8,7 @@ module Datadog
8
8
  # @public_api Changing resource names, tag names, or environment variables creates breaking changes.
9
9
  module Ext
10
10
  ENV_ENABLED = 'DD_TRACE_REDIS_ENABLED'.freeze
11
+ ENV_SERVICE_NAME = 'DD_TRACE_REDIS_SERVICE_NAME'.freeze
11
12
  ENV_ANALYTICS_ENABLED = 'DD_TRACE_REDIS_ANALYTICS_ENABLED'.freeze
12
13
  ENV_ANALYTICS_SAMPLE_RATE = 'DD_TRACE_REDIS_ANALYTICS_SAMPLE_RATE'.freeze
13
14
  ENV_COMMAND_ARGS = 'DD_REDIS_COMMAND_ARGS'.freeze
@@ -10,7 +10,7 @@ module Datadog
10
10
  module Tracing
11
11
  module Contrib
12
12
  module Redis
13
- # Instrumentation for Redis
13
+ # Instrumentation for Redis < 5
14
14
  module Instrumentation
15
15
  def self.included(base)
16
16
  base.prepend(InstanceMethods)
@@ -19,51 +19,58 @@ module Datadog
19
19
  # InstanceMethods - implementing instrumentation
20
20
  module InstanceMethods
21
21
  def call(*args, &block)
22
- response = nil
22
+ show_command_args = command_args?
23
+
23
24
  Tracing.trace(Contrib::Redis::Ext::SPAN_COMMAND) do |span|
24
- span.service = Datadog.configuration_for(redis_instance, :service_name) ||
25
- datadog_configuration[:service_name]
25
+ span.service = service_name
26
26
  span.span_type = Contrib::Redis::Ext::TYPE
27
- span.resource = get_command(args)
28
- Contrib::Redis::Tags.set_common_tags(self, span)
29
- span.set_tag(Tracing::Metadata::Ext::TAG_KIND, Tracing::Metadata::Ext::SpanKind::TAG_CLIENT)
27
+ span.resource = get_command(args, show_command_args)
28
+ Contrib::Redis::Tags.set_common_tags(self, span, show_command_args)
30
29
 
31
- response = super
30
+ super
32
31
  end
33
-
34
- response
35
32
  end
36
33
 
37
34
  def call_pipeline(*args, &block)
38
- response = nil
35
+ show_command_args = command_args?
36
+
39
37
  Tracing.trace(Contrib::Redis::Ext::SPAN_COMMAND) do |span|
40
- span.service = Datadog.configuration_for(redis_instance, :service_name) ||
41
- datadog_configuration[:service_name]
38
+ span.service = service_name
42
39
  span.span_type = Contrib::Redis::Ext::TYPE
43
- commands = get_pipeline_commands(args)
40
+ commands = get_pipeline_commands(args, show_command_args)
44
41
  span.resource = commands.any? ? commands.join("\n") : '(none)'
45
42
  span.set_metric Contrib::Redis::Ext::METRIC_PIPELINE_LEN, commands.length
46
- Contrib::Redis::Tags.set_common_tags(self, span)
47
- span.set_tag(Tracing::Metadata::Ext::TAG_KIND, Tracing::Metadata::Ext::SpanKind::TAG_CLIENT)
43
+ Contrib::Redis::Tags.set_common_tags(self, span, show_command_args)
48
44
 
49
- response = super
45
+ super
50
46
  end
51
-
52
- response
53
47
  end
54
48
 
55
49
  private
56
50
 
57
- def get_command(args)
58
- if datadog_configuration[:command_args]
51
+ def command_args?
52
+ pinned = Datadog.configuration_for(redis_instance, :command_args)
53
+
54
+ return pinned unless pinned.nil?
55
+
56
+ datadog_configuration[:command_args]
57
+ end
58
+
59
+ def service_name
60
+ Datadog.configuration_for(redis_instance, :service_name) ||
61
+ datadog_configuration[:service_name]
62
+ end
63
+
64
+ def get_command(args, show_command_args)
65
+ if show_command_args
59
66
  Contrib::Redis::Quantize.format_command_args(*args)
60
67
  else
61
68
  Contrib::Redis::Quantize.get_verb(*args)
62
69
  end
63
70
  end
64
71
 
65
- def get_pipeline_commands(args)
66
- if datadog_configuration[:command_args]
72
+ def get_pipeline_commands(args, show_command_args)
73
+ if show_command_args
67
74
  args[0].commands.map { |c| Contrib::Redis::Quantize.format_command_args(c) }
68
75
  else
69
76
  args[0].commands.map { |c| Contrib::Redis::Quantize.get_verb(c) }
@@ -13,21 +13,53 @@ module Datadog
13
13
  include Contrib::Integration
14
14
 
15
15
  MINIMUM_VERSION = Gem::Version.new('3.2')
16
- MAX_VERSION = Gem::Version.new('5')
16
+
17
+ # Support `Config#custom`
18
+ # https://github.com/redis-rb/redis-client/blob/master/CHANGELOG.md#0110
19
+ REDISCLIENT_MINIMUM_VERSION = Gem::Version.new('0.11.0')
17
20
 
18
21
  # @public_api Changing the integration name or integration options can cause breaking changes
19
22
  register_as :redis, auto_patch: true
20
23
 
24
+ # Until Redis 4, all instrumentation happened in one gem: redis.
25
+ # Since Redis 5, instrumentation happens in a separate gem: redis-client.
26
+ # Because Redis 4 does not depend on redis-client, it's possible for both gems to be installed at the same time.
27
+ # For example, if Sidekiq 7 and Redis 4 are installed: both redis and redis-client will be installed.
28
+ # If redis-client and redis > 5 are installed, than they will be in sync, and only redis-client will be installed.
21
29
  def self.version
30
+ redis_version || redis_client_version
31
+ end
32
+
33
+ def self.redis_version
22
34
  Gem.loaded_specs['redis'] && Gem.loaded_specs['redis'].version
23
35
  end
24
36
 
37
+ def self.redis_client_version
38
+ Gem.loaded_specs['redis-client'] && Gem.loaded_specs['redis-client'].version
39
+ end
40
+
25
41
  def self.loaded?
42
+ redis_loaded? || redis_client_loaded?
43
+ end
44
+
45
+ def self.redis_loaded?
26
46
  !defined?(::Redis).nil?
27
47
  end
28
48
 
49
+ def self.redis_client_loaded?
50
+ !defined?(::RedisClient).nil?
51
+ end
52
+
29
53
  def self.compatible?
30
- super && version >= MINIMUM_VERSION && version < MAX_VERSION
54
+ super && (redis_compatible? || redis_client_compatible?)
55
+ end
56
+
57
+ def self.redis_compatible?
58
+ !!(redis_version && redis_version >= MINIMUM_VERSION)
59
+ end
60
+
61
+ def self.redis_client_compatible?
62
+ !!(redis_client_version && redis_client_version >= REDISCLIENT_MINIMUM_VERSION)
31
63
  end
32
64
 
33
65
  def new_configuration
@@ -1,4 +1,4 @@
1
- # typed: false
1
+ # typed: ignore
2
2
 
3
3
  require_relative '../patcher'
4
4
  require_relative 'ext'
@@ -49,24 +49,28 @@ module Datadog
49
49
 
50
50
  module_function
51
51
 
52
- def target_version
53
- Integration.version
52
+ def default_tags
53
+ [].tap do |tags|
54
+ tags << "target_redis_version:#{Integration.redis_version}" if Integration.redis_version
55
+ tags << "target_redis_client_version:#{Integration.redis_client_version}" if Integration.redis_client_version
56
+ end
54
57
  end
55
58
 
56
- # patch applies our patch if needed
57
59
  def patch
58
- # do not require these by default, but only when actually patching
59
- require 'redis'
60
- require_relative 'tags'
61
- require_relative 'quantize'
62
- require_relative 'instrumentation'
60
+ # Redis 5+ extracts RedisClient to its own gem and provide instrumentation interface
61
+ if Integration.redis_client_compatible?
62
+ require_relative 'trace_middleware'
63
+
64
+ ::RedisClient.register(TraceMiddleware)
65
+ end
63
66
 
64
- # InstancePatch and ClientPatch allows the client object to access pin on redis instance
65
- ::Redis.include(InstancePatch)
66
- ::Redis::Client.include(ClientPatch)
67
+ if Integration.redis_compatible? && Integration.redis_version < Gem::Version.new('5.0.0')
68
+ require_relative 'instrumentation'
67
69
 
68
- # TODO: To support redis-rb 5.x, Redis::Client -> RedisClient
69
- ::Redis::Client.include(Instrumentation)
70
+ ::Redis.include(InstancePatch)
71
+ ::Redis::Client.include(ClientPatch)
72
+ ::Redis::Client.include(Instrumentation)
73
+ end
70
74
  end
71
75
  end
72
76
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # typed: true
2
4
 
3
5
  require 'set'
@@ -8,11 +10,13 @@ module Datadog
8
10
  module Redis
9
11
  # Quantize contains Redis-specific resource quantization tools.
10
12
  module Quantize
11
- PLACEHOLDER = '?'.freeze
12
- TOO_LONG_MARK = '...'.freeze
13
+ PLACEHOLDER = '?'
14
+ TOO_LONG_MARK = '...'
13
15
  VALUE_MAX_LEN = 50
14
16
  CMD_MAX_LEN = 500
15
17
 
18
+ AUTH_COMMANDS = %w[AUTH auth].freeze
19
+
16
20
  MULTI_VERB_COMMANDS = Set.new(
17
21
  %w[
18
22
  ACL
@@ -29,8 +33,7 @@ module Datadog
29
33
  module_function
30
34
 
31
35
  def format_arg(arg)
32
- str = arg.is_a?(Symbol) ? arg.to_s.upcase : arg.to_s
33
- str = Core::Utils.utf8_encode(str, binary: true, placeholder: PLACEHOLDER)
36
+ str = Core::Utils.utf8_encode(arg, binary: true, placeholder: PLACEHOLDER)
34
37
  Core::Utils.truncate(str, VALUE_MAX_LEN, TOO_LONG_MARK)
35
38
  rescue => e
36
39
  Datadog.logger.debug("non formattable Redis arg #{str}: #{e}")
@@ -41,8 +44,8 @@ module Datadog
41
44
  command_args = resolve_command_args(command_args)
42
45
  return 'AUTH ?' if auth_command?(command_args)
43
46
 
44
- cmd = command_args.map { |x| format_arg(x) }.join(' ')
45
- Core::Utils.truncate(cmd, CMD_MAX_LEN, TOO_LONG_MARK)
47
+ verb, *args = command_args.map { |x| format_arg(x) }
48
+ Core::Utils.truncate("#{verb.upcase} #{args.join(' ')}", CMD_MAX_LEN, TOO_LONG_MARK)
46
49
  end
47
50
 
48
51
  def get_verb(command_args)
@@ -50,8 +53,7 @@ module Datadog
50
53
 
51
54
  return get_verb(command_args.first) if command_args.first.is_a?(Array)
52
55
 
53
- arg = command_args.first
54
- verb = arg.is_a?(Symbol) ? arg.to_s.upcase : arg.to_s
56
+ verb = command_args.first.to_s.upcase
55
57
  return verb unless MULTI_VERB_COMMANDS.include?(verb) && command_args[1]
56
58
 
57
59
  "#{verb} #{command_args[1]}"
@@ -60,7 +62,8 @@ module Datadog
60
62
  def auth_command?(command_args)
61
63
  return false unless command_args.is_a?(Array) && !command_args.empty?
62
64
 
63
- command_args.first.to_sym == :auth
65
+ verb = command_args.first.to_s
66
+ AUTH_COMMANDS.include?(verb)
64
67
  end
65
68
 
66
69
  # Unwraps command array when Redis is called with the following syntax:
@@ -12,7 +12,7 @@ module Datadog
12
12
  # Tags handles generic common tags assignment.
13
13
  module Tags
14
14
  class << self
15
- def set_common_tags(client, span)
15
+ def set_common_tags(client, span, show_command_args)
16
16
  span.set_tag(Tracing::Metadata::Ext::TAG_COMPONENT, Ext::TAG_COMPONENT)
17
17
  span.set_tag(Tracing::Metadata::Ext::TAG_OPERATION, Ext::TAG_OPERATION_COMMAND)
18
18
 
@@ -20,6 +20,8 @@ module Datadog
20
20
  span.set_tag(Tracing::Metadata::Ext::TAG_PEER_SERVICE, span.service)
21
21
  span.set_tag(Tracing::Metadata::Ext::TAG_PEER_HOSTNAME, client.host)
22
22
 
23
+ span.set_tag(Tracing::Metadata::Ext::TAG_KIND, Tracing::Metadata::Ext::SpanKind::TAG_CLIENT)
24
+
23
25
  # Set analytics sample rate
24
26
  Contrib::Analytics.set_sample_rate(span, analytics_sample_rate) if analytics_enabled?
25
27
 
@@ -30,7 +32,7 @@ module Datadog
30
32
 
31
33
  span.set_tag Ext::TAG_DATABASE_INDEX, client.db.to_s
32
34
  span.set_tag Ext::TAG_DB, client.db
33
- span.set_tag Ext::TAG_RAW_COMMAND, span.resource if show_command_args?
35
+ span.set_tag Ext::TAG_RAW_COMMAND, span.resource if show_command_args
34
36
  end
35
37
 
36
38
  private
@@ -46,10 +48,6 @@ module Datadog
46
48
  def analytics_sample_rate
47
49
  datadog_configuration[:analytics_sample_rate]
48
50
  end
49
-
50
- def show_command_args?
51
- datadog_configuration[:command_args]
52
- end
53
51
  end
54
52
  end
55
53
  end