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 7 commits
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
1 change: 1 addition & 0 deletions packages/opentelemetry-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ 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
23 changes: 23 additions & 0 deletions packages/opentelemetry-api/src/platform/node/IdGenerator.ts
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 {
dyladan marked this conversation as resolved.
Show resolved Hide resolved
/** Returns a trace ID composed of 32 lowercase hex characters. */
generateTraceId(): string;
/** Returns a span ID composed of 16 lowercase hex characters. */
generateSpanId(): string;
}
1 change: 1 addition & 0 deletions packages/opentelemetry-api/src/platform/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
*/

export * from './globalThis';
export * from './IdGenerator';
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file needs to be in contrib, not this repo / package. https://github.com/open-telemetry/opentelemetry-js-contrib.

You can leave it out of this PR and we'll add it there later.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definitely does not belong in core. I'm not sure it belongs in contrib either though. We have been keeping proprietary vendor code out of our repositories across all of otel AFAIK. Dis something change?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Java we have them in the main repo

https://github.com/open-telemetry/opentelemetry-java/tree/master/sdk_extensions/aws_v1_support

From what I understand currently this is being left up to the language sigs. So if JS says separate repo we'll do that :)

* 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 crypto from 'crypto';
import * as api from '@opentelemetry/api';

const SPAN_ID_BYTES = 8;
const TRACE_ID_BYTES = 16;
const TIME_BYTES = 4;

