ddtrace 0.45.0 → 0.46.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (267) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +79 -0
  3. data/.rubocop.yml +250 -7
  4. data/.rubocop_todo.yml +396 -0
  5. data/Appraisals +15 -0
  6. data/CHANGELOG.md +68 -1
  7. data/Gemfile +38 -3
  8. data/Rakefile +5 -23
  9. data/ddtrace.gemspec +6 -36
  10. data/docs/DevelopmentGuide.md +28 -0
  11. data/docs/GettingStarted.md +8 -9
  12. data/integration/README.md +68 -0
  13. data/integration/apps/rack/.dockerignore +1 -0
  14. data/integration/apps/rack/.envrc.sample +1 -0
  15. data/integration/apps/rack/.gitignore +4 -0
  16. data/integration/apps/rack/.rspec +1 -0
  17. data/integration/apps/rack/Dockerfile +25 -0
  18. data/integration/apps/rack/Dockerfile-ci +11 -0
  19. data/integration/apps/rack/Gemfile +24 -0
  20. data/integration/apps/rack/README.md +93 -0
  21. data/integration/apps/rack/app/acme.rb +80 -0
  22. data/integration/apps/rack/app/datadog.rb +17 -0
  23. data/integration/apps/rack/bin/run +22 -0
  24. data/integration/apps/rack/bin/setup +17 -0
  25. data/integration/apps/rack/bin/test +24 -0
  26. data/integration/apps/rack/config.ru +6 -0
  27. data/integration/apps/rack/config/puma.rb +14 -0
  28. data/integration/apps/rack/config/unicorn.rb +23 -0
  29. data/integration/apps/rack/docker-compose.ci.yml +62 -0
  30. data/integration/apps/rack/docker-compose.yml +78 -0
  31. data/integration/apps/rack/script/build-images +38 -0
  32. data/integration/apps/rack/script/ci +50 -0
  33. data/integration/apps/rack/spec/integration/basic_spec.rb +10 -0
  34. data/integration/apps/rack/spec/spec_helper.rb +16 -0
  35. data/integration/apps/rack/spec/support/integration_helper.rb +22 -0
  36. data/integration/apps/rails-five/.dockerignore +1 -0
  37. data/integration/apps/rails-five/.env +3 -0
  38. data/integration/apps/rails-five/.envrc.sample +1 -0
  39. data/integration/apps/rails-five/.gitignore +30 -0
  40. data/integration/apps/rails-five/Dockerfile +25 -0
  41. data/integration/apps/rails-five/Dockerfile-ci +11 -0
  42. data/integration/apps/rails-five/Gemfile +104 -0
  43. data/integration/apps/rails-five/README.md +94 -0
  44. data/integration/apps/rails-five/Rakefile +6 -0
  45. data/integration/apps/rails-five/app/channels/application_cable/channel.rb +4 -0
  46. data/integration/apps/rails-five/app/channels/application_cable/connection.rb +4 -0
  47. data/integration/apps/rails-five/app/controllers/application_controller.rb +2 -0
  48. data/integration/apps/rails-five/app/controllers/basic_controller.rb +36 -0
  49. data/integration/apps/rails-five/app/controllers/concerns/.keep +0 -0
  50. data/integration/apps/rails-five/app/controllers/health_controller.rb +9 -0
  51. data/integration/apps/rails-five/app/controllers/jobs_controller.rb +12 -0
  52. data/integration/apps/rails-five/app/jobs/application_job.rb +2 -0
  53. data/integration/apps/rails-five/app/jobs/test_job.rb +12 -0
  54. data/integration/apps/rails-five/app/mailers/application_mailer.rb +4 -0
  55. data/integration/apps/rails-five/app/models/application_record.rb +3 -0
  56. data/integration/apps/rails-five/app/models/concerns/.keep +0 -0
  57. data/integration/apps/rails-five/app/models/test.rb +2 -0
  58. data/integration/apps/rails-five/app/views/layouts/mailer.html.erb +13 -0
  59. data/integration/apps/rails-five/app/views/layouts/mailer.text.erb +1 -0
  60. data/integration/apps/rails-five/bin/bundle +3 -0
  61. data/integration/apps/rails-five/bin/rails +9 -0
  62. data/integration/apps/rails-five/bin/rake +9 -0
  63. data/integration/apps/rails-five/bin/run +24 -0
  64. data/integration/apps/rails-five/bin/setup +27 -0
  65. data/integration/apps/rails-five/bin/spring +17 -0
  66. data/integration/apps/rails-five/bin/test +21 -0
  67. data/integration/apps/rails-five/bin/update +28 -0
  68. data/integration/apps/rails-five/config.ru +5 -0
  69. data/integration/apps/rails-five/config/application.rb +97 -0
  70. data/integration/apps/rails-five/config/boot.rb +4 -0
  71. data/integration/apps/rails-five/config/cable.yml +10 -0
  72. data/integration/apps/rails-five/config/credentials.yml.enc +1 -0
  73. data/integration/apps/rails-five/config/database.yml +28 -0
  74. data/integration/apps/rails-five/config/environment.rb +5 -0
  75. data/integration/apps/rails-five/config/environments/development.rb +51 -0
  76. data/integration/apps/rails-five/config/environments/production.rb +82 -0
  77. data/integration/apps/rails-five/config/environments/test.rb +43 -0
  78. data/integration/apps/rails-five/config/initializers/datadog.rb +18 -0
  79. data/integration/apps/rails-five/config/initializers/filter_parameter_logging.rb +4 -0
  80. data/integration/apps/rails-five/config/initializers/resque.rb +4 -0
  81. data/integration/apps/rails-five/config/initializers/rollbar.rb +5 -0
  82. data/integration/apps/rails-five/config/initializers/wrap_parameters.rb +14 -0
  83. data/integration/apps/rails-five/config/locales/en.yml +33 -0
  84. data/integration/apps/rails-five/config/puma.rb +24 -0
  85. data/integration/apps/rails-five/config/routes.rb +11 -0
  86. data/integration/apps/rails-five/config/spring.rb +6 -0
  87. data/integration/apps/rails-five/config/unicorn.rb +29 -0
  88. data/integration/apps/rails-five/db/migrate/20190927215052_create_tests.rb +11 -0
  89. data/integration/apps/rails-five/db/schema.rb +23 -0
  90. data/integration/apps/rails-five/db/seeds.rb +7 -0
  91. data/integration/apps/rails-five/docker-compose.ci.yml +98 -0
  92. data/integration/apps/rails-five/docker-compose.yml +100 -0
  93. data/integration/apps/rails-five/lib/tasks/.keep +0 -0
  94. data/integration/apps/rails-five/log/.keep +0 -0
  95. data/integration/apps/rails-five/public/robots.txt +1 -0
  96. data/integration/apps/rails-five/script/build-images +35 -0
  97. data/integration/apps/rails-five/script/ci +50 -0
  98. data/integration/apps/rails-five/spec/integration/basic_spec.rb +10 -0
  99. data/integration/apps/rails-five/spec/spec_helper.rb +16 -0
  100. data/integration/apps/rails-five/spec/support/integration_helper.rb +22 -0
  101. data/integration/apps/rails-five/storage/.keep +0 -0
  102. data/integration/apps/rails-five/tmp/.keep +0 -0
  103. data/integration/apps/rails-five/vendor/.keep +0 -0
  104. data/integration/apps/ruby/.dockerignore +1 -0
  105. data/integration/apps/ruby/.envrc.sample +1 -0
  106. data/integration/apps/ruby/.gitignore +2 -0
  107. data/integration/apps/ruby/Dockerfile +25 -0
  108. data/integration/apps/ruby/Dockerfile-ci +11 -0
  109. data/integration/apps/ruby/Gemfile +11 -0
  110. data/integration/apps/ruby/README.md +70 -0
  111. data/integration/apps/ruby/agent.yaml +3 -0
  112. data/integration/apps/ruby/app/datadog.rb +13 -0
  113. data/integration/apps/ruby/app/fibonacci.rb +58 -0
  114. data/integration/apps/ruby/bin/run +20 -0
  115. data/integration/apps/ruby/bin/setup +17 -0
  116. data/integration/apps/ruby/bin/test +21 -0
  117. data/integration/apps/ruby/docker-compose.ci.yml +51 -0
  118. data/integration/apps/ruby/docker-compose.yml +63 -0
  119. data/integration/apps/ruby/script/build-images +38 -0
  120. data/integration/apps/ruby/script/ci +50 -0
  121. data/integration/images/agent/Dockerfile +2 -0
  122. data/integration/images/agent/agent.yaml +3 -0
  123. data/integration/images/include/datadog/analyzer.rb +71 -0
  124. data/integration/images/include/datadog/demo_env.rb +101 -0
  125. data/integration/images/include/http-health-check +33 -0
  126. data/integration/images/ruby/2.0/Dockerfile +54 -0
  127. data/integration/images/ruby/2.1/Dockerfile +54 -0
  128. data/integration/images/ruby/2.2/Dockerfile +54 -0
  129. data/integration/images/ruby/2.3/Dockerfile +70 -0
  130. data/integration/images/ruby/2.4/Dockerfile +54 -0
  131. data/integration/images/ruby/2.5/Dockerfile +54 -0
  132. data/integration/images/ruby/2.6/Dockerfile +54 -0
  133. data/integration/images/ruby/2.7/Dockerfile +54 -0
  134. data/integration/images/ruby/3.0/Dockerfile +54 -0
  135. data/integration/images/wrk/Dockerfile +33 -0
  136. data/integration/images/wrk/scripts/entrypoint.sh +17 -0
  137. data/integration/images/wrk/scripts/scenarios/basic/default.lua +1 -0
  138. data/integration/images/wrk/scripts/scenarios/basic/fibonacci.lua +1 -0
  139. data/integration/script/build-images +43 -0
  140. data/lib/ddtrace.rb +0 -5
  141. data/lib/ddtrace/analytics.rb +2 -0
  142. data/lib/ddtrace/buffer.rb +4 -4
  143. data/lib/ddtrace/configuration.rb +1 -1
  144. data/lib/ddtrace/configuration/base.rb +1 -1
  145. data/lib/ddtrace/configuration/components.rb +2 -2
  146. data/lib/ddtrace/configuration/option_definition.rb +1 -3
  147. data/lib/ddtrace/configuration/options.rb +2 -3
  148. data/lib/ddtrace/configuration/settings.rb +17 -3
  149. data/lib/ddtrace/context.rb +5 -6
  150. data/lib/ddtrace/context_provider.rb +1 -0
  151. data/lib/ddtrace/contrib/action_cable/event.rb +1 -0
  152. data/lib/ddtrace/contrib/action_pack/action_controller/instrumentation.rb +1 -3
  153. data/lib/ddtrace/contrib/action_view/event.rb +1 -1
  154. data/lib/ddtrace/contrib/action_view/utils.rb +1 -1
  155. data/lib/ddtrace/contrib/active_record/configuration/resolver.rb +17 -5
  156. data/lib/ddtrace/contrib/active_record/utils.rb +1 -0
  157. data/lib/ddtrace/contrib/active_support/notifications/event.rb +2 -1
  158. data/lib/ddtrace/contrib/active_support/notifications/subscriber.rb +1 -0
  159. data/lib/ddtrace/contrib/active_support/notifications/subscription.rb +9 -5
  160. data/lib/ddtrace/contrib/auto_instrument.rb +1 -0
  161. data/lib/ddtrace/contrib/aws/patcher.rb +1 -0
  162. data/lib/ddtrace/contrib/aws/services.rb +1 -0
  163. data/lib/ddtrace/contrib/configuration/resolvers/pattern_resolver.rb +1 -4
  164. data/lib/ddtrace/contrib/cucumber/configuration/settings.rb +0 -10
  165. data/lib/ddtrace/contrib/cucumber/ext.rb +0 -2
  166. data/lib/ddtrace/contrib/cucumber/formatter.rb +5 -11
  167. data/lib/ddtrace/contrib/dalli/patcher.rb +0 -38
  168. data/lib/ddtrace/contrib/delayed_job/plugin.rb +0 -1
  169. data/lib/ddtrace/contrib/elasticsearch/quantize.rb +3 -2
  170. data/lib/ddtrace/contrib/ethon/easy_patch.rb +5 -5
  171. data/lib/ddtrace/contrib/excon/middleware.rb +2 -6
  172. data/lib/ddtrace/contrib/extensions.rb +1 -0
  173. data/lib/ddtrace/contrib/faraday/middleware.rb +1 -3
  174. data/lib/ddtrace/contrib/faraday/patcher.rb +0 -36
  175. data/lib/ddtrace/contrib/grape/endpoint.rb +8 -15
  176. data/lib/ddtrace/contrib/grape/patcher.rb +0 -42
  177. data/lib/ddtrace/contrib/grpc/datadog_interceptor.rb +8 -8
  178. data/lib/ddtrace/contrib/grpc/datadog_interceptor/server.rb +1 -0
  179. data/lib/ddtrace/contrib/grpc/patcher.rb +0 -36
  180. data/lib/ddtrace/contrib/http/circuit_breaker.rb +1 -3
  181. data/lib/ddtrace/contrib/http/instrumentation.rb +5 -5
  182. data/lib/ddtrace/contrib/httpclient/instrumentation.rb +2 -3
  183. data/lib/ddtrace/contrib/httprb/instrumentation.rb +2 -3
  184. data/lib/ddtrace/contrib/mongodb/instrumentation.rb +2 -0
  185. data/lib/ddtrace/contrib/mongodb/subscribers.rb +2 -3
  186. data/lib/ddtrace/contrib/patcher.rb +1 -1
  187. data/lib/ddtrace/contrib/qless/qless_job.rb +1 -0
  188. data/lib/ddtrace/contrib/qless/tracer_cleaner.rb +1 -0
  189. data/lib/ddtrace/contrib/que/ext.rb +19 -19
  190. data/lib/ddtrace/contrib/que/tracer.rb +1 -1
  191. data/lib/ddtrace/contrib/racecar/event.rb +1 -0
  192. data/lib/ddtrace/contrib/rack/configuration/settings.rb +3 -3
  193. data/lib/ddtrace/contrib/rack/middlewares.rb +5 -10
  194. data/lib/ddtrace/contrib/rack/patcher.rb +1 -3
  195. data/lib/ddtrace/contrib/rake/instrumentation.rb +4 -2
  196. data/lib/ddtrace/contrib/redis/quantize.rb +1 -0
  197. data/lib/ddtrace/contrib/redis/vendor/LICENSE +20 -0
  198. data/lib/ddtrace/contrib/redis/vendor/resolver.rb +6 -7
  199. data/lib/ddtrace/contrib/registry.rb +2 -2
  200. data/lib/ddtrace/contrib/resque/resque_job.rb +2 -0
  201. data/lib/ddtrace/contrib/rest_client/request_patch.rb +1 -3
  202. data/lib/ddtrace/contrib/rspec/configuration/settings.rb +0 -10
  203. data/lib/ddtrace/contrib/rspec/example.rb +24 -10
  204. data/lib/ddtrace/contrib/rspec/ext.rb +0 -3
  205. data/lib/ddtrace/contrib/rspec/integration.rb +1 -1
  206. data/lib/ddtrace/contrib/rspec/patcher.rb +0 -2
  207. data/lib/ddtrace/contrib/sequel/utils.rb +5 -6
  208. data/lib/ddtrace/contrib/shoryuken/tracer.rb +0 -1
  209. data/lib/ddtrace/contrib/sidekiq/server_tracer.rb +2 -7
  210. data/lib/ddtrace/contrib/sidekiq/tracing.rb +0 -1
  211. data/lib/ddtrace/contrib/sinatra/env.rb +1 -3
  212. data/lib/ddtrace/contrib/sinatra/headers.rb +1 -3
  213. data/lib/ddtrace/contrib/sinatra/tracer.rb +1 -3
  214. data/lib/ddtrace/contrib/sinatra/tracer_middleware.rb +3 -3
  215. data/lib/ddtrace/contrib/sneakers/ext.rb +11 -11
  216. data/lib/ddtrace/contrib/sneakers/tracer.rb +2 -4
  217. data/lib/ddtrace/contrib/status_code_matcher.rb +5 -3
  218. data/lib/ddtrace/correlation.rb +1 -0
  219. data/lib/ddtrace/diagnostics/environment_logger.rb +2 -1
  220. data/lib/ddtrace/distributed_tracing/headers/headers.rb +1 -0
  221. data/lib/ddtrace/distributed_tracing/headers/helpers.rb +1 -3
  222. data/lib/ddtrace/ext/ci.rb +2 -2
  223. data/lib/ddtrace/ext/distributed.rb +1 -1
  224. data/lib/ddtrace/ext/http.rb +1 -1
  225. data/lib/ddtrace/ext/runtime.rb +1 -1
  226. data/lib/ddtrace/forced_tracing.rb +2 -0
  227. data/lib/ddtrace/logger.rb +1 -1
  228. data/lib/ddtrace/metrics.rb +10 -6
  229. data/lib/ddtrace/opentracer/distributed_headers.rb +3 -0
  230. data/lib/ddtrace/opentracer/span.rb +2 -6
  231. data/lib/ddtrace/opentracer/thread_local_scope.rb +1 -0
  232. data/lib/ddtrace/patcher.rb +2 -3
  233. data/lib/ddtrace/pin.rb +3 -52
  234. data/lib/ddtrace/pipeline/span_filter.rb +1 -1
  235. data/lib/ddtrace/propagation/grpc_propagator.rb +1 -0
  236. data/lib/ddtrace/quantization/http.rb +1 -0
  237. data/lib/ddtrace/runtime/cgroup.rb +1 -1
  238. data/lib/ddtrace/runtime/container.rb +2 -2
  239. data/lib/ddtrace/sampler.rb +1 -1
  240. data/lib/ddtrace/sampling/rule_sampler.rb +1 -0
  241. data/lib/ddtrace/span.rb +7 -7
  242. data/lib/ddtrace/sync_writer.rb +7 -10
  243. data/lib/ddtrace/tracer.rb +1 -1
  244. data/lib/ddtrace/transport/http.rb +1 -3
  245. data/lib/ddtrace/transport/http/adapters/net.rb +9 -4
  246. data/lib/ddtrace/transport/http/adapters/registry.rb +1 -0
  247. data/lib/ddtrace/transport/http/adapters/unix_socket.rb +2 -4
  248. data/lib/ddtrace/transport/http/builder.rb +2 -0
  249. data/lib/ddtrace/transport/http/traces.rb +2 -3
  250. data/lib/ddtrace/transport/io.rb +1 -1
  251. data/lib/ddtrace/transport/traces.rb +3 -0
  252. data/lib/ddtrace/utils/time.rb +25 -1
  253. data/lib/ddtrace/vendor/active_record/MIT-LICENSE +20 -0
  254. data/lib/ddtrace/version.rb +1 -1
  255. data/lib/ddtrace/workers.rb +5 -0
  256. data/lib/ddtrace/workers/async.rb +8 -0
  257. data/lib/ddtrace/workers/loop.rb +3 -0
  258. data/lib/ddtrace/workers/polling.rb +1 -0
  259. data/lib/ddtrace/workers/trace_writer.rb +9 -10
  260. data/lib/ddtrace/writer.rb +3 -3
  261. metadata +133 -400
  262. data/lib/ddtrace/augmentation.rb +0 -13
  263. data/lib/ddtrace/augmentation/method_wrapper.rb +0 -20
  264. data/lib/ddtrace/augmentation/method_wrapping.rb +0 -38
  265. data/lib/ddtrace/augmentation/shim.rb +0 -102
  266. data/lib/ddtrace/contrib/rspec/example_group.rb +0 -61
  267. data/lib/ddtrace/monkey.rb +0 -58
