diff --git a/src/__tests__/index.js b/src/__tests__/index.js index b5e16e4..6a0f1fe 100644 --- a/src/__tests__/index.js +++ b/src/__tests__/index.js @@ -263,6 +263,29 @@ describe("Opentracing interface", () => { expect(zipkinTracer.recordBinary).toHaveBeenCalledWith("data", "42"); }); + it("should log all data in string format", () => { + const data = { user: "johndoe" }; + const id = 345; + span.log({ event: "data_received", data, id }); + + // should do it in a scope + expect(zipkinTracer.scoped).toHaveBeenCalled(); + zipkinTracer.scoped.mock.calls[0][0](); + + expect(zipkinTracer.recordBinary).toHaveBeenCalledWith( + "event", + "data_received" + ); + expect(zipkinTracer.recordBinary).toHaveBeenCalledWith( + "data", + JSON.stringify(data) + ); + expect(zipkinTracer.recordBinary).toHaveBeenCalledWith( + "id", + JSON.stringify(id) + ); + }); + it("should use the right id in log", () => { const otherSpan = tracer.startSpan("Other Span", { kind: "client" diff --git a/src/index.js b/src/index.js index aa88880..13dbc1e 100644 --- a/src/index.js +++ b/src/index.js @@ -132,7 +132,10 @@ function SpanCreator({ tracer, serviceName, kind }) { tracer.setId(this.id); Object.entries(obj).map(([key, value]) => { - tracer.recordBinary(key, value); + tracer.recordBinary( + key, + typeof value !== "string" ? JSON.stringify(value) : value + ); }); }); } @@ -294,6 +297,7 @@ class Tracing { } } + // These values should match https://github.com/opentracing/opentracing-javascript/blob/master/src/constants.ts Tracing.FORMAT_TEXT_MAP = FORMAT_TEXT_MAP; Tracing.FORMAT_HTTP_HEADERS = FORMAT_HTTP_HEADERS;