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

feat(logging): add option to disable logging MONGOSH-1988 #2325

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

gagik
Copy link
Contributor

@gagik gagik commented Jan 22, 2025

Adds ability to disable logging completely both through global file and local configurations.

@gagik gagik force-pushed the gagik/add-disable-logging branch 2 times, most recently from 93661c5 to 73ad25a Compare January 22, 2025 14:50
@gagik gagik changed the title feat(cli-repl): add option to disable logging MONGOSH-1988 WIP feat(cli-repl): add option to disable logging MONGOSH-1988 Jan 22, 2025
@gagik gagik marked this pull request as draft January 22, 2025 16:20
@gagik gagik force-pushed the gagik/add-disable-logging branch 6 times, most recently from 3b38cc7 to ef79cf4 Compare January 24, 2025 16:44
@gagik gagik changed the title WIP feat(cli-repl): add option to disable logging MONGOSH-1988 feat(cli-repl): add option to disable logging MONGOSH-1988 Jan 24, 2025
@gagik gagik force-pushed the gagik/add-disable-logging branch from 054080c to 5147039 Compare January 26, 2025 22:00
@gagik gagik marked this pull request as ready for review January 26, 2025 22:00
packages/cli-repl/src/cli-repl.spec.ts Outdated Show resolved Hide resolved
packages/cli-repl/src/cli-repl.ts Outdated Show resolved Hide resolved
packages/cli-repl/src/cli-repl.ts Outdated Show resolved Hide resolved
packages/logging/src/logging-and-telemetry.ts Show resolved Hide resolved
packages/logging/src/logging-and-telemetry.ts Outdated Show resolved Hide resolved
packages/logging/src/logging-and-telemetry.ts Outdated Show resolved Hide resolved
packages/logging/src/logging-and-telemetry.ts Outdated Show resolved Hide resolved
packages/logging/src/logging-and-telemetry.ts Outdated Show resolved Hide resolved
packages/logging/src/logging-and-telemetry.ts Outdated Show resolved Hide resolved
const disableLogging = this.getConfig('disableLogging');
if (disableLogging !== true) {
await this.startLogging(loggingAndTelemetry);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll want to add an e2e test for this as well, maybe even including one that tests that the global config file can disable logging via this option

@gagik gagik changed the title feat(cli-repl): add option to disable logging MONGOSH-1988 feat(logging): add option to disable logging MONGOSH-1988 Jan 27, 2025
* be aware of this distinction. */
private hasStartedMongoshRepl = false;

private apiCallTracking = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seemed best to move all variables into the class state and keep the setup listeners function scope clean of state but if it's making the class too bloated, could reconsider (or maybe scope them under eventListenersState or something)

@gagik gagik marked this pull request as draft January 29, 2025 13:04
gagik added 7 commits January 30, 2025 14:17
…initialization

I think this refactor provides much better readability and provides an easier interface for us to test and debug the functionality of this. This also moves the hookLoggers call to the point of initialization as
Adds validation for the disableLogging field and fixes the equivalent test.
Use async getConfig, add return types, remove unnecessary eslint comments, move bus variables into class state, use explicit private and public fields.
Will follow up with more end to end tests.
@gagik gagik force-pushed the gagik/add-disable-logging branch 2 times, most recently from 275b7ad to 214ca17 Compare January 30, 2025 13:31
} else {
this.loggingAndTelemetry?.detachLogger();
this.logWriter?.destroy();
this.logWriter = undefined;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we create a new logger (and new logId) each time logging gets reenabled, which also means if someone disable and then enables logging, a new file is created.
I think that is reasonable unless we'd rather make sure it writes to the same file again (thinking of it more of a "pause")

packages/cli-repl/src/cli-repl.ts Show resolved Hide resolved
@gagik gagik requested a review from addaleax January 30, 2025 13:43
@gagik gagik marked this pull request as ready for review January 30, 2025 13:43
Wrapping the bus can allow us to inject onBus onto the logger which can help us prevent logs being created from there. That said, we still have the problem of having to either re-register those hooks each time, so we may want to instead update hookLogger to take function for getting the latest log rather than just a variable.
@gagik gagik force-pushed the gagik/add-disable-logging branch from 51ef2b7 to 6f30993 Compare January 30, 2025 14:57
private userId: string | undefined;
private isDummyLog = true;
private isSetup = false;
private hookedExternalListenersLogId: string | undefined = undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this class's state space exploded a bit now, which makes this pretty hard to review and probably also hard to work with in the future

e.g. it's hard to tell whether isDummyLog = false, isSetup = true, log = null, pendingLogEvents = [...], hookedExternalListenersLogId = 'foo' is a valid state for this class or not and what that means specifically

I'll try to get more specific at individual points here but I think we might want to rethink this and look for opportunities to make this simpler

packages/logging/src/logging-and-telemetry.ts Show resolved Hide resolved
packages/logging/src/logging-and-telemetry.ts Outdated Show resolved Hide resolved
packages/logging/src/logging-and-telemetry.ts Outdated Show resolved Hide resolved
packages/logging/src/logging-and-telemetry.ts Outdated Show resolved Hide resolved
packages/cli-repl/src/cli-repl.ts Outdated Show resolved Hide resolved
packages/cli-repl/src/cli-repl.ts Outdated Show resolved Hide resolved
return {
anonymousId: this.telemetryAnonymousId ?? (this.userId as string),
/** Information used and set by different bus events. */
private busEventState: LoggingAndTelemetryBusEventState = {
Copy link
Contributor Author

@gagik gagik Jan 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seemed best for separating these kind of properties from the state though might need a better name.
can also be moved inside setupBusEventListeners

hookLogger(
this.bus,
{
info: (...args: Parameters<typeof this.log.info>) => {
Copy link
Contributor Author

@gagik gagik Jan 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically there are more methods in this... quite a few needed to actually satisfy the MongoLogWriter type since it has a bunch of internal / private methods exposed. Do we want to specify them here or potentially update LogWriter i.e. use an interface?

@gagik gagik force-pushed the gagik/add-disable-logging branch 3 times, most recently from df96f8d to 1bbbfb0 Compare January 31, 2025 12:31
Moved public-facing API to a function and interface. Reuse the same log writer in cli-repl. Removed session_id as a stored state and instead provide it dynamically through this.log.
@gagik gagik force-pushed the gagik/add-disable-logging branch from 1bbbfb0 to adf5199 Compare January 31, 2025 12:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants