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



63
64
65
# File 'lib/aws-xray-sdk/model/trace_header.rb', line 63

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

.from_entity(entity:) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/aws-xray-sdk/model/trace_header.rb', line 21

def self.from_entity(entity:)
  return empty_header if entity.nil?
  root = entity.segment.trace_id
  parent_id = entity.id
  sampled = entity.sampled ? 1 : 0
  new root: root, parent_id: parent_id, sampled: sampled
end

.from_header_string(header_str:) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/aws-xray-sdk/model/trace_header.rb', line 29

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.



52
53
54
55
56
57
58
59
60
61
# File 'lib/aws-xray-sdk/model/trace_header.rb', line 52

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

#root_stringString

Returns The header string of the root object.

Returns:

  • (String)

    The header string of the root object



47
48
49
# File 'lib/aws-xray-sdk/model/trace_header.rb', line 47

def root_string
	%(Root=#{root})
end