Skip to content

feat(NODE-4559): add mongodb-legacy metadata #5

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 11 commits into from
Sep 12, 2022

Conversation

nbbeeken
Copy link
Collaborator

@nbbeeken nbbeeken commented Aug 30, 2022

Description

What is changing?

The MongoClient will set the driverInfo.name property to contain mongodb-legacy

What is the motivation?

To get insight into callback usage

Double check the following

  • Ran npm run check:lint script
  • Self-review completed using the steps outlined here
  • PR title follows the correct format: <type>(NODE-xxxx)<!>: <description>
  • Changes are covered by tests
  • New TODOs have a related JIRA ticket

@nbbeeken nbbeeken force-pushed the NODE-4559/callback-metadata branch from 57659aa to ff54a8f Compare September 6, 2022 19:18
@nbbeeken nbbeeken marked this pull request as ready for review September 6, 2022 19:23
@@ -22,6 +24,54 @@ describe('legacy_wrappers/mongo_client.js', () => {
await client.close();
});

describe('client metadata', () => {
it('should set mongodb-legacy to the client metadata', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we structure these tests a bit better? I'm thinking context blocks explaining the context for the test, followed by a better description ("set mongodb-legacy to the client metadata" isn't what happens, and isn't descriptive). For example

context('when no driverInfo is passed to the MongoClient constructor', () => {
  it('sets the driver name to mongodb-legacy', () => {});
  it(`sets the version to ${version}`, () => {}
});

I think this (or a similar structure) would apply to every new test added in this PR

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Organized!

Copy link
Collaborator

Choose a reason for hiding this comment

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

three comments

  • the descriptions for the tests aren’t great. for example - what does should set mongodb-legacy to the client metadata mean? the test description should describe what the test is testing, should assign client metadata.name to mongodb-legacy . this leads into the second comment
  • the tests would be better organized with a single assertion per it block. should assign client metadata.name to mongodb-legacy and client metadata.version to the version in package.json is a mouthful, but if it’s split into two separate it blocks, should assign client metadata.name to mongodb-legacy and should assign client metadata.version to the version in the package.json are both easy to understand and describe exactly what the test is testing
  • the other tests would be much more understandable if we broke it block into a context + it (preferably two its actually). for example
it('should prepend mongodb-legacy to user passed driverInfo.name', () => {
        const client = new LegacyMongoClient(iLoveJs, { driverInfo: { name: 'mongoose' } });
        expect(client.options.metadata).to.have.nested.property(
          'driver.name',
          'nodejs|mongodb-legacy|mongoose'
        );
        expect(client.options.metadata)
          .to.have.property('version')
          .that.includes(currentLegacyVersion);
      });

would become

context('when driverInto.name is provided and driverInfo.version is not', () => { 
  it('should prepend mongodb-legacy to user passed driverInfo.name', () => {...}
  it('should assign driverInfo.version to the version in the package.json', () => { ... }
});

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

organized more!

Co-authored-by: Bailey Pearson <bailey.pearson@gmail.com>
baileympearson
baileympearson previously approved these changes Sep 9, 2022
src/utils.js Outdated
@@ -23,3 +25,23 @@ module.exports.maybeCallback = (promise, callback) => {

return promise;
};

module.exports.addLegacyMetadata = options => {
if (options.driverInfo == null) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess we don't have access to node 14 ? and ?? operators here, but I think we can still condense this logic to be a bit easier to parse, and we should also improve this to make sure we defer error handling to the base class as much as we can in the case that driverInfo isn't a proper object, too. Something along these lines (you can stick to template strings if you really want, but the idea is the same).

  if (typeof options.driverInfo !== 'object') {
    return;
  }
  options.driverInfo = options.driverInfo || {};
  const infoParts = {
    name: ['mongodb-legacy'],
    version: [version]
  };
  for (const prop of ['name', 'version']) {
    if (typeof options.driverInfo[prop] === 'string') {
      infoParts[prop].push(options.driverInfo[prop]);
    }
    options.driverInfo[prop] = infoParts[prop].join('|');
  }

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

@nbbeeken nbbeeken requested a review from dariakp September 9, 2022 17:20
@baileympearson
Copy link
Collaborator

posting here for visibility from slack (@dariakp fyi) - we need to adjust the implementation to not mutate the options object. Otherwise, creating multiple mongo clients with the same options object would prepend an additional name and version with each client construction

baileympearson
baileympearson previously approved these changes Sep 9, 2022
@baileympearson baileympearson dismissed their stale review September 9, 2022 20:17

missing tests

@nbbeeken nbbeeken requested a review from dariakp September 12, 2022 16:24
@baileympearson baileympearson merged commit 41437ea into main Sep 12, 2022
@baileympearson baileympearson deleted the NODE-4559/callback-metadata branch September 12, 2022 19:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants