Skip to content

Commit

Permalink
Add http.useragent tag
Browse files Browse the repository at this point in the history
While User-Agent is a header and can be included in the
http.request_headers tag, it is not by default, and even if it were it
oculd be removed from the list by the user.

This unconditionally obtains the request header value and stores it in a
dedicated tag.
  • Loading branch information
lloeki committed Sep 1, 2022
1 parent 7fa72c2 commit 8af4ff0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/datadog/tracing/contrib/rack/middlewares.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ def set_request_tags!(trace, request_span, env, status, headers, response, origi
request_headers = parse_request_headers(env)
response_headers = parse_response_headers(headers || {})

# request_headers is subject to filtering and configuration so we
# get the user agent separately
user_agent = parse_user_agent_header(env)

# The priority
# 1. User overrides span.resource
# 2. Configuration
Expand Down Expand Up @@ -194,6 +198,10 @@ def set_request_tags!(trace, request_span, env, status, headers, response, origi
request_span.set_tag(Tracing::Metadata::Ext::HTTP::TAG_STATUS_CODE, status)
end

if request_span.get_tag(Tracing::Metadata::Ext::HTTP::TAG_USER_AGENT).nil? && user_agent
request_span.set_tag(Tracing::Metadata::Ext::HTTP::TAG_USER_AGENT, user_agent)
end

# Request headers
request_headers.each do |name, value|
request_span.set_tag(name, value) if request_span.get_tag(name).nil?
Expand All @@ -219,6 +227,12 @@ def configuration
Datadog.configuration.tracing[:rack]
end

def parse_user_agent_header(env)
user_agent_header = header_to_rack_header(Tracing::Metadata::Ext::HTTP::HEADER_USER_AGENT)

env[user_agent_header] if env.key?(user_agent_header)
end

def parse_request_headers(env)
{}.tap do |result|
whitelist = configuration[:headers][:request] || []
Expand Down
2 changes: 2 additions & 0 deletions lib/datadog/tracing/metadata/ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ module HTTP
TAG_BASE_URL = 'http.base_url'
TAG_METHOD = 'http.method'
TAG_STATUS_CODE = 'http.status_code'
TAG_USER_AGENT = 'http.useragent'
TAG_URL = 'http.url'
TYPE_INBOUND = AppTypes::TYPE_WEB.freeze
TYPE_OUTBOUND = 'http'
TYPE_PROXY = 'proxy'
TYPE_TEMPLATE = 'template'
HEADER_USER_AGENT = 'User-Agent'

# General header functionality
module Headers
Expand Down
42 changes: 42 additions & 0 deletions spec/datadog/tracing/contrib/rack/integration_test_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,48 @@
end

describe 'GET request' do
context 'that does not sent user agent' do
subject(:response) { get '/headers/', {}, headers }

let(:headers) do
{}
end

before do
is_expected.to be_ok
expect(spans).to have(1).items
end

it_behaves_like 'a rack GET 200 span'

it do
expect(span.get_tag('http.useragent')).to be nil
expect(span.get_tag('http.request.headers.user-agent')).to be nil
end
end

context 'that sends user agent' do
subject(:response) { get '/headers/', {}, headers }

let(:headers) do
{
'HTTP_USER_AGENT' => 'SuperUserAgent',
}
end

before do
is_expected.to be_ok
expect(spans).to have(1).items
end

it_behaves_like 'a rack GET 200 span'

it do
expect(span.get_tag('http.useragent')).to eq('SuperUserAgent')
expect(span.get_tag('http.request.headers.user-agent')).to be nil
end
end

context 'that sends headers' do
subject(:response) { get '/headers/', {}, headers }

Expand Down

0 comments on commit 8af4ff0

Please sign in to comment.