Skip to content

feat: MongoDB Tracing Support - #3072

Merged
HazAT merged 10 commits into
masterfrom
node-mongo
Dec 4, 2020
Merged

feat: MongoDB Tracing Support#3072
HazAT merged 10 commits into
masterfrom
node-mongo

Conversation

@kamilogorek

@kamilogorekkamilogorek commented Nov 24, 2020

Copy link
Copy Markdown
Contributor

MongoDB tracing support.

Usage:

constSentry=require("@sentry/node");constTracing=require("@sentry/tracing");constmongodb=require("mongodb");Sentry.init({dsn: "__PUBLIC_DSN__",integrations: [newTracing.Integrations.Mongo(),],tracesSampleRate: 1.0,});constclient=newmongodb.MongoClient("mongodb://mongo:docker@localhost:27017");// the rest of the code, for raw-node code we need manual instrumentation, eg.asyncfunctionrun(){consttransaction=Sentry.startTransaction({op: "transaction",name: "My Transaction",});Sentry.configureScope((scope)=>{scope.setSpan(transaction);});try{awaitclient.connect();constdatabase=awaitclient.db("admin");constcollection=database.collection("movies");awaitcollection.insertOne({title: "Rick and Morty"});awaitcollection.findOne({title: "Back to the Future"});awaitcollection.updateOne({title: "Back to the Future"},{$set: {title: "South Park"}});awaitcollection.findOne({title: "South Park"});}finally{if(transaction)transaction.finish();awaitclient.close();}}run().catch(console.dir);

Works nicely with Express too:

constSentry=require("@sentry/node");constTracing=require("@sentry/tracing");constmongodb=require("mongodb");constexpress=require("express");Sentry.init({dsn: "__PUBLIC_DSN__",integrations: [newSentry.Integrations.Http({tracing: true}),newTracing.Integrations.Express({app: express.Router,methods: ["get"],}),newTracing.Integrations.Mongo(),],tracesSampleRate: 1.0,});constapp=express();constclient=newmongodb.MongoClient("mongodb://mongo:docker@localhost:27017");client.connect();app.use(Sentry.Handlers.requestHandler());app.use(Sentry.Handlers.tracingHandler());app.use(express.json());app.get("/hi",asyncfunctionsayHi(req,res,next){try{constdatabase=awaitclient.db("admin");constcollection=database.collection("movies");awaitcollection.insertOne({title: "Rick and Morty"});constresult=awaitcollection.findOne({title: "Rick and Morty"});res.send(result);}catch(err){next(err);}});app.use(Sentry.Handlers.errorHandler());app.listen(3000);

@github-actions

github-actionsBot commented Nov 24, 2020

Copy link
Copy Markdown
Contributor

size-limit report

PathSize
@sentry/browser - CDN Bundle (gzipped)19.73 KB (-0.01% 🔽)
@sentry/browser - Webpack20.6 KB (0%)
@sentry/react - Webpack20.6 KB (0%)
@sentry/browser + @sentry/tracing - CDN Bundle (gzipped)26.88 KB (0%)

@kamilogorek
kamilogorekforce-pushed the node-mongo branch 4 times, most recently from 6efe345 to 8926f90CompareDecember 1, 2020 14:05
@kamilogorek
kamilogorek marked this pull request as ready for review December 1, 2020 14:05
Comment threadpackages/tracing/src/integrations/mongo.ts
Comment threadpackages/tracing/src/integrations/mongo.ts Outdated
Comment threadpackages/tracing/src/integrations/mongo.ts Outdated
Comment threadpackages/tracing/src/integrations/mongo.ts
Comment threadpackages/tracing/src/integrations/mongo.ts
* Patches original collection methods
*/
private _instrumentOperations(collection: MongoCollection, operations: Operation[], getCurrentHub: () => Hub): void {
operations.forEach((operation: Operation) => this._patchOperation(collection, operation, getCurrentHub));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why pass in getCurrentHub?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is how integrations work internally, the function will be injected from outside and we should use that vs. global getCurrentHub.

Comment threadpackages/tracing/src/integrations/mongo.ts Outdated
Comment threadpackages/tracing/src/integrations/mongo.ts Outdated
public name: string = Mongo.id;

private _operations: Operation[];
private _describeOperations?: boolean | Operation[];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is "describe" your word or theirs? If it's yours, I wonder if we might name this option differently, to a) differentiate it from the description field in a transaction, and b) indicate that what it's really about is recording the operation's inputs.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think describe is used in database lingo.

@HazAT

HazAT commented Dec 4, 2020

Copy link
Copy Markdown
Member

Merging this now, has also few other fixes.

@HazAT
HazAT merged commit f7fc733 into masterDec 4, 2020
@HazAT
HazAT deleted the node-mongo branch December 4, 2020 11:38
@Copdate-Copdate

Copy link
Copy Markdown

Hi, question about MongoDB support.

I'm currently using Mongoose inside an Express app and when adding new Tracing.Integrations.Mongo() to integrations array in Sentry.init, I got the following error:

UnhandledPromiseRejectionWarning: TypeError: Tracing.Integrations.Mongo is not a constructor

Do I have to pass some config or it just doesn't support Mongoose right now?

@sol-austin

Copy link
Copy Markdown

I can't seem to get this working with Mongoose. I've added Tracing.Integrations.Mongo() to the integrations array without receiving an error message but it doesn't seem to be tracking database requests at all.

Has anyone successfully got this working with Mongoose yet?

@KevinEdry

Copy link
Copy Markdown

Same here, would love to see mongoose support for this.

Sign up for freeto 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.

6 participants

@kamilogorek@HazAT@Copdate-Copdate@sol-austin@KevinEdry@lobsterkatie