-
Notifications
You must be signed in to change notification settings - Fork 581
feat(opentelemetry-sampler-aws-xray): Add Rules Caching and Rules Matching Logic #2824
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
4b544b1
bb31a2f
a79cfd5
fbde983
29b441c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// Includes work from: | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { Attributes, Context, Link, SpanKind } from '@opentelemetry/api'; | ||
import { | ||
Sampler, | ||
SamplingResult, | ||
TraceIdRatioBasedSampler, | ||
} from '@opentelemetry/sdk-trace-base'; | ||
|
||
// FallbackSampler samples 1 req/sec and additional 5% of requests using TraceIdRatioBasedSampler. | ||
export class FallbackSampler implements Sampler { | ||
private fixedRateSampler: TraceIdRatioBasedSampler; | ||
|
||
constructor() { | ||
this.fixedRateSampler = new TraceIdRatioBasedSampler(0.05); | ||
} | ||
|
||
shouldSample( | ||
context: Context, | ||
traceId: string, | ||
spanName: string, | ||
spanKind: SpanKind, | ||
attributes: Attributes, | ||
links: Link[] | ||
): SamplingResult { | ||
// TODO: implement and use Rate Limiting Sampler | ||
|
||
return this.fixedRateSampler.shouldSample(context, traceId); | ||
} | ||
|
||
public toString(): string { | ||
return 'FallbackSampler{fallback sampling with sampling config of 1 req/sec and 5% of additional requests}'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// Includes work from: | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { Attributes } from '@opentelemetry/api'; | ||
import { Resource } from '@opentelemetry/resources'; | ||
import { SamplingRuleApplier } from './sampling-rule-applier'; | ||
|
||
// The cache expires 1 hour after the last refresh time. | ||
const RULE_CACHE_TTL_MILLIS: number = 60 * 60 * 1000; | ||
|
||
export class RuleCache { | ||
private ruleAppliers: SamplingRuleApplier[]; | ||
private lastUpdatedEpochMillis: number; | ||
private samplerResource: Resource; | ||
|
||
constructor(samplerResource: Resource) { | ||
this.ruleAppliers = []; | ||
this.samplerResource = samplerResource; | ||
this.lastUpdatedEpochMillis = Date.now(); | ||
} | ||
|
||
public isExpired(): boolean { | ||
const nowInMillis: number = Date.now(); | ||
return nowInMillis > this.lastUpdatedEpochMillis + RULE_CACHE_TTL_MILLIS; | ||
} | ||
|
||
public getMatchedRule( | ||
attributes: Attributes | ||
): SamplingRuleApplier | undefined { | ||
// `this.ruleAppliers` should be sorted by priority, so `find()` is used here | ||
// to determine the first highest priority rule that is matched. The last rule | ||
// in the list should be the 'Default' rule with hardcoded priority of 10000. | ||
return this.ruleAppliers.find( | ||
rule => | ||
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. Could this, in some cases, find the default rule before a higher priority rule? Will this matter? 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. This use of |
||
rule.matches(attributes, this.samplerResource) || | ||
rule.samplingRule.RuleName === 'Default' | ||
); | ||
} | ||
|
||
private sortRulesByPriority(): void { | ||
this.ruleAppliers.sort( | ||
(rule1: SamplingRuleApplier, rule2: SamplingRuleApplier): number => { | ||
if (rule1.samplingRule.Priority === rule2.samplingRule.Priority) { | ||
return rule1.samplingRule.RuleName < rule2.samplingRule.RuleName | ||
? -1 | ||
: 1; | ||
} | ||
return rule1.samplingRule.Priority - rule2.samplingRule.Priority; | ||
} | ||
); | ||
} | ||
|
||
public updateRules(newRuleAppliers: SamplingRuleApplier[]): void { | ||
const oldRuleAppliersMap = new Map<string, SamplingRuleApplier>(); | ||
|
||
this.ruleAppliers.forEach((rule: SamplingRuleApplier) => { | ||
oldRuleAppliersMap.set(rule.samplingRule.RuleName, rule); | ||
}); | ||
|
||
newRuleAppliers.forEach((newRule: SamplingRuleApplier, index: number) => { | ||
const ruleNameToCheck: string = newRule.samplingRule.RuleName; | ||
const oldRule = oldRuleAppliersMap.get(ruleNameToCheck); | ||
if (oldRule) { | ||
if (newRule.samplingRule.equals(oldRule.samplingRule)) { | ||
newRuleAppliers[index] = oldRule; | ||
} | ||
} | ||
}); | ||
this.ruleAppliers = newRuleAppliers; | ||
jj22ee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// sort ruleAppliers by priority and update lastUpdatedEpochMillis | ||
this.sortRulesByPriority(); | ||
this.lastUpdatedEpochMillis = Date.now(); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.