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(mongodb4): added mongodb4 instrumentation #794

Closed

Conversation

osherv
Copy link
Member

@osherv osherv commented Dec 22, 2021

MongoDB4 Instrumentation!

@osherv osherv requested a review from a team December 22, 2021 13:26
@dyladan
Copy link
Member

dyladan commented Dec 22, 2021

Is there a reason this is a new instrumentation instead of updating the existing mongodb instrumentation?

@osherv
Copy link
Member Author

osherv commented Dec 22, 2021

@dyladan
Yes :) Here's the related Issue:
#577
'I think we should support both of them since v3 is still massively used'

@dyladan
Copy link
Member

dyladan commented Dec 22, 2021

I was not suggesting to drop support for v3, but to add support for v4 to the existing instrumentation using a new InstrumentationNodeModuleDefinition.

@osherv
Copy link
Member Author

osherv commented Dec 22, 2021

@tydhot
I tried, but the new instrumentation won't be applyed in that case (in the ut):
Screen Shot 2021-12-22 at 17 42 54

Screen Shot 2021-12-22 at 17 44 09

Because i think that otel search in one mongodb only
Is there any option to fix that without create a new package?

@dyladan
Copy link
Member

dyladan commented Dec 22, 2021

In most other instrumentations where we patch multiple major versions, we only use the types from the latest version and use any or unknown or hand-rolled types for the other versions.

@osherv
Copy link
Member Author

osherv commented Dec 22, 2021

@dyladan Thanks! ill try!

@codecov
Copy link

codecov bot commented Dec 30, 2021

Codecov Report

Merging #794 (a0ab0a4) into main (7510757) will decrease coverage by 8.51%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##             main     #794      +/-   ##
==========================================
- Coverage   95.29%   86.78%   -8.52%     
==========================================
  Files          10       23      +13     
  Lines         701     1286     +585     
  Branches      142      260     +118     
==========================================
+ Hits          668     1116     +448     
- Misses         33      170     +137     
Impacted Files Coverage Δ
...instrumentation-nestjs-core/src/instrumentation.ts 95.65% <0.00%> (ø)
...-instrumentation-nestjs-core/src/enums/NestType.ts 100.00% <0.00%> (ø)
...opentelemetry-instrumentation-restify/src/types.ts 100.00% <0.00%> (ø)
...opentelemetry-instrumentation-restify/src/utils.ts 100.00% <0.00%> (ø)
...try-instrumentation-nestjs-core/src/enums/index.ts 100.00% <0.00%> (ø)
...opentelemetry-instrumentation-mongodb/src/types.ts 100.00% <0.00%> (ø)
...telemetry-instrumentation-restify/src/constants.ts 100.00% <0.00%> (ø)
...tapackages/auto-instrumentations-node/src/utils.ts 96.77% <0.00%> (ø)
...nstrumentation-generic-pool/src/instrumentation.ts 40.00% <0.00%> (ø)
...nstrumentation-restify/src/enums/AttributeNames.ts 100.00% <0.00%> (ø)
... and 3 more

@vmarchaud vmarchaud added the enhancement New feature or request label Dec 30, 2021
@osherv
Copy link
Member Author

osherv commented Jan 2, 2022

@dyladan Hey :)
The issue is almost ready!
Two last fails:

  1. mongo4 is not supported for node < 12
  2. Code coverage is now lower because the mongo3 tests is only under test-all-versions and not under tests.

Any idea how to handle with these? Thanks alot

Copy link
Member

@rauno56 rauno56 left a comment

Choose a reason for hiding this comment

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

Looks good! Thanks!

Please remove console.log(getTestSpans()); from mongodb-v3.test.ts as well.

Some of the small changes(few newlines, typecast to any) could also be taken to v3 test file, but that's not a deal breaker.

@rauno56
Copy link
Member

rauno56 commented Jan 5, 2022

mongo4 is not supported for node < 12

@osherv, please ignore the package in the CI matrix via lerna-extra-args in unit-test.yml. The same way as @opentelemetry/instrumentation-pino has been disabled.

@osherv
Copy link
Member Author

osherv commented Jan 6, 2022

@rauno56 Fixed everything! Thanks a lot for the review :)

@rauno56
Copy link
Member

rauno56 commented Jan 6, 2022

Nice! Even the smallest of my nits now addressed! 👍 I'd love to get another review before we merge.

