Class: Datadog::DistributedTracing::Headers::Headers

Inherits:
Object
  • Object
show all
Includes:
Ext::DistributedTracing
Defined in:
lib/ddtrace/distributed_tracing/headers/headers.rb

Overview

Headers provides easy access and validation methods for Rack headers

Constant Summary

Constants included from Ext::DistributedTracing

Ext::DistributedTracing::B3_HEADER_SAMPLED, Ext::DistributedTracing::B3_HEADER_SINGLE, Ext::DistributedTracing::B3_HEADER_SPAN_ID, Ext::DistributedTracing::B3_HEADER_TRACE_ID, Ext::DistributedTracing::GRPC_METADATA_ORIGIN, Ext::DistributedTracing::GRPC_METADATA_PARENT_ID, Ext::DistributedTracing::GRPC_METADATA_SAMPLING_PRIORITY, Ext::DistributedTracing::GRPC_METADATA_TRACE_ID, Ext::DistributedTracing::HTTP_HEADER_ORIGIN, Ext::DistributedTracing::HTTP_HEADER_PARENT_ID, Ext::DistributedTracing::HTTP_HEADER_SAMPLING_PRIORITY, Ext::DistributedTracing::HTTP_HEADER_TRACE_ID, Ext::DistributedTracing::ORIGIN_KEY, Ext::DistributedTracing::PROPAGATION_EXTRACT_STYLE_ENV_OLD, Ext::DistributedTracing::PROPAGATION_INJECT_STYLE_ENV_OLD, Ext::DistributedTracing::PROPAGATION_STYLE_B3, Ext::DistributedTracing::PROPAGATION_STYLE_B3_SINGLE_HEADER, Ext::DistributedTracing::PROPAGATION_STYLE_DATADOG, Ext::DistributedTracing::PROPAGATION_STYLE_EXTRACT_ENV, Ext::DistributedTracing::PROPAGATION_STYLE_INJECT_ENV, Ext::DistributedTracing::SAMPLING_PRIORITY_KEY

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Headers

Returns a new instance of Headers.



13
14
15
# File 'lib/ddtrace/distributed_tracing/headers/headers.rb', line 13

def initialize(env)
  @env = env
end

Instance Method Details

#header(name) ⇒ Object

TODO: Don't assume Rack format. Make distributed tracing headers apathetic.



19
20
21
22
23
24
25
26
# File 'lib/ddtrace/distributed_tracing/headers/headers.rb', line 19

def header(name)
  rack_header = "http-#{name}".upcase!.tr('-', '_')

  hdr = @env[rack_header]

  # Only return the value if it is not an empty string
  hdr if hdr != ''
end

#id(hdr, base = 10) ⇒ Object



28
29
30
# File 'lib/ddtrace/distributed_tracing/headers/headers.rb', line 28

def id(hdr, base = 10)
  value_to_id(header(hdr), base)
end

#number(hdr, base = 10) ⇒ Object



44
45
46
# File 'lib/ddtrace/distributed_tracing/headers/headers.rb', line 44

def number(hdr, base = 10)
  value_to_number(header(hdr), base)
end

#value_to_id(value, base = 10) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ddtrace/distributed_tracing/headers/headers.rb', line 32

def value_to_id(value, base = 10)
  id = value_to_number(value, base)

  # Return early if we could not parse a number
  return if id.nil?

  # Zero or greater than max allowed value of 2**64
  return if id.zero? || id > Span::EXTERNAL_MAX_ID

  id < 0 ? id + (2**64) : id
end

#value_to_number(value, base = 10) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ddtrace/distributed_tracing/headers/headers.rb', line 48

def value_to_number(value, base = 10)
  # It's important to make a difference between no header,
  # and a header defined to zero.
  return if value.nil?

  # Be sure we have a string
  value = value.to_s

  # If we are parsing base16 number then truncate to 64-bit
  value = DistributedTracing::Headers::Helpers.truncate_base16_number(value) if base == 16

  # Convert header to an integer
  # DEV: Ruby `.to_i` will return `0` if a number could not be parsed
  num = value.to_i(base)

  # Ensure the parsed number is the same as the original string value
  # e.g. We want to make sure to throw away `'nan'.to_i == 0`
  return unless num.to_s(base) == value

  num
end