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

Support MongoDB "query" logging #11419

Closed
Tracked by #8788
matthewmueller opened this issue Jan 26, 2022 · 4 comments · Fixed by prisma/prisma-engines#2752
Closed
Tracked by #8788

Support MongoDB "query" logging #11419

matthewmueller opened this issue Jan 26, 2022 · 4 comments · Fixed by prisma/prisma-engines#2752
Assignees
Labels
kind/feature A request for a new feature. team/client Issue for team Client. tech/engines Issue for tech Engines. topic: logging topic: mongodb
Milestone

Comments

@matthewmueller
Copy link
Contributor

matthewmueller commented Jan 26, 2022

Problem

Right now we don't have a way to log the underlying querying run against a MongoDB database. This will be important because the Prisma way of querying is higher-level than the database query and often it's not clear how you'd map a Prisma query to the underlying query.

Suggested Solution

We already support with other databases via

const prisma = new PrismaClient({
  log: ['query'],
})

We should investigate supporting this for MongoDB too. One possible suggestion would be to see how other ORMs like Mongoose do logging.

@janpio janpio added kind/feature A request for a new feature. team/client Issue for team Client. topic: logging topic: mongodb labels Jan 26, 2022
@matthewmueller matthewmueller added the tech/engines Issue for tech Engines. label Feb 10, 2022
@matthewmueller
Copy link
Contributor Author

Looks like it's possible to send queries from the MongoDB driver with https://docs.rs/mongodb/latest/mongodb/event/command/trait.CommandEventHandler.html

Internal Context: https://prisma-company.slack.com/archives/C0239SYUZV1/p1645130088849539

@matthewmueller
Copy link
Contributor Author

matthewmueller commented Feb 22, 2022

Talked with @janpio a bit about it and here's an idea that might be simpler that's more similar to what we currently do with logging SQL queries.

Before each of the mongodb driver calls, we serialize into a JSON log. Something like:

logger.query(JSON.stringify({
  findOne: "Pets",
  document: document
}))

// Actual driver call
driver.db("Pets").findOne(document)

This isn't going to be a standard MongoDB log, but they don't really have a standard. I think we could use the same format we have for $runCommandRaw.

As with SQL, this logger would be enabled by:

const prisma = new PrismaClient({
  log: ['query'],
})

Goal is to help people understand the underlying queries were run. Bonus points if you can copy and paste it somewhere without changing the format, so it's easier to test the query manually.

I prefer what @dpetrick mentioned about having it be in the format of the mongodb shell over the $runCommandRaw described above, but $runCommandRaw format (or some other logical format) would be fine too.

It does seem like we'll need some way to deal with newlines in the queries and newlines as separators. For that, I can share what I've used before: http://ndjson.org/, but I don't have an opinion on this.

@matthewmueller
Copy link
Contributor Author

matthewmueller commented Feb 22, 2022

  • Have a quick kickoff
  • Decide on the MongoDB format

@matthewmueller matthewmueller added this to the 3.11.0 milestone Feb 22, 2022
@cprieto
Copy link

cprieto commented Feb 24, 2022

We will be using something that would be useful for the mongo shell. Other libraries (like Mongoose) use a similar approach.

cprieto added a commit to prisma/prisma-engines that referenced this issue Mar 11, 2022
Support for query logging for MongoDB queries, we use the same format used by the Mongo Shell and Mongoose, so the query could be executed directly in the Mongo shell. A few important things need to be noted:

In DEBUG builds, the queries would be indented for easy reading
In RELEASE builds, the queries are plain, without indentation or car returns
This works exactly like the existing SQL query logging but a few details are not included:

Query execution time (maybe in a near future)
Query parameters (we don't use it)
When we retake logging this would be an area where things could be heavily simplified using spans for query logging. Another important improvement would be using a logging macro instead of the current functions.

This closes prisma/prisma#11419
Weakky pushed a commit to prisma/prisma-engines that referenced this issue Mar 11, 2022
Support for query logging for MongoDB queries, we use the same format used by the Mongo Shell and Mongoose, so the query could be executed directly in the Mongo shell. A few important things need to be noted:

In DEBUG builds, the queries would be indented for easy reading
In RELEASE builds, the queries are plain, without indentation or car returns
This works exactly like the existing SQL query logging but a few details are not included:

Query execution time (maybe in a near future)
Query parameters (we don't use it)
When we retake logging this would be an area where things could be heavily simplified using spans for query logging. Another important improvement would be using a logging macro instead of the current functions.

This closes prisma/prisma#11419
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature A request for a new feature. team/client Issue for team Client. tech/engines Issue for tech Engines. topic: logging topic: mongodb
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants