Class: XRay::TraceHeader

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/aws-xray-sdk/model/trace_header.rb

Overview

The sampling decision and trace ID are added to HTTP requests in tracing headers named “X-Amzn-Trace-Id“. The first X-Ray-integrated service that the request hits adds a tracing header, which is read by the X-Ray SDK and included in the response.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger, logger, logger=

Constructor Details

#initialize(root:, parent_id:, sampled:) ⇒ TraceHeader

Returns a new instance of TraceHeader.

Parameters:

  • root (String)

    Trace id.

  • parent_id (String)

    The id of the parent segment or subsegment.

  • sampled (Integer)

    0 means not sampled.



15
16
17
18
19
# File 'lib/aws-xray-sdk/model/trace_header.rb', line 15

def initialize(root:, parent_id:, sampled:)
  @root = root
  @parent_id = parent_id
  @sampled = sampled.to_i if sampled
end

Instance Attribute Details

#parent_idObject

Returns the value of attribute parent_id.



10
11
12
# File 'lib/aws-xray-sdk/model/trace_header.rb', line 10

def parent_id
  @parent_id
end

#rootObject

Returns the value of attribute root.



10
11
12
# File 'lib/aws-xray-sdk/model/trace_header.rb', line 10

def root
  @root
end

#sampledObject

Returns the value of attribute sampled.



10
11
12
# File 'lib/aws-xray-sdk/model/trace_header.rb', line 10

def sampled
  @sampled
end

Class Method Details

.empty_headerObject



50
51
52
# File 'lib/aws-xray-sdk/model/trace_header.rb', line 50

def self.empty_header
  new root: nil, parent_id: nil, sampled: nil
end

.from_header_string(header_str:) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/aws-xray-sdk/model/trace_header.rb', line 21

def self.from_header_string(header_str:)
  empty_header if header_str.to_s.empty?
  header = header_str.delete(' ').downcase
  tmp = {}
  begin
    fields = header.split(';')
    fields.each do |f|
      pair = f.split('=')
      tmp[pair[0].to_sym] = pair[1]
    end
    new root: tmp[:root], parent_id: tmp[:parent], sampled: tmp[:sampled]
  rescue StandardError
    logger.warn %(Invalid trace header #{header}. Ignored.)
    empty_header
  end
end

Instance Method Details

#header_stringString

Returns The heading string constructed based on this header object.

Returns:

  • (String)

    The heading string constructed based on this header object.



39
40
41
42
43
44
45
46
47
48
# File 'lib/aws-xray-sdk/model/trace_header.rb', line 39

def header_string
  return '' unless root
  if !parent_id
    %(Root=#{root};Sampled=#{sampled})
  elsif !sampled
    %(Root=#{root};Parent=#{parent_id})
  else
    %(Root=#{root};Parent=#{parent_id};Sampled=#{sampled})
  end
end