-
Notifications
You must be signed in to change notification settings - Fork 375
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
Add DD_TAGS environment variable #980
Changes from all commits
0402b90
635187e
cc426ae
a22c211
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,7 @@ | |
require 'logger' | ||
require 'pathname' | ||
|
||
require 'ddtrace/ext/environment' | ||
|
||
require 'ddtrace/environment' | ||
require 'ddtrace/span' | ||
require 'ddtrace/context' | ||
require 'ddtrace/logger' | ||
|
@@ -85,9 +84,7 @@ def initialize(options = {}) | |
end | ||
|
||
@mutex = Mutex.new | ||
@tags = {}.tap do |tags| | ||
tags[:env] = ENV[Ext::Environment::ENV_ENVIRONMENT] if ENV.key?(Ext::Environment::ENV_ENVIRONMENT) | ||
end | ||
@tags = Datadog::Environment.tags | ||
|
||
# Enable priority sampling by default | ||
activate_priority_sampling!(@sampler) | ||
|
@@ -158,7 +155,8 @@ def default_service | |
# | ||
# tracer.set_tags('env' => 'prod', 'component' => 'core') | ||
def set_tags(tags) | ||
@tags.update(tags) | ||
string_tags = Hash[tags.collect { |k, v| [k.to_s, v] }] | ||
@tags.update(string_tags) | ||
end | ||
|
||
# Guess context and parent from child_of entry. | ||
|
@@ -210,8 +208,8 @@ def start_span(name, options = {}) | |
# child span | ||
span.parent = parent # sets service, trace_id, parent_id, sampled | ||
end | ||
tags.each { |k, v| span.set_tag(k, v) } unless tags.empty? | ||
@tags.each { |k, v| span.set_tag(k, v) } unless @tags.empty? | ||
tags.each { |k, v| span.set_tag(k, v) } unless tags.empty? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fixes the other bug where default tags were overriding tags manually set upon the span. |
||
span.start_time = start_time | ||
|
||
# this could at some point be optional (start_active_span vs start_manual_span) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,8 +77,8 @@ | |
expect(Datadog::Logger.log).to eq(custom_log) | ||
expect(tracer.writer.transport.current_api.adapter.hostname).to eq('tracer.host.com') | ||
expect(tracer.writer.transport.current_api.adapter.port).to eq(1234) | ||
expect(tracer.tags[:env]).to eq(:config_test) | ||
expect(tracer.tags[:foo]).to eq(:bar) | ||
expect(tracer.tags['env']).to eq(:config_test) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think changing this behavior is okay? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm... I guess it depends if people are accessing tags directly via symbols? otherwise, this shouldn't break anything on encoding or sending to the agent |
||
expect(tracer.tags['foo']).to eq(:bar) | ||
end | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
require 'spec_helper' | ||
|
||
require 'ddtrace' | ||
require 'ddtrace/environment' | ||
|
||
RSpec.describe Datadog::Environment do | ||
describe '::env' do | ||
subject(:env) { described_class.env } | ||
context "when #{Datadog::Ext::Environment::ENV_ENVIRONMENT}" do | ||
around do |example| | ||
ClimateControl.modify(Datadog::Ext::Environment::ENV_ENVIRONMENT => environment) do | ||
example.run | ||
end | ||
end | ||
|
||
context 'is not defined' do | ||
let(:environment) { nil } | ||
it { is_expected.to be nil } | ||
end | ||
|
||
context 'is defined' do | ||
let(:environment) { 'env-value' } | ||
it { is_expected.to eq(environment) } | ||
end | ||
end | ||
end | ||
|
||
describe '::tags' do | ||
subject(:tags) { described_class.tags } | ||
|
||
context "when #{Datadog::Ext::Environment::ENV_TAGS}" do | ||
around do |example| | ||
ClimateControl.modify(Datadog::Ext::Environment::ENV_TAGS => env_tags) do | ||
example.run | ||
end | ||
end | ||
|
||
context 'is not defined' do | ||
let(:env_tags) { nil } | ||
it { is_expected.to eq({}) } | ||
end | ||
|
||
context 'is defined' do | ||
let(:env_tags) { 'a:1,b:2' } | ||
|
||
it { is_expected.to include('a' => '1', 'b' => '2') } | ||
|
||
context 'with an invalid tag' do | ||
context do | ||
let(:env_tags) { '' } | ||
it { is_expected.to eq({}) } | ||
end | ||
|
||
context do | ||
let(:env_tags) { 'a' } | ||
it { is_expected.to eq({}) } | ||
end | ||
|
||
context do | ||
let(:env_tags) { ':' } | ||
it { is_expected.to eq({}) } | ||
end | ||
|
||
context do | ||
let(:env_tags) { ',' } | ||
it { is_expected.to eq({}) } | ||
end | ||
|
||
context do | ||
let(:env_tags) { 'a:' } | ||
it { is_expected.to eq({}) } | ||
end | ||
end | ||
|
||
context 'and when ::env' do | ||
context 'is set' do | ||
before { allow(described_class).to receive(:env).and_return(nil) } | ||
it { is_expected.to_not include('env') } | ||
end | ||
|
||
context 'is not set' do | ||
let(:env_value) { 'env-value' } | ||
before { allow(described_class).to receive(:env).and_return(env_value) } | ||
it { is_expected.to include('env' => env_value) } | ||
end | ||
end | ||
end | ||
|
||
context 'conflicts with ::env' do | ||
let(:env_tags) { "env:#{tag_env_value}" } | ||
let(:tag_env_value) { 'tag-env-value' } | ||
let(:env_value) { 'env-value' } | ||
|
||
before { allow(described_class).to receive(:env).and_return(env_value) } | ||
|
||
it { is_expected.to include('env' => env_value) } | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fixes one of the bugs where Symbols and Strings weren't being quantized to the same key, causing duplicates.