Skip to content

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

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

Merged
merged 15 commits into from
Feb 5, 2025

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
const disableLogging = this.getConfig('disableLogging');
if (disableLogging !== true) {
await this.startLogging(loggingAndTelemetry);
}
Copy link
Collaborator

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 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")

@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
@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
Collaborator

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

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?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we want to specify them here or potentially update LogWriter i.e. use an interface?

I think that would be a very nice follow-up, but not required for this PR since hookLogger already defines the interface it needs

@gagik gagik force-pushed the gagik/add-disable-logging branch 2 times, most recently from d063090 to df96f8d Compare January 31, 2025 12:30
@gagik gagik force-pushed the gagik/add-disable-logging branch 2 times, most recently from 1bbbfb0 to adf5199 Compare January 31, 2025 12:41
Copy link
Collaborator

@addaleax addaleax left a comment

Choose a reason for hiding this comment

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

Looks great! Just a few (final 🤞) comments

@@ -22,6 +22,7 @@ import { once } from 'events';
import type { AddressInfo } from 'net';
const { EJSON } = bson;
import { sleep } from './util-helpers';
import type { LogEntry } from '@mongosh/shell-api/src/log-entry';
Copy link
Collaborator

Choose a reason for hiding this comment

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

We generally don't do cross-package imports, if we want this, we should either export it from shell-api or (preferably) move it to @mongosh/types and share it from there.

All that being said – it's the wrong type 🙂 LogEntry uses full-form names like timestamp, severity and message, but actual log entries use short identifiers like t, s and msg

Copy link
Contributor Author

@gagik gagik Feb 5, 2025

Choose a reason for hiding this comment

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

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

Choose a reason for hiding this comment

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

Do we want to specify them here or potentially update LogWriter i.e. use an interface?

I think that would be a very nice follow-up, but not required for this PR since hookLogger already defines the interface it needs

@gagik
Copy link
Contributor Author

gagik commented Feb 5, 2025

@addaleax @alenakhineika
do we usually merge stacked PRs i.e. #2355 like this or do we want to revert that, get this one into main and then merge the #2355 into main as well?

@gagik gagik changed the title feat(logging): add option to disable logging MONGOSH-1988 feat(logging): add configuration to disable logging MONGOSH-1988 Feb 5, 2025
@alenakhineika
Copy link
Contributor

From my side, I would merge them all into one and then merge them all into the main to be able to test the whole thing first. Let's wait for @addaleax thoughts about it.

@gagik gagik force-pushed the gagik/add-disable-logging branch 2 times, most recently from e7486f8 to d42d455 Compare February 5, 2025 09:56
@addaleax
Copy link
Collaborator

addaleax commented Feb 5, 2025

From my side, I would merge them all into one and then merge them all into the main to be able to test the whole thing first. Let's wait for @addaleax thoughts about it.

I don't see why we'd be more or less able to test this depending on how we merge things?

If this was just a "feature branch" PR, I think that would be okay, but right now it's a) a "feature branch" PR plus b) a refactor PR plus c) an actual feature PR, and that's something I'd like to get merged as quickly as possible because otherwise there's no way to properly the review the feature and refactor in isolation

@gagik gagik force-pushed the gagik/add-disable-logging branch from 1c799b2 to a743897 Compare February 5, 2025 11:45
gagik and others added 15 commits February 5, 2025 14:00
…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.
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.
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.
* feat(cli-repl): do not wait for log clean-up MONGOSH-1990

* test: add test for log manager throwing error

* test: remove only

* fix: mark time after log cleanup
Change typing of LogEntry and use EJSON.parse, remove unnecessary eslint comment
We use a type from the mongodb-log-writer but it was not in the dependencies, so we add it back to fix the check test.
@gagik gagik force-pushed the gagik/add-disable-logging branch from ce7e5b9 to 0ffae4c Compare February 5, 2025 13:01
@gagik gagik merged commit b340d79 into main Feb 5, 2025
52 of 55 checks passed
@gagik gagik deleted the gagik/add-disable-logging branch February 5, 2025 13:52
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.

3 participants