Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use tracer environment variables if provided #631

Merged
merged 1 commit into from
Nov 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DD_AGENT_HOST=localhost
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

DD_TRACE_AGENT_PORT=8126
TEST_DDAGENT_API_KEY=invalid_key_but_this_is_fine
TEST_DDAGENT_HOST=localhost
TEST_DDAGENT_PORT=8126
TEST_ELASTICSEARCH_HOST=127.0.0.1
TEST_ELASTICSEARCH_NATIVE_PORT=9300
TEST_ELASTICSEARCH_REST_PORT=9200
Expand Down
14 changes: 7 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ services:
env_file: ./.env
environment:
- BUNDLE_GEMFILE=/app/Gemfile
- DD_AGENT_HOST=ddagent
- TEST_DATADOG_INTEGRATION=1
- TEST_DDAGENT_HOST=ddagent
- TEST_ELASTICSEARCH_HOST=elasticsearch
- TEST_MEMCACHED_HOST=memcached
- TEST_MONGODB_HOST=mongodb
Expand Down Expand Up @@ -46,8 +46,8 @@ services:
env_file: ./.env
environment:
- BUNDLE_GEMFILE=/app/Gemfile
- DD_AGENT_HOST=ddagent
- TEST_DATADOG_INTEGRATION=1
- TEST_DDAGENT_HOST=ddagent
- TEST_ELASTICSEARCH_HOST=elasticsearch
- TEST_MEMCACHED_HOST=memcached
- TEST_MONGODB_HOST=mongodb
Expand Down Expand Up @@ -76,8 +76,8 @@ services:
env_file: ./.env
environment:
- BUNDLE_GEMFILE=/app/Gemfile
- DD_AGENT_HOST=ddagent
- TEST_DATADOG_INTEGRATION=1
- TEST_DDAGENT_HOST=ddagent
- TEST_ELASTICSEARCH_HOST=elasticsearch
- TEST_MEMCACHED_HOST=memcached
- TEST_MONGODB_HOST=mongodb
Expand Down Expand Up @@ -106,8 +106,8 @@ services:
env_file: ./.env
environment:
- BUNDLE_GEMFILE=/app/Gemfile
- DD_AGENT_HOST=ddagent
- TEST_DATADOG_INTEGRATION=1
- TEST_DDAGENT_HOST=ddagent
- TEST_ELASTICSEARCH_HOST=elasticsearch
- TEST_MEMCACHED_HOST=memcached
- TEST_MONGODB_HOST=mongodb
Expand Down Expand Up @@ -136,8 +136,8 @@ services:
env_file: ./.env
environment:
- BUNDLE_GEMFILE=/app/Gemfile
- DD_AGENT_HOST=ddagent
- TEST_DATADOG_INTEGRATION=1
- TEST_DDAGENT_HOST=ddagent
- TEST_ELASTICSEARCH_HOST=elasticsearch
- TEST_MEMCACHED_HOST=memcached
- TEST_MONGODB_HOST=mongodb
Expand Down Expand Up @@ -166,8 +166,8 @@ services:
env_file: ./.env
environment:
- BUNDLE_GEMFILE=/app/Gemfile
- DD_AGENT_HOST=ddagent
- TEST_DATADOG_INTEGRATION=1
- TEST_DDAGENT_HOST=ddagent
- TEST_ELASTICSEARCH_HOST=elasticsearch
- TEST_MEMCACHED_HOST=memcached
- TEST_MONGODB_HOST=mongodb
Expand All @@ -189,7 +189,7 @@ services:
expose:
- "8126"
ports:
- "${TEST_DDAGENT_PORT}:8126"
- "${DD_TRACE_AGENT_PORT}:8126"
elasticsearch:
# Note: ES 5.0 dies with error:
# max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
Expand Down
3 changes: 2 additions & 1 deletion lib/ddtrace/sync_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ class SyncWriter

