ddtrace 1.4.1 → 1.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (155) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +144 -1
  3. data/LICENSE-3rdparty.csv +1 -0
  4. data/ext/ddtrace_profiling_loader/ddtrace_profiling_loader.c +9 -2
  5. data/ext/ddtrace_profiling_loader/extconf.rb +17 -0
  6. data/ext/ddtrace_profiling_native_extension/NativeExtensionDesign.md +38 -2
  7. data/ext/ddtrace_profiling_native_extension/clock_id.h +1 -0
  8. data/ext/ddtrace_profiling_native_extension/clock_id_from_pthread.c +1 -0
  9. data/ext/ddtrace_profiling_native_extension/collectors_cpu_and_wall_time.c +517 -42
  10. data/ext/ddtrace_profiling_native_extension/collectors_cpu_and_wall_time.h +3 -0
  11. data/ext/ddtrace_profiling_native_extension/collectors_cpu_and_wall_time_worker.c +208 -30
  12. data/ext/ddtrace_profiling_native_extension/collectors_stack.c +156 -46
  13. data/ext/ddtrace_profiling_native_extension/collectors_stack.h +11 -2
  14. data/ext/ddtrace_profiling_native_extension/extconf.rb +11 -1
  15. data/ext/ddtrace_profiling_native_extension/http_transport.c +83 -64
  16. data/ext/ddtrace_profiling_native_extension/libdatadog_helpers.h +4 -4
  17. data/ext/ddtrace_profiling_native_extension/native_extension_helpers.rb +3 -4
  18. data/ext/ddtrace_profiling_native_extension/private_vm_api_access.c +59 -0
  19. data/ext/ddtrace_profiling_native_extension/private_vm_api_access.h +3 -0
  20. data/ext/ddtrace_profiling_native_extension/profiling.c +10 -0
  21. data/ext/ddtrace_profiling_native_extension/ruby_helpers.c +0 -1
  22. data/ext/ddtrace_profiling_native_extension/ruby_helpers.h +4 -2
  23. data/ext/ddtrace_profiling_native_extension/stack_recorder.c +45 -29
  24. data/ext/ddtrace_profiling_native_extension/stack_recorder.h +7 -7
  25. data/lib/datadog/appsec/assets/waf_rules/recommended.json +1169 -275
  26. data/lib/datadog/appsec/assets/waf_rules/risky.json +78 -78
  27. data/lib/datadog/appsec/assets/waf_rules/strict.json +278 -88
  28. data/lib/datadog/appsec/configuration/settings.rb +0 -2
  29. data/lib/datadog/appsec/contrib/rack/gateway/watcher.rb +25 -20
  30. data/lib/datadog/appsec/contrib/rack/reactive/request.rb +11 -11
  31. data/lib/datadog/appsec/contrib/rack/reactive/request_body.rb +11 -11
  32. data/lib/datadog/appsec/contrib/rack/reactive/response.rb +11 -11
  33. data/lib/datadog/appsec/contrib/rack/request.rb +3 -0
  34. data/lib/datadog/appsec/contrib/rack/request_middleware.rb +46 -19
  35. data/lib/datadog/appsec/contrib/rails/gateway/watcher.rb +7 -6
  36. data/lib/datadog/appsec/contrib/rails/integration.rb +1 -1
  37. data/lib/datadog/appsec/contrib/rails/reactive/action.rb +11 -11
  38. data/lib/datadog/appsec/contrib/rails/request.rb +3 -0
  39. data/lib/datadog/appsec/contrib/sinatra/gateway/watcher.rb +14 -12
  40. data/lib/datadog/appsec/contrib/sinatra/reactive/routed.rb +11 -11
  41. data/lib/datadog/appsec/event.rb +6 -10
  42. data/lib/datadog/appsec/instrumentation/gateway.rb +16 -2
  43. data/lib/datadog/appsec/processor.rb +18 -2
  44. data/lib/datadog/ci/ext/environment.rb +16 -4
  45. data/lib/datadog/core/configuration/agent_settings_resolver.rb +0 -3
  46. data/lib/datadog/core/configuration/components.rb +28 -16
  47. data/lib/datadog/core/configuration/settings.rb +127 -8
  48. data/lib/datadog/core/configuration.rb +1 -1
  49. data/lib/datadog/core/diagnostics/environment_logger.rb +5 -1
  50. data/lib/datadog/core/header_collection.rb +41 -0
  51. data/lib/datadog/core/telemetry/collector.rb +0 -2
  52. data/lib/datadog/core/utils/compression.rb +5 -1
  53. data/lib/datadog/core/workers/async.rb +0 -2
  54. data/lib/datadog/core.rb +0 -54
  55. data/lib/datadog/opentracer/tracer.rb +4 -6
  56. data/lib/datadog/profiling/collectors/cpu_and_wall_time.rb +12 -2
  57. data/lib/datadog/profiling/collectors/cpu_and_wall_time_worker.rb +5 -3
  58. data/lib/datadog/profiling/collectors/old_stack.rb +1 -1
  59. data/lib/datadog/profiling/exporter.rb +2 -4
  60. data/lib/datadog/profiling/http_transport.rb +1 -1
  61. data/lib/datadog/profiling.rb +1 -1
  62. data/lib/datadog/tracing/client_ip.rb +164 -0
  63. data/lib/datadog/tracing/configuration/ext.rb +14 -0
  64. data/lib/datadog/tracing/contrib/aws/instrumentation.rb +2 -0
  65. data/lib/datadog/tracing/contrib/aws/services.rb +0 -2
  66. data/lib/datadog/tracing/contrib/dalli/ext.rb +1 -0
  67. data/lib/datadog/tracing/contrib/dalli/instrumentation.rb +4 -0
  68. data/lib/datadog/tracing/contrib/elasticsearch/ext.rb +2 -0
  69. data/lib/datadog/tracing/contrib/elasticsearch/patcher.rb +3 -0
  70. data/lib/datadog/tracing/contrib/ethon/easy_patch.rb +2 -2
  71. data/lib/datadog/tracing/contrib/ethon/multi_patch.rb +2 -0
  72. data/lib/datadog/tracing/contrib/excon/middleware.rb +2 -0
  73. data/lib/datadog/tracing/contrib/ext.rb +25 -0
  74. data/lib/datadog/tracing/contrib/faraday/middleware.rb +3 -2
  75. data/lib/datadog/tracing/contrib/grape/endpoint.rb +0 -2
  76. data/lib/datadog/tracing/contrib/graphql/configuration/settings.rb +1 -1
  77. data/lib/datadog/tracing/contrib/grpc/datadog_interceptor/client.rb +5 -0
  78. data/lib/datadog/tracing/contrib/grpc/datadog_interceptor/server.rb +7 -1
  79. data/lib/datadog/tracing/contrib/grpc/ext.rb +2 -0
  80. data/lib/datadog/tracing/contrib/hanami/action_tracer.rb +47 -0
  81. data/lib/datadog/tracing/contrib/hanami/configuration/settings.rb +22 -0
  82. data/lib/datadog/tracing/contrib/hanami/ext.rb +24 -0
  83. data/lib/datadog/tracing/contrib/hanami/integration.rb +44 -0
  84. data/lib/datadog/tracing/contrib/hanami/patcher.rb +33 -0
  85. data/lib/datadog/tracing/contrib/hanami/plugin.rb +23 -0
  86. data/lib/datadog/tracing/contrib/hanami/renderer_policy_tracing.rb +41 -0
  87. data/lib/datadog/tracing/contrib/hanami/router_tracing.rb +44 -0
  88. data/lib/datadog/tracing/contrib/http/instrumentation.rb +2 -0
  89. data/lib/datadog/tracing/contrib/httpclient/instrumentation.rb +2 -0
  90. data/lib/datadog/tracing/contrib/httprb/instrumentation.rb +2 -0
  91. data/lib/datadog/tracing/contrib/mongodb/ext.rb +7 -0
  92. data/lib/datadog/tracing/contrib/mongodb/subscribers.rb +4 -0
  93. data/lib/datadog/tracing/contrib/mysql2/configuration/settings.rb +12 -0
  94. data/lib/datadog/tracing/contrib/mysql2/ext.rb +1 -0
  95. data/lib/datadog/tracing/contrib/mysql2/instrumentation.rb +16 -0
  96. data/lib/datadog/tracing/contrib/pg/configuration/settings.rb +12 -0
  97. data/lib/datadog/tracing/contrib/pg/ext.rb +2 -1
  98. data/lib/datadog/tracing/contrib/pg/instrumentation.rb +38 -21
  99. data/lib/datadog/tracing/contrib/propagation/sql_comment/comment.rb +43 -0
  100. data/lib/datadog/tracing/contrib/propagation/sql_comment/ext.rb +32 -0
  101. data/lib/datadog/tracing/contrib/propagation/sql_comment/mode.rb +28 -0
  102. data/lib/datadog/tracing/contrib/propagation/sql_comment.rb +49 -0
  103. data/lib/datadog/tracing/contrib/rack/header_collection.rb +35 -0
  104. data/lib/datadog/tracing/contrib/rack/middlewares.rb +105 -43
  105. data/lib/datadog/tracing/contrib/redis/ext.rb +2 -0
  106. data/lib/datadog/tracing/contrib/redis/instrumentation.rb +4 -2
  107. data/lib/datadog/tracing/contrib/redis/integration.rb +2 -1
  108. data/lib/datadog/tracing/contrib/redis/patcher.rb +40 -0
  109. data/lib/datadog/tracing/contrib/redis/tags.rb +5 -0
  110. data/lib/datadog/tracing/contrib/rest_client/request_patch.rb +2 -0
  111. data/lib/datadog/tracing/contrib/sinatra/env.rb +12 -23
  112. data/lib/datadog/tracing/contrib/sinatra/ext.rb +7 -3
  113. data/lib/datadog/tracing/contrib/sinatra/patcher.rb +2 -2
  114. data/lib/datadog/tracing/contrib/sinatra/tracer.rb +8 -80
  115. data/lib/datadog/tracing/contrib/sinatra/tracer_middleware.rb +14 -9
  116. data/lib/datadog/tracing/contrib/utils/quantization/http.rb +92 -10
  117. data/lib/datadog/tracing/contrib.rb +1 -0
  118. data/lib/datadog/tracing/distributed/datadog_tags_codec.rb +84 -0
  119. data/lib/datadog/tracing/distributed/headers/datadog.rb +122 -30
  120. data/lib/datadog/tracing/distributed/headers/ext.rb +2 -0
  121. data/lib/datadog/tracing/flush.rb +57 -35
  122. data/lib/datadog/tracing/metadata/ext.rb +11 -9
  123. data/lib/datadog/tracing/metadata/tagging.rb +9 -0
  124. data/lib/datadog/tracing/propagation/http.rb +9 -1
  125. data/lib/datadog/tracing/sampling/ext.rb +31 -0
  126. data/lib/datadog/tracing/sampling/priority_sampler.rb +46 -4
  127. data/lib/datadog/tracing/sampling/rate_by_key_sampler.rb +8 -9
  128. data/lib/datadog/tracing/sampling/rate_by_service_sampler.rb +29 -5
  129. data/lib/datadog/tracing/sampling/rate_limiter.rb +3 -0
  130. data/lib/datadog/tracing/sampling/rate_sampler.rb +20 -3
  131. data/lib/datadog/tracing/sampling/rule_sampler.rb +4 -3
  132. data/lib/datadog/tracing/sampling/span/ext.rb +25 -0
  133. data/lib/datadog/tracing/sampling/span/matcher.rb +9 -0
  134. data/lib/datadog/tracing/sampling/span/rule.rb +82 -0
  135. data/lib/datadog/tracing/sampling/span/rule_parser.rb +104 -0
  136. data/lib/datadog/tracing/sampling/span/sampler.rb +75 -0
  137. data/lib/datadog/tracing/span_operation.rb +0 -2
  138. data/lib/datadog/tracing/trace_digest.rb +3 -0
  139. data/lib/datadog/tracing/trace_operation.rb +32 -3
  140. data/lib/datadog/tracing/trace_segment.rb +7 -2
  141. data/lib/datadog/tracing/tracer.rb +34 -6
  142. data/lib/datadog/tracing/writer.rb +7 -0
  143. data/lib/ddtrace/transport/trace_formatter.rb +7 -0
  144. data/lib/ddtrace/transport/traces.rb +3 -1
  145. data/lib/ddtrace/version.rb +1 -1
  146. metadata +36 -18
  147. data/lib/datadog/profiling/old_ext.rb +0 -42
  148. data/lib/datadog/profiling/transport/http/api/endpoint.rb +0 -85
  149. data/lib/datadog/profiling/transport/http/api/instance.rb +0 -38
  150. data/lib/datadog/profiling/transport/http/api/spec.rb +0 -42
  151. data/lib/datadog/profiling/transport/http/api.rb +0 -45
  152. data/lib/datadog/profiling/transport/http/builder.rb +0 -30
  153. data/lib/datadog/profiling/transport/http/client.rb +0 -37
  154. data/lib/datadog/profiling/transport/http/response.rb +0 -21
  155. data/lib/datadog/profiling/transport/http.rb +0 -118
