forked from open-telemetry/opentelemetry-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Make ID generator configurable (open-telemetry#1331)
Co-authored-by: Daniel Dyla <[email protected]>
- Loading branch information
1 parent
0a0a487
commit 802c5ad
Showing
12 changed files
with
144 additions
and
95 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
64 changes: 64 additions & 0 deletions
64
packages/opentelemetry-core/src/platform/browser/RandomIdGenerator.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,64 @@ | ||
/* | ||
* 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'; | ||
|
||
type WindowWithMsCrypto = Window & { | ||
msCrypto?: Crypto; | ||
}; | ||
const cryptoLib = window.crypto || (window as WindowWithMsCrypto).msCrypto; | ||
const SPAN_ID_BYTES = 8; | ||
const TRACE_ID_BYTES = 16; | ||
const randomBytesArray = new Uint8Array(TRACE_ID_BYTES); | ||
|
||
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(): string { | ||
cryptoLib.getRandomValues(randomBytesArray); | ||
return this.toHex(randomBytesArray.slice(0, TRACE_ID_BYTES)); | ||
} | ||
|
||
/** | ||
* Returns a random 8-byte span ID formatted/encoded as a 16 lowercase hex | ||
* characters corresponding to 64 bits. | ||
*/ | ||
generateSpanId(): string { | ||
cryptoLib.getRandomValues(randomBytesArray); | ||
return this.toHex(randomBytesArray.slice(0, SPAN_ID_BYTES)); | ||
} | ||
|
||
/** | ||
* Get the hex string representation of a byte array | ||
* | ||
* @param byteArray | ||
*/ | ||
private toHex(byteArray: Uint8Array) { | ||
const chars: number[] = new Array(byteArray.length * 2); | ||
const alpha = 'a'.charCodeAt(0) - 10; | ||
const digit = '0'.charCodeAt(0); | ||
|
||
let p = 0; | ||
for (let i = 0; i < byteArray.length; i++) { | ||
let nibble = (byteArray[i] >>> 4) & 0xf; | ||
chars[p++] = nibble > 9 ? nibble + alpha : nibble + digit; | ||
nibble = byteArray[i] & 0xf; | ||
chars[p++] = nibble > 9 ? nibble + alpha : nibble + digit; | ||
} | ||
return String.fromCharCode.apply(null, chars); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/** 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; | ||
} |
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
2 changes: 1 addition & 1 deletion
2
packages/opentelemetry-exporter-collector/src/platform/node/protos
Submodule protos
updated
9 files
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