def initialize(options = {})
@transport = options.fetch(:transport) do
HTTPTransport.new(Writer::HOSTNAME, Writer::PORT)
transport_options = options.fetch(:transport_options, {})
HTTPTransport.new(transport_options)
end
end

Expand Down
9 changes: 6 additions & 3 deletions lib/ddtrace/transport.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class HTTPTransport
attr_accessor :hostname, :port
attr_reader :traces_endpoint, :services_endpoint

DEFAULT_AGENT_HOST = '127.0.0.1'.freeze
DEFAULT_TRACE_AGENT_PORT = '8126'.freeze

# seconds before the transport timeout
TIMEOUT = 1

Expand Down Expand Up @@ -45,11 +48,11 @@ class HTTPTransport

private_constant :API

def initialize(hostname, port, options = {})
def initialize(options = {})
api_version = options.fetch(:api_version, V3)

@hostname = hostname
@port = port
@hostname = options[:hostname] || ENV['DD_AGENT_HOST'] || DEFAULT_AGENT_HOST
@port = options[:port] || ENV['DD_TRACE_AGENT_PORT'] || DEFAULT_TRACE_AGENT_PORT
@api = API.fetch(api_version)
@encoder = options[:encoder] || @api[:encoder]
@response_callback = options[:response_callback]
Expand Down
5 changes: 1 addition & 4 deletions lib/ddtrace/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ module Datadog
class Writer
attr_reader :transport, :worker, :priority_sampler

HOSTNAME = '127.0.0.1'.freeze
PORT = '8126'.freeze

def initialize(options = {})
# writer and transport parameters
@buff_size = options.fetch(:buffer_size, 100)
Expand All @@ -24,7 +21,7 @@ def initialize(options = {})

# transport and buffers
@transport = options.fetch(:transport) do
HTTPTransport.new(HOSTNAME, PORT, transport_options)
HTTPTransport.new(transport_options)
end

@services = {}
Expand Down
1 change: 0 additions & 1 deletion spec/ddtrace/contrib/active_record/multi_db_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ def mysql_connection_string
Datadog.registry[:active_record].reset_configuration!

Datadog.configure do |c|
c.tracer hostname: ENV.fetch('TEST_DDAGENT_HOST', 'localhost')
c.use :active_record, configuration_options
end
end
Expand Down
1 change: 0 additions & 1 deletion spec/ddtrace/contrib/active_record/tracer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
Datadog.configuration[:active_record].reset_options!

Datadog.configure do |c|
c.tracer hostname: ENV.fetch('TEST_DDAGENT_HOST', 'localhost')
c.use :active_record, configuration_options
end
end
Expand Down
1 change: 0 additions & 1 deletion spec/ddtrace/contrib/rack/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

before(:each) do
Datadog.configure do |c|
c.tracer hostname: ENV.fetch('TEST_DDAGENT_HOST', 'localhost')
c.use :rack, configuration_options
end
end
Expand Down
1 change: 0 additions & 1 deletion spec/ddtrace/contrib/rails/railtie_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ def index

before(:each) do
Datadog.configure do |c|
c.tracer hostname: ENV.fetch('TEST_DDAGENT_HOST', 'localhost')
c.use :rails, rails_options if use_rails
end
end
Expand Down
4 changes: 1 addition & 3 deletions spec/ddtrace/contrib/redis/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
RSpec.describe 'Redis integration test' do
# Use real tracer
let(:tracer) do
Datadog::Tracer.new.tap do |tracer|
tracer.configure(hostname: ENV.fetch('TEST_DDAGENT_HOST', 'localhost'))
end
Datadog::Tracer.new
end

before(:each) do
Expand Down
1 change: 0 additions & 1 deletion spec/ddtrace/contrib/redis/method_replaced_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
skip unless ENV['TEST_DATADOG_INTEGRATION']