@@ -5,6 +5,10 @@ require 'ddtrace/runtime/metrics'
5
5
  module Datadog
6
6
  # SyncWriter flushes both services and traces synchronously
7
7
  # DEV: To be replaced by Datadog::Workers::TraceWriter.
8
+ #
9
+ # Note: If you're wondering if this class is used at all, since there are no other references to it on the codebase,
10
+ # the separate `datadog-lambda` uses it as of February 2021:
11
+ # <https://github.com/DataDog/datadog-lambda-rb/blob/c15f0f0916c90123416dc44e7d6800ef4a7cfdbf/lib/datadog/lambda.rb#L38>
8
12
  class SyncWriter
9
13
  attr_reader \
10
14
  :priority_sampler,
@@ -29,9 +33,7 @@ module Datadog
29
33
  end
30
34
  end
31
35
 
32
- perform_concurrently(
33
- proc { flush_trace(trace) }
34
- )
36
+ flush_trace(trace)
35
37
  rescue => e
36
38
  Datadog.logger.debug(e)
37
39
  end
@@ -44,13 +46,10 @@ module Datadog
44
46
 
45
47
  private
46
48
 
47
- def perform_concurrently(*tasks)
48
- tasks.map { |task| Thread.new(&task) }.each(&:join)
49
- end
50
-
51
49
  def flush_trace(trace)
