Module: XRay::Facets::Helper

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

Overview

Helper 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.



44
45
46
# File 'lib/aws-xray-sdk/facets/helper.rb', line 44

def prep_header_str(entity:)
  TraceHeader.from_entity(entity: entity).header_string
end

#should_sample?(header_obj:, recorder:, sampling_req:, **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 rules to decide.

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/aws-xray-sdk/facets/helper.rb', line 30

def should_sample?(header_obj:, recorder:, sampling_req:, **args)
  # check outside decision
  if i = header_obj.sampled
    !i.zero?
  # check sampling rules
  elsif recorder.sampling_enabled?
    recorder.sampler.sample_request?(sampling_req)
  # sample if no reason not to
  else
    true
  end
end