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

Change span ID to use random bytes #654

Merged
merged 12 commits into from
Jan 20, 2018
18 changes: 4 additions & 14 deletions src/span-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import * as crypto from 'crypto';
import * as util from 'util';

import {Constants} from './constants';
Expand Down Expand Up @@ -43,21 +42,12 @@ interface StackFrame {
column_number?: number;
}

// Use 6 bytes of randomness only as JS numbers are doubles not 64-bit ints.
const SPAN_ID_RANDOM_BYTES = 6;

// Use the faster crypto.randomFillSync when available (Node 7+) falling back to
// using crypto.randomBytes.
const spanIdBuffer = Buffer.alloc(SPAN_ID_RANDOM_BYTES);
const randomFillSync = crypto.randomFillSync;
const randomBytes = crypto.randomBytes;
const spanRandomBuffer = randomFillSync ?
() => randomFillSync(spanIdBuffer) :
() => randomBytes(SPAN_ID_RANDOM_BYTES);
// This evaluates to a large whole number such that any fractional number
// between 0 and 1 multiplied by it will always give a whole number.

This comment was marked as spam.

This comment was marked as spam.

const SPAN_ID_MAX = (1 << 30) * (1 << 30);

function randomSpanId() {
// tslint:disable-next-line:ban Needed to parse hexadecimal.
return parseInt(spanRandomBuffer().toString('hex'), 16).toString();
return String(Math.random() * SPAN_ID_MAX);

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

}

export class SpanData implements SpanDataInterface {
Expand Down
2 changes: 1 addition & 1 deletion test/test-span-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('SpanData', function() {
assert.ok(typeof spanId === 'string');
assert.ok(spanId.match(/\d+/));
assert.ok(Number(spanId) > 0);
assert.equal(Number(spanId).toString(), spanId);
assert.strictEqual(Number(spanId).toString(), spanId);
}
});
});
Expand Down
18 changes: 9 additions & 9 deletions test/test-trace-header-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,19 @@ describe('test-trace-header-context', function() {
var receivedSpanId = receivedSpanIdAndOptions[0];
var receivedTraceOptions = receivedSpanIdAndOptions[1];
// Trace ID and trace options should be the same in sender and receiver.
assert.equal(receivedTraceId, sentTraceId);
assert.equal(receivedTraceOptions, sentTraceOptions);
assert.strictEqual(receivedTraceId, sentTraceId);
assert.strictEqual(receivedTraceOptions, sentTraceOptions);
// Span ID should be different as receiver generates a new span ID.
assert.notEqual(receivedSpanId, sentSpanId);
assert.notStrictEqual(receivedSpanId, sentSpanId);

res.send(common.serverRes);
var traces = common.getTraces();
assert.equal(traces.length, 2);
assert.equal(traces[0].spans.length, 2);
assert.equal(traces[1].spans.length, 1);
assert.equal(traces[0].spans[0].name, '/');
assert.equal(traces[0].spans[1].name, 'localhost');
assert.equal(traces[1].spans[0].name, '/self');
assert.strictEqual(traces.length, 2);
assert.strictEqual(traces[0].spans.length, 2);
assert.strictEqual(traces[1].spans.length, 1);
assert.strictEqual(traces[0].spans[0].name, '/');
assert.strictEqual(traces[0].spans[1].name, 'localhost');
assert.strictEqual(traces[1].spans[0].name, '/self');
common.cleanTraces();
server.close();
done();
Expand Down