@@ -0,0 +1,24 @@
1
+ # typed: ignore
2
+
3
+ module Datadog
4
+ module Tracing
5
+ module Contrib
6
+ module Hanami
7
+ # Hanami integration constants
8
+ # @public_api Changing resource names, tag names, or environment variables creates breaking changes.
9
+ module Ext
10
+ ENV_ENABLED = 'DD_TRACE_HANAMI_ENABLED'.freeze
11
+
12
+ SPAN_ACTION = 'hanami.action'.freeze
13
+ SPAN_ROUTING = 'hanami.routing'.freeze
14
+ SPAN_RENDER = 'hanami.render'.freeze
15
+
16
+ TAG_COMPONENT = 'hanami'.freeze
17
+ TAG_OPERATION_ACTION = 'action'.freeze
18
+ TAG_OPERATION_ROUTING = 'routing'.freeze
19
+ TAG_OPERATION_RENDER = 'render'.freeze
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,44 @@
1
+ # typed: ignore
2
+
3
+ require_relative '../integration'
4
+ require_relative 'configuration/settings'
5
+ require_relative 'patcher'
6
+
7
+ module Datadog
8
+ module Tracing
9
+ module Contrib
10
+ module Hanami
11
+ # Description of Hanami integration
12
+ class Integration
13
+ include Contrib::Integration
14
+
15
+ MINIMUM_VERSION = Gem::Version.new('1.0.0')
16
+ MAXIMUM_VERSION = Gem::Version.new('2.0.0')
17
+
18
+ register_as :hanami
19
+
20
+ def self.version
21
+ Gem.loaded_specs['hanami'] && Gem.loaded_specs['hanami'].version
22
+ end
23
+
24
+ def self.loaded?
25
+ !defined?(::Hanami).nil?
26
+ end
27
+
28
+ def self.compatible?
29
+ # Tested with version larger than 1.x, but not 2.x version
30
+ super && version >= MINIMUM_VERSION && version < MAXIMUM_VERSION
31
+ end
32
+
33
+ def new_configuration
34
+ Configuration::Settings.new
35
+ end
36
+
37
+ def patcher
38
+ Patcher
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,33 @@
1
+ # typed: ignore
2
+
3
+ require_relative '../patcher'
4
+ require_relative 'action_tracer'
5
+ require_relative 'renderer_policy_tracing'
6
+ require_relative 'router_tracing'
7
+
8
+ module Datadog
9
+ module Tracing
10
+ module Contrib
11
+ module Hanami
12
+ # Patcher enables patching of Hanami
13
+ module Patcher
14
+ include Contrib::Patcher
15
+
16
+ module_function
17
+
18
+ def target_version
19
+ Integration.version
20
+ end
21
+
22
+ def patch
23
+ # For auto instrumentation, `plugin` must be required before `Hanami.boot`
24
+ require_relative 'plugin'
25
+
26
+ ::Hanami::Router.prepend(RouterTracing)
27
+ ::Hanami::RenderingPolicy.prepend(RendererPolicyTracing)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,23 @@
1
+ # typed: ignore
2
+
3
+ ::Hanami.plugin do
4
+ Datadog.configure do |c|
5
+ c.tracing.instrument :rack
6
+ end
7
+
8
+ middleware.use Datadog::Tracing::Contrib::Rack::TraceMiddleware
9
+ end
10
+
11
+ ::Hanami::Application.singleton_class.prepend(
12
+ Module.new do
13
+ def inherited(base)
14
+ super
15
+
16
+ base.configure do
17
+ controller.prepare do
18
+ use Datadog::Tracing::Contrib::Hanami::ActionTracer, self
19
+ end
20
+ end
21
+ end
22
+ end
23
+ )
@@ -0,0 +1,41 @@
1
+ # typed: ignore
2
+
3
+ require_relative 'ext'
4
+ require_relative '../../metadata/ext'
5
+
6
+ module Datadog
7
+ module Tracing
8
+ module Contrib
9
+ module Hanami
10
+ # Hanami Instrumentation for `hanami.render`
11
+ module RendererPolicyTracing
12
+ def render(env, response)
13
+ action = env['hanami.action']
14
+ # env['hanami.action'] could be empty for endpoints without an action
15
+ #
16
+ # For example in config/routes.rb:
17
+ # get '/hello', to: ->(env) { [200, {}, ['Hello from Hanami!']] }
18
+ action_klass = (action && action.class) ||
19
+ ::Hanami::Routing::Default::NullAction
20
+
21
+ Tracing.trace(
22
+ Ext::SPAN_RENDER,
23
+ service: configuration[:service_name],
24
+ resource: action_klass.to_s,
25
+ span_type: Tracing::Metadata::Ext::HTTP::TYPE_INBOUND
26
+ ) do |span_op, _trace_op|
27
+ span_op.set_tag(Tracing::Metadata::Ext::TAG_COMPONENT, Ext::TAG_COMPONENT)
28
+ span_op.set_tag(Tracing::Metadata::Ext::TAG_OPERATION, Ext::TAG_OPERATION_RENDER)
29
+
30
+ super
31
+ end
32
+ end
33
+
34
+ def configuration
35
+ Datadog.configuration.tracing[:hanami]
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,44 @@
1
+ # typed: ignore
2
+
3
+ require_relative 'ext'
4
+ require_relative '../../metadata/ext'
5
+
6
+ module Datadog
7
+ module Tracing
8
+ module Contrib
9
+ module Hanami
10
+ # Hanami Instrumentation for `hanami.routing`
11
+ module RouterTracing
12
+ def call(env)
13
+ return super if Tracing.active_span && Tracing.active_span.name == Ext::SPAN_ROUTING
14
+
15
+ Tracing.trace(
16
+ Ext::SPAN_ROUTING,
17
+ service: configuration[:service_name],
18
+ span_type: Tracing::Metadata::Ext::HTTP::TYPE_INBOUND
19
+ ) do |span_op, trace_op|
20
+ begin
21
+ span_op.set_tag(Tracing::Metadata::Ext::TAG_COMPONENT, Ext::TAG_COMPONENT)
22
+ span_op.set_tag(Tracing::Metadata::Ext::TAG_OPERATION, Ext::TAG_OPERATION_ROUTING)
23
+
24
+ span_op.resource = nil
25
+
26
+ super
27
+ ensure
28
+ span_op.resource ||= if trace_op.resource_override?
29
+ trace_op.resource
30
+ else
31
+ env['REQUEST_METHOD']
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ def configuration
38
+ Datadog.configuration.tracing[:hanami]
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -70,6 +70,8 @@ module Datadog
70
70
  end
71
71
 
72
72
  def annotate_span_with_request!(span, request, request_options)
73
+ span.set_tag(Tracing::Metadata::Ext::TAG_KIND, Tracing::Metadata::Ext::SpanKind::TAG_CLIENT)
74
+
73
75
  span.set_tag(Tracing::Metadata::Ext::TAG_COMPONENT, Ext::TAG_COMPONENT)
74
76
  span.set_tag(Tracing::Metadata::Ext::TAG_OPERATION, Ext::TAG_OPERATION_REQUEST)
75
77
 
@@ -51,6 +51,8 @@ module Datadog
51
51
  private
52
52
 
53
53
  def annotate_span_with_request!(span, req, req_options)
54
+ span.set_tag(Tracing::Metadata::Ext::TAG_KIND, Tracing::Metadata::Ext::SpanKind::TAG_CLIENT)
55
+
54
56
  span.set_tag(Tracing::Metadata::Ext::TAG_COMPONENT, Ext::TAG_COMPONENT)
55
57
  span.set_tag(Tracing::Metadata::Ext::TAG_OPERATION, Ext::TAG_OPERATION_REQUEST)
56
58
 
@@ -51,6 +51,8 @@ module Datadog
51
51
  private
52
52
 
53
53
  def annotate_span_with_request!(span, req, req_options)
54
+ span.set_tag(Tracing::Metadata::Ext::TAG_KIND, Tracing::Metadata::Ext::SpanKind::TAG_CLIENT)
55
+
54
56
  span.set_tag(Tracing::Metadata::Ext::TAG_COMPONENT, Ext::TAG_COMPONENT)
55
57
  span.set_tag(Tracing::Metadata::Ext::TAG_OPERATION, Ext::TAG_OPERATION_REQUEST)
56
58
 
@@ -20,6 +20,13 @@ module Datadog
20
20
  TAG_ROWS = 'mongodb.rows'.freeze
21
21
  TAG_COMPONENT = 'mongodb'.freeze
22
22
  TAG_OPERATION_COMMAND = 'command'.freeze
23
+ TAG_SYSTEM = 'mongodb'.freeze
24
+
25
+ # Temporary namespace to accommodate unified tags which has naming collision, before
26
+ # making breaking changes
27
+ module DB
28
+ TAG_COLLECTION = 'db.mongodb.collection'.freeze
29
+ end
23
30
  end
24
31
  end
25
32
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative '../analytics'
4
4
  require_relative 'ext'
5
+ require_relative '../ext'
5
6
  require_relative 'parsers'
6
7
  require_relative '../../metadata/ext'
7
8
 
@@ -30,6 +31,8 @@ module Datadog
30
31
  query = MongoDB.query_builder(event.command_name, event.database_name, event.command)
