Skip to content
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.

Commit

Permalink
Fix issues
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Biccherai <paul.biccherai@chronwell.com>
  • Loading branch information
PaulMiami committed Jun 24, 2019
1 parent 4ec48e6 commit a00710e
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 54 deletions.
1 change: 0 additions & 1 deletion src/_flow/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ declare type startSpanOptions = {
references?: Array<Reference>,
tags?: any,
startTime?: number,
traceId128bit?: boolean,
};
4 changes: 3 additions & 1 deletion src/span_context.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ export default class SpanContext {

get traceId(): any {
if (this._traceId == null && this._traceIdStr != null) {
// make sure that the HEX has an even number of digits, node is expecting 2 HEX character per byte
// https://github.com/nodejs/node/issues/21242
const safeTraceIdStr = this._traceIdStr.length % 2 == 0 ? this._traceIdStr : '0' + this._traceIdStr;
const tmpBuffer = Utils.newBuffer(safeTraceIdStr, 'hex');
const tmpBuffer = Utils.newBufferFromHex(safeTraceIdStr);
const size = tmpBuffer.length > 8 ? 16 : 8;
this._traceId = Utils.newBuffer(size);
tmpBuffer.copy(this._traceId, size - tmpBuffer.length);
Expand Down
17 changes: 11 additions & 6 deletions src/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default class Tracer {
_baggageSetter: BaggageSetter;
_debugThrottler: Throttler & ProcessSetter;
_process: Process;
_traceId128bit: boolean;

/**
* @param {String} [serviceName] - name of the current service or application.
Expand All @@ -56,6 +57,7 @@ export default class Tracer {
* @param {Object} [options.baggageRestrictionManager] - a baggageRestrictionManager matching
* @param {Object} [options.contextKey] - a name of the key to extract/inject context from headers
* @param {Object} [options.baggagePrefix] - a name of the context baggage key prefix
* @param {boolean} [options.traceId128bit] - generate root span with a 128bit traceId.
* BaggageRestrictionManager API from ./baggage.js.
*/
constructor(
Expand Down Expand Up @@ -115,6 +117,8 @@ export default class Tracer {
this._debugThrottler.setProcess(this._process);
// TODO update reporter to implement ProcessSetter
this._reporter.setProcess(this._process.serviceName, this._process.tags);

this._traceId128bit = options.traceId128bit;
}

_startInternalSpan(
Expand Down Expand Up @@ -188,8 +192,6 @@ export default class Tracer {
* the created Span object. The time should be specified in
* milliseconds as Unix timestamp. Decimal value are supported
* to represent time values with sub-millisecond accuracy.
* @param {boolean} [options.traceId128bit] - generate root span with a
* 128bit traceId.
* @return {Span} - a new Span object.
**/
startSpan(operationName: string, options: ?startSpanOptions): Span {
Expand Down Expand Up @@ -226,7 +228,6 @@ export default class Tracer {
let ctx: SpanContext = new SpanContext();
let internalTags: any = {};
if (!parent || !parent.isValid) {
let randomId = Utils.getRandom64();
let flags = 0;
if (this._sampler.isSampled(operationName, internalTags)) {
flags |= constants.SAMPLED_MASK;
Expand All @@ -241,12 +242,16 @@ export default class Tracer {
ctx.baggage = parent.baggage;
}

if (options.traceId128bit) {
ctx.traceId = Buffer.concat([Utils.getRandom64(), randomId]);
if (this._traceId128bit) {
let randomId = Utils.getRandom128();
ctx.traceId = randomId;
ctx.spanId = randomId.slice(-8);
} else {
let randomId = Utils.getRandom64();
ctx.traceId = randomId;
ctx.spanId = randomId;
}
ctx.spanId = randomId;

ctx.parentId = null;
ctx.flags = flags;
} else {
Expand Down
69 changes: 38 additions & 31 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,6 @@ import Int64 from 'node-int64';
import os from 'os';
import http from 'http';

type BufferEncoding =
| 'ascii'
| 'utf8'
| 'utf-8'
| 'utf16le'
| 'ucs2'
| 'ucs-2'
| 'base64'
| 'latin1'
| 'binary'
| 'hex';
export default class Utils {
/**
* Determines whether a string contains a given prefix.
Expand All @@ -53,7 +42,7 @@ export default class Utils {
}

/**
* Determines whether a string contains a given prefix.
* Get a random buffer representing a random 64 bit.
*
* @return {Buffer} - returns a buffer representing a random 64 bit
* number.
Expand All @@ -66,6 +55,23 @@ export default class Utils {
return buf;
}

/**
* Get a random buffer representing a random 128 bit.
*
* @return {Buffer} - returns a buffer representing a random 128 bit
* number.
**/
static getRandom128(): Buffer {
let randint1 = xorshift.randomint();
let randint2 = xorshift.randomint();
let buf = new Buffer(16);
buf.writeUInt32BE(randint1[0], 0);
buf.writeUInt32BE(randint1[1], 4);
buf.writeUInt32BE(randint2[0], 8);
buf.writeUInt32BE(randint2[1], 12);
return buf;
}

/**
* @param {string|number} numberValue - a string or number to be encoded
* as a 64 bit byte array.
Expand Down Expand Up @@ -161,26 +167,27 @@ export default class Utils {
}

/**
* @param {string|number} input - a string to store in the buffer
* or a number of octets to allocate.
* @param {string} encoding - identifies the character encoding. Default is 'utf8'.
* @return {Buffer} - returns a buffer representing the encoded string, or an empty buffer.
* @param {string|number} input - a hex encoded string to store in the buffer.
* @return {Buffer} - returns a buffer representing the hex encoded string.
**/
static newBuffer(input: string | number, encoding?: BufferEncoding): Buffer {
if (typeof input === 'string') {
if (Buffer.from && Buffer.from !== Uint8Array.from) {
return Buffer.from(input, encoding);
}
return new Buffer(input, encoding);
} else if (typeof input === 'number') {
if (Buffer.alloc) {
return Buffer.alloc(input);
}
const buffer = new Buffer(input);
buffer.fill(0);
return buffer;
} else {
throw new Error('The "input" argument must be a number or a string');
static newBufferFromHex(input: string): Buffer {
const encoding = 'hex';
if (Buffer.from && Buffer.from !== Uint8Array.from) {
return Buffer.from(input, encoding);
}
return new Buffer(input, encoding);
}

/**
* @param {number} input - a number of octets to allocate.
* @return {Buffer} - returns an empty buffer.
**/
static newBuffer(size: number): Buffer {
if (Buffer.alloc) {
return Buffer.alloc(size);
}
const buffer = new Buffer(size);
buffer.fill(0);
return buffer;
}
}
4 changes: 2 additions & 2 deletions test/thrift.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ describe('ThriftUtils', () => {

it('should convert span with 128 bit traceId', () => {
let reporter = new InMemoryReporter();
let tracer = new Tracer('test-service-name', reporter, new ConstSampler(true));
let span = tracer.startSpan('some operation', { traceId128bit: true });
let tracer = new Tracer('test-service-name', reporter, new ConstSampler(true), { traceId128bit: true });
let span = tracer.startSpan('some operation');
let childOfRef = new opentracing.Reference(opentracing.REFERENCE_CHILD_OF, span.context());
let childSpan = tracer.startSpan('some child operation', { references: [childOfRef] });
childSpan.finish();
Expand Down
5 changes: 2 additions & 3 deletions test/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,9 +451,8 @@ describe('tracer should', () => {
});

it('start a root span with 128 bit traceId', () => {
let span = tracer.startSpan('test-name', {
traceId128bit: true,
});
tracer = new Tracer('test-service-name', reporter, new ConstSampler(true), { traceId128bit: true });
let span = tracer.startSpan('test-name');

assert.deepEqual(span.context().traceId.slice(-8), span.context().spanId);
assert.isOk(span.context().traceId);
Expand Down
14 changes: 4 additions & 10 deletions test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,10 @@ describe('utils', () => {
assert.deepEqual(new Buffer([0, 0, 0, 0, 0, 0, 0, 0]), results);
});

it('should create new buffer from text', () => {
let expectedValue = 'test';
let results = Utils.newBuffer(expectedValue, 'utf-8');
it('should create new buffer from hex', () => {
let expectedValue = 'deadbeef';
let results = Utils.newBufferFromHex(expectedValue);
assert.isNotNull(results);
assert.equal(expectedValue, results.toString('utf-8'));
});

it('should fail to create new buffer', () => {
assert.throw(() => {
Utils.newBuffer((undefined: any));
}, 'The "input" argument must be a number or a string');
assert.equal(expectedValue, results.toString('hex'));
});
});

0 comments on commit a00710e

Please sign in to comment.