@rauno56
Copy link
Member

rauno56 commented Jan 7, 2022

❗ This cannot yet be merged. For some reason, the tests stay pending forever. Works locally.

@osherv, the only thing that stands out from the setup is the --parallel option. Could you please try removing that?

@@ -7,7 +7,8 @@
"repository": "open-telemetry/opentelemetry-js-contrib",
"scripts": {
"docker:start": "docker run -e MONGODB_DB=opentelemetry-tests -e MONGODB_PORT=27017 -e MONGODB_HOST=localhost -p 27017:27017 --rm mongo",
"test": "nyc ts-mocha --parallel -p tsconfig.json --require '@opentelemetry/contrib-test-utils' 'test/**/*.test.ts'",
"test": "nyc ts-mocha -p tsconfig.json --require '@opentelemetry/contrib-test-utils' 'test/**/mongodb-v4.test.ts'",
Copy link
Member

@blumamir blumamir Jan 9, 2022

Choose a reason for hiding this comment

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

this change means we are running PullRequests CI tests only for v4, and stop running v3 tests on PRs.

"test:ci:changed": "lerna run test --since origin/main",

something we might want to consider...

Copy link
Member Author

Choose a reason for hiding this comment

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

That is correct. I can't think about any other option. Suggestions?

Copy link
Member

Choose a reason for hiding this comment

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

Not a particularly good one
We can run tav here as well, possibly with just 2 versions. But that sounds clumsy and complex.
Let's wait for more minds on this one @rauno56 @dyladan

Copy link
Member Author

Choose a reason for hiding this comment

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

btw, The tests are only can run locally (we need mongodb server). So anyway i dont think i would change much

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I usually spin up a mongo server locally with docker and run the tests against it.
CI tests also start a mongo server here, so the tests can pass.

Copy link
Member

Choose a reason for hiding this comment

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

We talked about this in the SIG. We recommend to test the most popular major version (in this case v3) and test other versions with TAV.

@linux-foundation-easycla
Copy link

CLA Not Signed

  • ❌ The commit (af49bac ,6de0a7386a82b7e031aac51ebf99a718cc39612c ,28dd80f2744ce3820b603f99d86a362c7e1c4bfe ,af247e48e495f42c27f4368b4394dc05ec1e4f21 ,87efbc6a702758dffcc9f27d3908f82bf5e763e6 ,af3f981f827a71baa2a9ca5618cbad5834c020ce ,726294451bb304fcf385e5dbc246ac28acff1aa1 ,8333e5449ce915af798a77229f7e92edc725a65e ,33a729477f99aeccdc7c50fa8a5ef0dec521a11a ,d4ea2c6e947afb9a417e20ec0cbec279d8f45bef ,009f16f0bd65699ebb8db7e98756c24f32ad7342) is missing the User's ID, preventing the EasyCLA check. Consult GitHub Help to resolve.For further assistance with EasyCLA, please submit a support request ticket.

let commandObj: Record<string, unknown>;
if (command?.documents) {
commandObj = command.documents[0];
} else if (command.cursors) {
Copy link
Member

Choose a reason for hiding this comment

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

command?.cursor

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

@rauno56
Copy link
Member

rauno56 commented Jan 10, 2022

I turned on streaming Lerna output to see that MongoDB tests fail, but do not terminate the process. That's why the tests never finish:
https://github.com/Rauno56/opentelemetry-js-contrib/runs/4760394725?check_suite_focus=true GHA links are useless. The error was:

      Uncaught AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
+ actual - expected

+ '127.0.0.1'
- 'localhost'
      + expected - actual

      -127.0.0.1
      +localhost
      
      at Object.assertSpans (test/utils.ts:88:10)
      at /.../opentelemetry-js-contrib/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts:380:11
      at /.../opentelemetry-js-contrib/plugins/node/opentelemetry-instrumentation-mongodb/node_modules/mongodb/src/utils.ts:622:5
      at /.../opentelemetry-js-contrib/plugins/node/opentelemetry-instrumentation-mongodb/node_modules/mongodb/src/operations/execute_operation.ts:111:45
      at /.../opentelemetry-js-contrib/plugins/node/opentelemetry-instrumentation-mongodb/node_modules/mongodb/src/utils.ts:622:5
      at completeEndSession (node_modules/mongodb/src/sessions.ts:281:9)
      at /.../opentelemetry-js-contrib/plugins/node/opentelemetry-instrumentation-mongodb/node_modules/mongodb/src/sessions.ts:293:7
      at maybePromise (node_modules/mongodb/src/utils.ts:607:3)
      at ClientSession.endSession (node_modules/mongodb/src/sessions.ts:263:24)
      at /.../opentelemetry-js-contrib/plugins/node/opentelemetry-instrumentation-mongodb/node_modules/mongodb/src/operations/execute_operation.ts:111:26
      at /.../opentelemetry-js-contrib/plugins/node/opentelemetry-instrumentation-mongodb/node_modules/mongodb/src/operations/insert.ts:126:7
      at /.../opentelemetry-js-contrib/plugins/node/opentelemetry-instrumentation-mongodb/node_modules/mongodb/src/operations/bulk_write.ts:58:7
      at /.../opentelemetry-js-contrib/plugins/node/opentelemetry-instrumentation-mongodb/node_modules/mongodb/src/utils.ts:264:17
      at /.../opentelemetry-js-contrib/plugins/node/opentelemetry-instrumentation-mongodb/node_modules/mongodb/src/utils.ts:255:9
      at executeCommands (node_modules/mongodb/src/bulk/common.ts:591:12)
      at resultHandler (node_modules/mongodb/src/bulk/common.ts:618:5)
      at /.../opentelemetry-js-contrib/plugins/node/opentelemetry-instrumentation-mongodb/node_modules/mongodb/src/utils.ts:622:5
      at /.../opentelemetry-js-contrib/plugins/node/opentelemetry-instrumentation-mongodb/node_modules/mongodb/src/operations/execute_operation.ts:114:9
      at /.../opentelemetry-js-contrib/plugins/node/opentelemetry-instrumentation-mongodb/node_modules/mongodb/src/cmap/connection_pool.ts:469:13
      at handleOperationResult (node_modules/mongodb/src/sdam/server.ts:608:5)
      at /.../opentelemetry-js-contrib/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts:122:47
      at AsyncLocalStorage.run (node:async_hooks:320:14)
      at AsyncLocalStorageContextManager.with (node_modules/@opentelemetry/context-async-hooks/src/AsyncLocalStorageContextManager.ts:40:36)
      at ContextAPI.with (node_modules/@opentelemetry/api/src/api/context.ts:77:42)
      at patchedEnd (src/instrumentation.ts:122:47)
      at MessageStream.messageHandler (node_modules/mongodb/src/cmap/connection.ts:753:5)
      at MessageStream.emit (node:events:390:28)
      at processIncomingData (node_modules/mongodb/src/cmap/message_stream.ts:167:12)
      at MessageStream._write (node_modules/mongodb/src/cmap/message_stream.ts:64:5)
      at writeOrBuffer (node:internal/streams/writable:389:12)
      at _write (node:internal/streams/writable:330:10)
      at MessageStream.Writable.write (node:internal/streams/writable:334:10)
      at Socket.ondata (node:internal/streams/readable:754:22)
      at Socket.emit (node:events:390:28)
      at addChunk (node:internal/streams/readable:315:12)
      at readableAddChunk (node:internal/streams/readable:289:9)
      at Socket.Readable.push (node:internal/streams/readable:228:10)
      at TCP.onStreamRead (node:internal/stream_base_commons:199:23)
      at TCP.callbackTrampoline (node:internal/async_hooks:130:17)

@rauno56
Copy link
Member

rauno56 commented Jan 10, 2022

Regarding ☝️ That definitely needs to be fixed in two aspects:

  1. errors from the tests should always terminate the process. Some combination of MongoDB and mocha doesn't currently work well, because the test is marked failed by mocha internally but it doesn't really throw and terminate. I tried it with the instrumentation disabled(but the rest of the setup there) - same thing. To reproduce just throw in the callback of the mongo command. The only thing that comes to mind right now is to catch errors in callbacks and call done(err); ourselves. 😞
  2. The thrown error comes from the fact that mongo resolves localhost and stores the IP on the field we check against localhost. To bypass that, we should set the host to 127.0.0.1 from the get-go to avoid the discrepancy. That can be done

@zombispormedio
Copy link

any update about this PR? I am interested in

@osherv
Copy link
Member Author

osherv commented Jan 25, 2022

any update about this PR? I am interested in

Sry for the delay, i had to omicron virus -_-..
I'm working on it right now

@zombispormedio
Copy link

Sry for the delay, i had to omicron virus -_-.. I'm working on it right now

Sorry, just wondering. I hope you had a good recovery. Thank you.

@linux-foundation-easycla
Copy link

CLA Not Signed

  • ❌ The commit (af49bac ,6de0a7386a82b7e031aac51ebf99a718cc39612c ,28dd80f2744ce3820b603f99d86a362c7e1c4bfe ,af247e48e495f42c27f4368b4394dc05ec1e4f21 ,87efbc6a702758dffcc9f27d3908f82bf5e763e6 ,af3f981f827a71baa2a9ca5618cbad5834c020ce ,726294451bb304fcf385e5dbc246ac28acff1aa1 ,8333e5449ce915af798a77229f7e92edc725a65e ,33a729477f99aeccdc7c50fa8a5ef0dec521a11a ,d4ea2c6e947afb9a417e20ec0cbec279d8f45bef ,009f16f0bd65699ebb8db7e98756c24f32ad7342) is missing the User's ID, preventing the EasyCLA check. Consult GitHub Help to resolve.For further assistance with EasyCLA, please submit a support request ticket.

@linux-foundation-easycla
Copy link

CLA Not Signed

  • ❌ The commit (af49bac ,6de0a7386a82b7e031aac51ebf99a718cc39612c ,28dd80f2744ce3820b603f99d86a362c7e1c4bfe ,af247e48e495f42c27f4368b4394dc05ec1e4f21 ,87efbc6a702758dffcc9f27d3908f82bf5e763e6 ,af3f981f827a71baa2a9ca5618cbad5834c020ce ,726294451bb304fcf385e5dbc246ac28acff1aa1 ,8333e5449ce915af798a77229f7e92edc725a65e ,33a729477f99aeccdc7c50fa8a5ef0dec521a11a ,d4ea2c6e947afb9a417e20ec0cbec279d8f45bef ,009f16f0bd65699ebb8db7e98756c24f32ad7342) is missing the User's ID, preventing the EasyCLA check. Consult GitHub Help to resolve.For further assistance with EasyCLA, please submit a support request ticket.

@osherv
Copy link
Member Author

osherv commented Jan 25, 2022

@rauno56 @blumamir
Hey! Good to be back after the Omicron XD.
Fix all of your comments. I need to fix my CLA, so the moment you approves my PR, ill open new branch with these changes.
:)

@blumamir
Copy link
Member

@rauno56 @blumamir Hey! Good to be back after the Omicron XD. Fix all of your comments. I need to fix my CLA, so the moment you approves my PR, ill open new branch with these changes. :)