Datadog.configure do |c|
c.tracer hostname: ENV.fetch('TEST_DDAGENT_HOST', 'localhost')
c.use :redis
end
end
Expand Down
6 changes: 1 addition & 5 deletions spec/ddtrace/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
let(:tracer) do
Datadog::Tracer.new.tap do |t|
t.configure(
enabled: true,
hostname: ENV.fetch('TEST_DDAGENT_HOST', 'localhost'),
port: ENV.fetch('TEST_DDAGENT_PORT', 8126)
enabled: true
)
end
end
Expand Down Expand Up @@ -189,8 +187,6 @@ def agent_receives_span_step3(previous_success)
before(:each) do
tracer.configure(
enabled: true,
hostname: ENV.fetch('TEST_DDAGENT_HOST', 'localhost'),
port: ENV.fetch('TEST_DDAGENT_PORT', 8126),
priority_sampling: true
)
end
Expand Down
76 changes: 68 additions & 8 deletions spec/ddtrace/transport_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@
require 'ddtrace'

RSpec.describe Datadog::HTTPTransport do
let(:transport) do
described_class.new(
ENV.fetch('TEST_DDAGENT_HOST', 'localhost'),
ENV.fetch('TEST_DDAGENT_PORT', 8126),
options
)
end
subject(:transport) { described_class.new(options) }
let(:options) { {} }

before(:each) do
Expand All @@ -21,6 +15,72 @@
Datadog::Tracer.log.level = @original_level
end

describe '#initialize' do
context 'given :hostname and :port' do
let(:options) { { hostname: hostname, port: port } }
let(:hostname) { double('hostname') }
let(:port) { double('port') }

it { is_expected.to have_attributes(hostname: hostname, port: port) }
end

context 'given no options' do
before(:each) do
# Swap environment variables for test values
@original_hostname = ENV['DD_AGENT_HOST']
@original_port = ENV['DD_TRACE_AGENT_PORT']
ENV['DD_AGENT_HOST'] = hostname
ENV['DD_TRACE_AGENT_PORT'] = port
end

after(:each) do
# Restore environment variables
ENV['DD_AGENT_HOST'] = @original_hostname
ENV['DD_TRACE_AGENT_PORT'] = @original_port
end

context 'but environment variables are present' do
let(:hostname) { 'transport_test_host' }
let(:port) { '1234' }

it do
is_expected.to have_attributes(
hostname: hostname,
port: port
)
end
end

context 'or environment variables' do
let(:hostname) { nil }
let(:port) { nil }

it do
is_expected.to have_attributes(
hostname: described_class::DEFAULT_AGENT_HOST,
port: described_class::DEFAULT_TRACE_AGENT_PORT
)
end
end
end

context 'given neither options nor environment variables' do
before(:each) do
# Swap environment variables for test values
@original_hostname = ENV['DD_AGENT_HOST']
@original_port = ENV['DD_TRACE_AGENT_PORT']
ENV['DD_AGENT_HOST'] = hostname
ENV['DD_TRACE_AGENT_PORT'] = port
end

after(:each) do
# Restore environment variables
ENV['DD_AGENT_HOST'] = @original_hostname
ENV['DD_TRACE_AGENT_PORT'] = @original_port
end
end
end

describe '#handle_response' do
subject(:result) { transport.handle_response(response) }

Expand Down Expand Up @@ -66,7 +126,7 @@
end

context 'and a bad transport' do
let(:transport) { described_class.new('localhost', '8888') }
let(:transport) { described_class.new(hostname: 'localhost', port: '8888') }
it { expect(transport.server_error?(code)).to be true }
end

Expand Down
2 changes: 1 addition & 1 deletion spec/ddtrace/workers_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
end
end

let(:transport) { SpyTransport.new(hostname, port) }
let(:transport) { SpyTransport.new }