52
50
  processed_traces = Pipeline.process!([trace])
53
51
  return if processed_traces.empty?
52
+
54
53
  inject_hostname!(processed_traces.first) if Datadog.configuration.report_hostname
55
54
  transport.send_traces(processed_traces)
56
55
  end
@@ -58,9 +57,7 @@ module Datadog
58
57
  def inject_hostname!(trace)
59
58
  unless trace.first.nil?
60
59
  hostname = Datadog::Runtime::Socket.hostname
61
- unless hostname.nil? || hostname.empty?
62
- trace.first.set_tag(Ext::NET::TAG_HOSTNAME, hostname)
63
- end
60
+ trace.first.set_tag(Ext::NET::TAG_HOSTNAME, hostname) unless hostname.nil? || hostname.empty?
64
61
  end
65
62
  end
66
63
  end
@@ -1,4 +1,3 @@
1
- require 'thread'
2
1
  require 'logger'
3
2
  require 'pathname'
4
3
 
@@ -144,6 +143,7 @@ module Datadog
144
143
  # a service would be invalid and rejected.
145
144
  def default_service
146
145
  return @default_service if instance_variable_defined?(:@default_service) && @default_service
146
+
147
147
  begin
148
148
  @default_service = File.basename($PROGRAM_NAME, '.*')