31
32
  serialized_query = query.to_s
32
33
 
34
+ span.set_tag(Contrib::Ext::DB::TAG_SYSTEM, Ext::TAG_SYSTEM)
35
+
33
36
  span.set_tag(Tracing::Metadata::Ext::TAG_COMPONENT, Ext::TAG_COMPONENT)
34
37
  span.set_tag(Tracing::Metadata::Ext::TAG_OPERATION, Ext::TAG_OPERATION_COMMAND)
35
38
 
@@ -44,6 +47,7 @@ module Datadog
44
47
  # since it has been quantized and reduced
45
48
  span.set_tag(Ext::TAG_DB, query['database'])
46
49
  span.set_tag(Ext::TAG_COLLECTION, query['collection'])
50
+ span.set_tag(Ext::DB::TAG_COLLECTION, query['collection'])
47
51
  span.set_tag(Ext::TAG_OPERATION, query['operation'])
48
52
  span.set_tag(Ext::TAG_QUERY, serialized_query)
49
53
  span.set_tag(Tracing::Metadata::Ext::NET::TAG_TARGET_HOST, event.address.host)
@@ -3,6 +3,8 @@
3
3
  require_relative '../../configuration/settings'
4
4
  require_relative '../ext'
5
5
 
6
+ require_relative '../../propagation/sql_comment'
7
+
6
8
  module Datadog
7
9
  module Tracing
8
10
  module Contrib
@@ -27,6 +29,16 @@ module Datadog
27
29
  end
28
30
 
29
31
  option :service_name, default: Ext::DEFAULT_PEER_SERVICE_NAME
