Skip to content

feat(opentelemetry-sampler-aws-xray): Add Rate Limiter and Sampling Targets Poller Logic #2924

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
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jj22ee
Copy link
Contributor

@jj22ee jj22ee commented Jul 3, 2025

Which problem is this PR solving?

Short description of the changes

This PR is a followup to #2824

  • Added logic to fetch sampling targets for each SamplingRuleApplier.
    • The sampling targets are periodically fetched every 10 seconds by making the GetSamplingTargets API call to X-Ray.
    • The targets determine the reservoir quota and the rate at which a SamplingRuleApplier will sample the requests.
    • Each rule applier keeps and updates a sampling statistics document which is required in GetSamplingTargets call to determine the next target
  • Added a RateLimitingSampler (applied before the fixed rate sampler) to be used in each rule applier.
    • Together these samplers determine how many requests to sample every second and what percentage of additional requests to sample in that second.
    • The FallbackSampler is updated to be a combination of above samplers to sample 1 req/sec and 5% of additional requests in that second.

This PR's implementation was tested against X-Ray's Centralized Sampling Integration tests.

@jj22ee jj22ee requested a review from a team as a code owner July 3, 2025 18:31
@github-actions github-actions bot requested a review from yiyuan-he July 3, 2025 18:31
Copy link

codecov bot commented Jul 3, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.04%. Comparing base (5988c79) to head (496cc32).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2924      +/-   ##
==========================================
+ Coverage   88.99%   89.04%   +0.04%     
==========================================
  Files         188      188              
  Lines        9181     9181              
  Branches     1889     1889              
==========================================
+ Hits         8171     8175       +4     
+ Misses       1010     1006       -4     

see 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.


import { SamplingRuleApplier } from './sampling-rule-applier';
import { PACKAGE_NAME } from './version';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How this file is generated? I tried 'npm run setup:dev' and 'npm run compile', none of them generate this file and that is causing build failure.


constructor() {
this.fixedRateSampler = new TraceIdRatioBasedSampler(0.05);
this.rateLimitingSampler = new RateLimitingSampler(1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider allowing these values to be passed as params with default value. That will it more reusable.

Suggested change
this.rateLimitingSampler = new RateLimitingSampler(1);
constructor(rateLimit: number = 1, ratio: number = 0.05) {
this.fixedRateSampler = new TraceIdRatioBasedSampler(ratio);
this.rateLimitingSampler = new RateLimitingSampler(rateLimit);
}

if (
sampler.shouldSample(
context.active(),
'1234',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it true that we intentionally use an invalid trace ID to avoid being sampled by the Fixed Rate Sampler? If that is the case, can we use an const variable instead of the magic string? Or maybe a comment here. That will make it easier to understand the purpose of the UT.

Suggested change
'1234',
const InvaidTraceIdToAvoidFixedRateSampler = '1234',


const quotaPerMillis: number = this.quota / 1000.0;

// assume divide by zero not possible
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about combine line 45 and 48: = (cost * 1000) / quota; so you don't need this comment.

}

public toString(): string {
return `RateLimitingSampler{rate limiting sampling with sampling config of ${this.quota} req/sec and 0% of additional requests}`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: do we need 'and 0% of additional requests' in the context of rate limiting?

@@ -45,7 +45,7 @@ export class AWSXRaySamplingClient {
this.makeSamplingRequest<GetSamplingTargetsResponse>(
this.samplingTargetsEndpoint,
callback,
this.samplerDiag.debug,
this.samplerDiag.debug.bind(this.samplerDiag),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit. lambda is modern, more readable.
(message: string, ...args: unknown[]) => this.samplerDiag.debug(message, ...args)

targetDocuments: TargetMap,
lastRuleModification: number
): [boolean, number] {
let minPollingInteral: number | undefined = undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: do you mean 'interval'?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants