Class: Datadog::Tracing::Configuration::HTTP::HeaderTags

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/tracing/configuration/http.rb

Overview

Datadog tracing supports capturing HTTP request and response headers as span tags.

The provided configuration String for this feature has to be pre-processed to allow for ease of utilization by each HTTP integration.

This class process configuration, stores the result, and provides methods to utilize this configuration.

Instance Method Summary collapse

Constructor Details

#initialize(header_tags) ⇒ HeaderTags

Returns a new instance of HeaderTags.

Parameters:

  • header_tags (Array<String>)

    The list of strings from DD_TRACE_HEADER_TAGS.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/datadog/tracing/configuration/http.rb', line 16

def initialize(header_tags)
  @request_headers = {}
  @response_headers = {}
  @header_tags = header_tags

  @header_tags.each do |header_tag|
    header, tag = header_tag.split(':', 2)

    next unless header # Empty string guard

    if tag && !tag.empty?
      # When a custom tag name is provided, use that name for both
      # request and response tags.
      normalized_tag = Tracing::::Ext::HTTP::Headers.to_tag(tag, allow_nested: true)
      request = response = normalized_tag
    else
      # Otherwise, use our internal pattern of
      # "http.{request|response}.headers.{header}" as tag name.
      request = Tracing::::Ext::HTTP::RequestHeaders.to_tag(header)
      response = Tracing::::Ext::HTTP::ResponseHeaders.to_tag(header)
    end

    @request_headers[header] = request
    @response_headers[header] = response
  end
end

Instance Method Details

#request_tags(headers) ⇒ Object

Receives a case insensitive hash with the request headers and returns a list of tag names and values that can be set in a span.



45
46
47
48
49
50
51
52
# File 'lib/datadog/tracing/configuration/http.rb', line 45

def request_tags(headers)
  @request_headers.map do |header_name, span_tag|
    # Case-insensitive search
    header_value = headers[header_name]

    [span_tag, header_value] if header_value
  end.compact
end

#response_tags(headers) ⇒ Object

Receives a case insensitive hash with the response headers and returns a list of tag names and values that can be set in a span.



56
57
58
59
60
61
62
63
# File 'lib/datadog/tracing/configuration/http.rb', line 56

def response_tags(headers)
  @response_headers.map do |header_name, span_tag|
    # Case-insensitive search
    header_value = headers[header_name]

    [span_tag, header_value] if header_value
  end.compact
end

#to_sObject

For easy configuration inspection, print the original configuration setting.



67
68
69
# File 'lib/datadog/tracing/configuration/http.rb', line 67

def to_s
  @header_tags.join(',').to_s
end