32
+
33
+ option :comment_propagation do |o|
34
+ o.default do
35
+ ENV.fetch(
36
+ Contrib::Propagation::SqlComment::Ext::ENV_DBM_PROPAGATION_MODE,
37
+ Contrib::Propagation::SqlComment::Ext::DISABLED
38
+ )
39
+ end
40
+ o.lazy
41
+ end
30
42
  end
31
43
  end
32
44
  end
@@ -15,6 +15,7 @@ module Datadog
15
15
  TAG_DB_NAME = 'mysql2.db.name'.freeze
16
16
  TAG_COMPONENT = 'mysql2'.freeze
17
17
  TAG_OPERATION_QUERY = 'query'.freeze
18
+ TAG_SYSTEM = 'mysql'.freeze
18
19
  end
19
20
  end
20
21
  end
@@ -3,6 +3,9 @@
3
3
  require_relative '../../metadata/ext'
4
4
  require_relative '../analytics'
5
5
  require_relative 'ext'
6
+ require_relative '../ext'
7
+ require_relative '../propagation/sql_comment'
8
+ require_relative '../propagation/sql_comment/mode'
6
9
 
7
10
  module Datadog
8
11
  module Tracing
@@ -23,6 +26,9 @@ module Datadog
23
26
  span.resource = sql
24
27
  span.span_type = Tracing::Metadata::Ext::SQL::TYPE
25
28
 
29
+ span.set_tag(Contrib::Ext::DB::TAG_SYSTEM, Ext::TAG_SYSTEM)
30
+ span.set_tag(Tracing::Metadata::Ext::TAG_KIND, Tracing::Metadata::Ext::SpanKind::TAG_CLIENT)
31
+
26
32
  span.set_tag(Tracing::Metadata::Ext::TAG_COMPONENT, Ext::TAG_COMPONENT)
27
33
  span.set_tag(Tracing::Metadata::Ext::TAG_OPERATION, Ext::TAG_OPERATION_QUERY)
28
34
 
@@ -36,6 +42,12 @@ module Datadog
36
42
  span.set_tag(Ext::TAG_DB_NAME, query_options[:database])
37
43
  span.set_tag(Tracing::Metadata::Ext::NET::TAG_TARGET_HOST, query_options[:host])
38
44
  span.set_tag(Tracing::Metadata::Ext::NET::TAG_TARGET_PORT, query_options[:port])
45
+
46
+ propagation_mode = Contrib::Propagation::SqlComment::Mode.new(comment_propagation)
47
+
48
+ Contrib::Propagation::SqlComment.annotate!(span, propagation_mode)
49
+ sql = Contrib::Propagation::SqlComment.prepend_comment(sql, span, propagation_mode)
50
+
39
51
  super(sql, options)
40
52
  end
41
53
  end
@@ -53,6 +65,10 @@ module Datadog
53
65
  def analytics_sample_rate
54
66
  datadog_configuration[:analytics_sample_rate]
55
67
  end
68
+
69
+ def comment_propagation
70
+ datadog_configuration[:comment_propagation]
71
+ end
56
72
  end
57
73
  end
58
74
  end
@@ -3,6 +3,8 @@
3
3
  require_relative '../../configuration/settings'
4
4
  require_relative '../ext'
5
5
 
6
+ require_relative '../../propagation/sql_comment/ext'
7
+
6
8
  module Datadog
7
9
  module Tracing
8
10
  module Contrib
@@ -27,6 +29,16 @@ module Datadog
27
29
  end
28
30
 
29
31
  option :service_name, default: Ext::DEFAULT_PEER_SERVICE_NAME
32
+
33
+ option :comment_propagation do |o|
34
+ o.default do
35
+ ENV.fetch(
36
+ Contrib::Propagation::SqlComment::Ext::ENV_DBM_PROPAGATION_MODE,
37
+ Contrib::Propagation::SqlComment::Ext::DISABLED
38
+ )
39
+ end
40
+ o.lazy
41
+ end
30
42
  end
31
43
  end
32
44
  end
@@ -11,7 +11,6 @@ module Datadog
11
11
  ENV_ANALYTICS_ENABLED = 'DD_TRACE_PG_ANALYTICS_ENABLED'.freeze
12
12
  ENV_ANALYTICS_SAMPLE_RATE = 'DD_TRACE_PG_ANALYTICS_SAMPLE_RATE'.freeze
13
13
  DEFAULT_PEER_SERVICE_NAME = 'pg'.freeze
14
- SPAN_SYSTEM = 'postgresql'.freeze
15
14
  SPAN_EXEC = 'pg.exec'.freeze
16
15
  SPAN_EXEC_PARAMS = 'pg.exec.params'.freeze
17
16
  SPAN_EXEC_PREPARED = 'pg.exec.prepared'.freeze
@@ -24,6 +23,8 @@ module Datadog
24
23
  TAG_DB_NAME = 'pg.db.name'.freeze
