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

add legacy mode to jaeger tracing exporter to support 128bit trace ids #1314

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
"author": "MoleculerJS",
"license": "MIT",
"devDependencies": {
"@icebob/node-memwatch": "^2.1.0",
"@sinonjs/fake-timers": "^9.1.2",
"@types/bunyan": "^1.8.11",
"@types/ioredis": "^4.28.10",
Expand Down
33 changes: 26 additions & 7 deletions src/tracing/exporters/jaeger.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* moleculer
* Copyright (c) 2020 MoleculerJS (https://github.com/moleculerjs/moleculer)
* Copyright (c) 2025 MoleculerJS (https://github.com/moleculerjs/moleculer)
* MIT Licensed
*/

Expand Down 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
Loading