Skip to content

Commit

Permalink
feat(core): rename ProbabilitySampler to TraceIdRatioBasedSampler (#1562
Browse files Browse the repository at this point in the history
)

* 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
legendecas authored Oct 3, 2020
1 parent c6d9ed6 commit fa9af4a
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 178 deletions.
4 changes: 2 additions & 2 deletions packages/opentelemetry-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ Samples a configurable percentage of traces, and additionally samples any trace

```js
const { NodeTracerProvider } = require("@opentelemetry/node");
const { ProbabilitySampler } = require("@opentelemetry/core");
const { TraceIdRatioBasedSampler } = require("@opentelemetry/core");

const tracerProvider = new NodeTracerProvider({
sampler: new ProbabilitySampler(0.5)
sampler: new TraceIdRatioBasedSampler(0.5)
});
```

Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export * from './trace/NoRecordingSpan';
export * from './trace/sampler/AlwaysOffSampler';
export * from './trace/sampler/AlwaysOnSampler';
export * from './trace/sampler/ParentOrElseSampler';
export * from './trace/sampler/ProbabilitySampler';
export * from './trace/sampler/TraceIdRatioBasedSampler';
export * from './trace/TraceState';
export * from './trace/IdGenerator';
export * from './utils/deep-merge';
Expand Down

This file was deleted.

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { AlwaysOnSampler } from '../../src/trace/sampler/AlwaysOnSampler';
import { ParentOrElseSampler } from '../../src/trace/sampler/ParentOrElseSampler';
import { TraceFlags, SpanKind } from '@opentelemetry/api';
import { AlwaysOffSampler } from '../../src/trace/sampler/AlwaysOffSampler';
import { ProbabilitySampler } from '../../src';
import { TraceIdRatioBasedSampler } from '../../src';

const traceId = 'd4cda95b652f4a1592b449d5929fda1b';
const spanId = '6e0c63257de34c92';
Expand All @@ -33,10 +33,10 @@ describe('ParentOrElseSampler', () => {
sampler = new ParentOrElseSampler(new AlwaysOnSampler());
assert.strictEqual(sampler.toString(), 'ParentOrElse{AlwaysOnSampler}');

sampler = new ParentOrElseSampler(new ProbabilitySampler(0.5));
sampler = new ParentOrElseSampler(new TraceIdRatioBasedSampler(0.5));
assert.strictEqual(
sampler.toString(),
'ParentOrElse{ProbabilitySampler{0.5}}'
'ParentOrElse{TraceIdRatioBased{0.5}}'
);
});

Expand Down
112 changes: 0 additions & 112 deletions packages/opentelemetry-core/test/trace/ProbabilitySampler.test.ts

This file was deleted.

Loading

0 comments on commit fa9af4a

Please sign in to comment.