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

Only store #tracer on Pin when configured #1041

Merged
merged 1 commit into from
May 26, 2020
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
33 changes: 17 additions & 16 deletions lib/ddtrace/pin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,34 @@ def self.get_from(obj)
obj.datadog_pin
end

attr_reader :service_name
attr_accessor :app
attr_accessor :tags
attr_accessor :app_type
attr_accessor :name
attr_accessor :tracer
attr_accessor :config
attr_accessor :name
attr_accessor :service_name
attr_accessor :tags
attr_writer :tracer
attr_accessor :writer

alias service= service_name=
alias service service_name

def initialize(service_name, options = {})
@app = options[:app]
@tags = options[:tags]
@app_type = options[:app_type]
@name = nil # this would rarely be overriden as it's really span-specific
@tracer = options[:tracer] || Datadog.tracer
@config = options[:config]
self.service_name = service_name
@name = nil # this would rarely be overriden as it's really span-specific
@service_name = service_name
@tags = options[:tags]
@tracer = options[:tracer]
end

def tracer
@tracer || Datadog.tracer
end

def enabled?
return @tracer.enabled if @tracer
return tracer.enabled if tracer
false
end

Expand All @@ -56,13 +64,6 @@ def datadog_pin
obj.datadog_pin = self
end

def service_name=(name)
@service_name = name
end

alias service= service_name=
alias service service_name

def to_s
"Pin(service:#{service},app:#{app},app_type:#{app_type},name:#{name})"
end
Expand Down
53 changes: 42 additions & 11 deletions spec/ddtrace/pin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,56 @@
before(:each) { pin }

context 'when given some options' do
let(:options) { { app: 'anapp' } }
let(:options) do
{
app: double('app'),
app_type: double('app_type'),
config: double('config'),
name: double('name'),
tags: double('tags'),
tracer: double('tracer'),
writer: double('writer')
}
end

it do
expect(pin.service).to eq(service_name)
expect(pin.app).to eq(options[:app])
is_expected.to have_attributes(
app: options[:app],
app_type: options[:app_type],
config: options[:config],
name: nil,
service_name: service_name,
tags: options[:tags],
tracer: options[:tracer],
writer: nil
)
end
end
end

describe '#tracer' do
subject(:tracer) { pin.tracer }

context 'when given sufficient info' do
let(:options) { { app: 'test-app', app_type: 'test-type', tracer: tracer } }
let(:tracer) { get_test_tracer }
context 'when a tracer has been provided' do
let(:options) { super().merge(tracer: tracer_option) }
let(:tracer_option) { get_test_tracer }
it { is_expected.to be tracer_option }
end

context 'when given insufficient info' do
let(:options) { { app_type: 'test-type', tracer: tracer } }
let(:tracer) { get_test_tracer }
context 'when no tracer has been provided' do
it { is_expected.to be Datadog.tracer }

it 'does not sets the service info' do
expect(tracer.services).to be_empty
context 'and the default tracer mutates' do
let(:new_tracer) { get_test_tracer }

it 'gets the current tracer' do
old_tracer = Datadog.tracer

expect { allow(Datadog).to receive(:tracer).and_return(new_tracer) }
.to change { pin.tracer }
.from(old_tracer)
.to(new_tracer)
end
end
end
end
Expand Down