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

[rails] provide tags setting to include global tags from Rails settings #101

Merged
merged 3 commits into from
Mar 30, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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: 3 additions & 1 deletion docs/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ of the Datadog tracer, you can override the following defaults:
debug: false,
trace_agent_hostname: 'localhost',
trace_agent_port: 8126,
env: Rails.env
env: Rails.env,
tags: {}
Copy link
Contributor Author

@palazzem palazzem Mar 30, 2017

Choose a reason for hiding this comment

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

@ufoot I don't like having both, it seems we provide two different paths to do the same thing.

Copy link
Contributor

Choose a reason for hiding this comment

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

As I said in #100 this is fine with me, providing what is the final value (if both are defined and different) needs be explicit in the docs.

}

Available settings are:
Expand All @@ -88,6 +89,7 @@ Available settings are:
* ``trace_agent_hostname``: set the hostname of the trace agent.
* ``trace_agent_port``: set the port the trace agent is listening on.
* ``env``: set the environment. Defaults to the Rails environment
* ``tags``: set global tags that should be applied to all spans. Defaults to an empty hash

### Sinatra

Expand Down
9 changes: 6 additions & 3 deletions lib/ddtrace/contrib/rails/framework.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ module Framework
debug: false,
trace_agent_hostname: Datadog::Writer::HOSTNAME,
trace_agent_port: Datadog::Writer::PORT,
env: ::Rails.env
env: ::Rails.env,
tags: {}
}.freeze

# configure Datadog settings
Expand All @@ -47,6 +48,10 @@ def self.configure(config)
port: datadog_config[:trace_agent_port]
)

# set default tracer tags
datadog_config[:tracer].set_tags(datadog_config[:tags])
Copy link
Contributor

Choose a reason for hiding this comment

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

Wondering about the order here, as I understand it, what is in env: would be overridden by the contents of tags:? I, intuitively, would think it the other way around, but won't enforce it, just curious to know why you chose that order.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that was a temporary order before choosing if env should be removed or not. In that case we can enforce the env tag so that it takes precedence among global tags.

datadog_config[:tracer].set_tags('env' => datadog_config[:env])
Copy link
Contributor

Choose a reason for hiding this comment

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

In think we're almost there, but what happens here if :env is not defined it the conf at all? Could this happen and override a legit :tags value?


# set default service details
datadog_config[:tracer].set_service_info(
datadog_config[:default_service],
Expand All @@ -60,8 +65,6 @@ def self.configure(config)
Datadog::Ext::AppTypes::CACHE
)

datadog_config[:tracer].set_tags('env' => datadog_config[:env])

if defined?(::ActiveRecord)
begin
# set default database service details and store it in the configuration
Expand Down
23 changes: 23 additions & 0 deletions test/contrib/rails/tracer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class TracerTest < ActionController::TestCase
assert_equal(Rails.configuration.datadog_trace[:trace_agent_hostname], Datadog::Writer::HOSTNAME)
assert_equal(Rails.configuration.datadog_trace[:trace_agent_port], Datadog::Writer::PORT)
assert_equal(Rails.configuration.datadog_trace[:env], 'test')
assert_equal(Rails.configuration.datadog_trace[:tags], {})
end

test 'a default service and database should be properly set' do
Expand Down Expand Up @@ -122,4 +123,26 @@ class TracerTest < ActionController::TestCase

assert_equal(tracer.tags['env'], 'dev')
end

test 'tracer global tags can be changed by the user' do
update_config(:tags, 'component' => 'api', 'section' => 'users')

tracer = Rails.configuration.datadog_trace[:tracer]

assert_equal(tracer.tags['component'], 'api')
assert_equal(tracer.tags['section'], 'users')
end

test 'tracer env setting has precedence over tags setting' do
# default case
update_config(:tags, 'env' => 'foo')
tracer = Rails.configuration.datadog_trace[:tracer]
assert_equal(tracer.tags['env'], 'test')

# explicit set
update_config(:env, 'dev')
update_config(:tags, 'env' => 'bar')
tracer = Rails.configuration.datadog_trace[:tracer]
assert_equal(tracer.tags['env'], 'dev')
end
end