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

Better remote calls metrics #78

Merged
merged 2 commits into from
Aug 16, 2017
Merged
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
2 changes: 1 addition & 1 deletion examples/multi-server/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let ServiceBroker = require("../../src/service-broker");

// Create broker
let broker = new ServiceBroker({
namespace: "multi",
//namespace: "multi",
nodeID: process.argv[2] || "client-" + process.pid,
transporter: "NATS",

Expand Down
2 changes: 1 addition & 1 deletion examples/multi-server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let { MoleculerError } = require("../../src/errors");

// Create broker
let broker = new ServiceBroker({
namespace: "multi",
//namespace: "multi",
nodeID: process.argv[2] || "server-" + process.pid,
transporter: "NATS",
logger: console
Expand Down
13 changes: 7 additions & 6 deletions src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Context {
this.action = action;
this.nodeID = null;
this.parentID = null;
this.callerNodeID = null;

this.metrics = false;
this.level = 1;
Expand Down Expand Up @@ -150,7 +151,7 @@ class Context {
requestID: this.requestID,
level: this.level,
startTime: this.startTime,
remoteCall: !!this.nodeID
remoteCall: !!this.callerNodeID
};
if (this.action) {
payload.action = {
Expand All @@ -161,8 +162,8 @@ class Context {
payload.parent = this.parentID;

payload.nodeID = this.broker.nodeID;
if (this.nodeID)
payload.targetNodeID = this.nodeID;
if (this.callerNodeID)
payload.callerNodeID = this.callerNodeID;

this.broker.emit("metrics.trace.span.start", payload);
}
Expand Down Expand Up @@ -192,7 +193,7 @@ class Context {
startTime: this.startTime,
endTime: this.stopTime,
duration: this.duration,
remoteCall: !!this.nodeID,
remoteCall: !!this.callerNodeID,
fromCache: this.cachedResult
};
if (this.action) {
Expand All @@ -204,8 +205,8 @@ class Context {
payload.parent = this.parentID;

payload.nodeID = this.broker.nodeID;
if (this.nodeID)
payload.targetNodeID = this.nodeID;
if (this.callerNodeID)
payload.callerNodeID = this.callerNodeID;

if (error) {
payload.error = {
Expand Down
1 change: 1 addition & 0 deletions src/transit.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ class Transit {
ctx.parentID = payload.parentID;
ctx.level = payload.level;
ctx.metrics = payload.metrics;
ctx.callerNodeID = payload.sender;
ctx.meta = payload.meta;
ctx.setParams(payload.params);

Expand Down
10 changes: 5 additions & 5 deletions test/unit/context.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,22 +185,22 @@ describe("Test _metricStart method", () => {

it("should emit start event", () => {
broker.emit.mockClear();
ctx.nodeID = "remote-node";
ctx.callerNodeID = "remote-node";
ctx._metricStart(true);

expect(ctx.startTime).toBeDefined();
expect(ctx.stopTime).toBeNull();
expect(ctx.duration).toBe(0);

expect(broker.emit).toHaveBeenCalledTimes(1);
expect(broker.emit).toHaveBeenCalledWith("metrics.trace.span.start", {"action": {"name": "users.get"}, "id": ctx.id, "level": 1, "parent": 123, "remoteCall": true, "requestID": "abcdef", "startTime": ctx.startTime, "nodeID": broker.nodeID, "targetNodeID": "remote-node"});
expect(broker.emit).toHaveBeenCalledWith("metrics.trace.span.start", {"action": {"name": "users.get"}, "id": ctx.id, "level": 1, "parent": 123, "remoteCall": true, "requestID": "abcdef", "startTime": ctx.startTime, "nodeID": broker.nodeID, "callerNodeID": "remote-node"});
});
});

describe("Test _metricFinish method", () => {
let broker = new ServiceBroker({ metrics: true });
let ctx = new Context(broker, { name: "users.get" });
ctx.nodeID = "server-2";
ctx.callerNodeID = "server-2";
ctx.parentID = 123;
ctx.metrics = true;
ctx.generateID();
Expand All @@ -218,7 +218,7 @@ describe("Test _metricFinish method", () => {
expect(ctx.duration).toBeGreaterThan(0);

expect(broker.emit).toHaveBeenCalledTimes(1);
expect(broker.emit).toHaveBeenCalledWith("metrics.trace.span.finish", {"action": {"name": "users.get"}, "duration": ctx.duration, "id": ctx.id, "parent": 123, "requestID": ctx.requestID, "startTime": ctx.startTime, "endTime": ctx.stopTime, "fromCache": false, "level": 1, "remoteCall": true, "nodeID": broker.nodeID, "targetNodeID": "server-2"});
expect(broker.emit).toHaveBeenCalledWith("metrics.trace.span.finish", {"action": {"name": "users.get"}, "duration": ctx.duration, "id": ctx.id, "parent": 123, "requestID": ctx.requestID, "startTime": ctx.startTime, "endTime": ctx.stopTime, "fromCache": false, "level": 1, "remoteCall": true, "nodeID": broker.nodeID, "callerNodeID": "server-2"});

resolve();
}, 100);
Expand All @@ -233,7 +233,7 @@ describe("Test _metricFinish method", () => {
expect(ctx.stopTime).toBeGreaterThan(0);

expect(broker.emit).toHaveBeenCalledTimes(1);
expect(broker.emit).toHaveBeenCalledWith("metrics.trace.span.finish", {"action": {"name": "users.get"}, "duration": ctx.duration, "error": { "message": "Some error!", "name": "MoleculerError", "code": 511, "type": "ERR_CUSTOM" }, "id": ctx.id, "parent": 123, "requestID": ctx.requestID, "startTime": ctx.startTime, "endTime": ctx.stopTime, "fromCache": false, "level": 1, "remoteCall": true, "nodeID": broker.nodeID, "targetNodeID": "server-2" });
expect(broker.emit).toHaveBeenCalledWith("metrics.trace.span.finish", {"action": {"name": "users.get"}, "duration": ctx.duration, "error": { "message": "Some error!", "name": "MoleculerError", "code": 511, "type": "ERR_CUSTOM" }, "id": ctx.id, "parent": 123, "requestID": ctx.requestID, "startTime": ctx.startTime, "endTime": ctx.stopTime, "fromCache": false, "level": 1, "remoteCall": true, "nodeID": broker.nodeID, "callerNodeID": "server-2" });

resolve();
});
Expand Down