diff --git a/lib/datadog/tracing/tracer.rb b/lib/datadog/tracing/tracer.rb index d31418f0406..a6da0e98199 100644 --- a/lib/datadog/tracing/tracer.rb +++ b/lib/datadog/tracing/tracer.rb @@ -442,7 +442,7 @@ def resolve_tags(tags, service) tags || @tags.dup end # Remove version tag if service is not the default service - if merged_tags.key?(Core::Environment::Ext::TAG_VERSION) && service != @default_service + if merged_tags.key?(Core::Environment::Ext::TAG_VERSION) && service && service != @default_service merged_tags.delete(Core::Environment::Ext::TAG_VERSION) end merged_tags diff --git a/spec/datadog/tracing/tracer_spec.rb b/spec/datadog/tracing/tracer_spec.rb index ed0b8de2b99..945b3884e25 100644 --- a/spec/datadog/tracing/tracer_spec.rb +++ b/spec/datadog/tracing/tracer_spec.rb @@ -126,15 +126,45 @@ expect(span.get_tag('my')).to eq('tag') end - context 'contains version and span.service is not equal to the default tracer service' do - let(:tracer_options) { { default_service: 'global-service', tags: { version: '1.1.0' } } } - let(:service) { 'my-service' } - it 'does not set version on the span' do - expect(tracer.default_service).to eq('global-service') - expect(span.service).not_to eq('my-service') - - expect(tracer.tags).to include(version: '1.1.0') - expect(span.tags).not_to include(:version) + context 'contains version and the span service name' do + let(:tracer_options) do + { default_service: 'global-service', tags: { Datadog::Core::Environment::Ext::TAG_VERSION => '1.1.0' } } + end + let(:options) { { service: service } } + + context 'is nil' do + let(:service) { nil } + + it 'sets version' do + expect(tracer.default_service).to eq('global-service') + expect(span.service).to eq('global-service') + + expect(tracer.tags).to include('version' => '1.1.0') + expect(span.tags).to include('version' => '1.1.0') + end + end + + context 'is equal to the default tracer service' do + let(:service) { 'global-service' } + + it 'sets version' do + expect(tracer.default_service).to eq('global-service') + expect(span.service).to eq('global-service') + + expect(tracer.tags).to include('version' => '1.1.0') + expect(span.tags).to include('version' => '1.1.0') + end + end + + context 'is not equal to the default tracer service' do + let(:service) { 'local-service' } + it 'does not set version' do + expect(tracer.default_service).to eq('global-service') + expect(span.service).to eq('local-service') + + expect(tracer.tags).to include('version' => '1.1.0') + expect(span.tags).not_to include('version' => '1.1.0') + end end end