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
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {IdGenerator} from '../../trace/IdGenerator';
import { IdGenerator } from '../../trace/IdGenerator';

declare type WindowWithMsCrypto = Window & {
msCrypto?: Crypto;
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.
Expand Down Expand Up @@ -53,15 +51,14 @@ export class RandomIdGenerator implements IdGenerator {
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
*/

import * as crypto from 'crypto';
import {IdGenerator} from '../../trace/IdGenerator';
import { IdGenerator } from '../../trace/IdGenerator';

const SPAN_ID_BYTES = 8;
const TRACE_ID_BYTES = 16;

export class RandomIdGenerator implements IdGenerator {

/**
* Returns a random 16-byte trace ID formatted/encoded as a 32 lowercase hex
* characters corresponding to 128 bits.
Expand Down
8 changes: 4 additions & 4 deletions packages/opentelemetry-tracing/src/Tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ export class Tracer implements api.Tracer {
private readonly _defaultAttributes: api.Attributes;
private readonly _sampler: api.Sampler;
private readonly _traceParams: TraceParams;
private readonly _idGenerator: IdGenerator;
readonly resource: Resource;
readonly instrumentationLibrary: InstrumentationLibrary;
readonly logger: api.Logger;
readonly idGenerator: IdGenerator;

/**
* Constructs a new Tracer instance.
Expand All @@ -56,10 +56,10 @@ export class Tracer implements api.Tracer {
this._defaultAttributes = localConfig.defaultAttributes;
this._sampler = localConfig.sampler;
this._traceParams = localConfig.traceParams;
this._idGenerator = config.idGenerator || new RandomIdGenerator();
this.resource = _tracerProvider.resource;
this.instrumentationLibrary = instrumentationLibrary;
this.logger = config.logger || new ConsoleLogger(config.logLevel);
this.idGenerator = config.idGenerator || new RandomIdGenerator();
}

/**
Expand All @@ -72,12 +72,12 @@ export class Tracer implements api.Tracer {
context = api.context.active()
): api.Span {
const parentContext = getParent(options, context);
const spanId = this.idGenerator.generateSpanId();
const spanId = this._idGenerator.generateSpanId();
let traceId;
let traceState;
if (!parentContext || !isValid(parentContext)) {
// New root span.
traceId = this.idGenerator.generateTraceId();
traceId = this._idGenerator.generateTraceId();
} else {
// New child span.
traceId = parentContext.traceId;
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry-tracing/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export interface TracerConfig {
resource?: Resource;

/**
* Generator of trace and span IDs
* Generator of trace and span IDs
* The prototype IdGenerator generates random 16-byte trace ID and 8-byte span ID
*/
idGenerator?: IdGenerator;
Expand Down