149
149
  rescue StandardError => e
@@ -69,9 +69,7 @@ module Datadog
69
69
  }.tap do |headers|
70
70
  # Add container ID, if present.
71
71
  container_id = Datadog::Runtime::Container.container_id
72
- unless container_id.nil?
73
- headers[Datadog::Ext::Transport::HTTP::HEADER_CONTAINER_ID] = container_id
74
- end
72
+ headers[Datadog::Ext::Transport::HTTP::HEADER_CONTAINER_ID] = container_id unless container_id.nil?
75
73
  end
76
74
  end
77
75
 
@@ -19,7 +19,7 @@ module Datadog
19
19
  @timeout = options[:timeout] || DEFAULT_TIMEOUT
20
20
  end
21
21
 
22
- def open
22
+ def open(&block)
23
23
  # DEV Initializing +Net::HTTP+ directly help us avoid expensive
24
24
  # options processing done in +Net::HTTP.start+:
25
25
  # https://github.com/ruby/ruby/blob/b2d96abb42abbe2e01f010ffc9ac51f0f9a50002/lib/net/http.rb#L614-L618
@@ -27,9 +27,7 @@ module Datadog
27
27
 
28
28
  req.open_timeout = req.read_timeout = timeout
29
29
 
30
- req.start do |http|
31
- yield(http)
32
- end
30
+ req.start(&block)
33
31
  end
