Skip to content

Commit

Permalink
[rails] provide tags setting to include global tags from Rails settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Emanuele Palazzetti committed Mar 30, 2017
1 parent 5594367 commit 8c37afc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
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: {}
}

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('env' => datadog_config[:env])
datadog_config[:tracer].set_tags(datadog_config[:tags])

# 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
10 changes: 10 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,13 @@ 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
end

0 comments on commit 8c37afc

Please sign in to comment.