Skip to content

Commit

Permalink
feat: format json affects error logging
Browse files Browse the repository at this point in the history
Specifying `--format=json` will cause errors to be logged in json format. ErrorPolykeyRemote errors also now contain additional metadata about the origin node of the error.

#323
  • Loading branch information
emmacasolin committed Jun 7, 2022
1 parent cd19bba commit f18be47
Show file tree
Hide file tree
Showing 25 changed files with 608 additions and 201 deletions.
112 changes: 111 additions & 1 deletion package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
"ts-node": "^10.4.0",
"tsconfig-paths": "^3.9.0",
"typedoc": "^0.22.15",
"typescript": "^4.5.2"
"typescript": "^4.5.2",
"typescript-cached-transpile": "0.0.6"
}
}
10 changes: 10 additions & 0 deletions src/agent/GRPCClientAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class GRPCClientAgent extends GRPCClient<AgentServiceClient> {
nodeId: this.nodeId,
host: this.host,
port: this.port,
command: this.echo.name,
},
this.client.echo,
)(...args);
Expand All @@ -101,6 +102,7 @@ class GRPCClientAgent extends GRPCClient<AgentServiceClient> {
nodeId: this.nodeId,
host: this.host,
port: this.port,
command: this.vaultsGitInfoGet.name,
},
this.client.vaultsGitInfoGet,
)(...args);
Expand All @@ -120,6 +122,7 @@ class GRPCClientAgent extends GRPCClient<AgentServiceClient> {
nodeId: this.nodeId,
host: this.host,
port: this.port,
command: this.vaultsGitPackGet.name,
},
this.client.vaultsGitPackGet,
)(...args);
Expand All @@ -138,6 +141,7 @@ class GRPCClientAgent extends GRPCClient<AgentServiceClient> {
nodeId: this.nodeId,
host: this.host,
port: this.port,
command: this.vaultsScan.name,
},
this.client.vaultsScan,
)(...args);
Expand All @@ -151,6 +155,7 @@ class GRPCClientAgent extends GRPCClient<AgentServiceClient> {
nodeId: this.nodeId,
host: this.host,
port: this.port,
command: this.nodesClosestLocalNodesGet.name,
},
this.client.nodesClosestLocalNodesGet,
)(...args);
Expand All @@ -164,6 +169,7 @@ class GRPCClientAgent extends GRPCClient<AgentServiceClient> {
nodeId: this.nodeId,
host: this.host,
port: this.port,
command: this.nodesClaimsGet.name,
},
this.client.nodesClaimsGet,
)(...args);
Expand All @@ -177,6 +183,7 @@ class GRPCClientAgent extends GRPCClient<AgentServiceClient> {
nodeId: this.nodeId,
host: this.host,
port: this.port,
command: this.nodesChainDataGet.name,
},
this.client.nodesChainDataGet,
)(...args);
Expand All @@ -190,6 +197,7 @@ class GRPCClientAgent extends GRPCClient<AgentServiceClient> {
nodeId: this.nodeId,
host: this.host,
port: this.port,
command: this.nodesHolePunchMessageSend.name,
},
this.client.nodesHolePunchMessageSend,
)(...args);
Expand All @@ -203,6 +211,7 @@ class GRPCClientAgent extends GRPCClient<AgentServiceClient> {
nodeId: this.nodeId,
host: this.host,
port: this.port,
command: this.notificationsSend.name,
},
this.client.notificationsSend,
)(...args);
Expand All @@ -225,6 +234,7 @@ class GRPCClientAgent extends GRPCClient<AgentServiceClient> {
nodeId: this.nodeId,
host: this.host,
port: this.port,
command: this.nodesCrossSignClaim.name,
},
this.client.nodesCrossSignClaim,
)(...args);
Expand Down
6 changes: 5 additions & 1 deletion src/agent/service/nodesCrossSignClaim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ function nodesCrossSignClaim({
call: grpc.ServerDuplexStream<nodesPB.CrossSign, nodesPB.CrossSign>,
) => {
const nodeId = keyManager.getNodeId();
const genClaims = grpcUtils.generatorDuplex(call, { nodeId }, true);
const genClaims = grpcUtils.generatorDuplex(
call,
{ nodeId, command: nodesCrossSignClaim.name },
true,
);
try {
await db.withTransactionF(async (tran) => {
const readStatus = await genClaims.read();
Expand Down
6 changes: 5 additions & 1 deletion src/agent/service/vaultsGitPackGet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ function vaultsGitPackGet({
call: grpc.ServerDuplexStream<vaultsPB.PackChunk, vaultsPB.PackChunk>,
): Promise<void> => {
const nodeId = keyManager.getNodeId();
const genDuplex = grpcUtils.generatorDuplex(call, { nodeId }, true);
const genDuplex = grpcUtils.generatorDuplex(
call,
{ nodeId, command: vaultsGitPackGet.name },
true,
);
try {
const clientBodyBuffers: Uint8Array[] = [];
const clientRequest = (await genDuplex.read()).value;
Expand Down
2 changes: 2 additions & 0 deletions src/bin/CommandPolykey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class CommandPolykey extends commander.Command {
public action(fn: (...args: any[]) => void | Promise<void>): this {
return super.action(async (...args: any[]) => {
const opts = this.opts();
// Set the format for error logging for the exit handlers
this.exitHandlers.errFormat = opts.format === 'json' ? 'json' : 'error';
// Set the logger according to the verbosity
this.logger.setLevel(binUtils.verboseToLogLevel(opts.verbose));
// Set the global upstream GRPC logger
Expand Down
1 change: 1 addition & 0 deletions src/bin/agent/CommandStart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ class CommandStart extends CommandPolykey {
});
const messageIn: AgentChildProcessInput = {
logLevel: this.logger.getEffectiveLevel(),
format: options.format,
workers: options.workers,
agentConfig,
};
Expand Down
23 changes: 10 additions & 13 deletions src/bin/polykey-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ async function main(_argv = process.argv): Promise<number> {
resolveMessageInP(data);
});
const messageIn = await messageInP;
const errFormat = messageIn.format === 'json' ? 'json' : 'error';
exitHandlers.errFormat = errFormat;
logger.setLevel(messageIn.logLevel);
// Set the global upstream GRPC logger
grpcSetLogger(logger.getChild('grpc'));
Expand Down Expand Up @@ -71,20 +73,17 @@ async function main(_argv = process.argv): Promise<number> {
if (e instanceof ErrorPolykey) {
process.stderr.write(
binUtils.outputFormatter({
type: 'error',
name: e.name,
description: e.description,
message: e.message,
type: errFormat,
data: e,
}),
);
process.exitCode = e.exitCode;
} else {
// Unknown error, this should not happen
process.stderr.write(
binUtils.outputFormatter({
type: 'error',
name: e.name,
description: e.message,
type: errFormat,
data: e,
}),
);
process.exitCode = 255;
Expand All @@ -107,9 +106,8 @@ async function main(_argv = process.argv): Promise<number> {
// There's no point attempting to propagate the error to the parent
process.stderr.write(
binUtils.outputFormatter({
type: 'error',
name: e.name,
description: e.message,
type: errFormat,
data: e,
}),
);
process.exitCode = 255;
Expand Down Expand Up @@ -137,9 +135,8 @@ async function main(_argv = process.argv): Promise<number> {
// There's no point attempting to propagate the error to the parent
process.stderr.write(
binUtils.outputFormatter({
type: 'error',
name: e.name,
description: e.message,
type: errFormat,
data: e,
}),
);
process.exitCode = 255;
Expand Down
12 changes: 5 additions & 7 deletions src/bin/polykey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ async function main(argv = process.argv): Promise<number> {
// Successful execution (even if the command was non-terminating)
process.exitCode = 0;
} catch (e) {
const errFormat = rootCommand.opts().format === 'json' ? 'json' : 'error';
if (e instanceof commander.CommanderError) {
// Commander writes help and error messages on stderr automatically
if (
Expand All @@ -78,20 +79,17 @@ async function main(argv = process.argv): Promise<number> {
} else if (e instanceof ErrorPolykey) {
process.stderr.write(
binUtils.outputFormatter({
type: 'error',
name: e.name,
description: e.description,
message: e.message,
type: errFormat,
data: e,
}),
);
process.exitCode = e.exitCode;
} else {
// Unknown error, this should not happen
process.stderr.write(
binUtils.outputFormatter({
type: 'error',
name: e.name,
description: e.message,
type: errFormat,
data: e,
}),
);
process.exitCode = 255;
Expand Down
1 change: 1 addition & 0 deletions src/bin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type AgentStatusLiveData = Omit<StatusLive['data'], 'nodeId'> & {
*/
type AgentChildProcessInput = {
logLevel: LogLevel;
format: 'human' | 'json';
workers?: number;
agentConfig: {
password: string;
Expand Down
Loading

0 comments on commit f18be47

Please sign in to comment.