Dd-trace-rb: Support for custom attributes on traces

Created on 9 Apr 2018  路  3Comments  路  Source: DataDog/dd-trace-rb

NewRelic tracer has the ability to send misc attributes along with the trace, particularly for knowing which user triggered the trace.

::NewRelic::Agent.add_custom_attributes({
  user_id: current_user.try(:id),
  user_name: current_user.try(:name),
})

Would be handy to be able to do something similar with Datadog so we can contact users affected in certain cases.

Most helpful comment

Thus far we do allow for tagging all traces with particular attributes:

Datadog.configure do |c|
  c.tracer tags: { 'env' => 'prod' }
end

Alternativately, in your own code, you can access the current span and add tags as you see fit. e.g. in a Rails controller:

before_action :set_tags

def index
  # Some code...
end

private

def set_tags
  tracer = Datadog.configuration[:rails][:tracer]
  span = tracer.active_span
  span.set_tag('user_id' current_user.try(:id))
  span.set_tag('user_name' current_user.try(:name))
end

All 3 comments

Thus far we do allow for tagging all traces with particular attributes:

Datadog.configure do |c|
  c.tracer tags: { 'env' => 'prod' }
end

Alternativately, in your own code, you can access the current span and add tags as you see fit. e.g. in a Rails controller:

before_action :set_tags

def index
  # Some code...
end

private

def set_tags
  tracer = Datadog.configuration[:rails][:tracer]
  span = tracer.active_span
  span.set_tag('user_id' current_user.try(:id))
  span.set_tag('user_name' current_user.try(:name))
end

@delner Perfect. Thank you. Would be good to have this documented on the Datadog Rails installation page for future reference for others.

No problem. We did just add a reference to active_span to our documentation in 0.12-dev under "Manual Instrumentation" (although it isn't tagged as "custom attributes".)

Going to close this one since it seems like that'll work for you.

Was this page helpful?
0 / 5 - 0 ratings