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

Flexible header matching for HTTP propagator #2504

Merged
merged 1 commit into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 10 additions & 3 deletions lib/datadog/tracing/contrib/http/distributed/fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@ module Tracing
module Contrib
module HTTP
module Distributed
# Retrieves Rack formatted headers from HTTP headers.
# Retrieves HTTP headers from carrier.
# Headers will also match if Rack-formatted:
# 'my-header' will match 'my-header' and 'HTTP_MY_HEADER'.
#
# In case both variants are present, the verbatim match will be used.
class Fetcher < Tracing::Distributed::Fetcher
# TODO: Don't assume Rack format.
# Make distributed tracing headers apathetic.
# DEV: Should we try to parse both verbatim an Rack-formatted headers,
# DEV: given Rack-formatted is the most common format in Ruby?
def [](name)
# Try to fetch with the plain key
value = super(name)
return value if value && !value.empty?

# If not found, try the Rack-formatted key
rack_header = "HTTP-#{name}"
rack_header.upcase!
rack_header.tr!('-'.freeze, '_'.freeze)
Expand Down
23 changes: 19 additions & 4 deletions spec/datadog/tracing/contrib/http/distributed/fetcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,29 @@
context 'that is not empty' do
let(:env) { { 'HTTP_MY_KEY' => 'value' } }
it { is_expected.to eq('value') }

context 'and a plain header' do
let(:env) { super().merge('my-key' => 'plain-match') }

it 'prefers the plain header match' do
is_expected.to eq('plain-match')
end
end
end
end

context 'with a header not Rack formatted' do
let(:key) { 'my-key' }
let(:env) { { key => 'value' } }
context 'with a plain header associated' do
let(:key) { 'rack.session' }

it { is_expected.to be_nil }
context 'that is empty' do
let(:env) { { key => '' } }
it { is_expected.to be_nil }
end

context 'that is not empty' do
let(:env) { { key => 'value' } }
it { is_expected.to eq('value') }
end
end
end
end