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

Transaction is not used in Mongoose create operation after OpenTelemetry auto-instrumentation is added #1678

Closed
juliana-tse opened this issue Sep 12, 2023 · 4 comments · Fixed by #2009
Assignees
Labels
bug Something isn't working priority:p1 Bugs which cause problems in end-user applications such as crashes, data inconsistencies

Comments

@juliana-tse
Copy link

juliana-tse commented Sep 12, 2023

What version of OpenTelemetry are you using?

"@opentelemetry/api": "^1.5.0",
"@opentelemetry/auto-instrumentations-node": "^0.39.2",

What version of Node are you using?

v16.15.1

What did you do?

When running some database operation inside the transaction

const User = db.model('User', new Schema({ name: String }));
const session = await User.startSession();
await session.startTransaction();
const [newStaff] = await User.create([ {name: 'staffA'} ], {session});
await session.abortTransaction();
await session.endSession();

What did you expect to see?

It is expected the transaction will be aborted, and user with name "staffA" will not be created in the database.

What did you see instead?

The transaction was not rolled back, and the record was created in the database.

Additional Info

I am using "mongoose": "^6.2.1"
The transaction works with findOneAndUpdate, for example the user name is not changed to staffB in the following case

const User = db.model('User', new Schema({ name: String }));
const [newStaff] = await User.create([ {name: 'staffA'} ]);
const session = await User.startSession();
await session.startTransaction();
await User.findOneAndUpdate({name: 'staffA'}, {name: 'staffB'}).session(session);
await session.abortTransaction();
await session.endSession();

However, when session is used in the create operation, the lastUse in serverSession is not updated across the whole function, which means the session is not used at all.

@juliana-tse juliana-tse added the bug Something isn't working label Sep 12, 2023
@juliana-tse juliana-tse changed the title Mongoose transaction committed onError after added OpenTelemetry auto-instrumentation Transaction is not used in Mongoose create operation after added OpenTelemetry auto-instrumentation Sep 12, 2023
@juliana-tse juliana-tse changed the title Transaction is not used in Mongoose create operation after added OpenTelemetry auto-instrumentation Transaction is not used in Mongoose create operation after OpenTelemetry auto-instrumentation is added Sep 12, 2023
@pichlermarc pichlermarc added the priority:p1 Bugs which cause problems in end-user applications such as crashes, data inconsistencies label Sep 13, 2023
@juliana-tse
Copy link
Author

Hi, is there any updates on this issue? This is blocking me from using auto-instrumentation.

@benthayer
Copy link

I am experiencing this issue also. It breaks critical portions of our code, so we're unable to use it

@benthayer
Copy link

In mongoose.ts,

const contextCaptureFunctions = [
  'remove',
  'deleteOne',
  'deleteMany',
  'find',
  'findOne',
  'estimatedDocumentCount',
  'countDocuments',
  'count',
  'distinct',
  'where',
  '$where',
  'findOneAndUpdate',
  'findOneAndDelete',
  'findOneAndReplace',
  'findOneAndRemove',
];

Maybe a partial solution is to add the create function here also?

@ferrelucas
Copy link
Contributor

Hi, I believe I figured what's causing the issue.

The mongoose instrumentation would unintentionally change the behaviour of some Model methods like save() and remove() by not passing the original function arguments - options (that contains the session to handle transactions) and callback - to the final call after instrumentation.

I have just opened a PR addressing this here: #2009

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working priority:p1 Bugs which cause problems in end-user applications such as crashes, data inconsistencies
Projects
None yet
5 participants