Add $config['db']['annotate_queries'] option to include caller ID comments into queries. Comment can then be seen in process list, slow query logs on the server, aiding in tracking down unexpected slow queries.

SELECT /* queuedaemon.php Ostatus_profile->processPost */ * FROM notice WHERE ( notice.uri = 'http://stormcloud.local/mublog2/notice/479' )
INSERT /* POST Notice::saveNew */ INTO notice (profile_id , content ....
This commit is contained in:
Brion Vibber 2010-03-29 12:57:16 -07:00
parent 03f6706468
commit d44e5ac935
2 changed files with 69 additions and 0 deletions

View File

@ -330,6 +330,10 @@ class Memcached_DataObject extends Safe_DataObject
*/ */
function _query($string) function _query($string)
{ {
if (common_config('db', 'annotate_queries')) {
$string = $this->annotateQuery($string);
}
$start = microtime(true); $start = microtime(true);
$result = parent::_query($string); $result = parent::_query($string);
$delta = microtime(true) - $start; $delta = microtime(true) - $start;
@ -342,6 +346,70 @@ class Memcached_DataObject extends Safe_DataObject
return $result; return $result;
} }
/**
* Find the first caller in the stack trace that's not a
* low-level database function and add a comment to the
* query string. This should then be visible in process lists
* and slow query logs, to help identify problem areas.
*
* Also marks whether this was a web GET/POST or which daemon
* was running it.
*
* @param string $string SQL query string
* @return string SQL query string, with a comment in it
*/
function annotateQuery($string)
{
$ignore = array('annotateQuery',
'_query',
'query',
'get',
'insert',
'delete',
'update',
'find');
$ignoreStatic = array('staticGet',
'pkeyGet',
'cachedQuery');
$here = get_class($this); // if we get confused
$bt = debug_backtrace();
// Find the first caller that's not us?
foreach ($bt as $frame) {
$func = $frame['function'];
if (isset($frame['type']) && $frame['type'] == '::') {
if (in_array($func, $ignoreStatic)) {
continue;
}
$here = $frame['class'] . '::' . $func;
break;
} else if (isset($frame['type']) && $frame['type'] == '->') {
if ($frame['object'] === $this && in_array($func, $ignore)) {
continue;
}
if (in_array($func, $ignoreStatic)) {
continue; // @fixme this shouldn't be needed?
}
$here = get_class($frame['object']) . '->' . $func;
break;
}
$here = $func;
break;
}
if (php_sapi_name() == 'cli') {
$context = basename($_SERVER['PHP_SELF']);
} else {
$context = $_SERVER['REQUEST_METHOD'];
}
// Slip the comment in after the first command,
// or DB_DataObject gets confused about handling inserts and such.
$parts = explode(' ', $string, 2);
$parts[0] .= " /* $context $here */";
return implode(' ', $parts);
}
// Sanitize a query for logging // Sanitize a query for logging
// @fixme don't trim spaces in string literals // @fixme don't trim spaces in string literals
function sanitizeQuery($string) function sanitizeQuery($string)

View File

@ -72,6 +72,7 @@ $default =
'quote_identifiers' => false, 'quote_identifiers' => false,
'type' => 'mysql', 'type' => 'mysql',
'schemacheck' => 'runtime', // 'runtime' or 'script' 'schemacheck' => 'runtime', // 'runtime' or 'script'
'annotate_queries' => false, // true to add caller comments to queries, eg /* POST Notice::saveNew */
'log_queries' => false, // true to log all DB queries 'log_queries' => false, // true to log all DB queries
'log_slow_queries' => 0), // if set, log queries taking over N seconds 'log_slow_queries' => 0), // if set, log queries taking over N seconds
'syslog' => 'syslog' =>