34
32
 
35
33
  def call(env)
@@ -82,36 +80,43 @@ module Datadog
82
80
 
83
81
  def payload
84
82
  return super if http_response.nil?
83
+
85
84
  http_response.body
86
85
  end
87
86
 
88
87
  def code
89
88
  return super if http_response.nil?
89
+
90
90
  http_response.code.to_i
91
91
  end
92
92
 
93
93
  def ok?
94
94
  return super if http_response.nil?
95
+
95
96
  code.between?(200, 299)
96
97
  end
97
98
 
98
99
  def unsupported?
99
100
  return super if http_response.nil?
101
+
100
102
  code == 415
101
103
  end
102
104
 
103
105
  def not_found?
104
106
  return super if http_response.nil?
107
+
105
108
  code == 404
106
109
  end
107
110
 
108
111
  def client_error?
109
112
  return super if http_response.nil?
113
+
110
114
  code.between?(400, 499)
111
115
  end
112
116
 
113
117
  def server_error?
114
118
  return super if http_response.nil?
119
+
115
120
  code.between?(500, 599)
116
121
  end
117
122
 
@@ -15,6 +15,7 @@ module Datadog
15
15
  def set(klass, name = nil)
16
16
  name ||= klass.to_s
17
17
  return if name.nil?
18
+
18
19
  @adapters[name] = klass
