From d67cb8c03cb32be501c8c32c6daf2a0c5c269fed Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Tue, 18 Feb 2020 11:05:37 -0500 Subject: [PATCH] fix: left pad jaeger trace ids (#743) --- .../src/transform.ts | 5 +-- .../test/transform.test.ts | 32 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/packages/opentelemetry-exporter-jaeger/src/transform.ts b/packages/opentelemetry-exporter-jaeger/src/transform.ts index 0ce2338497d..5ed607e4534 100644 --- a/packages/opentelemetry-exporter-jaeger/src/transform.ts +++ b/packages/opentelemetry-exporter-jaeger/src/transform.ts @@ -40,8 +40,9 @@ const DEFAULT_FLAGS = 0x1; * @param span Span to be translated */ export function spanToThrift(span: ReadableSpan): ThriftSpan { - const traceIdHigh = span.spanContext.traceId.slice(0, 16); - const traceIdLow = span.spanContext.traceId.slice(16); + const traceId = span.spanContext.traceId.padStart(32, '0'); + const traceIdHigh = traceId.slice(0, 16); + const traceIdLow = traceId.slice(16); const parentSpan = span.parentSpanId ? Utils.encodeInt64(span.parentSpanId) : ThriftUtils.emptyBuffer; diff --git a/packages/opentelemetry-exporter-jaeger/test/transform.test.ts b/packages/opentelemetry-exporter-jaeger/test/transform.test.ts index 4e81dd724bf..2189b97d4d3 100644 --- a/packages/opentelemetry-exporter-jaeger/test/transform.test.ts +++ b/packages/opentelemetry-exporter-jaeger/test/transform.test.ts @@ -219,5 +219,37 @@ describe('transform', () => { assert.strictEqual(ref1.spanId.toString('hex'), '3e0c63257de34c92'); assert.strictEqual(ref1.refType, ThriftReferenceType.CHILD_OF); }); + + it('should left pad trace ids', () => { + const readableSpan: ReadableSpan = { + name: 'my-span1', + kind: types.SpanKind.CLIENT, + spanContext: { + traceId: '92b449d5929fda1b', + spanId: '6e0c63257de34c92', + }, + startTime: [1566156729, 709], + endTime: [1566156731, 709], + status: { + code: types.CanonicalCode.DATA_LOSS, + message: 'data loss', + }, + attributes: {}, + links: [], + events: [], + duration: [32, 800000000], + }; + + const thriftSpan = spanToThrift(readableSpan); + + assert.strictEqual( + thriftSpan.traceIdLow.toString('hex'), + '92b449d5929fda1b' + ); + assert.strictEqual( + thriftSpan.traceIdHigh.toString('hex'), + '0000000000000000' + ); + }); }); });