diff --git a/docs/GettingStarted.md b/docs/GettingStarted.md index a076565664b..2ab8796504a 100644 --- a/docs/GettingStarted.md +++ b/docs/GettingStarted.md @@ -1356,15 +1356,7 @@ The sampler can set the priority to the following values: - `Datadog::Ext::Priority::AUTO_REJECT`: the sampler automatically decided to reject the trace. - `Datadog::Ext::Priority::AUTO_KEEP`: the sampler automatically decided to keep the trace. -For now, priority sampling is disabled by default. Enabling it ensures that your sampled distributed traces will be complete. To enable the priority sampling: - -```ruby -Datadog.configure do |c| - c.tracer priority_sampling: true -end -``` - -Once enabled, the sampler will automatically assign a priority of 0 or 1 to traces, depending on their service and volume. +Priority sampling is enabled by default. Enabling it ensures that your sampled distributed traces will be complete. Once enabled, the sampler will automatically assign a priority of 0 or 1 to traces, depending on their service and volume. You can also set this priority manually to either drop a non-interesting trace or to keep an important one. For that, set the `context#sampling_priority` to: diff --git a/lib/ddtrace/tracer.rb b/lib/ddtrace/tracer.rb index 9aa97a6cd04..a7c19882aaa 100644 --- a/lib/ddtrace/tracer.rb +++ b/lib/ddtrace/tracer.rb @@ -126,7 +126,7 @@ def configure(options = {}) # Those are rare "power-user" options. sampler = options.fetch(:sampler, nil) - priority_sampling = options[:priority_sampling] + priority_sampling = options.fetch(:priority_sampling, nil) max_spans_before_partial_flush = options.fetch(:max_spans_before_partial_flush, nil) min_spans_before_partial_flush = options.fetch(:min_spans_before_partial_flush, nil) partial_flush_timeout = options.fetch(:partial_flush_timeout, nil) @@ -134,9 +134,15 @@ def configure(options = {}) @enabled = enabled unless enabled.nil? @sampler = sampler unless sampler.nil? - if priority_sampling + # Re-build the sampler and writer if priority sampling is enabled, + # but neither are configured. Verify the sampler isn't already a + # priority sampler too, so we don't wrap one with another. + if priority_sampling != false && !@sampler.is_a?(PrioritySampler) @sampler = PrioritySampler.new(base_sampler: @sampler) @writer = Writer.new(priority_sampler: @sampler) + elsif priority_sampling == false + @sampler = sampler || Datadog::AllSampler.new if @sampler.is_a?(PrioritySampler) + @writer = Writer.new end @writer.transport.hostname = hostname unless hostname.nil? diff --git a/spec/ddtrace/configuration/pin_setup_spec.rb b/spec/ddtrace/configuration/pin_setup_spec.rb index ab3bbf732f0..d9d3d89bf0f 100644 --- a/spec/ddtrace/configuration/pin_setup_spec.rb +++ b/spec/ddtrace/configuration/pin_setup_spec.rb @@ -24,7 +24,7 @@ } end - let(:tracer) { Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } it do expect(target.datadog_pin.service).to eq('my-service') diff --git a/spec/ddtrace/context_spec.rb b/spec/ddtrace/context_spec.rb index acf986d598b..677df748037 100644 --- a/spec/ddtrace/context_spec.rb +++ b/spec/ddtrace/context_spec.rb @@ -5,7 +5,7 @@ RSpec.describe Datadog::Context do subject(:context) { described_class.new(options) } let(:options) { {} } - let(:tracer) { ::Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } describe '#current_root_span' do subject(:current_root_span) { context.current_root_span } diff --git a/spec/ddtrace/contrib/active_model_serializers/patcher_spec.rb b/spec/ddtrace/contrib/active_model_serializers/patcher_spec.rb index eb17b8d37d2..28ddf0c5ff8 100644 --- a/spec/ddtrace/contrib/active_model_serializers/patcher_spec.rb +++ b/spec/ddtrace/contrib/active_model_serializers/patcher_spec.rb @@ -10,7 +10,7 @@ RSpec.describe 'ActiveModelSerializers patcher' do include_context 'AMS serializer' - let(:tracer) { ::Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } def all_spans tracer.writer.spans(:keep) diff --git a/spec/ddtrace/contrib/active_record/multi_db_spec.rb b/spec/ddtrace/contrib/active_record/multi_db_spec.rb index 18dbf955f70..924b2df1a1c 100644 --- a/spec/ddtrace/contrib/active_record/multi_db_spec.rb +++ b/spec/ddtrace/contrib/active_record/multi_db_spec.rb @@ -6,7 +6,7 @@ require 'sqlite3' RSpec.describe 'ActiveRecord multi-database implementation' do - let(:tracer) { ::Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:configuration_options) { { tracer: tracer, service_name: default_db_service_name } } let(:default_db_service_name) { 'default-db' } diff --git a/spec/ddtrace/contrib/active_record/performance_spec.rb b/spec/ddtrace/contrib/active_record/performance_spec.rb index a1b4aac1f3d..aee3eaba788 100644 --- a/spec/ddtrace/contrib/active_record/performance_spec.rb +++ b/spec/ddtrace/contrib/active_record/performance_spec.rb @@ -5,7 +5,7 @@ require 'sqlite3' RSpec.describe 'ActiveRecord tracing performance' do - let(:tracer) { ::Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:options) { { tracer: tracer } } let(:spans) { tracer.writer.spans } diff --git a/spec/ddtrace/contrib/active_record/tracer_spec.rb b/spec/ddtrace/contrib/active_record/tracer_spec.rb index 8ea9f479951..decef947731 100644 --- a/spec/ddtrace/contrib/active_record/tracer_spec.rb +++ b/spec/ddtrace/contrib/active_record/tracer_spec.rb @@ -4,7 +4,7 @@ require_relative 'app' RSpec.describe 'ActiveRecord instrumentation' do - let(:tracer) { ::Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:configuration_options) { { tracer: tracer } } before(:each) do diff --git a/spec/ddtrace/contrib/active_support/notifications/subscription_spec.rb b/spec/ddtrace/contrib/active_support/notifications/subscription_spec.rb index afee9f296e1..3e6fd6b27db 100644 --- a/spec/ddtrace/contrib/active_support/notifications/subscription_spec.rb +++ b/spec/ddtrace/contrib/active_support/notifications/subscription_spec.rb @@ -7,7 +7,7 @@ RSpec.describe Datadog::Contrib::ActiveSupport::Notifications::Subscription do describe 'instance' do subject(:subscription) { described_class.new(tracer, span_name, options, &block) } - let(:tracer) { ::Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:span_name) { double('span_name') } let(:options) { {} } let(:payload) { {} } diff --git a/spec/ddtrace/contrib/aws/instrumentation_spec.rb b/spec/ddtrace/contrib/aws/instrumentation_spec.rb index a7eed337119..36a46f61aea 100644 --- a/spec/ddtrace/contrib/aws/instrumentation_spec.rb +++ b/spec/ddtrace/contrib/aws/instrumentation_spec.rb @@ -6,7 +6,7 @@ require 'ddtrace/ext/http' RSpec.describe 'AWS instrumentation' do - let(:tracer) { ::Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:client) { ::Aws::S3::Client.new(stub_responses: responses) } let(:responses) { true } diff --git a/spec/ddtrace/contrib/concurrent_ruby/integration_spec.rb b/spec/ddtrace/contrib/concurrent_ruby/integration_spec.rb index b32bae667ad..0ea383c421b 100644 --- a/spec/ddtrace/contrib/concurrent_ruby/integration_spec.rb +++ b/spec/ddtrace/contrib/concurrent_ruby/integration_spec.rb @@ -12,7 +12,7 @@ remove_patch!(:concurrent_ruby) end - let(:tracer) { ::Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:configuration_options) { { tracer: tracer } } subject(:deferred_execution) do diff --git a/spec/ddtrace/contrib/dalli/instrumentation_spec.rb b/spec/ddtrace/contrib/dalli/instrumentation_spec.rb index f6592f73c46..a626ecb6b26 100644 --- a/spec/ddtrace/contrib/dalli/instrumentation_spec.rb +++ b/spec/ddtrace/contrib/dalli/instrumentation_spec.rb @@ -9,7 +9,7 @@ let(:test_port) { ENV.fetch('TEST_MEMCACHED_PORT', '11211') } let(:client) { ::Dalli::Client.new("#{test_host}:#{test_port}") } - let(:tracer) { ::Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:pin) { ::Dalli.datadog_pin } def all_spans diff --git a/spec/ddtrace/contrib/delayed_job/plugin_spec.rb b/spec/ddtrace/contrib/delayed_job/plugin_spec.rb index 23a70b01f69..1157c02bb07 100644 --- a/spec/ddtrace/contrib/delayed_job/plugin_spec.rb +++ b/spec/ddtrace/contrib/delayed_job/plugin_spec.rb @@ -24,7 +24,7 @@ def job_data end) end - let(:tracer) { ::Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } before do Datadog.configure { |c| c.use :delayed_job, tracer: tracer } diff --git a/spec/ddtrace/contrib/elasticsearch/patcher_spec.rb b/spec/ddtrace/contrib/elasticsearch/patcher_spec.rb index e8798fea60e..2ad06417758 100644 --- a/spec/ddtrace/contrib/elasticsearch/patcher_spec.rb +++ b/spec/ddtrace/contrib/elasticsearch/patcher_spec.rb @@ -9,7 +9,7 @@ let(:client) { Elasticsearch::Client.new(url: server) } let(:pin) { Datadog::Pin.get_from(client) } - let(:tracer) { Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } before do Datadog.configure do |c| diff --git a/spec/ddtrace/contrib/elasticsearch/transport_spec.rb b/spec/ddtrace/contrib/elasticsearch/transport_spec.rb index dbe5206a475..e93b747b0f2 100644 --- a/spec/ddtrace/contrib/elasticsearch/transport_spec.rb +++ b/spec/ddtrace/contrib/elasticsearch/transport_spec.rb @@ -22,7 +22,7 @@ let(:server) { "http://#{host}:#{port}" } let(:client) { Elasticsearch::Client.new(url: server) } - let(:tracer) { Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:spans) { tracer.writer.spans } let(:span) { spans.first } diff --git a/spec/ddtrace/contrib/excon/instrumentation_spec.rb b/spec/ddtrace/contrib/excon/instrumentation_spec.rb index 9ab1d64abf0..452c2b7f73a 100644 --- a/spec/ddtrace/contrib/excon/instrumentation_spec.rb +++ b/spec/ddtrace/contrib/excon/instrumentation_spec.rb @@ -5,7 +5,7 @@ require 'ddtrace/contrib/excon/middleware' RSpec.describe Datadog::Contrib::Excon::Middleware do - let(:tracer) { Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:connection_options) { { mock: true } } let(:middleware_options) { {} } diff --git a/spec/ddtrace/contrib/faraday/middleware_spec.rb b/spec/ddtrace/contrib/faraday/middleware_spec.rb index 04dc8bc6547..469c0dfa2bb 100644 --- a/spec/ddtrace/contrib/faraday/middleware_spec.rb +++ b/spec/ddtrace/contrib/faraday/middleware_spec.rb @@ -5,7 +5,7 @@ require 'ddtrace/ext/distributed' RSpec.describe 'Faraday middleware' do - let(:tracer) { Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:client) do ::Faraday.new('http://example.com') do |builder| diff --git a/spec/ddtrace/contrib/graphql/tracer_spec.rb b/spec/ddtrace/contrib/graphql/tracer_spec.rb index 32c2e8af07a..9cb6651538f 100644 --- a/spec/ddtrace/contrib/graphql/tracer_spec.rb +++ b/spec/ddtrace/contrib/graphql/tracer_spec.rb @@ -14,7 +14,7 @@ end end - let(:tracer) { ::Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } def pop_spans tracer.writer.spans(:keep) diff --git a/spec/ddtrace/contrib/http/patcher_spec.rb b/spec/ddtrace/contrib/http/patcher_spec.rb index 69dd7ff040d..e65b596f497 100644 --- a/spec/ddtrace/contrib/http/patcher_spec.rb +++ b/spec/ddtrace/contrib/http/patcher_spec.rb @@ -3,7 +3,7 @@ require 'net/http' RSpec.describe 'net/http patcher' do - let(:tracer) { ::Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:host) { 'example.com' } before do diff --git a/spec/ddtrace/contrib/mongodb/client_spec.rb b/spec/ddtrace/contrib/mongodb/client_spec.rb index 04d83b8864f..7a7990c1b7c 100644 --- a/spec/ddtrace/contrib/mongodb/client_spec.rb +++ b/spec/ddtrace/contrib/mongodb/client_spec.rb @@ -4,7 +4,7 @@ require 'mongo' RSpec.describe 'Mongo::Client instrumentation' do - let(:tracer) { Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:client) { Mongo::Client.new(*client_options) } let(:client_options) { [["#{host}:#{port}"], { database: database }] } diff --git a/spec/ddtrace/contrib/mysql2/patcher_spec.rb b/spec/ddtrace/contrib/mysql2/patcher_spec.rb index 44924f94219..c68e67f6691 100644 --- a/spec/ddtrace/contrib/mysql2/patcher_spec.rb +++ b/spec/ddtrace/contrib/mysql2/patcher_spec.rb @@ -4,7 +4,7 @@ require 'mysql2' RSpec.describe 'Mysql2::Client patcher' do - let(:tracer) { Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:client) do Mysql2::Client.new( diff --git a/spec/ddtrace/contrib/racecar/patcher_spec.rb b/spec/ddtrace/contrib/racecar/patcher_spec.rb index 2679b4f87f0..e1a039e17a5 100644 --- a/spec/ddtrace/contrib/racecar/patcher_spec.rb +++ b/spec/ddtrace/contrib/racecar/patcher_spec.rb @@ -5,7 +5,7 @@ require 'active_support' require 'ddtrace' RSpec.describe 'Racecar patcher' do - let(:tracer) { ::Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } def all_spans tracer.writer.spans(:keep) diff --git a/spec/ddtrace/contrib/rack/distributed_spec.rb b/spec/ddtrace/contrib/rack/distributed_spec.rb index 63bef3fbb5f..de13aa804b4 100644 --- a/spec/ddtrace/contrib/rack/distributed_spec.rb +++ b/spec/ddtrace/contrib/rack/distributed_spec.rb @@ -8,7 +8,7 @@ RSpec.describe 'Rack integration distributed tracing' do include Rack::Test::Methods - let(:tracer) { Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:rack_options) { { tracer: tracer } } let(:spans) { tracer.writer.spans } diff --git a/spec/ddtrace/contrib/rack/integration_spec.rb b/spec/ddtrace/contrib/rack/integration_spec.rb index 900161789cc..e2e9aa1a745 100644 --- a/spec/ddtrace/contrib/rack/integration_spec.rb +++ b/spec/ddtrace/contrib/rack/integration_spec.rb @@ -9,7 +9,7 @@ RSpec.describe 'Rack integration tests' do include Rack::Test::Methods - let(:tracer) { Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:rack_options) { { tracer: tracer } } let(:spans) { tracer.writer.spans } diff --git a/spec/ddtrace/contrib/rack/middleware_spec.rb b/spec/ddtrace/contrib/rack/middleware_spec.rb index 337155a94cb..fd8c20bf363 100644 --- a/spec/ddtrace/contrib/rack/middleware_spec.rb +++ b/spec/ddtrace/contrib/rack/middleware_spec.rb @@ -8,7 +8,7 @@ subject(:middleware) { described_class.new(app) } let(:app) { instance_double(Rack::Builder) } - let(:tracer) { Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:configuration_options) { { tracer: tracer } } before(:each) do diff --git a/spec/ddtrace/contrib/rack/request_queuing_spec.rb b/spec/ddtrace/contrib/rack/request_queuing_spec.rb index c6f9f9b014d..4fe5aa06d7c 100644 --- a/spec/ddtrace/contrib/rack/request_queuing_spec.rb +++ b/spec/ddtrace/contrib/rack/request_queuing_spec.rb @@ -8,7 +8,7 @@ RSpec.describe 'Rack integration request queuing' do include Rack::Test::Methods - let(:tracer) { Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:rack_options) { { tracer: tracer } } let(:spans) { tracer.writer.spans } diff --git a/spec/ddtrace/contrib/rack/resource_name_spec.rb b/spec/ddtrace/contrib/rack/resource_name_spec.rb index f9bc60f650d..190b7e1624d 100644 --- a/spec/ddtrace/contrib/rack/resource_name_spec.rb +++ b/spec/ddtrace/contrib/rack/resource_name_spec.rb @@ -8,7 +8,7 @@ RSpec.describe 'Rack integration with other middleware' do include Rack::Test::Methods - let(:tracer) { Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:rack_options) do { application: app, diff --git a/spec/ddtrace/contrib/rails/action_controller_spec.rb b/spec/ddtrace/contrib/rails/action_controller_spec.rb index f6f65f1ec5e..af852c237aa 100644 --- a/spec/ddtrace/contrib/rails/action_controller_spec.rb +++ b/spec/ddtrace/contrib/rails/action_controller_spec.rb @@ -1,7 +1,7 @@ require 'ddtrace/contrib/rails/rails_helper' RSpec.describe 'ActionController tracing' do - let(:tracer) { ::Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:rails_options) { { tracer: tracer } } before(:each) do diff --git a/spec/ddtrace/contrib/rails/middleware_spec.rb b/spec/ddtrace/contrib/rails/middleware_spec.rb index d9baf2dc101..e20fe8b7bfb 100644 --- a/spec/ddtrace/contrib/rails/middleware_spec.rb +++ b/spec/ddtrace/contrib/rails/middleware_spec.rb @@ -15,7 +15,7 @@ def index end) end - let(:tracer) { ::Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } def all_spans tracer.writer.spans(:keep) diff --git a/spec/ddtrace/contrib/rails/railtie_spec.rb b/spec/ddtrace/contrib/rails/railtie_spec.rb index 29e0c33f9e7..fe134fec204 100644 --- a/spec/ddtrace/contrib/rails/railtie_spec.rb +++ b/spec/ddtrace/contrib/rails/railtie_spec.rb @@ -7,7 +7,7 @@ before(:each) { skip 'Test not compatible with Rails < 4.0' if Rails.version < '4.0' } include_context 'Rails test application' - let(:tracer) { ::Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:routes) { { '/' => 'test#index' } } let(:controllers) { [controller] } diff --git a/spec/ddtrace/contrib/rake/instrumentation_spec.rb b/spec/ddtrace/contrib/rake/instrumentation_spec.rb index 5e8568f8d59..ab9c8e3893f 100644 --- a/spec/ddtrace/contrib/rake/instrumentation_spec.rb +++ b/spec/ddtrace/contrib/rake/instrumentation_spec.rb @@ -7,7 +7,7 @@ require 'ddtrace/contrib/rake/patcher' RSpec.describe Datadog::Contrib::Rake::Instrumentation do - let(:tracer) { Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:configuration_options) { { tracer: tracer, enabled: true } } let(:spans) { tracer.writer.spans } let(:span) { spans.first } diff --git a/spec/ddtrace/contrib/redis/miniapp_spec.rb b/spec/ddtrace/contrib/redis/miniapp_spec.rb index 3c77353b521..1e6e9b674f8 100644 --- a/spec/ddtrace/contrib/redis/miniapp_spec.rb +++ b/spec/ddtrace/contrib/redis/miniapp_spec.rb @@ -6,7 +6,7 @@ require 'ddtrace' RSpec.describe 'Redis mini app test' do - let(:tracer) { ::Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } def all_spans tracer.writer.spans(:keep) diff --git a/spec/ddtrace/contrib/redis/redis_spec.rb b/spec/ddtrace/contrib/redis/redis_spec.rb index 8e4314820d4..accdb9e496c 100644 --- a/spec/ddtrace/contrib/redis/redis_spec.rb +++ b/spec/ddtrace/contrib/redis/redis_spec.rb @@ -6,7 +6,7 @@ require 'ddtrace' RSpec.describe 'Redis test' do - let(:tracer) { ::Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } def all_spans tracer.writer.spans(:keep) diff --git a/spec/ddtrace/contrib/resque/instrumentation_spec.rb b/spec/ddtrace/contrib/resque/instrumentation_spec.rb index 67594b55607..39ff4f0319a 100644 --- a/spec/ddtrace/contrib/resque/instrumentation_spec.rb +++ b/spec/ddtrace/contrib/resque/instrumentation_spec.rb @@ -6,7 +6,7 @@ RSpec.describe 'Resque instrumentation' do include_context 'Resque job' - let(:tracer) { ::Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:pin) { ::Resque.datadog_pin } let(:spans) { tracer.writer.spans } let(:span) { spans.first } diff --git a/spec/ddtrace/contrib/rest_client/request_patch_spec.rb b/spec/ddtrace/contrib/rest_client/request_patch_spec.rb index b5a73641d5a..6abaf01a14f 100644 --- a/spec/ddtrace/contrib/rest_client/request_patch_spec.rb +++ b/spec/ddtrace/contrib/rest_client/request_patch_spec.rb @@ -5,7 +5,7 @@ require 'restclient/request' RSpec.describe Datadog::Contrib::RestClient::RequestPatch do - let(:tracer) { Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:rest_client_options) { {} } before do diff --git a/spec/ddtrace/contrib/sequel/configuration_spec.rb b/spec/ddtrace/contrib/sequel/configuration_spec.rb index c94f6423c12..de5e5a1dc50 100644 --- a/spec/ddtrace/contrib/sequel/configuration_spec.rb +++ b/spec/ddtrace/contrib/sequel/configuration_spec.rb @@ -6,7 +6,7 @@ require 'ddtrace/contrib/sequel/patcher' RSpec.describe 'Sequel configuration' do - let(:tracer) { Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:spans) { tracer.writer.spans } let(:span) { spans.first } diff --git a/spec/ddtrace/contrib/sequel/instrumentation_spec.rb b/spec/ddtrace/contrib/sequel/instrumentation_spec.rb index fc1c890d110..f1f39dba60f 100644 --- a/spec/ddtrace/contrib/sequel/instrumentation_spec.rb +++ b/spec/ddtrace/contrib/sequel/instrumentation_spec.rb @@ -6,7 +6,7 @@ require 'ddtrace/contrib/sequel/integration' RSpec.describe 'Sequel instrumentation' do - let(:tracer) { Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:configuration_options) { { tracer: tracer } } let(:sequel) do Sequel.sqlite(':memory:').tap do |s| diff --git a/spec/ddtrace/contrib/shoryuken/tracer_spec.rb b/spec/ddtrace/contrib/shoryuken/tracer_spec.rb index 00b8be4423d..afa15f5c60b 100644 --- a/spec/ddtrace/contrib/shoryuken/tracer_spec.rb +++ b/spec/ddtrace/contrib/shoryuken/tracer_spec.rb @@ -3,7 +3,7 @@ require 'shoryuken' RSpec.describe Datadog::Contrib::Shoryuken::Tracer do - let(:tracer) { ::Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:options) { { tracer: tracer } } let(:spans) { tracer.writer.spans } let(:span) { spans.first } diff --git a/spec/ddtrace/contrib/sinatra/activerecord_spec.rb b/spec/ddtrace/contrib/sinatra/activerecord_spec.rb index 0ad21d92ad0..96501580945 100644 --- a/spec/ddtrace/contrib/sinatra/activerecord_spec.rb +++ b/spec/ddtrace/contrib/sinatra/activerecord_spec.rb @@ -11,7 +11,7 @@ RSpec.describe 'Sinatra instrumentation with ActiveRecord' do include Rack::Test::Methods - let(:tracer) { Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:options) { { tracer: tracer } } let(:span) { spans.first } diff --git a/spec/ddtrace/contrib/sinatra/multi_app_spec.rb b/spec/ddtrace/contrib/sinatra/multi_app_spec.rb index 925f070a740..580f7a63966 100644 --- a/spec/ddtrace/contrib/sinatra/multi_app_spec.rb +++ b/spec/ddtrace/contrib/sinatra/multi_app_spec.rb @@ -9,7 +9,7 @@ RSpec.describe 'Sinatra instrumentation for multi-apps' do include Rack::Test::Methods - let(:tracer) { Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:options) { { tracer: tracer } } let(:span) { spans.first } diff --git a/spec/ddtrace/contrib/sinatra/tracer_spec.rb b/spec/ddtrace/contrib/sinatra/tracer_spec.rb index 38bb8949620..8bd41ec3b36 100644 --- a/spec/ddtrace/contrib/sinatra/tracer_spec.rb +++ b/spec/ddtrace/contrib/sinatra/tracer_spec.rb @@ -9,7 +9,7 @@ RSpec.describe 'Sinatra instrumentation' do include Rack::Test::Methods - let(:tracer) { Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } let(:options) { { tracer: tracer } } let(:span) { spans.first } @@ -386,7 +386,7 @@ include_context 'app with simple route' subject(:response) { get '/' } - let(:tracer) { Datadog::Tracer.new(writer: FauxWriter.new, enabled: false) } + let(:tracer) { get_test_tracer(enabled: false) } it do is_expected.to be_ok diff --git a/spec/ddtrace/integration_spec.rb b/spec/ddtrace/integration_spec.rb index 901e874126d..750c0a1cb0d 100644 --- a/spec/ddtrace/integration_spec.rb +++ b/spec/ddtrace/integration_spec.rb @@ -148,14 +148,8 @@ def agent_receives_span_step3(previous_success) end describe 'sampling priority metrics' do - let(:tracer) do - get_test_tracer.tap do |t| - t.configure(priority_sampling: true) - t.writer = writer - end - end - - let(:writer) { FauxWriter.new(priority_sampler: Datadog::PrioritySampler.new) } + # Sampling priority is enabled by default + let(:tracer) { get_test_tracer } context 'when #sampling_priority is set on a child span' do let(:parent_span) { tracer.start_span('parent span') } @@ -168,7 +162,7 @@ def agent_receives_span_step3(previous_success) end.finish end.finish - try_wait_until { writer.spans(:keep).any? } + try_wait_until { tracer.writer.spans(:keep).any? } end it do diff --git a/spec/ddtrace/pin_spec.rb b/spec/ddtrace/pin_spec.rb index c6d1d80182a..41cbb08e621 100644 --- a/spec/ddtrace/pin_spec.rb +++ b/spec/ddtrace/pin_spec.rb @@ -23,7 +23,7 @@ context 'when given sufficient info' do let(:options) { { app: 'test-app', app_type: 'test-type', tracer: tracer } } - let(:tracer) { Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } it 'sets the service info' do expect(tracer.services.key?(service_name)).to be true @@ -35,7 +35,7 @@ context 'when given insufficient info' do let(:options) { { app_type: 'test-type', tracer: tracer } } - let(:tracer) { Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } it 'does not sets the service info' do expect(tracer.services).to be_empty diff --git a/spec/ddtrace/propagation/http_propagator_spec.rb b/spec/ddtrace/propagation/http_propagator_spec.rb index 1bbf84db715..5a868ddc782 100644 --- a/spec/ddtrace/propagation/http_propagator_spec.rb +++ b/spec/ddtrace/propagation/http_propagator_spec.rb @@ -4,7 +4,7 @@ require 'ddtrace/propagation/http_propagator' RSpec.describe Datadog::HTTPPropagator do - let(:tracer) { ::Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } describe '#inject!' do let(:env) { { 'something' => 'alien' } } diff --git a/spec/ddtrace/propagation/propagation_integration_spec.rb b/spec/ddtrace/propagation/propagation_integration_spec.rb index 51197163e8e..72c7384d44d 100644 --- a/spec/ddtrace/propagation/propagation_integration_spec.rb +++ b/spec/ddtrace/propagation/propagation_integration_spec.rb @@ -4,7 +4,7 @@ require 'ddtrace/propagation/http_propagator' RSpec.describe 'Context propagation' do - let(:tracer) { Datadog::Tracer.new(writer: FauxWriter.new) } + let(:tracer) { get_test_tracer } describe 'when max context size is exceeded' do let(:max_size) { 3 } diff --git a/spec/support/tracer_helpers.rb b/spec/support/tracer_helpers.rb index 706a22ee4ce..3d87f1606dd 100644 --- a/spec/support/tracer_helpers.rb +++ b/spec/support/tracer_helpers.rb @@ -4,8 +4,39 @@ module TracerHelpers # Return a test tracer instance with a faux writer. - def get_test_tracer - Datadog::Tracer.new(writer: FauxWriter.new) + def get_test_tracer(options = {}) + options = { writer: FauxWriter.new }.merge(options) + Datadog::Tracer.new(options).tap do |tracer| + # TODO: Let's try to get rid of this override, which has too much + # knowledge about the internal workings of the tracer. + # It is done to prevent the activation of priority sampling + # from wiping out the configured test writer, by replacing it. + tracer.define_singleton_method(:configure) do |opts = {}| + super(opts) + + # Re-configure the tracer with a new test writer + # since priority sampling will wipe out the old test writer. + unless @writer.is_a?(FauxWriter) + @writer = if @sampler.is_a?(Datadog::PrioritySampler) + FauxWriter.new(priority_sampler: @sampler) + else + FauxWriter.new + end + + hostname = opts.fetch(:hostname, nil) + port = opts.fetch(:port, nil) + + @writer.transport.hostname = hostname unless hostname.nil? + @writer.transport.port = port unless port.nil? + + statsd = opts.fetch(:statsd, nil) + unless statsd.nil? + @writer.statsd = statsd + @writer.transport.statsd = statsd + end + end + end + end end # Return some test traces diff --git a/test/context_flush_test.rb b/test/context_flush_test.rb index c238ba0b411..80446434786 100644 --- a/test/context_flush_test.rb +++ b/test/context_flush_test.rb @@ -346,7 +346,7 @@ def test_tracer_configure assert_nil(tracer.context_flush) # If given a partial_flush option, then uses default context flush. - flush_tracer = Datadog::Tracer.new(writer: FauxWriter.new, partial_flush: true) + flush_tracer = get_test_tracer(partial_flush: true) refute_nil(flush_tracer.context_flush) # If not configured with any flush options, context flush still doesn't exist. diff --git a/test/helper.rb b/test/helper.rb index 3f5fe96dbd1..937432b9e3c 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -35,8 +35,39 @@ class Span end # Return a test tracer instance with a faux writer. -def get_test_tracer - Datadog::Tracer.new(writer: FauxWriter.new) +def get_test_tracer(options = {}) + options = { writer: FauxWriter.new }.merge(options) + Datadog::Tracer.new(options).tap do |tracer| + # TODO: Let's try to get rid of this override, which has too much + # knowledge about the internal workings of the tracer. + # It is done to prevent the activation of priority sampling + # from wiping out the configured test writer, by replacing it. + tracer.define_singleton_method(:configure) do |opts = {}| + super(opts) + + # Re-configure the tracer with a new test writer + # since priority sampling will wipe out the old test writer. + unless @writer.is_a?(FauxWriter) + @writer = if @sampler.is_a?(Datadog::PrioritySampler) + FauxWriter.new(priority_sampler: @sampler) + else + FauxWriter.new + end + + hostname = opts.fetch(:hostname, nil) + port = opts.fetch(:port, nil) + + @writer.transport.hostname = hostname unless hostname.nil? + @writer.transport.port = port unless port.nil? + + statsd = opts.fetch(:statsd, nil) + unless statsd.nil? + @writer.statsd = statsd + @writer.transport.statsd = statsd + end + end + end + end end # Return some test traces diff --git a/test/sampler_test.rb b/test/sampler_test.rb index 84a62974db6..624918674b3 100644 --- a/test/sampler_test.rb +++ b/test/sampler_test.rb @@ -79,7 +79,7 @@ def test_tracer_with_rate_sampler prng = Random.new(123) tracer = get_test_tracer() - tracer.configure(sampler: Datadog::RateSampler.new(0.5)) + tracer.configure(sampler: Datadog::RateSampler.new(0.5), priority_sampling: false) nb_spans = 10000 nb_spans.times do @@ -87,7 +87,7 @@ def test_tracer_with_rate_sampler span.finish() end - spans = tracer.writer.spans() + spans = tracer.writer.spans expected = nb_spans * 0.5 assert_in_delta(spans.length, expected, 0.1 * expected)