19
20
  end
20
21
  end
@@ -18,7 +18,7 @@ module Datadog
18
18
  @timeout = options.fetch(:timeout, DEFAULT_TIMEOUT)
19
19
  end
20
20
 
21
- def open
21
+ def open(&block)
22
22
  # Open connection
23
23
  connection = HTTP.new(
24
24
  filepath,
@@ -26,9 +26,7 @@ module Datadog
26
26
  continue_timeout: timeout
27
27
  )
28
28
 
29
- connection.start do |http|
30
- yield(http)
31
- end
29
+ connection.start(&block)
32
30
  end
33
31
 
34
32
  def url
@@ -36,6 +36,7 @@ module Datadog
36
36
  @default_adapter = if type.is_a?(Symbol)
37
37
  registry_klass = REGISTRY.get(type)
38
38
  raise UnknownAdapterError, type if registry_klass.nil?
39
+
39
40
  registry_klass.new(*args)
40
41
  else
41
42
  type
@@ -67,6 +68,7 @@ module Datadog
67
68
 
68
69
  def default_api=(key)
69
70
  raise UnknownApiError, key unless @apis.key?(key)
71
+
70
72
  @default_api = key
71
73
  end
72
74
 
@@ -41,6 +41,7 @@ module Datadog
41
41
 
42
42
  def send_traces(env, &block)
43
43
  raise NoTraceEndpointDefinedError, self if traces.nil?
44
+
44
45
  traces.call(env, &block)
45
46
  end
46
47
 
