diff --git a/docs/GettingStarted.md b/docs/GettingStarted.md index 657d1f056ba..fcf614aa2ea 100644 --- a/docs/GettingStarted.md +++ b/docs/GettingStarted.md @@ -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: @@ -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 diff --git a/lib/ddtrace/contrib/rails/framework.rb b/lib/ddtrace/contrib/rails/framework.rb index 1e510d577e3..099de932bd6 100644 --- a/lib/ddtrace/contrib/rails/framework.rb +++ b/lib/ddtrace/contrib/rails/framework.rb @@ -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 @@ -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], @@ -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 diff --git a/test/contrib/rails/tracer_test.rb b/test/contrib/rails/tracer_test.rb index 6fc83f02eee..fafd323bea5 100644 --- a/test/contrib/rails/tracer_test.rb +++ b/test/contrib/rails/tracer_test.rb @@ -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 @@ -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