-
Notifications
You must be signed in to change notification settings - Fork 204
feat: AWS X-Ray Remote Sampler Part 3 - Add Rate Limiter and Sampling Targets Poller Logic #1536
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
Open
jj22ee
wants to merge
5
commits into
open-telemetry:main
Choose a base branch
from
jj22ee:xray-sampler-pr2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c292d77
Add Rate Limiter and Sampling Targets Poller Logic
jj22ee 410ce42
fix test cases related to rate limiter and timecop usage
jj22ee 5e43f4b
add example for x-ray sampling on rails
jj22ee 73ac041
Merge branch 'main' into xray-sampler-pr2
jj22ee 7408898
Merge branch 'main' into xray-sampler-pr2
jj22ee 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
101 changes: 101 additions & 0 deletions
101
sampler/xray/example/xray_sampling_on_rails_demonstration.ru
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,101 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright The OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
require 'bundler/inline' | ||
|
||
gemfile(true) do | ||
source 'https://rubygems.org' | ||
|
||
gem 'concurrent-ruby', '1.3.4' | ||
gem 'rails', '~> 7.0.4' | ||
gem 'puma' | ||
|
||
gem 'opentelemetry-sdk' | ||
gem 'opentelemetry-instrumentation-rails' | ||
gem 'opentelemetry-sampler-xray', path: './../' # Use local version of the X-Ray Sampler | ||
# gem 'opentelemetry-sampler-xray' # Use RubyGems version of the X-Ray Sampler | ||
end | ||
|
||
require "action_controller/railtie" | ||
require "action_mailer/railtie" | ||
require "rails/test_unit/railtie" | ||
|
||
class App < Rails::Application | ||
config.root = __dir__ | ||
config.consider_all_requests_local = true | ||
|
||
routes.append do | ||
root to: 'welcome#index' | ||
get "/test" => 'welcome#test' | ||
end | ||
end | ||
|
||
class WelcomeController < ActionController::Base | ||
def index | ||
render inline: 'Successfully called "/" endpoint' | ||
end | ||
|
||
def test | ||
render inline: 'Successfully called "/test" endpoint' | ||
end | ||
end | ||
|
||
ENV['OTEL_TRACES_EXPORTER'] = 'console' | ||
ENV['OTEL_SERVICE_NAME'] = 'xray-sampler-on-rails-service' | ||
OpenTelemetry::SDK.configure do |c| | ||
c.use_all({ 'OpenTelemetry::Instrumentation::ActiveRecord' => { enabled: false } }) | ||
end | ||
|
||
OpenTelemetry.tracer_provider.sampler = OpenTelemetry::Sampler::XRay::AWSXRayRemoteSampler.new(resource:OpenTelemetry::SDK::Resources::Resource.create({ | ||
"service.name"=>"xray-sampler-on-rails-service" | ||
})) | ||
|
||
App.initialize! | ||
|
||
run App | ||
|
||
#### Running and using the Sample App | ||
# To run this example run the `rackup` command with this file | ||
# Example: rackup trace_request_demonstration.ru | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "trace_request_demonstration", typo? |
||
# Navigate to http://localhost:9292/ | ||
# Spans for any requests sampled by the X-Ray Sampler will appear in the console | ||
|
||
#### Required configuration in the OpenTelemetry Collector | ||
# In order for sampling rules to be obtained from AWS X-Ray, the awsproxy extension | ||
# must be configured in the OpenTelemetry Collector, which will use your AWS credentials. | ||
# - https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/extension/awsproxy#aws-proxy | ||
# Without the awsproxy extension, the X-Ray Sampler will use a fallback sampler | ||
# with a sampling strategy of "1 request/second, plus 5% of any additional requests" | ||
|
||
#### Testing out configurable X-Ray Sampling Rules against the "service.name" resource attribute. | ||
# Create a new Sampling Rule with the following matching criteria in AWS CloudWatch Settings for X-Ray Traces. | ||
# - https://console.aws.amazon.com/cloudwatch/home#xray:settings/sampling-rules | ||
# Matching Criteria | ||
# ServiceName = xray-sampler-on-rails-service | ||
# ServiceType = * | ||
# Host = * | ||
# ResourceARN = * | ||
# HTTPMethod = * | ||
# URLPath = * | ||
# For the above matching criteria, try out the following settings to sample or not sample requests | ||
# - Limit to 0r/sec then 0 fixed rate | ||
# - Limit to 1r/sec then 0 fixed rate (May take 30 seconds for this setting to apply) | ||
# - Limit to 0r/sec then 100% fixed rate | ||
|
||
#### Testing out configurable X-Ray Sampling Rules against the "/test" endpoint in this sample app. | ||
# Create a new Sampling Rule with the following matching criteria in AWS CloudWatch Settings for X-Ray Traces. | ||
# - https://console.aws.amazon.com/cloudwatch/home#xray:settings/sampling-rules | ||
# Matching Criteria | ||
# ServiceName = * | ||
# ServiceType = * | ||
# Host = * | ||
# ResourceARN = * | ||
# HTTPMethod = * | ||
# URLPath = /test | ||
# For the above matching criteria, try out the following settings to sample or not sample requests | ||
# - Limit to 0r/sec then 0 fixed rate | ||
# - Limit to 1r/sec then 0 fixed rate (May take 30 seconds for this setting to apply) | ||
# - Limit to 0r/sec then 100% fixed rate |
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
48 changes: 48 additions & 0 deletions
48
sampler/xray/lib/opentelemetry/sampler/xray/rate_limiter.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,48 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
module OpenTelemetry | ||
module Sampler | ||
module XRay | ||
# RateLimiter keeps track of the current reservoir quota balance available (measured via available time) | ||
# If enough time has elapsed, the RateLimiter will allow quota balance to be consumed/taken (decrease available time) | ||
# A RateLimitingSampler uses this RateLimiter to determine if it should sample or not based on the quota balance available. | ||
class RateLimiter | ||
def initialize(quota, max_balance_in_seconds = 1) | ||
@max_balance_millis = max_balance_in_seconds * 1000.0 | ||
@quota = quota | ||
@wallet_floor_millis = Time.now.to_f * 1000 | ||
# current "balance" would be `ceiling - floor` | ||
@lock = Mutex.new | ||
end | ||
|
||
def take(cost = 1) | ||
return false if @quota.zero? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. <= 0? |
||
|
||
quota_per_millis = @quota / 1000.0 | ||
|
||
# assume divide by zero not possible | ||
cost_in_millis = cost / quota_per_millis | ||
|
||
@lock.synchronize do | ||
wallet_ceiling_millis = Time.now.to_f * 1000 | ||
current_balance_millis = wallet_ceiling_millis - @wallet_floor_millis | ||
current_balance_millis = [current_balance_millis, @max_balance_millis].min | ||
pending_remaining_balance_millis = current_balance_millis - cost_in_millis | ||
|
||
if pending_remaining_balance_millis >= 0 | ||
@wallet_floor_millis = wallet_ceiling_millis - pending_remaining_balance_millis | ||
return true | ||
end | ||
|
||
# No changes to the wallet state | ||
false | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
43 changes: 43 additions & 0 deletions
43
sampler/xray/lib/opentelemetry/sampler/xray/rate_limiting_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,43 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
require_relative 'rate_limiter' | ||
|
||
module OpenTelemetry | ||
module Sampler | ||
module XRay | ||
# RateLimitingSampler is a Sampler that uses a RateLimiter to determine | ||
# if it should sample or not based on the quota balance available. | ||
class RateLimitingSampler | ||
def initialize(quota) | ||
@quota = quota | ||
@reservoir = RateLimiter.new(quota) | ||
end | ||
|
||
def should_sample?(trace_id:, parent_context:, links:, name:, kind:, attributes:) | ||
tracestate = OpenTelemetry::Trace.current_span(parent_context).context.tracestate | ||
if @reservoir.take(1) | ||
OpenTelemetry::SDK::Trace::Samplers::Result.new( | ||
decision: OpenTelemetry::SDK::Trace::Samplers::Decision::RECORD_AND_SAMPLE, | ||
tracestate: tracestate, | ||
attributes: attributes | ||
) | ||
else | ||
OpenTelemetry::SDK::Trace::Samplers::Result.new( | ||
decision: OpenTelemetry::SDK::Trace::Samplers::Decision::DROP, | ||
tracestate: tracestate, | ||
attributes: attributes | ||
) | ||
end | ||
end | ||
|
||
def to_s | ||
"RateLimitingSampler{rate limiting sampling with sampling config of #{@quota} req/sec and 0% 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
enable override.