def wait_for_flush(num = 1, period = 0.1)
(20 * flush_interval).to_i.times do
Expand Down
14 changes: 6 additions & 8 deletions spec/ddtrace/writer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
context 'and default transport options' do
it do
sampling_method = described_class.new.method(:sampling_updater)
expect(Datadog::HTTPTransport).to receive(:new) do |hostname, port, options|
expect(hostname).to eq(described_class::HOSTNAME)
expect(port).to eq(described_class::PORT)
expect(Datadog::HTTPTransport).to receive(:new) do |options|
expect(options).to be_a_kind_of(Hash)
expect(options[:api_version]).to eq(Datadog::HTTPTransport::V4)
expect(options[:response_callback].source_location).to eq(sampling_method.source_location)
Expand All @@ -43,9 +41,7 @@
let(:response_callback) { double('response callback') }

it do
expect(Datadog::HTTPTransport).to receive(:new) do |hostname, port, options|
expect(hostname).to eq(described_class::HOSTNAME)
expect(port).to eq(described_class::PORT)
expect(Datadog::HTTPTransport).to receive(:new) do |options|
expect(options).to include(
api_version: api_version,
response_callback: response_callback
Expand All @@ -70,7 +66,9 @@
let(:callback) { double('callback method') }

let!(:request) { stub_request(:post, endpoint).to_return(response) }
let(:endpoint) { "#{Datadog::Writer::HOSTNAME}:#{Datadog::Writer::PORT}/#{api_version}/traces" }
let(:hostname) { ENV.fetch('DD_AGENT_HOST', Datadog::HTTPTransport::DEFAULT_AGENT_HOST) }
let(:port) { ENV.fetch('DD_TRACE_AGENT_PORT', Datadog::HTTPTransport::DEFAULT_TRACE_AGENT_PORT) }
let(:endpoint) { "#{hostname}:#{port}/#{api_version}/traces" }
let(:response) { { body: body } }
let(:body) { 'body' }

Expand All @@ -96,7 +94,7 @@
let(:response) { super().merge!(status: 404) }
let!(:fallback_request) { stub_request(:post, fallback_endpoint).to_return(body: body) }
let(:fallback_endpoint) do
"#{Datadog::Writer::HOSTNAME}:#{Datadog::Writer::PORT}/#{fallback_version}/traces"
"#{hostname}:#{port}/#{fallback_version}/traces"
end
let(:body) { 'body' }

Expand Down
2 changes: 1 addition & 1 deletion spec/support/faux_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# FauxWriter is a dummy writer that buffers spans locally.
class FauxWriter < Datadog::Writer
def initialize(options = {})
options[:transport] ||= FauxTransport.new(HOSTNAME, PORT)
options[:transport] ||= FauxTransport.new
super
@mutex = Mutex.new

Expand Down
4 changes: 2 additions & 2 deletions spec/support/spy_transport.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
class SpyTransport < Datadog::HTTPTransport
attr_reader :helper_sent

def initialize(hostname, port)
super(hostname, port)
def initialize(options = {})
super
@helper_sent = { 200 => {}, 500 => {} }
@helper_mutex = Mutex.new
@helper_error_mode = false
Expand Down
1 change: 0 additions & 1 deletion test/contrib/rack/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ def setup
@tracer = get_test_tracer

Datadog.configure do |c|
c.tracer hostname: ENV.fetch('TEST_DDAGENT_HOST', 'localhost')
c.use :http
c.use :rack, tracer: @tracer
end
Expand Down
1 change: 0 additions & 1 deletion test/contrib/rails/apps/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ def config.database_configuration
def test_config
# Enables the auto-instrumentation for the testing application
Datadog.configure do |c|
c.tracer hostname: ENV.fetch('TEST_DDAGENT_HOST', 'localhost')
c.use :rails
c.use :redis
end
Expand Down
1 change: 0 additions & 1 deletion test/contrib/rails/apps/rails3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class Rails3 < Rails::Application

# Enables the auto-instrumentation for the testing application
Datadog.configure do |c|
c.tracer hostname: ENV.fetch('TEST_DDAGENT_HOST', 'localhost')
c.use :rails
c.use :redis
end
Expand Down
Loading