/** IdGenerator that generates trace IDs conforming to AWS X-Ray format.
* https://docs.aws.amazon.com/xray/latest/devguide/xray-api-sendingdata.html#xray-api-traceids
*/
export class AWSXrayIdGenerator implements api.IdGenerator {
EdZou marked this conversation as resolved.
Show resolved Hide resolved

/**
* Returns a random 16-byte trace ID formatted/encoded as a 32 lowercase hex
* characters corresponding to 128 bits. The first 4 bytes correspond to the current
* time, in seconds, as per X-Ray trace ID format.
*/
generateTraceId(): string {
const nowSec = Math.floor(Date.now() / 1000).toString(16);
return nowSec + crypto.randomBytes(TRACE_ID_BYTES - TIME_BYTES).toString('hex');
}

/**
* Returns a random 8-byte span ID formatted/encoded as a 16 lowercase hex
* characters corresponding to 64 bits.
*/
generateSpanId(): string {
return crypto.randomBytes(SPAN_ID_BYTES).toString('hex');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,26 @@
*/

import * as crypto from 'crypto';
import * as api from '@opentelemetry/api';

const SPAN_ID_BYTES = 8;
const TRACE_ID_BYTES = 16;

/**
* Returns a random 16-byte trace ID formatted/encoded as a 32 lowercase hex
* characters corresponding to 128 bits.
*/
export function randomTraceId(): string {
return crypto.randomBytes(TRACE_ID_BYTES).toString('hex');
}
export class RandomIdGenerator implements api.IdGenerator {

/**
* Returns a random 16-byte trace ID formatted/encoded as a 32 lowercase hex
* characters corresponding to 128 bits.
*/
generateTraceId(): string {
return crypto.randomBytes(TRACE_ID_BYTES).toString('hex');
}

/**
* Returns a random 8-byte span ID formatted/encoded as a 16 lowercase hex
* characters corresponding to 64 bits.
*/
export function randomSpanId(): string {
return crypto.randomBytes(SPAN_ID_BYTES).toString('hex');
/**
* Returns a random 8-byte span ID formatted/encoded as a 16 lowercase hex
* characters corresponding to 64 bits.
*/
generateSpanId(): string {
return crypto.randomBytes(SPAN_ID_BYTES).toString('hex');
}
}
3 changes: 2 additions & 1 deletion packages/opentelemetry-core/src/platform/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
export * from './BasePlugin';
export * from './environment';
export * from './hex-to-base64';
export * from './id';
export * from './RandomIdGenerator';
EdZou marked this conversation as resolved.
Show resolved Hide resolved
export * from './performance';
export * from './sdk-info';
export * from './timer-util';
export * from './AWSXrayIdGenerator';
8 changes: 4 additions & 4 deletions packages/opentelemetry-core/test/context/composite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ import * as assert from 'assert';
import {
CompositePropagator,
HttpTraceContext,
randomSpanId,
randomTraceId,
RandomIdGenerator,
} from '../../src';
import {
getExtractedSpanContext,
Expand All @@ -49,8 +48,9 @@ describe('Composite Propagator', () => {
let spanId: string;

beforeEach(() => {
traceId = randomTraceId();
spanId = randomSpanId();
const idGenerator = new RandomIdGenerator();
traceId = idGenerator.generateTraceId();
spanId = idGenerator.generateSpanId();
});

describe('inject', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
dyladan marked this conversation as resolved.
Show resolved Hide resolved
* 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 { AWSXrayIdGenerator } from '../../src/platform';

const idGenerator = new AWSXrayIdGenerator();

describe('AWSXrayTraceId', () => {
let traceId1: string, traceId2: string;
let prevTime: number, currTime: number, nextTime: number;
beforeEach(() => {
prevTime = Math.floor(Date.now() / 1000);
traceId1 = idGenerator.generateTraceId();
currTime = parseInt(traceId1.substring(0, 8), 16);
nextTime = Math.floor(Date.now() / 1000);
console.log(traceId1.length);
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);
});

it('using current time to encode trace id', () => {
assert.ok(currTime >= prevTime);
assert.ok(currTime <= nextTime);
});
});

describe('AWSXraySpanId', () => {
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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,41 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as assert from 'assert';
import { randomSpanId, randomTraceId } from '../../src/platform';
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', () => {
const traceId = randomTraceId();
assert.ok(traceId.match(/[a-f0-9]{32}/));
assert.ok(!traceId.match(/^0+$/));
assert.ok(traceId1.match(/[a-f0-9]{32}/));
assert.ok(!traceId1.match(/^0+$/));
});

it('returns different ids on each call', () => {
const traceId1 = randomTraceId();
const traceId2 = randomTraceId();

assert.notDeepStrictEqual(traceId1, traceId2);
});
});

describe('randomSpanId', () => {
let spanId1: string, spanId2: string;
beforeEach(() => {
spanId1 = idGenerator.generateSpanId();
spanId2 = idGenerator.generateSpanId();
});

it('returns 16 character hex strings', () => {
const spanId = randomSpanId();
assert.ok(spanId.match(/[a-f0-9]{16}/));
assert.ok(!spanId.match(/^0+$/));
assert.ok(spanId1.match(/[a-f0-9]{16}/));
assert.ok(!spanId1.match(/^0+$/));
});

it('returns different ids on each call', () => {
const spanId1 = randomSpanId();
const spanId2 = randomSpanId();

assert.notDeepStrictEqual(spanId1, spanId2);
});
});
9 changes: 5 additions & 4 deletions packages/opentelemetry-tracing/src/Tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import {
InstrumentationLibrary,
isValid,
NoRecordingSpan,
randomSpanId,
randomTraceId,
RandomIdGenerator,
setActiveSpan,
} from '@opentelemetry/core';
import { Resource } from '@opentelemetry/resources';
Expand All @@ -42,6 +41,7 @@ export class Tracer implements api.Tracer {
readonly resource: Resource;
readonly instrumentationLibrary: InstrumentationLibrary;
readonly logger: api.Logger;
readonly idGenerator: api.IdGenerator;

/**
* Constructs a new Tracer instance.
Expand All @@ -58,6 +58,7 @@ export class Tracer implements api.Tracer {
this.resource = _tracerProvider.resource;
this.instrumentationLibrary = instrumentationLibrary;
this.logger = config.logger || new ConsoleLogger(config.logLevel);
this.idGenerator = config.idGenerator || new RandomIdGenerator();
}

/**
Expand All @@ -70,12 +71,12 @@ export class Tracer implements api.Tracer {
context = api.context.active()
): api.Span {
const parentContext = getParent(options, context);
const spanId = randomSpanId();
const spanId = this.idGenerator.generateSpanId();
let traceId;
let traceState;
if (!parentContext || !isValid(parentContext)) {
// New root span.
traceId = randomTraceId();
traceId = this.idGenerator.generateTraceId();
} else {
// New child span.
traceId = parentContext.traceId;
Expand Down
7 changes: 7 additions & 0 deletions packages/opentelemetry-tracing/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
HttpTextPropagator,
Logger,
Sampler,
IdGenerator,
} from '@opentelemetry/api';
import { LogLevel } from '@opentelemetry/core';
import { ContextManager } from '@opentelemetry/context-base';
Expand Down Expand Up @@ -52,6 +53,12 @@ export interface TracerConfig {

/** Resource associated with trace telemetry */
resource?: Resource;

/**
* Generator of trace and span IDs
* The prototype IdGenerator generates random 16-byte trace ID and 8-byte span ID
EdZou marked this conversation as resolved.
Show resolved Hide resolved
*/
idGenerator?: IdGenerator;
}

/**
Expand Down