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

[12.x] Add QueriesExecuted to expose cumulative query information #54674

Closed

Conversation

LogicSatinn
Copy link
Contributor

⚠️ BREAKING CHANGE: This PR changes the event type dispatched by DB::whenQueryingForLongerThan() and requires DB::enableQueryLog() to be called.

Background

While investigating long-running queries using DB::whenQueryingForLongerThan(), I discovered that the method only provides information about individual queries. This can be misleading since the threshold is actually triggered by cumulative query time across the request lifecycle, not just a single long query.

For example, if you set a threshold of 500ms:

  • A single 600ms query will trigger the method
  • Ten 60ms queries (totaling 600ms) will also trigger it
  • However, in the second case, you only see details for one of those queries (probably the last query before the method is triggered)

Changes

This PR introduces:

  1. A new QueriesExecuted class that replaces QueryExecuted in the whenQueryingForLongerThan callback
  2. The new event provides:
  • List of all queries executed up to the threshold with individual query metrics and bindings
  • Database connection details

Why This Matters

While Laravel does provide getQueryLog() and totalQueryDuration(), developers new to the framework may not discover these methods without diving into the source code. Having comprehensive query information directly in the method from the event makes the feature more intuitive and useful.

Breaking Changes

  • whenQueryingForLongerThan() now passes QueriesExecuted instead of QueryExecuted
  • Existing callback implementations will need to be updated to handle the new event type
  • Query logging must be explicitly enabled via DB::enableQueryLog() for this feature to function

Note

This PR builds upon and extends the groundwork laid in #42744. Many thanks to @timacdonald for implementing the query duration monitoring feature that made this enhancement possible.

Copy link

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@LogicSatinn
Copy link
Contributor Author

See laravel/docs#10179 for updated docs.

@LogicSatinn LogicSatinn marked this pull request as ready for review February 18, 2025 16:30
@crynobone crynobone changed the base branch from master to 12.x February 18, 2025 22:01
@taylorotwell
Copy link
Member

I would rather not have the breaking change here since I think the benefit is relatively minor.

@LogicSatinn LogicSatinn deleted the feature/queries-executed-event branch February 19, 2025 22:28
Sign up for free to 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.

2 participants