Module: Datadog::Tracing::Contrib::Rack::HeaderTagging

Defined in:
lib/datadog/tracing/contrib/rack/header_tagging.rb

Overview

Matches Rack-style headers with a matcher and sets matching headers into a span.

Class Method Summary collapse

Class Method Details

.tag_request_headers(span, env, configuration) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/datadog/tracing/contrib/rack/header_tagging.rb', line 9

def self.tag_request_headers(span, env, configuration)
  # Wrap env in a case-insensitive Rack-style accessor.
  headers = env.is_a?(Header::RequestHeaderCollection) ? env : Header::RequestHeaderCollection.new(env)

  # Use global DD_TRACE_HEADER_TAGS if integration-level configuration is not provided
  tags = if configuration.using_default?(:headers) && !Datadog.configuration.tracing.using_default?(:header_tags)
           Datadog.configuration.tracing.header_tags.request_tags(headers)
         else
           whitelist = configuration[:headers][:request] || []
           whitelist.each_with_object({}) do |header, result|
             header_value = headers.get(header)
             unless header_value.nil?
               header_tag = Tracing::Metadata::Ext::HTTP::RequestHeaders.to_tag(header)
               result[header_tag] = header_value
             end
           end
         end

  span.set_tags(tags)
end

.tag_response_headers(span, headers, configuration) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/datadog/tracing/contrib/rack/header_tagging.rb', line 30

def self.tag_response_headers(span, headers, configuration)
  headers = Core::Utils::Hash::CaseInsensitiveWrapper.new(headers) # Make header access case-insensitive

  # Use global DD_TRACE_HEADER_TAGS if integration-level configuration is not provided
  tags = if configuration.using_default?(:headers) && !Datadog.configuration.tracing.using_default?(:header_tags)
           Datadog.configuration.tracing.header_tags.response_tags(headers)
         else
           whitelist = configuration[:headers][:response] || []
           whitelist.each_with_object({}) do |header, result|
             header_value = headers[header]

             next if header_value.nil?

             header_tag = Tracing::Metadata::Ext::HTTP::ResponseHeaders.to_tag(header)

             # Maintain the value format between Rack 2 and 3
             #
             # Rack 2.x => { 'foo' => 'bar,baz' }
             # Rack 3.x => { 'foo' => ['bar', 'baz'] }
             result[header_tag] = if header_value.is_a? Array
                                    header_value.join(',')
                                  else
                                    header_value
                                  end
           end
         end

  span.set_tags(tags)
end