-
Notifications
You must be signed in to change notification settings - Fork 209
feat: AWS X-Ray Remote Sampler Part 2 - Add Rules Caching and Rules Matching Logic #1498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
511bc66
Add Rules Caching and Rules Matching Logic
jj22ee 2a42b5e
allow sampling_rules_poller to persist in event of an error
jj22ee a7dc5ca
Merge branch 'main' into xray-sampler-pr1
kaylareopelle 6bfe214
cleanup tests, logs, and semconv
jj22ee 9fcffce
Merge branch 'main' into xray-sampler-pr1
kaylareopelle 4dc45e9
Merge branch 'main' into xray-sampler-pr1
kaylareopelle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
sampler/xray/lib/opentelemetry/sampler/xray/fallback_sampler.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
module OpenTelemetry | ||
module Sampler | ||
module XRay | ||
# FallbackSampler samples 1 req/sec and additional 5% of requests using TraceIdRatioBasedSampler. | ||
class FallbackSampler | ||
def initialize | ||
@fixed_rate_sampler = OpenTelemetry::SDK::Trace::Samplers::TraceIdRatioBased.new(0.05) | ||
end | ||
|
||
def should_sample?(trace_id:, parent_context:, links:, name:, kind:, attributes:) | ||
# TODO: implement and use Rate Limiting Sampler | ||
|
||
@fixed_rate_sampler.should_sample?(trace_id: trace_id, parent_context: parent_context, links: links, name: name, kind: kind, attributes: attributes) | ||
end | ||
|
||
def description | ||
'FallbackSampler{fallback sampling with sampling config of 1 req/sec and 5% of additional requests}' | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
module OpenTelemetry | ||
module Sampler | ||
module XRay | ||
# RuleCache stores all the Sampling Rule Appliers, each corresponding | ||
# to the user's Sampling Rules that were retrieved from AWS X-Ray | ||
class RuleCache | ||
# The cache expires 1 hour after the last refresh time. | ||
RULE_CACHE_TTL_MILLIS = 60 * 60 * 1000 | ||
|
||
def initialize(sampler_resource) | ||
@rule_appliers = [] | ||
@sampler_resource = sampler_resource | ||
@last_updated_epoch_millis = Time.now.to_i * 1000 | ||
@cache_lock = Mutex.new | ||
end | ||
|
||
def expired? | ||
now_in_millis = Time.now.to_i * 1000 | ||
now_in_millis > @last_updated_epoch_millis + RULE_CACHE_TTL_MILLIS | ||
end | ||
|
||
def get_matched_rule(attributes) | ||
@rule_appliers.find do |rule| | ||
rule.matches?(attributes, @sampler_resource) || rule.sampling_rule.rule_name == 'Default' | ||
end | ||
end | ||
|
||
def update_rules(new_rule_appliers) | ||
old_rule_appliers_map = {} | ||
|
||
@cache_lock.synchronize do | ||
jj22ee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@rule_appliers.each do |rule| | ||
old_rule_appliers_map[rule.sampling_rule.rule_name] = rule | ||
end | ||
|
||
new_rule_appliers.each_with_index do |new_rule, index| | ||
jj22ee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
rule_name_to_check = new_rule.sampling_rule.rule_name | ||
next unless old_rule_appliers_map.key?(rule_name_to_check) | ||
|
||
old_rule = old_rule_appliers_map[rule_name_to_check] | ||
new_rule_appliers[index] = old_rule if new_rule.sampling_rule.equals?(old_rule.sampling_rule) | ||
end | ||
|
||
@rule_appliers = new_rule_appliers | ||
sort_rules_by_priority | ||
@last_updated_epoch_millis = Time.now.to_i * 1000 | ||
end | ||
end | ||
|
||
private | ||
|
||
def sort_rules_by_priority | ||
@rule_appliers.sort! do |rule1, rule2| | ||
if rule1.sampling_rule.priority == rule2.sampling_rule.priority | ||
rule1.sampling_rule.rule_name < rule2.sampling_rule.rule_name ? -1 : 1 | ||
else | ||
rule1.sampling_rule.priority - rule2.sampling_rule.priority | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
require 'test_helper' | ||
|
||
describe OpenTelemetry::Sampler::XRay::FallbackSampler do | ||
# TODO: Add tests for Fallback sampler when Rate Limiter is implemented | ||
|
||
it 'test_to_string' do | ||
assert_equal( | ||
'FallbackSampler{fallback sampling with sampling config of 1 req/sec and 5% of additional requests}', | ||
OpenTelemetry::Sampler::XRay::FallbackSampler.new.description | ||
) | ||
end | ||
end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.