Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/sentry/src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Constants
{
public const TRACE_CARRIER = 'sentry.tracing.trace_carrier';

public const TRACE_DB_USE_READ_PDO = 'sentry.tracing.db.use_read_pdo';

public const TRACE_DB_SERVER_ADDRESS = 'sentry.tracing.db.server.address';

public const TRACE_DB_SERVER_PORT = 'sentry.tracing.db.server.port';
Expand Down
7 changes: 6 additions & 1 deletion src/sentry/src/Tracing/Aspect/DbConnectionAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ class DbConnectionAspect extends AbstractAspect

public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
return tap($proceedingJoinPoint->process(), function ($pdo) {
return tap($proceedingJoinPoint->process(), function ($pdo) use ($proceedingJoinPoint) {
if ($proceedingJoinPoint->methodName === 'getPdoForSelect') {
$arguments = $proceedingJoinPoint->arguments['keys'] ?? [];
Context::set(Constants::TRACE_DB_USE_READ_PDO, $arguments['useReadPdo'] ?? false);
}

Context::getOrSet(self::class, function () use ($pdo) {
$connectionStatus = $pdo->getAttribute(PDO::ATTR_CONNECTION_STATUS);
[$host] = explode(' ', $connectionStatus);
Expand Down
21 changes: 19 additions & 2 deletions src/sentry/src/Tracing/Listener/EventHandleListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,32 @@ protected function handleDbQueryExecuted(DbEvent\QueryExecuted $event): void
$data['db.collection.name'] = $sqlParse['table'];
}

// Get port from config
$host = (string) Context::get(Constants::TRACE_DB_SERVER_ADDRESS, 'localhost');
if (! Context::has(Constants::TRACE_DB_SERVER_PORT)) {
$useReadPdo = (bool) Context::get(Constants::TRACE_DB_USE_READ_PDO, false);
$dbConfig = (fn () => $this->config ?? ['host' => $host, 'port' => 3306])->call($event->connection);
$hosts = $dbConfig['write']['host'] ?? [$dbConfig['host']];
$ports = $dbConfig['write']['port'] ?? [$dbConfig['port']];
if ($useReadPdo) {
$hosts = $dbConfig['read']['host'] ?? $hosts;
$ports = $dbConfig['read']['port'] ?? $ports;
}
$index = array_search($host, $hosts, true);
$port = $ports[$index] ?? $ports[0] ?? 3306;
} else {
$port = (int) Context::get(Constants::TRACE_DB_SERVER_PORT);
}

$pool = $this->container->get(PoolFactory::class)->getPool($event->connectionName);
$data += [
'db.pool.name' => $event->connectionName,
'db.pool.max' => $pool->getOption()->getMaxConnections(),
'db.pool.max_idle_time' => $pool->getOption()->getMaxIdleTime(),
'db.pool.idle' => $pool->getConnectionsInChannel(),
'db.pool.using' => $pool->getCurrentConnections(),
'server.address' => (string) Context::get(Constants::TRACE_DB_SERVER_ADDRESS, 'localhost'),
'server.port' => (int) Context::get(Constants::TRACE_DB_SERVER_PORT, 3306),
'server.address' => $host,
'server.port' => $port,
];

if ($this->feature->isTracingTagEnabled('db.sql.bindings', true)) {
Expand Down