Skip to content

Commit

Permalink
Ashish: Add log functionality to tracer
Browse files Browse the repository at this point in the history
  • Loading branch information
ashishmahi committed Aug 29, 2020
1 parent f601099 commit 411c4a0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 24 deletions.
1 change: 1 addition & 0 deletions __mocks__/zipkin.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ module.exports = {
};
}),
recordAnnotation: jest.fn(),
recordMessage: jest.fn(),
recordBinary: jest.fn(),
recordServiceName: jest.fn(),
scoped: jest.fn(cb => process.nextTick(cb)),
Expand Down
13 changes: 3 additions & 10 deletions src/__tests__/e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,7 @@ describe("zipkin-javascript-opentracing", () => {

it("should record logs", () => {
const span = tracer.startSpan("My Span");
span.log({
statusCode: "200",
objectId: "42"
});
span.log("LogMessage");

span.finish();

Expand All @@ -208,13 +205,9 @@ describe("zipkin-javascript-opentracing", () => {
"duration"
])
);
expect(json[0].annotations.length).toBe(2);
expect(json[0].annotations.length).toBe(3);
expect(json[0].annotations[0].endpoint.serviceName).toBe("my service");
expect(json[0].binaryAnnotations.length).toBe(2);
expect(json[0].binaryAnnotations[0].key).toBe("statusCode");
expect(json[0].binaryAnnotations[0].value).toBe("200");
expect(json[0].binaryAnnotations[1].key).toBe("objectId");
expect(json[0].binaryAnnotations[1].value).toBe("42");
expect(json[0].annotations[0].value).toBe("LogMessage");
expect(json[0].name).toBe("my span");
});
});
Expand Down
22 changes: 14 additions & 8 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,17 +256,23 @@ describe("Opentracing interface", () => {
});

it("should log data", () => {
span.log({ event: "data_received", data: "42" });
span.log("data to log");

// 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", "42");
expect(zipkinTracer.recordMessage).toHaveBeenCalledWith("data to log");
});

it("should not log if empty data is passed", () => {
span.log();

// should do it in a scope
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();

expect(zipkinTracer.recordMessage).not.toHaveBeenCalled();
});

it("should use the right id in log", () => {
Expand All @@ -276,7 +282,7 @@ describe("Opentracing interface", () => {

zipkinTracer.scoped.mockReset();
zipkinTracer.setId.mockReset();
span.log({ event: "other event" });
span.log("other event");

// should do it in a scope
expect(zipkinTracer.scoped).toHaveBeenCalled();
Expand All @@ -285,7 +291,7 @@ describe("Opentracing interface", () => {
zipkinTracer.scoped.mockReset();
zipkinTracer.setId.mockReset();

otherSpan.log({ event: "yet another event" });
otherSpan.log("yet another event");
// should do it in a scope
expect(zipkinTracer.scoped).toHaveBeenCalled();
zipkinTracer.scoped.mock.calls[0][0]();
Expand Down
11 changes: 5 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,13 @@ function SpanCreator({ tracer, serviceName, kind }) {
}
}

log(obj = {}) {
log(obj) {
tracer.scoped(() => {
// make sure correct id is set
tracer.setId(this.id);

Object.entries(obj).map(([key, value]) => {
tracer.recordBinary(key, value);
});
if (obj) {
tracer.setId(this.id);
tracer.recordMessage(obj);
}
});
}
setTag(key, value) {
Expand Down

0 comments on commit 411c4a0

Please sign in to comment.