Skip to content

Integrating tracer into object lifecycles #403

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

Open
wants to merge 8 commits into
base: staging
Choose a base branch
from
Open
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
346 changes: 346 additions & 0 deletions async-init.patch

Large diffs are not rendered by default.

577 changes: 577 additions & 0 deletions index.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion npmDepsHash
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sha256-4fkSE29rEC53aecV/DYYwP5Xmd2wDjw6sBwGGaaDjF0=
sha256-yOUsAbtVeXMdGjR92SvSCFAlPTHxm1xQCipqbdkxb+o=
14 changes: 7 additions & 7 deletions package-lock.json

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

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
"postversion": "npm install --package-lock-only --ignore-scripts --silent",
"dependencies": "node ./scripts/npmDepsHash.mjs",
"tsx": "tsx",
"test": "node ./scripts/test.mjs",
"test": "node ./scripts/build.mjs && node ./scripts/test.mjs",
"lint": "eslint '{src,tests,scripts,benches}/**/*.{js,mjs,ts,mts,jsx,tsx}'",
"lintfix": "eslint '{src,tests,scripts,benches}/**/*.{js,mjs,ts,mts,jsx,tsx}' --fix",
"lint-shell": "find ./src ./tests ./scripts -type f -regextype posix-extended -regex '.*\\.(sh)' -exec shellcheck {} +",
Expand All @@ -137,7 +137,7 @@
},
"devDependencies": {
"@matrixai/errors": "^2.1.3",
"@matrixai/logger": "^4.0.3",
"@matrixai/logger": "^4.0.4-alpha.5",
"@matrixai/exec": "^1.0.3",
"@fast-check/jest": "^2.1.1",
"@swc/core": "1.3.82",
Expand Down Expand Up @@ -171,5 +171,8 @@
"tsx": "^3.12.7",
"typedoc": "^0.24.8",
"typescript": "^5.1.6"
},
"overrides": {
"@matrixai/logger": "^4.0.4-alpha.5"
}
}
23,985 changes: 23,985 additions & 0 deletions span.jsonl

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions src/CommandPolykey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Logger, {
formatting,
levelToString,
evalLogDataValue,
tracer,
} from '@matrixai/logger';
import * as binUtils from './utils/index.js';
import * as binOptions from './utils/options.js';
Expand All @@ -23,6 +24,7 @@ class CommandPolykey extends Command {
protected logger: Logger = logger;
protected fs: FileSystem;
protected exitHandlers: binUtils.ExitHandlers;
protected tracerProm: Promise<void> | undefined;

public constructor({
exitHandlers,
Expand Down Expand Up @@ -94,6 +96,21 @@ class CommandPolykey extends Command {
if (opts.nodePath == null) {
throw new errors.ErrorPolykeyCLINodePath();
}
// If verbose level has been enabled, then we want to start tracing
if (opts.verbose && this.tracerProm == null) {
this.tracerProm = (async () => {
const fs = await import('node:fs');
const spanFile = await fs.promises.open('span.jsonl', 'w');
const gen = tracer.streamEvents();
for await (const event of gen) {
await spanFile.write(JSON.stringify(event) + '\n');
}
await spanFile.close();
})();
this.exitHandlers.setTracerProm(this.tracerProm);
} else {
tracer.disableTracing();
}
await fn(...args);
});
}
Expand Down
31 changes: 21 additions & 10 deletions src/polykey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@ process.removeAllListeners('SIGTERM');
/**
* Set the main entrypoint filepath.
* This can be referred to globally.
* For ESM, change to using `import.meta.url`.
*/
const filename = fileURLToPath(import.meta.url);
globalThis.PK_MAIN_EXECUTABLE = filename;

import type { SpanId } from '@matrixai/logger';

let rootSpan: SpanId;

async function polykeyAgentMain(): Promise<number> {
const {
default: Logger,
StreamHandler,
formatting,
tracer,
} = await import('@matrixai/logger');
const { default: PolykeyAgent } = await import('polykey/PolykeyAgent.js');
const { default: ErrorPolykey } = await import('polykey/ErrorPolykey.js');
Expand All @@ -51,6 +55,7 @@ async function polykeyAgentMain(): Promise<number> {
const messageIn = await messageInP;
const errFormat = messageIn.format === 'json' ? 'json' : 'error';
exitHandlers.errFormat = errFormat;
exitHandlers.setPreExit(() => tracer.endSpan(rootSpan));
// Set the logger according to the verbosity
logger.setLevel(messageIn.logLevel);
// Set the logger formatter according to the format
Expand Down Expand Up @@ -245,15 +250,21 @@ async function polykeyMain(argv: Array<string>): Promise<number> {
}

async function main(argv = process.argv): Promise<number> {
if (argv[argv.length - 1] === '--agent-mode') {
// This is an internal mode for running `PolykeyAgent` as a child process
// This is not supposed to be used directly by the user
process.title = 'polykey-agent';
return polykeyAgentMain();
} else {
process.title = 'polykey';
return polykeyMain(argv);
}
const { spanContext } = await import('@matrixai/async-init/utils.js');
const { tracer } = await import('@matrixai/logger');

rootSpan = tracer.startSpan('polykeyRoot');
return spanContext.run({ currentSpanId: rootSpan }, async () => {
if (argv[argv.length - 1] === '--agent-mode') {
// This is an internal mode for running `PolykeyAgent` as a child process
// This is not supposed to be used directly by the user
process.title = 'polykey-agent';
return await polykeyAgentMain();
} else {
process.title = 'polykey';
return await polykeyMain(argv);
}
});
}

if (import.meta.url.startsWith('file:')) {
Expand Down
26 changes: 26 additions & 0 deletions src/utils/ExitHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import process from 'node:process';
import { tracer } from '@matrixai/logger';
import ErrorPolykey from 'polykey/ErrorPolykey.js';
import * as binUtils from './utils.js';
import * as errors from '../errors.js';
Expand All @@ -11,6 +12,16 @@ class ExitHandlers {
public handlers: Array<(signal?: NodeJS.Signals) => Promise<void>>;
protected _exiting: boolean = false;
protected _errFormat: 'json' | 'error';
protected tracerProm: Promise<void> | undefined;
protected preExit: () => void | Promise<void>;

public setTracerProm(prom: Promise<void>) {
this.tracerProm = prom;
}

public setPreExit(f: () => void | Promise<void>) {
this.preExit = f;
}

/**
* Handles termination signals
Expand Down Expand Up @@ -49,6 +60,11 @@ class ExitHandlers {
} finally {
// Uninstall all handlers to prevent signal loop
this.uninstall();
await this.preExit?.();
if (this.tracerProm) {
tracer.endTracing();
await this.tracerProm;
}
// Propagate signal to NodeJS VM handlers
process.kill(process.pid, signal);
}
Expand All @@ -75,6 +91,11 @@ class ExitHandlers {
);
process.exitCode = error.exitCode;
// Fail fast pattern
await this.preExit?.();
if (this.tracerProm) {
tracer.endTracing();
await this.tracerProm;
}
process.exit();
};

Expand All @@ -99,6 +120,11 @@ class ExitHandlers {
);
process.exitCode = error.exitCode;
// Fail fast pattern
await this.preExit?.();
if (this.tracerProm) {
tracer.endTracing();
await this.tracerProm;
}
process.exit();
};

Expand Down
Loading