25
24
  TAG_COMPONENT = 'pg'.freeze
26
25
  TAG_OPERATION_QUERY = 'query'.freeze
26
+
27
+ TAG_SYSTEM = 'postgresql'.freeze
27
28
  end
28
29
  end
29
30
  end
@@ -2,8 +2,12 @@
2
2
 
3
3
  require_relative '../../metadata/ext'
4
4
  require_relative '../analytics'
5
+ require_relative '../ext'
5
6
  require_relative 'ext'
6
7
 
8
+ require_relative '../propagation/sql_comment'
9
+ require_relative '../propagation/sql_comment/mode'
10
+
7
11
  module Datadog
8
12
  module Tracing
9
13
  module Contrib
@@ -17,68 +21,77 @@ module Datadog
17
21
  # PG::Connection patch methods
18
22
  module InstanceMethods
19
23
  def exec(sql, *args)
20
- trace(Ext::SPAN_EXEC, resource: sql) do
21
- super(sql, *args)
24
+ trace(Ext::SPAN_EXEC, sql: sql) do |sql_statement|
25
+ super(sql_statement, *args)
22
26
  end
23
27
  end
24
28
 
25
29
  def exec_params(sql, params, *args)
26
- trace(Ext::SPAN_EXEC_PARAMS, resource: sql) do
27
- super(sql, params, *args)
30
+ trace(Ext::SPAN_EXEC_PARAMS, sql: sql) do |sql_statement|
31
+ super(sql_statement, params, *args)
28
32
  end
29
33
  end
30
34
 
31
35
  def exec_prepared(statement_name, params, *args)
32
- trace(Ext::SPAN_EXEC_PREPARED, resource: statement_name) do
36
+ trace(Ext::SPAN_EXEC_PREPARED, statement_name: statement_name) do
33
37
  super(statement_name, params, *args)
34
38
  end
35
39
  end
36
40
 
37
41
  def async_exec(sql, *args)
38
- trace(Ext::SPAN_ASYNC_EXEC, resource: sql) do
39
- super(sql, *args)
42
+ trace(Ext::SPAN_ASYNC_EXEC, sql: sql) do |sql_statement|
43
+ super(sql_statement, *args)
40
44
  end
41
45
  end
42
46
 
43
47
  def async_exec_params(sql, params, *args)
44
- trace(Ext::SPAN_ASYNC_EXEC_PARAMS, resource: sql) do
45
- super(sql, params, *args)
48
+ trace(Ext::SPAN_ASYNC_EXEC_PARAMS, sql: sql) do |sql_statement|
49
+ super(sql_statement, params, *args)
46
50
  end
47
51
  end
48
52
 
49
53
  def async_exec_prepared(statement_name, params, *args)
50
- trace(Ext::SPAN_ASYNC_EXEC_PREPARED, resource: statement_name) do
54
+ trace(Ext::SPAN_ASYNC_EXEC_PREPARED, statement_name: statement_name) do
51
55
  super(statement_name, params, *args)
52
56
  end
53
57
  end
54
58
 
55
59
  def sync_exec(sql, *args)
56
- trace(Ext::SPAN_SYNC_EXEC, resource: sql) do
57
- super(sql, *args)
60
+ trace(Ext::SPAN_SYNC_EXEC, sql: sql) do |sql_statement|
61
+ super(sql_statement, *args)
58
62
  end
59
63
  end
60
64
 
61
65
  def sync_exec_params(sql, params, *args)
62
- trace(Ext::SPAN_SYNC_EXEC_PARAMS, resource: sql) do
63
- super(sql, params, *args)
66
+ trace(Ext::SPAN_SYNC_EXEC_PARAMS, sql: sql) do |sql_statement|
67
+ super(sql_statement, params, *args)
64
68
  end
65
69
  end
66
70
 
67
71
  def sync_exec_prepared(statement_name, params, *args)
68
- trace(Ext::SPAN_SYNC_EXEC_PREPARED, resource: statement_name) do
72
+ trace(Ext::SPAN_SYNC_EXEC_PREPARED, statement_name: statement_name) do
69
73
  super(statement_name, params, *args)
70
74
  end
71
75
  end
72
76
 
73
77
  private
74
78
 
75
- def trace(name, resource:)
79
+ def trace(name, sql: nil, statement_name: nil)
76
80
  service = Datadog.configuration_for(self, :service_name) || datadog_configuration[:service_name]
81
+ resource = statement_name || sql
82
+
77
83
  Tracing.trace(name, service: service, resource: resource, type: Tracing::Metadata::Ext::SQL::TYPE) do |span|
78
84
  annotate_span_with_query!(span, service)
79
85
  # Set analytics sample rate
80
86
  Contrib::Analytics.set_sample_rate(span, analytics_sample_rate) if analytics_enabled?
81
- result = yield
87
+
88
+ if sql
89
+ propagation_mode = Contrib::Propagation::SqlComment::Mode.new(comment_propagation)
90
+ Contrib::Propagation::SqlComment.annotate!(span, propagation_mode)
91
+ propagated_sql_statement = Contrib::Propagation::SqlComment.prepend_comment(sql, span, propagation_mode)
92
+ end
93
+
94
+ result = yield(propagated_sql_statement)
82
95
  annotate_span_with_result!(span, result)
83
96
  result
84
97
  end
@@ -95,9 +108,9 @@ module Datadog
95
108
  span.set_tag(Tracing::Metadata::Ext::TAG_PEER_SERVICE, service)
96
109
  span.set_tag(Tracing::Metadata::Ext::TAG_PEER_HOSTNAME, host)
97
110
 
98
- span.set_tag(Tracing::Metadata::Ext::DB::TAG_INSTANCE, db)
99
- span.set_tag(Tracing::Metadata::Ext::DB::TAG_USER, user)
100
- span.set_tag(Tracing::Metadata::Ext::DB::TAG_SYSTEM, Ext::SPAN_SYSTEM)
111
+ span.set_tag(Contrib::Ext::DB::TAG_INSTANCE, db)
112
+ span.set_tag(Contrib::Ext::DB::TAG_USER, user)
113
+ span.set_tag(Contrib::Ext::DB::TAG_SYSTEM, Ext::TAG_SYSTEM)
101
114
 
102
115
  span.set_tag(Tracing::Metadata::Ext::NET::TAG_TARGET_HOST, host)
103
116
  span.set_tag(Tracing::Metadata::Ext::NET::TAG_TARGET_PORT, port)
@@ -106,7 +119,7 @@ module Datadog
106
119
  end
107
120
 
108
121
  def annotate_span_with_result!(span, result)
109
- span.set_tag(Tracing::Metadata::Ext::DB::TAG_ROW_COUNT, result.ntuples)
122
+ span.set_tag(Contrib::Ext::DB::TAG_ROW_COUNT, result.ntuples)
110
123
  end
111
124
 
112
125
  def datadog_configuration
@@ -120,6 +133,10 @@ module Datadog
120
133
  def analytics_sample_rate
121
134
  datadog_configuration[:analytics_sample_rate]
122
135
  end
136
+
137
+ def comment_propagation
138
+ datadog_configuration[:comment_propagation]
139
+ end
123
140
  end
124
141
  end
125
142
  end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ # typed: false
4
+
5
+ require 'erb'
6
+
7
+ module Datadog
8
+ module Tracing
9
+ module Contrib
10
+ module Propagation
11
+ module SqlComment
12
+ # To be prepended to a sql statement.
13
+ class Comment
14
+ def initialize(hash)
15
+ @hash = hash
16
+ end
17
+
18
+ def to_s
19
+ @string ||= begin
20
+ ret = String.new
21
+
22
+ @hash.each do |key, value|
23
+ next if value.nil?
24
+
25
+ # Url encode
26
+ value = ERB::Util.url_encode(value)
27
+
28
+ # Escape SQL
29
+ ret << "#{key}='#{value}',"
30
+ end
31
+
32
+ # Remove the last `,`
33
+ ret.chop!
34
+
35
+ "/*#{ret}*/"
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,32 @@
1
+ # typed: false
2
+
3
+ module Datadog
4
+ module Tracing
5
+ module Contrib
6
+ module Propagation
7
+ module SqlComment
8
+ module Ext
9
+ ENV_DBM_PROPAGATION_MODE = 'DD_DBM_PROPAGATION_MODE'.freeze
10
+
11
+ # The default mode for sql comment propagation
12
+ DISABLED = 'disabled'.freeze
13
+
14
+ # The `service` mode propagates service configuration
15
+ SERVICE = 'service'.freeze
16
+
17
+ # The `full` mode propagates service configuration + trace context
18
+ FULL = 'full'.freeze
19
+
20
+ # The value should be `true` when `full` mode
21
+ TAG_DBM_TRACE_INJECTED = '_dd.dbm_trace_injected'.freeze
22
+
23
+ KEY_DATABASE_SERVICE = 'dddbs'.freeze
24
+ KEY_ENVIRONMENT = 'dde'.freeze
25
+ KEY_PARENT_SERVICE = 'ddps'.freeze
26
+ KEY_VERSION = 'ddpv'.freeze
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,28 @@
1
+ # typed: false
2
+
3
+ require_relative 'ext'
4
+
5
+ module Datadog
6
+ module Tracing
7
+ module Contrib
8
+ module Propagation
9
+ # Implements sql comment propagation related contracts.
10
+ module SqlComment
11
+ Mode = Struct.new(:mode) do
12
+ def enabled?
13
+ service? || full?
14
+ end
15
+
16
+ def service?
17
+ mode == Ext::SERVICE
18
+ end
19
+
20
+ def full?
21
+ mode == Ext::FULL
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end