Skip to content

Commit a79cfd5

Browse files
committed
address comments
1 parent bb31a2f commit a79cfd5

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

incubator/opentelemetry-sampler-aws-xray/src/fallback-sampler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ export class FallbackSampler implements Sampler {
4747
}
4848

4949
public toString(): string {
50-
return 'FallbackSampler{fallback sampling with sampling config of 1 req/sec and 5% of additional requests';
50+
return 'FallbackSampler{fallback sampling with sampling config of 1 req/sec and 5% of additional requests}';
5151
}
5252
}

incubator/opentelemetry-sampler-aws-xray/src/rule-cache.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ export class RuleCache {
4444
public getMatchedRule(
4545
attributes: Attributes
4646
): SamplingRuleApplier | undefined {
47+
// `this.ruleAppliers` should be sorted by priority, so `find()` is used here
48+
// to determine the first highest priority rule that is matched. The last rule
49+
// in the list should be the 'Default' rule with hardcoded priority of 10000.
4750
return this.ruleAppliers.find(
4851
rule =>
4952
rule.matches(attributes, this.samplerResource) ||
@@ -65,17 +68,16 @@ export class RuleCache {
6568
}
6669

6770
public updateRules(newRuleAppliers: SamplingRuleApplier[]): void {
68-
const oldRuleAppliersMap: { [key: string]: SamplingRuleApplier } = {};
71+
const oldRuleAppliersMap = new Map<string, SamplingRuleApplier>();
6972

7073
this.ruleAppliers.forEach((rule: SamplingRuleApplier) => {
71-
oldRuleAppliersMap[rule.samplingRule.RuleName] = rule;
74+
oldRuleAppliersMap.set(rule.samplingRule.RuleName, rule);
7275
});
7376

7477
newRuleAppliers.forEach((newRule: SamplingRuleApplier, index: number) => {
7578
const ruleNameToCheck: string = newRule.samplingRule.RuleName;
76-
if (ruleNameToCheck in oldRuleAppliersMap) {
77-
const oldRule: SamplingRuleApplier =
78-
oldRuleAppliersMap[ruleNameToCheck];
79+
const oldRule = oldRuleAppliersMap.get(ruleNameToCheck);
80+
if (oldRule) {
7981
if (newRule.samplingRule.equals(oldRule.samplingRule)) {
8082
newRuleAppliers[index] = oldRule;
8183
}

incubator/opentelemetry-sampler-aws-xray/src/sampling-rule-applier.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
Context,
2525
Link,
2626
SpanKind,
27+
diag,
2728
} from '@opentelemetry/api';
2829
import { Resource } from '@opentelemetry/resources';
2930
import {
@@ -119,11 +120,16 @@ export class SamplingRuleApplier {
119120
// Per spec, url.full is always populated with scheme://
120121
// If scheme is not present, assume it's bad instrumentation and ignore.
121122
if (schemeEndIndex > -1) {
122-
// urlparse("scheme://netloc/path;parameters?query#fragment")
123-
httpTarget = new URL(httpUrl).pathname;
124-
if (httpTarget === '') httpTarget = '/';
123+
try {
124+
httpTarget = new URL(httpUrl).pathname;
125+
if (httpTarget === '') httpTarget = '/';
126+
} catch (e: unknown) {
127+
diag.debug(`Unable to create URL object from url: ${httpUrl}`, e);
128+
}
125129
}
126-
} else if (httpTarget === undefined && httpUrl === undefined) {
130+
}
131+
132+
if (httpTarget === undefined) {
127133
// When missing, the URL Path is assumed to be '/'
128134
httpTarget = '/';
129135
}

incubator/opentelemetry-sampler-aws-xray/test/fallback-sampler.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('FallBackSampler', () => {
2626

2727
it('toString()', () => {
2828
expect(new FallbackSampler().toString()).toEqual(
29-
'FallbackSampler{fallback sampling with sampling config of 1 req/sec and 5% of additional requests'
29+
'FallbackSampler{fallback sampling with sampling config of 1 req/sec and 5% of additional requests}'
3030
);
3131
});
3232
});

0 commit comments

Comments
 (0)