Skip to content

Commit

Permalink
Refactor SQL parsing in Tracing classes
Browse files Browse the repository at this point in the history
  • Loading branch information
huangdijia committed Apr 16, 2024
1 parent ccf508b commit c0527ff
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
"hyperf/http-server": "~3.1.0",
"hyperf/support": "~3.1.0",
"hyperf/tappable": "~3.1.0",
"phpmyadmin/sql-parser": "^5.9",
"sentry/sentry": "^4.4.0"
},
"suggest": {
"elasticsearch/elasticsearch": "Required to use the elasticsearch client (^7.0|^8.0).",
"hyperf/amqp": "Required to use the amqp event (~3.1.9).",
"hyperf/crontab": "Required to use the crontab event (~3.1.7).",
"hyperf/database": "Required to use the crontab event (~3.1.0).",
"elasticsearch/elasticsearch": "Required to use the elasticsearch client (^7.0|^8.0)."
"phpmyadmin/sql-parser": "Required to use the sql parser (^5.9)."
},
"config": {
"sort-packages": true
Expand Down
10 changes: 3 additions & 7 deletions src/Tracing/Aspect/DbAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,32 +52,29 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
}

$arguments = $proceedingJoinPoint->arguments['keys'];

$poolName = (fn () => $this->poolName)->call($proceedingJoinPoint->getInstance());
/** @var \Hyperf\Pool\Pool $pool */
$pool = $this->container->get(PoolFactory::class)->getPool($poolName);

$operation = $arguments['name'];
$database = '';
$driver = 'unknown';
$table = '';
if ($pool instanceof \Hyperf\DB\Pool\Pool) {
$config = $pool->getConfig();

$database = $config['database'] ?? '';
$driver = $config['driver'] ?? 'unknown';
}

if (! empty($arguments['arguments']['query'])) {
$table = SqlParser::parse($arguments['arguments']['query'])['tables'];
if (! empty($sql = $arguments['arguments']['query'])) {
$table = SqlParser::parse($sql)['tables'];
if ($table) {
$table = '.' . $table;
}
}

// 规则: operation dbName.tableName
$op = sprintf('%s %s%s', $operation, $database, $table);
$description = sprintf('%s::%s()', $proceedingJoinPoint->className, $arguments['name']);
$description = $sql;

// Already check in the previous context
/** @var \Sentry\Tracing\Span $span */
Expand All @@ -89,7 +86,6 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
'db.name' => $database,
'db.collection.name' => $table,
'db.operation.name' => $database,

'db.pool.name' => $poolName,
'db.pool.max' => $pool->getOption()->getMaxConnections(),
'db.pool.max_idle_time' => $pool->getOption()->getMaxIdleTime(),
Expand Down
14 changes: 9 additions & 5 deletions src/Tracing/Listener/TracingDbQueryListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,20 @@ protected function queryExecutedHandler(object $event): void
return;
}

$sqlParse = SqlParser::parse($event->sql);

$data = [
'coroutine.id' => Coroutine::id(),
'db.system' => $event->connection->getDriverName(),
'db.name' => $event->connection->getDatabaseName(),
'db.collection.name' => $sqlParse['tables'],
'db.operation.name' => $sqlParse['operation'],
];

$sqlParse = SqlParser::parse($event->sql);
if (! empty($sqlParse['tables'])) {
$data['db.collection.name'] = $sqlParse['tables'];
}
if (! empty($sqlParse['operation'])) {
$data['db.operation.name'] = $sqlParse['operation'];
}

foreach ($event->bindings as $key => $value) {
$data['db.parameter.' . $key] = $value;
}
Expand All @@ -89,7 +93,7 @@ protected function queryExecutedHandler(object $event): void

$startTimestamp = microtime(true) - $event->time / 1000;

// 规则: opeate dbName.tableName
// 规则: operate dbName.tableName
$op = sprintf(
'%s %s%s',
$sqlParse['operation'],
Expand Down
3 changes: 1 addition & 2 deletions src/Util/SqlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ class SqlParser
*/
public static function parse(string $sql): array
{
if (empty($sql)) {
if (empty($sql) || ! class_exists('PhpMyAdmin\SqlParser\Parser')) {
return [
'operation' => '',
'tables' => '',
];
}

$parser = new \PhpMyAdmin\SqlParser\Parser($sql);

$operation = $parser->list[0]->keyword;
$table = [];

Expand Down

0 comments on commit c0527ff

Please sign in to comment.