Allow the database query log to be dispatched as log events

Created on 4 October 2022, about 2 years ago
Updated 19 March 2023, almost 2 years ago

Problem/Motivation

When instrumenting a Drupal-based project, it's helpful to give performance tracing/profiling code real-time insight into operations and their sub-operations. For example, rendering one template might cause an another template to render, which causes a database query to retrieve some data from the cache. This hierarchy of parent and child operations (aka spans) can be visualized by the profiler.

The database subsystem has a built-in query log which allows database queries to be collected in a log and later retrieved by a profiler, but doesn't do anything to allow profilers to fire off a callback as queries execute. As a result, profilers can't easily build that nice hierarchical view of operations, whereas this is easily doable for other subsystems, such as Twig, which allows extensions to execute code as templates are rendered.

Proposed resolution

When database query logging is enabled will now an event being dispatched at the start of the query and at the end of the query. This will allow profilers to collect and log database query starts and ends. This can than be combined with other logged data to better analyse and debug Drupal.

Remaining tasks

Review, write tests, add change record.

User interface changes

None.

API changes

API additions:

  1. The method Drupal\Core\Database\Log::logFromEvent() adds database query loggings events data to the query log.
  2. The method Drupal\Core\Database\Connection::isEventEnabled() returns the status of the database API events being dispatched.
  3. The methods Drupal\Core\Database\Connection::enableEvents() and Drupal\Core\Database\Connection::disableEvents() enable or disable the dispatching of database API events.
  4. The method Drupal\Core\Database\Connection::dispatchEvent() is used to throw database API events.
  5. The method Drupal\Core\Database\Connection::findCallerFromDebugBacktrace() is used get the last non-database call from the debug backtrace data.
  6. The method Drupal\Core\Database\Connection::removeDatabaseEntriesFromDebugBacktrace() is used remove database calls from the debug backtrace data.

API deprecations:

  1. The method Drupal\Core\Database\Log::log() and is replaced by the new method Drupal\Core\Database\Log::logFromEvent().
  2. The method Drupal\Core\Database\Log::findCaller() and is replaced by the new method Drupal\Core\Database\Connection::findCallerFromDebugBacktrace()
  3. The method Drupal\Core\Database\Log::removeDatabaseEntries() and is replaced by the new method Drupal\Core\Database\Connection::removeDatabaseEntriesFromDebugBacktrace()
  4. The method Drupal\Core\Database\Log::getDebugBacktrace() and there is no replacement.
  5. Data model changes

    None.

    Release notes snippet

    Improved performance tracing and debugging capabilities of the database layer by allowing events to be triggered on statement execution. See the related change record โ†’ for a full description of this enhancement.

โœจ Feature request
Status

Fixed

Version

10.1 โœจ

Component
Databaseย  โ†’

Last updated 2 days ago

  • Maintained by
  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands @daffie
Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States mfb San Francisco

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024