opentelemetry-js-contrib icon indicating copy to clipboard operation
opentelemetry-js-contrib copied to clipboard

fix(instrumentation-mongoose): Fix instrumentation for Mongoose Model methods, save() and remove(), by passing options argument to original method calls

Open ferrelucas opened this issue 1 year ago • 4 comments

Which problem is this PR solving?

Fixes #1678

This PR fixes the issue where transactions cannot be used for the Model methods save() and remove() (and their aliases eg. create()) because after instrumentation is applied to these methods, specifically when both options and callback are defined, the final method call loses the first argument option making the final call not take the option.session into consideration when making database operations. This could lead to save updates in transactions not being properly rolled back as well as multiple operations that are supposed to share the same session not being able to do so because their calls would eventually be made without the session reference.

Short description of the changes

  • Pass the original args into handleCallbackResponse()
  • Updates the implementation of handleCallbackResponse() so that we create newArgs including the original options and the instrumentedcallback function.
  • Added tests to cover the base case of empty-object but defined options argument with callback
  • Added tests cases to cover committed and aborted transactions

ferrelucas avatar Mar 14 '24 02:03 ferrelucas

CLA Signed

The committers listed above are authorized under a signed CLA.

  • :white_check_mark: login: ferrelucas / name: Lucas Ferreira (908b8af034db5031e660b71c21f9879e7caa1562, b414324984d8856ef2e7fe63f3f84c5af02552ed, 763a1a2b51d42982bd8945de068f58cdc5f03306, adde5d2121627b9cf8b8898df46ef6ccfa8a2952, 4d99a63ed8371662acb69bd6bb3f7f31015123fa, 019d49dc325e4b2bab9aa2dc929b240a7608d6b7, d3294fea2055aca6710aedb1cc307a6db16a7000, 138182364c6470e782a185ccdd4c660bd18e2223, 0a2ffb3f905adaf13ef7a1155004966e130e0ecd, 95c1042063864636538de88af45cd09d2d9b6696, d609e1f9ef4a10a5659254fbcfece607ccf0bbfa, 6b7cf21038951c317d99cc6f479f9a9dd86889f4, d89bc0fed08906459f4249439866e41fd88c57a6, f205174af758967402f3973091bd67872ec2b9d5, 8e9630fe0f239dfd68dca4b3f05c2896a80a5f88, b6b04f7a8903e30963705eba6b294f5490b92692, cbce832777ecb89b7b923a00708224d2212b5eca, 7d3b46072165afd8ef5b648444cc05b281c98f37, fac12517fc1eba7c2542db42cc920d3e84682af8, 8aa008ec749b7e294328ff745860506cf4f83772, 2283303670f96fd85dc922d1cfae3c570acac165, a8219a1cc3f9163445defefe496f727c8a8e465c, ea60ff1ca5ff78f5f692437e4815f36e8832dcda, 784e99e54da2e7722708d3238783c6a478452500, 57a9e9f3f67c0d2ebc3d26f5475e764d52b89688, 4a6a12bb3ecf8b2d3c69b25a0a58df109de96c45, a4336d3d9db9e1ff8a12416281bf99d0c5fa11d3, 453f3a7024b323ebc5b853f4d8b063d3bfcbd093, 1361a24cd104bcbe44175b10ec5135fdee674587)
  • :white_check_mark: login: blumamir / name: Amir Blum (1764acf7c83c151da5fce4739904f3820636fdd3, 7f128607af7f65139d7f90ea89c29195c45fb876)

Hi @blumamir, wondering if you could have a look at this PR when you have some time?

ferrelucas avatar Mar 25 '24 02:03 ferrelucas

@open-telemetry/javascript-approvers Hi there, wondering if somebody could have a look at this PR?

ferrelucas avatar Apr 03 '24 13:04 ferrelucas

Hello, @blumamir and @open-telemetry/javascript-approvers just reaching out to check if anyone could have a look at this PR?

ferrelucas avatar Apr 19 '24 02:04 ferrelucas

@blumamir , I've update the PR to simplify the usage of args

Please let me know what you think Thanks!

ferrelucas avatar May 22 '24 11:05 ferrelucas

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 90.01%. Comparing base (dfb2dff) to head (a4336d3). Report is 139 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2009      +/-   ##
==========================================
- Coverage   90.97%   90.01%   -0.97%     
==========================================
  Files         146      140       -6     
  Lines        7492     6909     -583     
  Branches     1502     1457      -45     
==========================================
- Hits         6816     6219     -597     
- Misses        676      690      +14     

see 50 files with indirect coverage changes

codecov[bot] avatar May 22 '24 20:05 codecov[bot]

No worries! Thanks for the review! 🙌

ferrelucas avatar May 23 '24 01:05 ferrelucas

@ferrelucas let me know if you need any help fixing the CI issues. once it is green we can merge and release this fix

blumamir avatar May 23 '24 07:05 blumamir

@blumamir thanks, I have fixed the lint issues.

I just realized the Unit Test issues are giving the MongoServerError: Transaction numbers are only allowed on a replica set member or mongos error message.

I happen to not have this issue running the tests locally because I was already running a MongDB Replica Set. It seems we would need to update the MongoDB test setup to use replica sets so it can support transactions.

I can have a better look on how to do it soon - any directions on it would be helpful Thanks!

ferrelucas avatar May 23 '24 08:05 ferrelucas

Hi @blumamir I've taken one of the CI unit tests as example Run test-all-versions (20). I've seen that the MongoDB setup is in the step called Initialize containers, under Starting mongo service container.

It is running:

/usr/bin/docker create --name fa2fac6564d8478ebd791968ddae63b9_mongo_11c61e --label 62ce9a --network github_network_60aacba5f9dc4f70be079739b8ce42a8 --network-alias mongo -p 27017:27017  -e GITHUB_ACTIONS=true -e CI=true mongo

I think we could add the --replSet parameter to the command to start the MongoDB instance, then initialize the replica set.

I believe I don't have permissions to make these changes in the Github Actions Would it be feasible to do it on your side? Thanks!

ferrelucas avatar May 23 '24 13:05 ferrelucas

@blumamir thanks, I have fixed the lint issues.

I just realized the Unit Test issues are giving the MongoServerError: Transaction numbers are only allowed on a replica set member or mongos error message.

I happen to not have this issue running the tests locally because I was already running a MongDB Replica Set. It seems we would need to update the MongoDB test setup to use replica sets so it can support transactions.

I can have a better look on how to do it soon - any directions on it would be helpful Thanks!

perhaps we can use another option property instead of session to make sure it is passed correctly to the driver?

I can see you use this code to test passing both options and callback

await user.save({ session }, async () => {

we can consider using something else instead of session which is simpler and does not require replica set https://mongoosejs.com/docs/6.x/docs/api/model.html#model_Model-save

blumamir avatar May 23 '24 13:05 blumamir

I believe I don't have permissions to make these changes in the Github Actions Would it be feasible to do it on your side?

If I am not mistake, the mongo service is started here which you can edit, but I really think it would be easier to just use some other option for the test instead of session, if one of them is simpler.

blumamir avatar May 23 '24 13:05 blumamir

@blumamir thanks! I've updated the PR unit tests to use wtimeout instead of session in the options object

ferrelucas avatar May 24 '24 04:05 ferrelucas

Hey, @blumamir thanks again for the review! 🙌

ferrelucas avatar May 24 '24 07:05 ferrelucas