Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit d8462a2

Browse files
jflorenciokjin
authored andcommitted
fix: Fix SpanId generation for interoperability (#126)
* Fix SpanId formatting for interoperability SpanIds in opencensus-node are currently generated, sent in traces, and propagated to other services as numeric values. This isn't consistent with other opencensus libraries (e.g.: [opencensus-java](https://github.com/census-instrumentation/opencensus-java/blob/master/api/src/main/java/io/opencensus/trace/SpanId.java) serializes the span id as lowercase base16). When opencensus-node is setup with B3 propagation, this SpanId format violates the [B3 specification](https://github.com/openzipkin/b3-propagation#spanid-1) which mandates lowercase base16 encoding. Generally speaking, opencensus-java only accepts spans in this format so it will fail to correctly link to any span generated by opencensus-node. The best case scenario is a shared trace id with a missing parent span link. This PR removes the integer parsing, and leaves it as lowercase hex characters. I tested locally with B3 propagation where node creates the root span and propagates it to a Java service instrumented with opencensus-java, and verified the data is correctly ingested and correlated in Jaeger. * Convert spanId into a numeric value for stackdriver Using the hex2dec library also used by https://github.com/googleapis/cloud-trace-nodejs/ , so assuming this dependency is okay. * Fix formatting
1 parent 62e3a6c commit d8462a2

File tree

8 files changed

+1520
-972
lines changed

8 files changed

+1520
-972
lines changed

packages/opencensus-core/src/internal/util.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919

2020
import * as crypto from 'crypto';
2121

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

2524
// Use the faster crypto.randomFillSync when available (Node 7+) falling back to
2625
// using crypto.randomBytes.
@@ -32,6 +31,5 @@ const spanRandomBuffer = randomFillSync ?
3231
() => randomBytes(SPAN_ID_RANDOM_BYTES);
3332

3433
export function randomSpanId() {
35-
// tslint:disable-next-line:ban Needed to parse hexadecimal.
36-
return parseInt(spanRandomBuffer().toString('hex'), 16).toString();
37-
}
34+
return spanRandomBuffer().toString('hex');
35+
}

packages/opencensus-propagation-b3/src/b3-format.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ export class B3Format implements Propagation {
7676
generate(): SpanContext {
7777
return {
7878
traceId: uuid.v4().split('-').join(''),
79-
// tslint:disable-next-line:ban Needed to parse hexadecimal.
80-
spanId: parseInt(crypto.randomBytes(6).toString('hex'), 16).toString(),
79+
spanId: crypto.randomBytes(8).toString('hex'),
8180
options: SAMPLED_VALUE
8281
} as SpanContext;
8382
}

packages/opencensus-propagation-jaeger/src/jaeger-format.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ export class JaegerFormat implements Propagation {
9797
generate(): SpanContext {
9898
return {
9999
traceId: uuid.v4().split('-').join(''),
100-
// tslint:disable-next-line:ban Needed to parse hexadecimal.
101-
spanId: parseInt(crypto.randomBytes(6).toString('hex'), 16).toString(),
100+
spanId: crypto.randomBytes(8).toString('hex'),
102101
options: SAMPLED_VALUE
103102
} as SpanContext;
104103
}

0 commit comments

Comments
 (0)