-
Notifications
You must be signed in to change notification settings - Fork 850
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): rename ProbabilitySampler to TraceIdRatioBasedSampler (#1562
) * feat(core): rename ProbabilitySampler to TraceIdRatioBasedSampler TraceIdRatioBasedSamplers are determinist samplers based on trace ids. * fixup!: rename ProbabilitySampler to TraceIdRatioBasedSampler * fixup!: explicitly set spanContexts in tests
- Loading branch information
1 parent
c6d9ed6
commit fa9af4a
Showing
8 changed files
with
209 additions
and
178 deletions.
There are no files selected for viewing
This file contains 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 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
58 changes: 0 additions & 58 deletions
58
packages/opentelemetry-core/src/trace/sampler/ProbabilitySampler.ts
This file was deleted.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
packages/opentelemetry-core/src/trace/sampler/TraceIdRatioBasedSampler.ts
This file contains 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,55 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import { | ||
Sampler, | ||
SamplingDecision, | ||
SamplingResult, | ||
SpanContext, | ||
} from '@opentelemetry/api'; | ||
|
||
/** Sampler that samples a given fraction of traces based of trace id deterministically. */ | ||
export class TraceIdRatioBasedSampler implements Sampler { | ||
constructor(private readonly _ratio: number = 0) { | ||
this._ratio = this._normalize(_ratio); | ||
} | ||
|
||
shouldSample( | ||
parentContext: SpanContext | undefined, | ||
traceId: string | ||
): SamplingResult { | ||
let accumulation = 0; | ||
for (let idx = 0; idx < traceId.length; idx++) { | ||
accumulation += traceId.charCodeAt(idx); | ||
} | ||
const cmp = (accumulation % 100) / 100; | ||
return { | ||
decision: | ||
cmp < this._ratio | ||
? SamplingDecision.RECORD_AND_SAMPLED | ||
: SamplingDecision.NOT_RECORD, | ||
}; | ||
} | ||
|
||
toString(): string { | ||
return `TraceIdRatioBased{${this._ratio}}`; | ||
} | ||
|
||
private _normalize(ratio: number): number { | ||
if (typeof ratio !== 'number' || isNaN(ratio)) return 0; | ||
return ratio >= 1 ? 1 : ratio <= 0 ? 0 : ratio; | ||
} | ||
} |
This file contains 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
112 changes: 0 additions & 112 deletions
112
packages/opentelemetry-core/test/trace/ProbabilitySampler.test.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.