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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

gagik
Copy link
Contributor

@gagik gagik commented Jan 22, 2025

Includes a refactor for better readability and to provide an easier interface for testing and debugging the functionality of this. This also moves the hookLoggers call to the point of initialization.

@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
import { MultiSet, toSnakeCase } from './helpers';
import { Writable } from 'stream';

export class MongoshLoggingAndTelemetry {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think structuring it as a class provided much better readability and an easier interface for us to test and debug the functionality of this compared to a function. Open to using the function structure though if there's a reason I may be missing here, I started doing it that way and just realized the ability to stub and access the scope of the class may be worth some greater changes.

Naming of it I'm less sure of.

Copy link
Contributor

@addaleax addaleax Jan 27, 2025

Choose a reason for hiding this comment

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

A couple of thoughts:

  • Turning this into a class seems fine as a way of acknowleding that this code has a bit of state that it manages on its own.
  • "the ability to stub and access the scope of the class" is usually a bad reason to turn something into a class – exposing internal state as a way to test something is often a code smell that means that the API of what you're testing doesn't directly allow you to observe behavior that should be externally visible
  • This kind of change should probably be mentioned in the tech design and ideally addressed as a separate item, so it's more obvious what the refactor and the functional changes are, each (probably not necessary at this point since disentangling this is probably more work than not) – and so the review process doesn't get too bloated 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense, I did think it may be too big of a change. I do not mind writing this into a function, I was thinking of doing that first and then introducing an extra commit I could reverse but seemed easier for me to reason to have this state first.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, to be clear, this is not something that needs to change again here :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As in, it could be left as a class?

Also, with this:

exposing internal state as a way to test something is often a code smell that means that the API of what you're testing doesn't directly allow you to observe behavior that should be externally visible

do I understand correctly that the flaw here is that tests should focus on testing expected behavior and not some arbitrary internal state one exposes?

Copy link
Contributor

@addaleax addaleax Jan 27, 2025

Choose a reason for hiding this comment

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

Yeah, leaving it as a class seems fine. I'd maybe consider marking as many of the properties private as possible, but otherwise it's not like "function with a bunch of local variables" and "class with a bunch of properties" are terribly different concepts.

exposing internal state as a way to test something is often a code smell that means that the API of what you're testing doesn't directly allow you to observe behavior that should be externally visible

do I understand correctly that the flaw here is that tests should focus on testing expected behavior and not some arbitrary internal state one exposes?

Yeah, exactly. If I get to a point where I'd want to test internal state, that's usually a good point to wonder if maybe the class can be broken down into smaller pieces that can be unit tested individually, rather than starting to rely on the ability to inspect and/or mock internals. But testing externally visible behavior should generally take precedence anyway. 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

otherwise it's not like "function with a bunch of local variables" and "class with a bunch of properties" are terribly different concepts

good throwback to pre-ES6 JavaScript I guess.

sounds good, thank you for the explanation 😀

packages/logging/src/logging-and-telemetry.ts Show resolved Hide resolved
gagik added 3 commits January 26, 2025 23: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.
@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
import { MultiSet, toSnakeCase } from './helpers';
import { Writable } from 'stream';

export class MongoshLoggingAndTelemetry {
Copy link
Contributor

@addaleax addaleax Jan 27, 2025

Choose a reason for hiding this comment

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

A couple of thoughts:

  • Turning this into a class seems fine as a way of acknowleding that this code has a bit of state that it manages on its own.
  • "the ability to stub and access the scope of the class" is usually a bad reason to turn something into a class – exposing internal state as a way to test something is often a code smell that means that the API of what you're testing doesn't directly allow you to observe behavior that should be externally visible
  • This kind of change should probably be mentioned in the tech design and ideally addressed as a separate item, so it's more obvious what the refactor and the functional changes are, each (probably not necessary at this point since disentangling this is probably more work than not) – and so the review process doesn't get too bloated 🙂

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

Use async getConfig, add return types, remove unnecessary eslint comments, move bus variables into class state, use explicit private and public fields.
@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)

Will follow up with more end to end tests.
@gagik gagik marked this pull request as draft January 29, 2025 13:04
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