Module: XRay::Facets::Helper

Included in:
AwsSDKPlugin::Handler, NetHttp::HTTPInstanceInterceptor, Rack::Middleware
Defined in:
lib/aws-xray-sdk/facets/helper.rb

Overview

Hepler functions shared for all external frameworks/libraries like make sampling decisions from incoming http requests etc.

Constant Summary collapse

TRACE_HEADER =
'X-Amzn-Trace-Id'.freeze
TRACE_HEADER_PROXY =
'HTTP_X_AMZN_TRACE_ID'.freeze

Instance Method Summary collapse

Instance Method Details

#construct_header(headers:) ⇒ TraceHeader

Construct a ‘TraceHeader` object from headers of the incoming request. This method should always return a `TraceHeader` object regardless of tracing header’s presence in the incoming request.

Parameters:

  • headers (Hash)

    Hash that contains X-Ray trace header key.

Returns:

  • (TraceHeader)

    The new constructed trace header object.



17
18
19
20
21
22
23
# File 'lib/aws-xray-sdk/facets/helper.rb', line 17

def construct_header(headers:)
  if v = headers[TRACE_HEADER_PROXY] || headers[TRACE_HEADER]
    TraceHeader.from_header_string header_str: v
  else
    TraceHeader.empty_header
  end
end

#prep_header_str(entity:) ⇒ Object

Prepares a X-Ray header string based on the provided Segment/Subsegment.



51
52
53
54
55
56
57
58
# File 'lib/aws-xray-sdk/facets/helper.rb', line 51

def prep_header_str(entity:)
  return '' if entity.nil?
  root = entity.segment.trace_id
  parent_id = entity.id
  sampled = entity.sampled ? 1 : 0
  header = TraceHeader.new root: root, parent_id: parent_id, sampled: sampled
  header.header_string
end

#should_sample?(header_obj:, recorder:, host: nil, method: nil, path: nil, **args) ⇒ Boolean

The sampling decision coming from ‘trace_header` always has the highest precedence. If the `trace_header` doesn’t contain sampling decision then it checks if sampling is enabled or not in the recorder. If not enbaled it returns ‘true’. Otherwise it uses sampling rule to decide.

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/aws-xray-sdk/facets/helper.rb', line 30

def should_sample?(header_obj:, recorder:,
                   host: nil, method: nil, path: nil,
                   **args)
  # check outside decision
  if i = header_obj.sampled
    if i.zero?
      false
    else
      true
    end
  # check sampling rules
  elsif recorder.sampling_enabled?
    recorder.sampler.sample_request?(service_name: host, url_path: path,
                                     http_method: method)
  # sample if no reason not to
  else
    true
  end
end