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

Feature: Log long running queries #4695

Open
rubiin opened this issue Sep 12, 2023 · 6 comments
Open

Feature: Log long running queries #4695

rubiin opened this issue Sep 12, 2023 · 6 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@rubiin
Copy link
Contributor

rubiin commented Sep 12, 2023

Is your feature request related to a problem? Please describe.
I'd like to be able to log slow queries to console/file and keep track of average query times. This will greatly help developers optimize their query as the Orm would inform them of any slow queries
Hibertnate supports this with the config option. Also typeorm seems to support it as well

Describe the solution you'd like
Add an option in config with maxExecutionTime in ms and log queries that exceed this time limit.

Describe alternatives you've considered
I have been using console.time and console.timeEnd but that is just the hacky way and is not precise

Additional context
Add any other context about the feature request here.
https://thorben-janssen.com/hibernate-slow-query-log/#activating-hibernates-slow-query-log
https://orkhan.gitbook.io/typeorm/docs/logging#log-long-running-queries

@rubiin rubiin added the enhancement New feature or request label Sep 12, 2023
@B4nan B4nan added help wanted Extra attention is needed good first issue Good for newcomers labels Sep 12, 2023
@rubiin
Copy link
Contributor Author

rubiin commented Sep 12, 2023

I think the easiest solution will be to add a log line here.
Here
We can then check if context.took >= maxExecutionTime and log in the query along with some arbitary message
such as SlowQuery: 26 milliseconds. SQL: 'SELECT * FROM Author a'

@B4nan
Copy link
Member

B4nan commented Sep 12, 2023

This could be a bit smarter, I'd say it should be a new logger namespace, and queries that will be over the threshold should be printed as warnings (so in orange or something like that) when query logging is enabled, when not (but the slow query logging is), only the slow queries should be logged. Moreover, we should have a way to configure separate logging for the slow queries, so users can dump those to a dedicated file, as they could be easily lost in the regular app logs.

So the changes should go in here:

protected logQuery(query: string, context: LogContext = {}): void {

The level should be conditionally set based on the query time to warning, and there will be some changes needed elsewhere too, so we don't print things twice for the slow queries.

@B4nan
Copy link
Member

B4nan commented Sep 12, 2023

Maybe @SpencerKaiser will have some opinions on this, as he did quite a lot of improvements in the logging in v6.

@SpencerKaiser
Copy link
Contributor

Super neat idea and I'd use it!

@isuvorov
Copy link

isuvorov commented Jan 7, 2024

Wonderful Idea!

Maybe you have one more, how to catch large requests (in bytes, not in row counts)?

context.size >= 100kb

@rubiin
Copy link
Contributor Author

rubiin commented Mar 29, 2024

@isuvorov I think this is more suited on the http side imo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants