From ce6010319127f6cfd5ea35d0045441e8c1847f85 Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Fri, 10 Jan 2025 14:08:22 +0100 Subject: [PATCH] feat(core): remove deprecated samplers --- CHANGELOG.md | 5 + packages/opentelemetry-core/src/index.ts | 4 - .../src/trace/sampler/AlwaysOffSampler.ts | 33 --- .../src/trace/sampler/AlwaysOnSampler.ts | 33 --- .../src/trace/sampler/ParentBasedSampler.ts | 143 ------------- .../trace/sampler/TraceIdRatioBasedSampler.ts | 63 ------ .../test/trace/AlwaysOffSampler.test.ts | 32 --- .../test/trace/AlwaysOnSampler.test.ts | 32 --- .../test/trace/ParentBasedSampler.test.ts | 168 --------------- .../trace/TraceIdRatioBasedSampler.test.ts | 197 ------------------ .../test-package/foo/bar/internal.d.ts | 16 -- .../fixtures/test-package/foo/bar/internal.js | 4 - .../test/trace/fixtures/test-package/index.js | 4 - .../trace/fixtures/test-package/package.json | 11 - 14 files changed, 5 insertions(+), 740 deletions(-) delete mode 100644 packages/opentelemetry-core/src/trace/sampler/AlwaysOffSampler.ts delete mode 100644 packages/opentelemetry-core/src/trace/sampler/AlwaysOnSampler.ts delete mode 100644 packages/opentelemetry-core/src/trace/sampler/ParentBasedSampler.ts delete mode 100644 packages/opentelemetry-core/src/trace/sampler/TraceIdRatioBasedSampler.ts delete mode 100644 packages/opentelemetry-core/test/trace/AlwaysOffSampler.test.ts delete mode 100644 packages/opentelemetry-core/test/trace/AlwaysOnSampler.test.ts delete mode 100644 packages/opentelemetry-core/test/trace/ParentBasedSampler.test.ts delete mode 100644 packages/opentelemetry-core/test/trace/TraceIdRatioBasedSampler.test.ts delete mode 100644 packages/opentelemetry-core/test/trace/fixtures/test-package/foo/bar/internal.d.ts delete mode 100644 packages/opentelemetry-core/test/trace/fixtures/test-package/foo/bar/internal.js delete mode 100644 packages/opentelemetry-core/test/trace/fixtures/test-package/index.js delete mode 100644 packages/opentelemetry-core/test/trace/fixtures/test-package/package.json diff --git a/CHANGELOG.md b/CHANGELOG.md index a7be4d5841f..e9ab36d2718 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,11 @@ For semantic convention package changes, see the [semconv CHANGELOG](packages/se * chore!: update typescript to version `5.0.4` [#5145](https://github.com/open-telemetry/opentelemetry-js/pull/5145) @david-luna * (user-facing) dropped support for `typescript@<5.0.4` * (user-facing) all packages published from this repository will from now on drop support for old versions of `typescript` in minor releases. We will only drop support for versions that are older than 2 years. +* feat(core)!: remove deprecated samplers [#????](https://github.com/open-telemetry/opentelemetry-js/pull/????) @pichlermarc + * (user-facing): deprecated `AlwaysOnSampler` has moved to `@opentelemetry/sdk-trace-base` + * (user-facing): deprecated `AlwaysOffSampler` has moved to `@opentelemetry/sdk-trace-base` + * (user-facing): deprecated `TraceIdRatioSampler` has moved to `@opentelemetry/sdk-trace-base` + * (user-facing): deprecated `TraceIdRatioSampler` has moved to `@opentelemetry/sdk-trace-base` ### :rocket: (Enhancement) diff --git a/packages/opentelemetry-core/src/index.ts b/packages/opentelemetry-core/src/index.ts index ca082331238..714e120e49d 100644 --- a/packages/opentelemetry-core/src/index.ts +++ b/packages/opentelemetry-core/src/index.ts @@ -88,10 +88,6 @@ export { getRPCMetadata, setRPCMetadata, } from './trace/rpc-metadata'; -export { AlwaysOffSampler } from './trace/sampler/AlwaysOffSampler'; -export { AlwaysOnSampler } from './trace/sampler/AlwaysOnSampler'; -export { ParentBasedSampler } from './trace/sampler/ParentBasedSampler'; -export { TraceIdRatioBasedSampler } from './trace/sampler/TraceIdRatioBasedSampler'; export { isTracingSuppressed, suppressTracing, diff --git a/packages/opentelemetry-core/src/trace/sampler/AlwaysOffSampler.ts b/packages/opentelemetry-core/src/trace/sampler/AlwaysOffSampler.ts deleted file mode 100644 index ebe824d90ba..00000000000 --- a/packages/opentelemetry-core/src/trace/sampler/AlwaysOffSampler.ts +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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 } from '@opentelemetry/api'; - -/** - * @deprecated Use the one defined in @opentelemetry/sdk-trace-base instead. - * Sampler that samples no traces. - */ -export class AlwaysOffSampler implements Sampler { - shouldSample(): SamplingResult { - return { - decision: SamplingDecision.NOT_RECORD, - }; - } - - toString(): string { - return 'AlwaysOffSampler'; - } -} diff --git a/packages/opentelemetry-core/src/trace/sampler/AlwaysOnSampler.ts b/packages/opentelemetry-core/src/trace/sampler/AlwaysOnSampler.ts deleted file mode 100644 index 8967d496454..00000000000 --- a/packages/opentelemetry-core/src/trace/sampler/AlwaysOnSampler.ts +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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 } from '@opentelemetry/api'; - -/** - * @deprecated Use the one defined in @opentelemetry/sdk-trace-base instead. - * Sampler that samples all traces. - */ -export class AlwaysOnSampler implements Sampler { - shouldSample(): SamplingResult { - return { - decision: SamplingDecision.RECORD_AND_SAMPLED, - }; - } - - toString(): string { - return 'AlwaysOnSampler'; - } -} diff --git a/packages/opentelemetry-core/src/trace/sampler/ParentBasedSampler.ts b/packages/opentelemetry-core/src/trace/sampler/ParentBasedSampler.ts deleted file mode 100644 index dcfe3a253ca..00000000000 --- a/packages/opentelemetry-core/src/trace/sampler/ParentBasedSampler.ts +++ /dev/null @@ -1,143 +0,0 @@ -/* - * 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 { - Context, - isSpanContextValid, - Link, - Sampler, - SamplingResult, - Attributes, - SpanKind, - TraceFlags, - trace, -} from '@opentelemetry/api'; -import { globalErrorHandler } from '../../common/global-error-handler'; -import { AlwaysOffSampler } from './AlwaysOffSampler'; -import { AlwaysOnSampler } from './AlwaysOnSampler'; - -/** - * @deprecated Use the one defined in @opentelemetry/sdk-trace-base instead. - * A composite sampler that either respects the parent span's sampling decision - * or delegates to `delegateSampler` for root spans. - */ -export class ParentBasedSampler implements Sampler { - private _root: Sampler; - private _remoteParentSampled: Sampler; - private _remoteParentNotSampled: Sampler; - private _localParentSampled: Sampler; - private _localParentNotSampled: Sampler; - - constructor(config: ParentBasedSamplerConfig) { - this._root = config.root; - - if (!this._root) { - globalErrorHandler( - new Error('ParentBasedSampler must have a root sampler configured') - ); - this._root = new AlwaysOnSampler(); - } - - this._remoteParentSampled = - config.remoteParentSampled ?? new AlwaysOnSampler(); - this._remoteParentNotSampled = - config.remoteParentNotSampled ?? new AlwaysOffSampler(); - this._localParentSampled = - config.localParentSampled ?? new AlwaysOnSampler(); - this._localParentNotSampled = - config.localParentNotSampled ?? new AlwaysOffSampler(); - } - - shouldSample( - context: Context, - traceId: string, - spanName: string, - spanKind: SpanKind, - attributes: Attributes, - links: Link[] - ): SamplingResult { - const parentContext = trace.getSpanContext(context); - - if (!parentContext || !isSpanContextValid(parentContext)) { - return this._root.shouldSample( - context, - traceId, - spanName, - spanKind, - attributes, - links - ); - } - - if (parentContext.isRemote) { - if (parentContext.traceFlags & TraceFlags.SAMPLED) { - return this._remoteParentSampled.shouldSample( - context, - traceId, - spanName, - spanKind, - attributes, - links - ); - } - return this._remoteParentNotSampled.shouldSample( - context, - traceId, - spanName, - spanKind, - attributes, - links - ); - } - - if (parentContext.traceFlags & TraceFlags.SAMPLED) { - return this._localParentSampled.shouldSample( - context, - traceId, - spanName, - spanKind, - attributes, - links - ); - } - - return this._localParentNotSampled.shouldSample( - context, - traceId, - spanName, - spanKind, - attributes, - links - ); - } - - toString(): string { - return `ParentBased{root=${this._root.toString()}, remoteParentSampled=${this._remoteParentSampled.toString()}, remoteParentNotSampled=${this._remoteParentNotSampled.toString()}, localParentSampled=${this._localParentSampled.toString()}, localParentNotSampled=${this._localParentNotSampled.toString()}}`; - } -} - -interface ParentBasedSamplerConfig { - /** Sampler called for spans with no parent */ - root: Sampler; - /** Sampler called for spans with a remote parent which was sampled. Default AlwaysOn */ - remoteParentSampled?: Sampler; - /** Sampler called for spans with a remote parent which was not sampled. Default AlwaysOff */ - remoteParentNotSampled?: Sampler; - /** Sampler called for spans with a local parent which was sampled. Default AlwaysOn */ - localParentSampled?: Sampler; - /** Sampler called for spans with a local parent which was not sampled. Default AlwaysOff */ - localParentNotSampled?: Sampler; -} diff --git a/packages/opentelemetry-core/src/trace/sampler/TraceIdRatioBasedSampler.ts b/packages/opentelemetry-core/src/trace/sampler/TraceIdRatioBasedSampler.ts deleted file mode 100644 index c4928d1cf06..00000000000 --- a/packages/opentelemetry-core/src/trace/sampler/TraceIdRatioBasedSampler.ts +++ /dev/null @@ -1,63 +0,0 @@ -/* - * 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, - isValidTraceId, -} from '@opentelemetry/api'; - -/** - * @deprecated Use the one defined in @opentelemetry/sdk-trace-base instead. - * Sampler that samples a given fraction of traces based of trace id deterministically. - */ -export class TraceIdRatioBasedSampler implements Sampler { - private _upperBound: number; - - constructor(private readonly _ratio: number = 0) { - this._ratio = this._normalize(_ratio); - this._upperBound = Math.floor(this._ratio * 0xffffffff); - } - - shouldSample(context: unknown, traceId: string): SamplingResult { - return { - decision: - isValidTraceId(traceId) && this._accumulate(traceId) < this._upperBound - ? 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; - } - - private _accumulate(traceId: string): number { - let accumulation = 0; - for (let i = 0; i < traceId.length / 8; i++) { - const pos = i * 8; - const part = parseInt(traceId.slice(pos, pos + 8), 16); - accumulation = (accumulation ^ part) >>> 0; - } - return accumulation; - } -} diff --git a/packages/opentelemetry-core/test/trace/AlwaysOffSampler.test.ts b/packages/opentelemetry-core/test/trace/AlwaysOffSampler.test.ts deleted file mode 100644 index bda93eddd38..00000000000 --- a/packages/opentelemetry-core/test/trace/AlwaysOffSampler.test.ts +++ /dev/null @@ -1,32 +0,0 @@ -/* - * 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 * as assert from 'assert'; -import * as api from '@opentelemetry/api'; -import { AlwaysOffSampler } from '../../src/trace/sampler/AlwaysOffSampler'; - -describe('AlwaysOffSampler', () => { - it('should reflect sampler name', () => { - const sampler = new AlwaysOffSampler(); - assert.strictEqual(sampler.toString(), 'AlwaysOffSampler'); - }); - - it('should return decision: api.SamplingDecision.NOT_RECORD for AlwaysOffSampler', () => { - const sampler = new AlwaysOffSampler(); - assert.deepStrictEqual(sampler.shouldSample(), { - decision: api.SamplingDecision.NOT_RECORD, - }); - }); -}); diff --git a/packages/opentelemetry-core/test/trace/AlwaysOnSampler.test.ts b/packages/opentelemetry-core/test/trace/AlwaysOnSampler.test.ts deleted file mode 100644 index fce88b19bba..00000000000 --- a/packages/opentelemetry-core/test/trace/AlwaysOnSampler.test.ts +++ /dev/null @@ -1,32 +0,0 @@ -/* - * 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 * as assert from 'assert'; -import * as api from '@opentelemetry/api'; -import { AlwaysOnSampler } from '../../src/trace/sampler/AlwaysOnSampler'; - -describe('AlwaysOnSampler', () => { - it('should reflect sampler name', () => { - const sampler = new AlwaysOnSampler(); - assert.strictEqual(sampler.toString(), 'AlwaysOnSampler'); - }); - - it('should return api.SamplingDecision.RECORD_AND_SAMPLED for AlwaysOnSampler', () => { - const sampler = new AlwaysOnSampler(); - assert.deepStrictEqual(sampler.shouldSample(), { - decision: api.SamplingDecision.RECORD_AND_SAMPLED, - }); - }); -}); diff --git a/packages/opentelemetry-core/test/trace/ParentBasedSampler.test.ts b/packages/opentelemetry-core/test/trace/ParentBasedSampler.test.ts deleted file mode 100644 index 828a32fa3ea..00000000000 --- a/packages/opentelemetry-core/test/trace/ParentBasedSampler.test.ts +++ /dev/null @@ -1,168 +0,0 @@ -/* - * 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 * as assert from 'assert'; -import * as api from '@opentelemetry/api'; -import { AlwaysOnSampler } from '../../src/trace/sampler/AlwaysOnSampler'; -import { ParentBasedSampler } from '../../src/trace/sampler/ParentBasedSampler'; -import { TraceFlags, SpanKind, trace } from '@opentelemetry/api'; -import { AlwaysOffSampler } from '../../src/trace/sampler/AlwaysOffSampler'; -import { TraceIdRatioBasedSampler } from '../../src'; - -const traceId = 'd4cda95b652f4a1592b449d5929fda1b'; -const spanId = '6e0c63257de34c92'; -const spanName = 'foobar'; - -describe('ParentBasedSampler', () => { - it('should reflect sampler name with delegate sampler', () => { - let sampler = new ParentBasedSampler({ root: new AlwaysOnSampler() }); - assert.strictEqual( - sampler.toString(), - 'ParentBased{root=AlwaysOnSampler, remoteParentSampled=AlwaysOnSampler, remoteParentNotSampled=AlwaysOffSampler, localParentSampled=AlwaysOnSampler, localParentNotSampled=AlwaysOffSampler}' - ); - - sampler = new ParentBasedSampler({ root: new AlwaysOffSampler() }); - assert.strictEqual( - sampler.toString(), - 'ParentBased{root=AlwaysOffSampler, remoteParentSampled=AlwaysOnSampler, remoteParentNotSampled=AlwaysOffSampler, localParentSampled=AlwaysOnSampler, localParentNotSampled=AlwaysOffSampler}' - ); - - sampler = new ParentBasedSampler({ - root: new TraceIdRatioBasedSampler(0.5), - }); - assert.strictEqual( - sampler.toString(), - 'ParentBased{root=TraceIdRatioBased{0.5}, remoteParentSampled=AlwaysOnSampler, remoteParentNotSampled=AlwaysOffSampler, localParentSampled=AlwaysOnSampler, localParentNotSampled=AlwaysOffSampler}' - ); - }); - - it('should return api.SamplingDecision.NOT_RECORD for not sampled parent while composited with AlwaysOnSampler', () => { - const sampler = new ParentBasedSampler({ root: new AlwaysOnSampler() }); - - const spanContext = { - traceId, - spanId, - traceFlags: TraceFlags.NONE, - }; - assert.deepStrictEqual( - sampler.shouldSample( - trace.setSpanContext(api.ROOT_CONTEXT, spanContext), - traceId, - spanName, - SpanKind.CLIENT, - {}, - [] - ), - { - decision: api.SamplingDecision.NOT_RECORD, - } - ); - }); - - it('should return api.SamplingDecision.RECORD_AND_SAMPLED for invalid parent spanContext while composited with AlwaysOnSampler', () => { - const sampler = new ParentBasedSampler({ root: new AlwaysOnSampler() }); - - assert.deepStrictEqual( - sampler.shouldSample( - trace.setSpanContext(api.ROOT_CONTEXT, api.INVALID_SPAN_CONTEXT), - traceId, - spanName, - SpanKind.CLIENT, - {}, - [] - ), - { - decision: api.SamplingDecision.RECORD_AND_SAMPLED, - } - ); - }); - - it('should return api.SamplingDecision.RECORD_AND_SAMPLED while composited with AlwaysOnSampler', () => { - const sampler = new ParentBasedSampler({ root: new AlwaysOnSampler() }); - - assert.deepStrictEqual( - sampler.shouldSample( - api.ROOT_CONTEXT, - traceId, - spanName, - SpanKind.CLIENT, - {}, - [] - ), - { - decision: api.SamplingDecision.RECORD_AND_SAMPLED, - } - ); - }); - - it('should return api.SamplingDecision.RECORD_AND_SAMPLED for sampled parent while composited with AlwaysOffSampler', () => { - const sampler = new ParentBasedSampler({ root: new AlwaysOffSampler() }); - - const spanContext = { - traceId, - spanId, - traceFlags: TraceFlags.SAMPLED, - }; - assert.deepStrictEqual( - sampler.shouldSample( - trace.setSpanContext(api.ROOT_CONTEXT, spanContext), - traceId, - spanName, - SpanKind.CLIENT, - {}, - [] - ), - { - decision: api.SamplingDecision.RECORD_AND_SAMPLED, - } - ); - }); - - it('should return api.SamplingDecision.NOT_RECORD for invalid parent spanContext while composited with AlwaysOffSampler', () => { - const sampler = new ParentBasedSampler({ root: new AlwaysOffSampler() }); - - assert.deepStrictEqual( - sampler.shouldSample( - trace.setSpanContext(api.ROOT_CONTEXT, api.INVALID_SPAN_CONTEXT), - traceId, - spanName, - SpanKind.CLIENT, - {}, - [] - ), - { - decision: api.SamplingDecision.NOT_RECORD, - } - ); - }); - - it('should return api.SamplingDecision.RECORD_AND_SAMPLED while composited with AlwaysOffSampler', () => { - const sampler = new ParentBasedSampler({ root: new AlwaysOffSampler() }); - - assert.deepStrictEqual( - sampler.shouldSample( - api.ROOT_CONTEXT, - traceId, - spanName, - SpanKind.CLIENT, - {}, - [] - ), - { - decision: api.SamplingDecision.NOT_RECORD, - } - ); - }); -}); diff --git a/packages/opentelemetry-core/test/trace/TraceIdRatioBasedSampler.test.ts b/packages/opentelemetry-core/test/trace/TraceIdRatioBasedSampler.test.ts deleted file mode 100644 index c94534c776f..00000000000 --- a/packages/opentelemetry-core/test/trace/TraceIdRatioBasedSampler.test.ts +++ /dev/null @@ -1,197 +0,0 @@ -/* - * 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 * as assert from 'assert'; -import * as api from '@opentelemetry/api'; -import { TraceIdRatioBasedSampler } from '../../src/trace/sampler/TraceIdRatioBasedSampler'; - -const spanContext = (traceId = '1') => ({ - traceId, - spanId: '1.1', - traceFlags: api.TraceFlags.NONE, -}); - -const traceId = (part: string) => ('0'.repeat(32) + part).slice(-32); - -describe('TraceIdRatioBasedSampler', () => { - it('should reflect sampler name with ratio', () => { - let sampler = new TraceIdRatioBasedSampler(1.0); - assert.strictEqual(sampler.toString(), 'TraceIdRatioBased{1}'); - - sampler = new TraceIdRatioBasedSampler(0.5); - assert.strictEqual(sampler.toString(), 'TraceIdRatioBased{0.5}'); - - sampler = new TraceIdRatioBasedSampler(0); - assert.strictEqual(sampler.toString(), 'TraceIdRatioBased{0}'); - - sampler = new TraceIdRatioBasedSampler(-0); - assert.strictEqual(sampler.toString(), 'TraceIdRatioBased{0}'); - - sampler = new TraceIdRatioBasedSampler(undefined); - assert.strictEqual(sampler.toString(), 'TraceIdRatioBased{0}'); - - sampler = new TraceIdRatioBasedSampler(NaN); - assert.strictEqual(sampler.toString(), 'TraceIdRatioBased{0}'); - - sampler = new TraceIdRatioBasedSampler(+Infinity); - assert.strictEqual(sampler.toString(), 'TraceIdRatioBased{1}'); - - sampler = new TraceIdRatioBasedSampler(-Infinity); - assert.strictEqual(sampler.toString(), 'TraceIdRatioBased{0}'); - }); - - it('should return a always sampler for 1', () => { - const sampler = new TraceIdRatioBasedSampler(1); - assert.deepStrictEqual( - sampler.shouldSample(spanContext(traceId('1')), traceId('1')), - { - decision: api.SamplingDecision.RECORD_AND_SAMPLED, - } - ); - }); - - it('should return a always sampler for >1', () => { - const sampler = new TraceIdRatioBasedSampler(100); - assert.deepStrictEqual( - sampler.shouldSample(spanContext(traceId('1')), traceId('1')), - { - decision: api.SamplingDecision.RECORD_AND_SAMPLED, - } - ); - }); - - it('should return a never sampler for 0', () => { - const sampler = new TraceIdRatioBasedSampler(0); - assert.deepStrictEqual( - sampler.shouldSample(spanContext(traceId('1')), traceId('1')), - { - decision: api.SamplingDecision.NOT_RECORD, - } - ); - }); - - it('should return a never sampler for <0', () => { - const sampler = new TraceIdRatioBasedSampler(-1); - assert.deepStrictEqual( - sampler.shouldSample(spanContext(traceId('1')), traceId('1')), - { - decision: api.SamplingDecision.NOT_RECORD, - } - ); - }); - - it('should handle NaN', () => { - const sampler = new TraceIdRatioBasedSampler(NaN); - assert.strictEqual(sampler.toString(), 'TraceIdRatioBased{0}'); - assert.deepStrictEqual( - sampler.shouldSample(spanContext(traceId('1')), traceId('1')), - { - decision: api.SamplingDecision.NOT_RECORD, - } - ); - }); - - it('should handle -NaN', () => { - const sampler = new TraceIdRatioBasedSampler(-NaN); - assert.strictEqual(sampler.toString(), 'TraceIdRatioBased{0}'); - assert.deepStrictEqual( - sampler.shouldSample(spanContext(traceId('1')), traceId('1')), - { - decision: api.SamplingDecision.NOT_RECORD, - } - ); - }); - - it('should handle undefined', () => { - const sampler = new TraceIdRatioBasedSampler(undefined); - assert.strictEqual(sampler.toString(), 'TraceIdRatioBased{0}'); - assert.deepStrictEqual( - sampler.shouldSample(spanContext(traceId('1')), traceId('1')), - { - decision: api.SamplingDecision.NOT_RECORD, - } - ); - }); - - it('should sample based on trace id', () => { - const sampler = new TraceIdRatioBasedSampler(0.2); - assert.deepStrictEqual( - sampler.shouldSample(spanContext(traceId('1')), traceId('1')), - { - decision: api.SamplingDecision.RECORD_AND_SAMPLED, - } - ); - - assert.deepStrictEqual( - sampler.shouldSample( - spanContext(traceId('33333333')), - traceId('33333333') - ), - { - decision: api.SamplingDecision.NOT_RECORD, - } - ); - }); - - it('should not sample with a invalid trace id', () => { - const sampler = new TraceIdRatioBasedSampler(1); - assert.deepStrictEqual(sampler.shouldSample(spanContext(''), ''), { - decision: api.SamplingDecision.NOT_RECORD, - }); - - assert.deepStrictEqual( - sampler.shouldSample(spanContext(traceId('g')), traceId('g')), - { - decision: api.SamplingDecision.NOT_RECORD, - } - ); - }); - - it('should sample traces that a lower sampling ratio would sample', () => { - const sampler10 = new TraceIdRatioBasedSampler(0.1); - const sampler20 = new TraceIdRatioBasedSampler(0.2); - - const id1 = traceId((Math.floor(0xffffffff * 0.1) - 1).toString(16)); - assert.deepStrictEqual(sampler10.shouldSample(spanContext(id1), id1), { - decision: api.SamplingDecision.RECORD_AND_SAMPLED, - }); - assert.deepStrictEqual(sampler20.shouldSample(spanContext(id1), id1), { - decision: api.SamplingDecision.RECORD_AND_SAMPLED, - }); - - const id2 = traceId((Math.floor(0xffffffff * 0.2) - 1).toString(16)); - assert.deepStrictEqual(sampler10.shouldSample(spanContext(id2), id2), { - decision: api.SamplingDecision.NOT_RECORD, - }); - assert.deepStrictEqual(sampler20.shouldSample(spanContext(id2), id2), { - decision: api.SamplingDecision.RECORD_AND_SAMPLED, - }); - - const id2delta = traceId(Math.floor(0xffffffff * 0.2).toString(16)); - assert.deepStrictEqual( - sampler10.shouldSample(spanContext(id2delta), id2delta), - { - decision: api.SamplingDecision.NOT_RECORD, - } - ); - assert.deepStrictEqual( - sampler20.shouldSample(spanContext(id2delta), id2delta), - { - decision: api.SamplingDecision.NOT_RECORD, - } - ); - }); -}); diff --git a/packages/opentelemetry-core/test/trace/fixtures/test-package/foo/bar/internal.d.ts b/packages/opentelemetry-core/test/trace/fixtures/test-package/foo/bar/internal.d.ts deleted file mode 100644 index 59d191849c2..00000000000 --- a/packages/opentelemetry-core/test/trace/fixtures/test-package/foo/bar/internal.d.ts +++ /dev/null @@ -1,16 +0,0 @@ -/* - * 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. - */ -export declare function internallyExportedFunction(): boolean; diff --git a/packages/opentelemetry-core/test/trace/fixtures/test-package/foo/bar/internal.js b/packages/opentelemetry-core/test/trace/fixtures/test-package/foo/bar/internal.js deleted file mode 100644 index 0f0e71ee74b..00000000000 --- a/packages/opentelemetry-core/test/trace/fixtures/test-package/foo/bar/internal.js +++ /dev/null @@ -1,4 +0,0 @@ -Object.defineProperty(exports, "__esModule", { value: true }); -exports.internallyExportedFunction = function internallyExportedFunction() { - return true; -} diff --git a/packages/opentelemetry-core/test/trace/fixtures/test-package/index.js b/packages/opentelemetry-core/test/trace/fixtures/test-package/index.js deleted file mode 100644 index bddb4ea0028..00000000000 --- a/packages/opentelemetry-core/test/trace/fixtures/test-package/index.js +++ /dev/null @@ -1,4 +0,0 @@ -Object.defineProperty(exports, "__esModule", { value: true }); -exports.externallyExportedFunction = function externallyExportedFunction() { - return true; -} diff --git a/packages/opentelemetry-core/test/trace/fixtures/test-package/package.json b/packages/opentelemetry-core/test/trace/fixtures/test-package/package.json deleted file mode 100644 index e80faeea41b..00000000000 --- a/packages/opentelemetry-core/test/trace/fixtures/test-package/package.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "test-package", - "version": "0.1.1", - "description": "", - "main": "index.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "author": "", - "license": "ISC" -}