Skip to content

Commit

Permalink
Flexible header matching for HTTP propagator
Browse files Browse the repository at this point in the history
  • Loading branch information
marcotc committed Dec 22, 2022
1 parent 89d7ebf commit c1470eb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
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

0 comments on commit c1470eb

Please sign in to comment.