diff --git a/CHANGELOG.md b/CHANGELOG.md index a7be4d5841f..9fd7fb4232b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ For semantic convention package changes, see the [semconv CHANGELOG](packages/se * refactor(sdk-trace-base)!: remove `new Span` constructor in favor of `Tracer.startSpan` API [#5048](https://github.com/open-telemetry/opentelemetry-js/pull/5048) @david-luna * refactor(sdk-trace-base)!: remove `BasicTracerProvider.addSpanProcessor` API in favor of constructor options. [#5134](https://github.com/open-telemetry/opentelemetry-js/pull/5134) @david-luna * refactor(sdk-trace-base)!: make `resource` property private in `BasicTracerProvider` and remove `getActiveSpanProcessor` API. [#5192](https://github.com/open-telemetry/opentelemetry-js/pull/5192) @david-luna +* feat(core)!: remove deprecated `IdGenerator` and `RandomIdGenerator` [#5309](https://github.com/open-telemetry/opentelemetry-js/pull/5309) @pichlermarc * feat(core)!: remove deprecated type `InstrumentationLibrary` [#5308](https://github.com/open-telemetry/opentelemetry-js/pull/5308) @pichlermarc * (user-facing): please use equivalent type `InstrumentationScope` instead * feat(sdk-trace-base)!: replace usages fo `InstrumentationLibrary` with `InstrumentationScope` [#5308](https://github.com/open-telemetry/opentelemetry-js/pull/5308) @pichlermarc diff --git a/packages/opentelemetry-core/src/index.ts b/packages/opentelemetry-core/src/index.ts index ca082331238..591d3458cfa 100644 --- a/packages/opentelemetry-core/src/index.ts +++ b/packages/opentelemetry-core/src/index.ts @@ -61,7 +61,6 @@ export const baggageUtils = { parsePairKeyValue, }; export { - RandomIdGenerator, SDK_INFO, _globalThis, getEnv, @@ -80,7 +79,6 @@ export { W3CTraceContextPropagator, parseTraceParent, } from './trace/W3CTraceContextPropagator'; -export { IdGenerator } from './trace/IdGenerator'; export { RPCMetadata, RPCType, diff --git a/packages/opentelemetry-core/src/platform/browser/RandomIdGenerator.ts b/packages/opentelemetry-core/src/platform/browser/RandomIdGenerator.ts deleted file mode 100644 index 634ba0a32f1..00000000000 --- a/packages/opentelemetry-core/src/platform/browser/RandomIdGenerator.ts +++ /dev/null @@ -1,53 +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 { IdGenerator } from '../../trace/IdGenerator'; - -const SPAN_ID_BYTES = 8; -const TRACE_ID_BYTES = 16; - -/** - * @deprecated Use the one defined in @opentelemetry/sdk-trace-base instead. - */ -export class RandomIdGenerator implements IdGenerator { - /** - * Returns a random 16-byte trace ID formatted/encoded as a 32 lowercase hex - * characters corresponding to 128 bits. - */ - generateTraceId = getIdGenerator(TRACE_ID_BYTES); - - /** - * Returns a random 8-byte span ID formatted/encoded as a 16 lowercase hex - * characters corresponding to 64 bits. - */ - generateSpanId = getIdGenerator(SPAN_ID_BYTES); -} - -const SHARED_CHAR_CODES_ARRAY = Array(32); -function getIdGenerator(bytes: number): () => string { - return function generateId() { - for (let i = 0; i < bytes * 2; i++) { - SHARED_CHAR_CODES_ARRAY[i] = Math.floor(Math.random() * 16) + 48; - // valid hex characters in the range 48-57 and 97-102 - if (SHARED_CHAR_CODES_ARRAY[i] >= 58) { - SHARED_CHAR_CODES_ARRAY[i] += 39; - } - } - return String.fromCharCode.apply( - null, - SHARED_CHAR_CODES_ARRAY.slice(0, bytes * 2) - ); - }; -} diff --git a/packages/opentelemetry-core/src/platform/browser/index.ts b/packages/opentelemetry-core/src/platform/browser/index.ts index 9fedc44d735..7ce43661f36 100644 --- a/packages/opentelemetry-core/src/platform/browser/index.ts +++ b/packages/opentelemetry-core/src/platform/browser/index.ts @@ -17,7 +17,6 @@ export { getEnvWithoutDefaults, getEnv } from './environment'; export { _globalThis } from './globalThis'; export { hexToBase64 } from './hex-to-base64'; -export { RandomIdGenerator } from './RandomIdGenerator'; export { otperformance } from './performance'; export { SDK_INFO } from './sdk-info'; export { unrefTimer } from './timer-util'; diff --git a/packages/opentelemetry-core/src/platform/index.ts b/packages/opentelemetry-core/src/platform/index.ts index f2139e6451c..b0c9af51158 100644 --- a/packages/opentelemetry-core/src/platform/index.ts +++ b/packages/opentelemetry-core/src/platform/index.ts @@ -14,7 +14,6 @@ * limitations under the License. */ export { - RandomIdGenerator, SDK_INFO, _globalThis, getEnv, diff --git a/packages/opentelemetry-core/src/platform/node/RandomIdGenerator.ts b/packages/opentelemetry-core/src/platform/node/RandomIdGenerator.ts deleted file mode 100644 index 34d4971e559..00000000000 --- a/packages/opentelemetry-core/src/platform/node/RandomIdGenerator.ts +++ /dev/null @@ -1,58 +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 { IdGenerator } from '../../trace/IdGenerator'; -const SPAN_ID_BYTES = 8; -const TRACE_ID_BYTES = 16; - -/** - * @deprecated Use the one defined in @opentelemetry/sdk-trace-base instead. - */ -export class RandomIdGenerator implements IdGenerator { - /** - * Returns a random 16-byte trace ID formatted/encoded as a 32 lowercase hex - * characters corresponding to 128 bits. - */ - generateTraceId = getIdGenerator(TRACE_ID_BYTES); - - /** - * Returns a random 8-byte span ID formatted/encoded as a 16 lowercase hex - * characters corresponding to 64 bits. - */ - generateSpanId = getIdGenerator(SPAN_ID_BYTES); -} - -const SHARED_BUFFER = Buffer.allocUnsafe(TRACE_ID_BYTES); -function getIdGenerator(bytes: number): () => string { - return function generateId() { - for (let i = 0; i < bytes / 4; i++) { - // unsigned right shift drops decimal part of the number - // it is required because if a number between 2**32 and 2**32 - 1 is generated, an out of range error is thrown by writeUInt32BE - SHARED_BUFFER.writeUInt32BE((Math.random() * 2 ** 32) >>> 0, i * 4); - } - - // If buffer is all 0, set the last byte to 1 to guarantee a valid w3c id is generated - for (let i = 0; i < bytes; i++) { - if (SHARED_BUFFER[i] > 0) { - break; - } else if (i === bytes - 1) { - SHARED_BUFFER[bytes - 1] = 1; - } - } - - return SHARED_BUFFER.toString('hex', 0, bytes); - }; -} diff --git a/packages/opentelemetry-core/src/platform/node/index.ts b/packages/opentelemetry-core/src/platform/node/index.ts index 9fedc44d735..7ce43661f36 100644 --- a/packages/opentelemetry-core/src/platform/node/index.ts +++ b/packages/opentelemetry-core/src/platform/node/index.ts @@ -17,7 +17,6 @@ export { getEnvWithoutDefaults, getEnv } from './environment'; export { _globalThis } from './globalThis'; export { hexToBase64 } from './hex-to-base64'; -export { RandomIdGenerator } from './RandomIdGenerator'; export { otperformance } from './performance'; export { SDK_INFO } from './sdk-info'; export { unrefTimer } from './timer-util'; diff --git a/packages/opentelemetry-core/src/trace/IdGenerator.ts b/packages/opentelemetry-core/src/trace/IdGenerator.ts deleted file mode 100644 index bf027f905a1..00000000000 --- a/packages/opentelemetry-core/src/trace/IdGenerator.ts +++ /dev/null @@ -1,26 +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. - */ - -/** - * @deprecated Use the one defined in @opentelemetry/sdk-trace-base instead. - * IdGenerator provides an interface for generating Trace Id and Span Id. - */ -export interface IdGenerator { - /** Returns a trace ID composed of 32 lowercase hex characters. */ - generateTraceId(): string; - /** Returns a span ID composed of 16 lowercase hex characters. */ - generateSpanId(): string; -} diff --git a/packages/opentelemetry-core/test/platform/RandomIdGenerator.test.ts b/packages/opentelemetry-core/test/platform/RandomIdGenerator.test.ts deleted file mode 100644 index 2400fa6ccca..00000000000 --- a/packages/opentelemetry-core/test/platform/RandomIdGenerator.test.ts +++ /dev/null @@ -1,53 +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 { RandomIdGenerator } from '../../src/platform'; - -const idGenerator = new RandomIdGenerator(); - -describe('randomTraceId', () => { - let traceId1: string, traceId2: string; - beforeEach(() => { - traceId1 = idGenerator.generateTraceId(); - traceId2 = idGenerator.generateTraceId(); - }); - - it('returns 32 character hex strings', () => { - assert.ok(traceId1.match(/[a-f0-9]{32}/)); - assert.ok(!traceId1.match(/^0+$/)); - }); - - it('returns different ids on each call', () => { - assert.notDeepStrictEqual(traceId1, traceId2); - }); -}); - -describe('randomSpanId', () => { - let spanId1: string, spanId2: string; - beforeEach(() => { - spanId1 = idGenerator.generateSpanId(); - spanId2 = idGenerator.generateSpanId(); - }); - - it('returns 16 character hex strings', () => { - assert.ok(spanId1.match(/[a-f0-9]{16}/)); - assert.ok(!spanId1.match(/^0+$/)); - }); - - it('returns different ids on each call', () => { - assert.notDeepStrictEqual(spanId1, spanId2); - }); -}); diff --git a/packages/opentelemetry-core/test/propagation/composite.test.ts b/packages/opentelemetry-core/test/propagation/composite.test.ts index a2bad6f5dcc..7dfa8f7a328 100644 --- a/packages/opentelemetry-core/test/propagation/composite.test.ts +++ b/packages/opentelemetry-core/test/propagation/composite.test.ts @@ -25,11 +25,7 @@ import { } from '@opentelemetry/api'; import { Context, ROOT_CONTEXT } from '@opentelemetry/api'; import * as assert from 'assert'; -import { - CompositePropagator, - W3CTraceContextPropagator, - RandomIdGenerator, -} from '../../src'; +import { CompositePropagator, W3CTraceContextPropagator } from '../../src'; import { TRACE_PARENT_HEADER, TRACE_STATE_HEADER, @@ -52,14 +48,8 @@ class DummyPropagator implements TextMapPropagator { } describe('Composite Propagator', () => { - let traceId: string; - let spanId: string; - - beforeEach(() => { - const idGenerator = new RandomIdGenerator(); - traceId = idGenerator.generateTraceId(); - spanId = idGenerator.generateSpanId(); - }); + const traceId = 'd4cda95b652f4a1592b449d5929fda1b'; + const spanId = '6e0c63257de34c92'; describe('inject', () => { let carrier: { [key: string]: unknown };