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
Next Next commit
style(context): changed id generator from function to interface
  • Loading branch information
EdZou committed Jul 20, 2020
commit 5c1f4d5601c692839bf92d4b57209d49f6d45219
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,14 @@
* limitations under the License.
*/

import * as crypto from 'crypto';

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.
* IdGenerator provides an interface for generating Trace Id and Span Id
*
* The prototype IdGenerator generates random 16-byte trace ID and 8-byte span ID
EdZou marked this conversation as resolved.
Show resolved Hide resolved
*/
export function randomTraceId(): 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');
export interface IdGenerator {
dyladan marked this conversation as resolved.
Show resolved Hide resolved
GenerateTraceId(): string;
EdZou marked this conversation as resolved.
Show resolved Hide resolved

GenerateSpanId(): string;
}
52 changes: 52 additions & 0 deletions packages/opentelemetry-core/src/platform/node/RandomIdGenerator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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;

export class RandomIdGenerator implements api.IdGenerator {
private SpanIdBytes: number = SPAN_ID_BYTES;
private TraceIdBytes: number = TRACE_ID_BYTES;

// In case if user would like to adjust the length of ID
EdZou marked this conversation as resolved.
Show resolved Hide resolved
constructor(Span_Id_Bytes?: number, Trace_Id_Bytes?: number) {
if (Span_Id_Bytes) {
this.SpanIdBytes = Span_Id_Bytes;
}
if (Trace_Id_Bytes) {
this.TraceIdBytes = Trace_Id_Bytes;
}
}

/**
* Returns a random 16-byte trace ID formatted/encoded as a 32 lowercase hex
* characters corresponding to 128 bits.
*/
GenerateTraceId(): string {
return crypto.randomBytes(this.TraceIdBytes).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(this.SpanIdBytes).toString('hex');
}
}
2 changes: 1 addition & 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,7 @@
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';
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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();
const IdGeneratorWithParameters = new RandomIdGenerator(8, 16);

describe('IdGeneratorConstructor', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Once you remove the length parameters, make sure this is mostly just copy-paste from previous tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I actually added beforeEach() keyword to separate obtaining value part and testing value part. Is that good or not?

it('Default provides 16-byte TraceId and 8-byte SpanId', () => {
assert.equal(
IdGenerator.GenerateTraceId().length,
IdGeneratorWithParameters.GenerateTraceId().length
);
assert.equal(
IdGenerator.GenerateSpanId().length,
IdGeneratorWithParameters.GenerateSpanId().length
);
});
});

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);
});
});
48 changes: 0 additions & 48 deletions packages/opentelemetry-core/test/platform/id.test.ts

This file was deleted.