Skip to content

Commit

Permalink
chore: add legacy mode to jaeger tracing exporter to support 128bit (…
Browse files Browse the repository at this point in the history
…32 digits) trace ids
  • Loading branch information
Jan Kühnlein committed Nov 27, 2024
1 parent 4014a8c commit c71ce76
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 15 deletions.
31 changes: 25 additions & 6 deletions src/tracing/exporters/jaeger.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ class JaegerTraceExporter extends BaseTraceExporter {
tracerOptions: {},

/** @type {Object?} Default span tags */
defaultTags: null
defaultTags: null,

/** @type {boolean?} Use legacy mode with 64 bit trace ids*/
legacyMode: true
});

this.tracers = {};
Expand Down Expand Up @@ -199,8 +202,8 @@ class JaegerTraceExporter extends BaseTraceExporter {
let parentCtx;
if (span.parentID) {
parentCtx = new Jaeger.SpanContext(
this.convertID(span.traceID), // traceId,
this.convertID(span.parentID), // spanId,
this.convertTraceID(span.traceID), // traceId,
this.convertSpanID(span.parentID), // spanId,
null, // parentId,
null, // traceIdStr
null, // spanIdStr
Expand Down Expand Up @@ -235,8 +238,8 @@ class JaegerTraceExporter extends BaseTraceExporter {
);

const sc = jaegerSpan.context();
sc.traceId = this.convertID(span.traceID);
sc.spanId = this.convertID(span.id);
sc.traceId = this.convertTraceID(span.traceID);
sc.spanId = this.convertSpanID(span.id);

if (span.error) {
this.addTags(jaegerSpan, Jaeger.opentracing.Tags.ERROR, true);
Expand Down Expand Up @@ -291,11 +294,27 @@ class JaegerTraceExporter extends BaseTraceExporter {
* @param {String} id
* @returns {String}
*/
convertID(id) {
convertSpanID(id) {
if (id) return Buffer.from(id.replace(/-/g, "").substring(0, 16), "hex");

return null;
}

/**
* Convert Trace ID to Jaeger format. Return 128 bit IDs or 64 bit IDs if legacy is set.
*
* @param {String} id
* @returns {String}
*/
convertTraceID(id) {
if (id)
return Buffer.from(
id.replace(/-/g, "").substring(0, this.opts.legacyMode ? 16 : 32),
"hex"
);

return null;
}
}

module.exports = JaegerTraceExporter;
69 changes: 60 additions & 9 deletions test/unit/tracing/exporters/jaeger.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ describe("Test Jaeger tracing exporter class", () => {
options: {}
},
tracerOptions: {},
legacyMode: true,
defaultTags: null
});

Expand Down Expand Up @@ -84,6 +85,7 @@ describe("Test Jaeger tracing exporter class", () => {
safetyTags: false,
endpoint: "http://jaeger:9411",
host: "127.0.0.1",
legacyMode: true,
port: 6832,
sampler: {
type: "Const",
Expand Down Expand Up @@ -481,22 +483,71 @@ describe("Test Jaeger tracing exporter class", () => {
});
});

describe("Test convertID method", () => {
describe("Test convert ids", () => {
const fakeTracer = { broker, logger: broker.logger };
const exporter = new JaegerTraceExporter({});
const legacyExporter = new JaegerTraceExporter({});
legacyExporter.init(fakeTracer);
const exporter = new JaegerTraceExporter({ legacyMode: false });
exporter.init(fakeTracer);

it("should truncate ID", () => {
expect(exporter.convertID()).toBeNull();
expect(exporter.convertID("")).toBeNull();
expect(exporter.convertID("12345678")).toEqual(Buffer.from([18, 52, 86, 120]));
expect(exporter.convertID("123456789-0123456")).toEqual(
it("should truncate trace ID to 64 bit in legacy mode", () => {
expect(legacyExporter.convertTraceID()).toBeNull();
expect(legacyExporter.convertTraceID("")).toBeNull();
expect(legacyExporter.convertTraceID("12345678")).toEqual(
Buffer.from([18, 52, 86, 120])
);
expect(legacyExporter.convertTraceID("123456789-0123456")).toEqual(
Buffer.from([18, 52, 86, 120, 144, 18, 52, 86])
);
expect(legacyExporter.convertTraceID("123456789-0123456789-abcdef")).toEqual(
Buffer.from([18, 52, 86, 120, 144, 18, 52, 86])
);
expect(exporter.convertID("123456789-0123456789-abcdef")).toEqual(
expect(legacyExporter.convertTraceID("abcdef")).toEqual(Buffer.from([171, 205, 239]));
});

it("should truncate trace ID to 128bit in non legacy mode", () => {
expect(exporter.convertTraceID()).toBeNull();
expect(exporter.convertTraceID("")).toBeNull();
expect(exporter.convertTraceID("12345678")).toEqual(Buffer.from([18, 52, 86, 120]));
expect(exporter.convertTraceID("123456789-0123456")).toEqual(
Buffer.from([18, 52, 86, 120, 144, 18, 52, 86])
);
expect(exporter.convertID("abcdef")).toEqual(Buffer.from([171, 205, 239]));
expect(exporter.convertTraceID("123456789-0123456789-abcdef-abcdef-abcdef")).toEqual(
Buffer.from([
18, 52, 86, 120, 144, 18, 52, 86, 120, 154, 188, 222, 250, 188, 222, 250
])
);
expect(exporter.convertTraceID("abcdef")).toEqual(Buffer.from([171, 205, 239]));
});

it("should truncate span ID to 64 bit", () => {
expect(legacyExporter.convertSpanID()).toBeNull();
expect(exporter.convertSpanID()).toBeNull();

expect(legacyExporter.convertSpanID("")).toBeNull();
expect(exporter.convertSpanID("")).toBeNull();

expect(legacyExporter.convertSpanID("12345678")).toEqual(
Buffer.from([18, 52, 86, 120])
);
expect(exporter.convertSpanID("12345678")).toEqual(Buffer.from([18, 52, 86, 120]));

expect(legacyExporter.convertSpanID("123456789-0123456")).toEqual(
Buffer.from([18, 52, 86, 120, 144, 18, 52, 86])
);
expect(exporter.convertSpanID("123456789-0123456")).toEqual(
Buffer.from([18, 52, 86, 120, 144, 18, 52, 86])
);

expect(legacyExporter.convertSpanID("123456789-0123456789-abcdef")).toEqual(
Buffer.from([18, 52, 86, 120, 144, 18, 52, 86])
);
expect(exporter.convertSpanID("123456789-0123456")).toEqual(
Buffer.from([18, 52, 86, 120, 144, 18, 52, 86])
);

expect(legacyExporter.convertSpanID("abcdef")).toEqual(Buffer.from([171, 205, 239]));
expect(exporter.convertSpanID("abcdef")).toEqual(Buffer.from([171, 205, 239]));
});
});

Expand Down

0 comments on commit c71ce76

Please sign in to comment.