Class: OpenTelemetry::Sampler::XRay::InternalAWSXRayRemoteSampler

Inherits:
Object
  • Object
show all
Defined in:
lib/opentelemetry/sampler/xray/aws_xray_remote_sampler.rb

Overview

InternalAWSXRayRemoteSampler contains all core XRay Sampler Functionality, however it is NOT Parent-based (e.g. Sample logic runs for each span)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint: '127.0.0.1:2000', polling_interval: DEFAULT_RULES_POLLING_INTERVAL_SECONDS, resource: OpenTelemetry::SDK::Resources::Resource.create) ⇒ InternalAWSXRayRemoteSampler

Returns a new instance of InternalAWSXRayRemoteSampler.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/opentelemetry/sampler/xray/aws_xray_remote_sampler.rb', line 46

def initialize(endpoint: '127.0.0.1:2000', polling_interval: DEFAULT_RULES_POLLING_INTERVAL_SECONDS, resource: OpenTelemetry::SDK::Resources::Resource.create)
  if polling_interval.nil? || polling_interval < 10
    OpenTelemetry.logger.warn(
      "'polling_interval' is undefined or too small. Defaulting to #{DEFAULT_RULES_POLLING_INTERVAL_SECONDS} seconds"
    )
    @rule_polling_interval_millis = DEFAULT_RULES_POLLING_INTERVAL_SECONDS * 1000
  else
    @rule_polling_interval_millis = polling_interval * 1000
  end

  @rule_polling_jitter_millis = rand * 5 * 1000
  @target_polling_interval = DEFAULT_TARGET_POLLING_INTERVAL_SECONDS
  @target_polling_jitter_millis = (rand / 10) * 1000

  @aws_proxy_endpoint = endpoint || DEFAULT_AWS_PROXY_ENDPOINT
  @fallback_sampler = OpenTelemetry::Sampler::XRay::FallbackSampler.new
  @client_id = self.class.generate_client_id
  @rule_cache = OpenTelemetry::Sampler::XRay::RuleCache.new(resource)

  @sampling_client = OpenTelemetry::Sampler::XRay::AWSXRaySamplingClient.new(@aws_proxy_endpoint)

  # Start the Sampling Rules poller
  start_sampling_rules_poller

  # Start the Sampling Targets poller
  start_sampling_targets_poller
end

Class Method Details

.generate_client_idObject



193
194
195
196
# File 'lib/opentelemetry/sampler/xray/aws_xray_remote_sampler.rb', line 193

def generate_client_id
  hex_chars = ('0'..'9').to_a + ('a'..'f').to_a
  Array.new(24) { hex_chars.sample }.join
end

Instance Method Details

#descriptionObject



97
98
99
# File 'lib/opentelemetry/sampler/xray/aws_xray_remote_sampler.rb', line 97

def description
  "InternalAWSXRayRemoteSampler{aws_proxy_endpoint=#{@aws_proxy_endpoint}, rule_polling_interval_millis=#{@rule_polling_interval_millis}}"
end

#should_sample?(trace_id:, parent_context:, links:, name:, kind:, attributes:) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/opentelemetry/sampler/xray/aws_xray_remote_sampler.rb', line 74

def should_sample?(trace_id:, parent_context:, links:, name:, kind:, attributes:)
  if @rule_cache.expired?
    OpenTelemetry.logger.debug('Rule cache is expired, so using fallback sampling strategy')
    return @fallback_sampler.should_sample?(
      trace_id: trace_id, parent_context: parent_context, links: links, name: name, kind: kind, attributes: attributes
    )
  end

  matched_rule = @rule_cache.get_matched_rule(attributes)
  if matched_rule
    return matched_rule.should_sample?(
      trace_id: trace_id, parent_context: parent_context, links: links, name: name, kind: kind, attributes: attributes
    )
  end

  OpenTelemetry.logger.debug(
    'Using fallback sampler as no rule match was found. This is likely due to a bug, since default rule should always match'
  )
  @fallback_sampler.should_sample?(
    trace_id: trace_id, parent_context: parent_context, links: links, name: name, kind: kind, attributes: attributes
  )
end