@@ -121,9 +122,7 @@ module Datadog
121
122
  # Parse service rates, if configured to do so.
122
123
  if service_rates? && !http_response.payload.to_s.empty?
123
124
  body = JSON.parse(http_response.payload)
124
- if body.is_a?(Hash) && body.key?(SERVICE_RATE_KEY)
125
- options[:service_rates] = body[SERVICE_RATE_KEY]
126
- end
125
+ options[:service_rates] = body[SERVICE_RATE_KEY] if body.is_a?(Hash) && body.key?(SERVICE_RATE_KEY)
127
126
  end
128
127
  end
129
128
 
@@ -17,7 +17,7 @@ module Datadog
17
17
  # Pass options to override any settings.
18
18
  def default(options = {})
19
19
  new(
20
- options.fetch(:out, STDOUT),
20
+ options.fetch(:out, $stdout),
21
21
  options.fetch(:encoder, Encoding::JSONEncoder)
22
22
  )
23
23
  end
@@ -142,17 +142,20 @@ module Datadog
142
142
 
143
143
  def downgrade?(response)
144
144
  return false unless apis.fallbacks.key?(@current_api_id)
145
+
145
146
  response.not_found? || response.unsupported?
146
147
  end
147
148
 
148
149
  def downgrade!
149
150
  downgrade_api_id = apis.fallbacks[@current_api_id]
150
151
  raise NoDowngradeAvailableError, @current_api_id if downgrade_api_id.nil?
152
+
151
153
  change_api!(downgrade_api_id)
152
154
  end
153
155
 
154
156
  def change_api!(api_id)
155
157
  raise UnknownApiVersionError, api_id unless apis.key?(api_id)
158
+
156
159
  @current_api_id = api_id
157
160
  @client = HTTP::Client.new(current_api)
158
161
  end
@@ -6,8 +6,32 @@ module Datadog
6
6
 
7
7
  module_function
8
8
 
9
+ # Current monotonic time.
10
+ # Falls back to `now` if monotonic clock
11
+ # is not available.
12
+ #
13
+ # @return [Float] in seconds, since some unspecified starting point
9
14
  def get_time
10
- PROCESS_TIME_SUPPORTED ? Process.clock_gettime(Process::CLOCK_MONOTONIC) : ::Time.now.to_f
15
+ PROCESS_TIME_SUPPORTED ? Process.clock_gettime(Process::CLOCK_MONOTONIC) : now.to_f
16
+ end
17
+
18
+ # Current wall time.
19
+ #
20
+ # @return [Time] current time object
21
+ def now
22
+ ::Time.now
23
+ end
24
+
25
+ # Overrides the implementation of `#now
26
+ # with the provided callable.
27
+ #
28
+ # Overriding the method `#now` instead of
29
+ # indirectly calling `block` removes
30
+ # one level of method call overhead.
31
+ #
32
+ # @param block [Proc] block that returns a `Time` object representing the current wall time
33
+ def now_provider=(block)
34
+ define_singleton_method(:now, &block)
11
35
  end
12
36
  end
13
37
  end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2005-2018 David Heinemeier Hansson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,7 +1,7 @@
1
1
  module Datadog
2
2
  module VERSION
3
3
  MAJOR = 0
4
- MINOR = 45
4
+ MINOR = 46
5
5
  PATCH = 0
6
6
  PRE = nil
7
7
 
@@ -67,9 +67,13 @@ module Datadog
67
67
  def start
68
68
  @mutex.synchronize do
69
69
  return if @run
70
+
70
71
  @run = true
71
72
  Datadog.logger.debug("Starting thread in the process: #{Process.pid}")
72
73
  @worker = Thread.new { perform }
74
+ @worker.name = self.class.name unless Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.3')
75
+
76
+ nil
73
77
  end
74
78
  end
75
79
 
@@ -108,6 +112,7 @@ module Datadog
108
112
 
109
113
  @mutex.synchronize do
110
114
  return if !@run && @trace_buffer.empty?
115
+
111
116
  @shutdown.wait(@mutex, @back_off) if @run # do not wait when shutting down
112
117
  end
113
118
  end
@@ -31,11 +31,13 @@ module Datadog
31
31
 
32
32
  def join(timeout = nil)
33
33
  return true unless running?
34
+
34
35
  !worker.join(timeout).nil?
35
36
  end
36
37
 
37
38
  def terminate
38
39
  return false unless running?
40
+
39
41
  @run_async = false
40
42
  worker.terminate
41
43
  true
@@ -43,6 +45,7 @@ module Datadog
43
45
 