Good to have you back and thanks for the great work. Hope you are feeling well now :)
You can also open a new PR with the fixed CLA and I'll finish the review there. It will probably take me a day or two to get to it.

@linux-foundation-easycla
Copy link

CLA Not Signed

  • ❌ The commit (af49bac ,6de0a7386a82b7e031aac51ebf99a718cc39612c ,28dd80f2744ce3820b603f99d86a362c7e1c4bfe ,af247e48e495f42c27f4368b4394dc05ec1e4f21 ,87efbc6a702758dffcc9f27d3908f82bf5e763e6 ,af3f981f827a71baa2a9ca5618cbad5834c020ce ,726294451bb304fcf385e5dbc246ac28acff1aa1 ,8333e5449ce915af798a77229f7e92edc725a65e ,33a729477f99aeccdc7c50fa8a5ef0dec521a11a ,d4ea2c6e947afb9a417e20ec0cbec279d8f45bef ,009f16f0bd65699ebb8db7e98756c24f32ad7342) is missing the User's ID, preventing the EasyCLA check. Consult GitHub Help to resolve.For further assistance with EasyCLA, please submit a support request ticket.

@blumamir
Copy link
Member

blumamir commented Feb 6, 2022

Thanks for addressing all the comments and for adding the support for v4 🥇
After the CLA is fixed, this can be merged as far as I am concerned

@osherv
Copy link
Member Author

osherv commented Feb 6, 2022

Thanks! next pr will be muck more quicker :)
I'll open new pr with the fixed CLA

@blumamir
Copy link
Member

blumamir commented Feb 6, 2022

closing as this PR migrated to #869 due to CLA issues

@blumamir blumamir closed this Feb 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants