Skip to content

Commit

Permalink
Bunyan error not sent correctly (#915)
Browse files Browse the repository at this point in the history
* Bunyan error not sent correctly

* package.-lock
  • Loading branch information
hectorhdzg authored Feb 17, 2022
1 parent 811975d commit cc3c2e7
Show file tree
Hide file tree
Showing 4 changed files with 1,031 additions and 715 deletions.
5 changes: 4 additions & 1 deletion AutoCollection/diagnostic-channel/bunyan.sub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ const subscriber = (event: IStandardEvent<bunyan.IBunyanData>) => {
// Try to parse message as Bunyan log is JSON
let log: any = JSON.parse(message);
if (log.err) {
client.trackException({ exception: log.err });
let bunyanError = new Error(log.err.message);
bunyanError.name = log.err.name;
bunyanError.stack = log.err.stack;
client.trackException({ exception: bunyanError });
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Library/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class Util {
* Check if an object is of type Error
*/
public static isError(obj: any): boolean {
return obj instanceof Error;
return Object.prototype.toString.call(obj) === "[object Error]";
}

public static isPrimitive(input: any): boolean {
Expand Down
48 changes: 33 additions & 15 deletions Tests/AutoCollection/bunyan.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,23 @@ import { bunyan } from "diagnostic-channel-publishers";
import Util = require("../../Library/Util");

describe("diagnostic-channel/bunyan", () => {
var sandbox: sinon.SinonSandbox;

before(() => {
sandbox = sinon.sandbox.create();
});

afterEach(() => {
AppInsights.dispose();
sandbox.restore();
disable();
});
it("should call trackException for errors, trackTrace for logs", () => {
it("should call trackException for errors", () => {
AppInsights.setup("1aa11111-bbbb-1ccc-8ddd-eeeeffff3333");
AppInsights.start();

const trackExceptionStub = sinon.stub(AppInsights.defaultClient, "trackException");
const trackTraceStub = sinon.stub(AppInsights.defaultClient, "trackTrace");
const trackExceptionSpy = sandbox.spy(AppInsights.defaultClient, "trackException");
const trackStub = sandbox.stub(AppInsights.defaultClient, "track");

disable();
enable(true, AppInsights.defaultClient);
Expand All @@ -25,26 +32,37 @@ describe("diagnostic-channel/bunyan", () => {
level: 50 // Error should still log as MessageData
};

const dummyError = { stack: "Test error" };
const dummyError = { message: "Test Message", name: "Test Name", stack: "Test Stack" };
const bunyanJson = Util.stringify({ err: dummyError });
const errorEvent: bunyan.IBunyanData = {
result: bunyanJson,
level: 10, // Verbose should still log as ExceptionData
};
channel.publish("bunyan", errorEvent);
assert.ok(trackExceptionSpy.calledOnce);
assert.deepEqual(trackExceptionSpy.args[0][0].exception.message, dummyError.message);
assert.deepEqual(trackExceptionSpy.args[0][0].exception.name, dummyError.name);
assert.deepEqual(trackExceptionSpy.args[0][0].exception.stack, dummyError.stack);

assert.ok(trackStub.calledOnce);
// No new error is created as valid one was provided
assert.deepEqual(trackStub.args[0][0].exception.message, dummyError.message);
assert.deepEqual(trackStub.args[0][0].exception.name, dummyError.name);
assert.deepEqual(trackStub.args[0][0].exception.stack, dummyError.stack);
});

it("should call trackTrace for logs", () => {
AppInsights.setup("1aa11111-bbbb-1ccc-8ddd-eeeeffff3333");
AppInsights.start();
const trackTraceStub = sandbox.stub(AppInsights.defaultClient, "trackTrace");
disable();
enable(true, AppInsights.defaultClient);
const logEvent: bunyan.IBunyanData = {
result: "test log",
level: 50 // Error should still log as MessageData
};
channel.publish("bunyan", logEvent);
assert.ok(trackExceptionStub.notCalled);
assert.ok(trackTraceStub.calledOnce);
assert.deepEqual(trackTraceStub.args[0][0].message, "test log");
trackExceptionStub.reset();
trackTraceStub.reset();

channel.publish("bunyan", errorEvent);
assert.ok(trackExceptionStub.calledOnce);
assert.ok(trackTraceStub.notCalled);
assert.deepEqual(trackExceptionStub.args[0][0].exception, dummyError);

trackExceptionStub.restore();
trackTraceStub.restore();
});
});
Loading

0 comments on commit cc3c2e7

Please sign in to comment.