44
46
  def run_async?
45
47
  return false unless instance_variable_defined?(:@run_async)
48
+
46
49
  @run_async == true
47
50
  end
48
51
 
@@ -56,6 +59,7 @@ module Datadog
56
59
 
57
60
  def error?
58
61
  return false unless instance_variable_defined?(:@error)
62
+
59
63
  !@error.nil?
60
64
  end
61
65
 
@@ -104,6 +108,7 @@ module Datadog
104
108
  def start(&block)
105
109
  mutex.synchronize do
106
110
  return if running?
111
+
107
112
  if forked?
108
113
  case fork_policy
109
114
  when FORK_POLICY_STOP
@@ -133,6 +138,9 @@ module Datadog
133
138
  raise
134
139
  end
135
140
  end
141
+ @worker.name = self.class.name unless Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.3')
142
+
143
+ nil
136
144
  end
137
145
 
138
146
  def stop_fork
@@ -21,6 +21,7 @@ module Datadog
21
21
  def stop_loop
22
22
  mutex.synchronize do
23
23
  return false unless run_loop?
24
+
24
25
  @run_loop = false
25
26
  shutdown.signal
26
27
  end
@@ -34,6 +35,7 @@ module Datadog
34
35
 
35
36
  def run_loop?
36
37
  return false unless instance_variable_defined?(:@run_loop)
38
+
37
39
  @run_loop == true
38
40
  end
39
41
 
@@ -92,6 +94,7 @@ module Datadog
92
94
  # Wait for an interval, unless shutdown has been signaled.
93
95
  mutex.synchronize do
94
96
  return unless run_loop? || work_pending?
97
+
95
98
  shutdown.wait(mutex, loop_wait_time) if run_loop?
96
99
  end
97
100
  end
@@ -35,6 +35,7 @@ module Datadog
35
35
 
36
36
  def enabled?
37
37
  return true unless instance_variable_defined?(:@enabled)
38
+
38
39
  @enabled
39
40
  end
40
41
 
@@ -16,9 +16,7 @@ module Datadog
16
16
  def initialize(options = {})
17
17
  transport_options = options.fetch(:transport_options, {})
18
18
 
19
- if transport_options.is_a?(Proc)
20
- transport_options = { on_build: transport_options }
21
- end
19
+ transport_options = { on_build: transport_options } if transport_options.is_a?(Proc)
22
20
 
23
21
  transport_options[:hostname] = options[:hostname] if options.key?(:hostname)
24
22
  transport_options[:port] = options[:port] if options.key?(:port)
@@ -66,9 +64,7 @@ module Datadog
66
64
  next if trace.first.nil?
67
65
 
68
66
  hostname = Datadog::Runtime::Socket.hostname
69
- unless hostname.nil? || hostname.empty?
70
- trace.first.set_tag(Ext::NET::TAG_HOSTNAME, hostname)
71
- end
67
+ trace.first.set_tag(Ext::NET::TAG_HOSTNAME, hostname) unless hostname.nil? || hostname.empty?
72
68
  end
73
69
  end
74
70
 
@@ -84,10 +80,6 @@ module Datadog
84
80
  def initialize
85
81
  super(:flush_completed)
86
82
  end
87
-
88
- def publish(response)
89
- super(response)
90
- end
91
83
  end
92
84
  end
93
85
 
@@ -127,10 +119,15 @@ module Datadog
127
119
 
128
120
  # NOTE: #perform is wrapped by other modules:
129
121
  # Polling --> Async --> IntervalLoop --> AsyncTraceWriter --> TraceWriter
122
+ #
123
+ # WARNING: This method breaks the Liskov Substitution Principle -- TraceWriter#perform is spec'd to return the
124
+ # result from the writer, whereas this method always returns nil.
130
125
  def perform(traces)
131
126
  super(traces).tap do |responses|
132
127
  loop_back_off! if responses.find(&:server_error?)
133
128
  end
129
+
130
+ nil
134
131
  end
135
132
 
136
133
  def stop(*args)
@@ -187,6 +184,8 @@ module Datadog
187
184
  @async = false if @writer_fork_policy == FORK_POLICY_SYNC
188
185
  end
189
186
 
187
+ # WARNING: This method breaks the Liskov Substitution Principle -- TraceWriter#write is spec'd to return the
188
+ # result from the writer, whereas this method returns something else when running in async mode.
190
189
  def write(trace)
191
190
  # Start worker thread. If the process has forked, it will trigger #after_fork to
192
191
  # reconfigure the worker accordingly.