Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Make ID generator configurable #1331

Merged
merged 15 commits into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: remove AWS components, move interface of IdGenerator and add it…
…s web version
  • Loading branch information
EdZou committed Jul 22, 2020
commit 6f31180d3c0e81e2ab875659b2c0efa02f483fb0
1 change: 0 additions & 1 deletion packages/opentelemetry-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export * from './trace/trace_flags';
export * from './trace/trace_state';
export * from './trace/tracer_provider';
export * from './trace/tracer';
export * from './platform/node/IdGenerator';

export { Context } from '@opentelemetry/context-base';

Expand Down
1 change: 0 additions & 1 deletion packages/opentelemetry-api/src/platform/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@
*/

export * from './globalThis';
export * from './IdGenerator';
1 change: 1 addition & 0 deletions packages/opentelemetry-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ export * from './trace/sampler/ParentOrElseSampler';
export * from './trace/sampler/ProbabilitySampler';
export * from './trace/spancontext-utils';
export * from './trace/TraceState';
export * from './trace/IdGenerator';
export * from './utils/url';
export * from './utils/wrap';
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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';
EdZou marked this conversation as resolved.
Show resolved Hide resolved

declare type WindowWithMsCrypto = Window & {
EdZou marked this conversation as resolved.
Show resolved Hide resolved
dyladan marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
EdZou marked this conversation as resolved.
Show resolved Hide resolved
56 changes: 0 additions & 56 deletions packages/opentelemetry-core/src/platform/browser/id.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/opentelemetry-core/src/platform/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
export * from './BasePlugin';
export * from './environment';
export * from './hex-to-base64';
export * from './id';
export * from './RandomIdGenerator';
export * from './performance';
export * from './sdk-info';
export * from './timer-util';

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
*/

import * as crypto from 'crypto';
import * as api from '@opentelemetry/api';
import {IdGenerator} from '../../trace/IdGenerator';
EdZou marked this conversation as resolved.
Show resolved Hide resolved

const SPAN_ID_BYTES = 8;
const TRACE_ID_BYTES = 16;

export class RandomIdGenerator implements api.IdGenerator {
export class RandomIdGenerator implements IdGenerator {

/**
* Returns a random 16-byte trace ID formatted/encoded as a 32 lowercase hex
Expand Down
1 change: 0 additions & 1 deletion packages/opentelemetry-core/src/platform/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ export * from './RandomIdGenerator';
export * from './performance';
export * from './sdk-info';
export * from './timer-util';
export * from './AWSXrayIdGenerator';

This file was deleted.

3 changes: 2 additions & 1 deletion packages/opentelemetry-tracing/src/Tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
InstrumentationLibrary,
isValid,
NoRecordingSpan,
IdGenerator,
RandomIdGenerator,
setActiveSpan,
} from '@opentelemetry/core';
Expand All @@ -41,7 +42,7 @@ export class Tracer implements api.Tracer {
readonly resource: Resource;
readonly instrumentationLibrary: InstrumentationLibrary;
readonly logger: api.Logger;
readonly idGenerator: api.IdGenerator;
readonly idGenerator: IdGenerator;
EdZou marked this conversation as resolved.
Show resolved Hide resolved

/**
* Constructs a new Tracer instance.
Expand Down
3 changes: 1 addition & 2 deletions packages/opentelemetry-tracing/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ import {
HttpTextPropagator,
Logger,
Sampler,
IdGenerator,
} from '@opentelemetry/api';
import { LogLevel } from '@opentelemetry/core';
import { LogLevel, IdGenerator } from '@opentelemetry/core';
import { ContextManager } from '@opentelemetry/context-base';
import { Resource } from '@opentelemetry/resources';

Expand Down