-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\Middleware\Doctrine\Metric; | ||
|
||
use Doctrine\DBAL\Driver\Middleware\AbstractConnectionMiddleware; | ||
|
||
class Connection extends AbstractConnectionMiddleware | ||
{ | ||
} |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\Middleware\Doctrine\Metric; | ||
|
||
use Doctrine\DBAL\Driver as DriverInterface; | ||
use Doctrine\DBAL\Driver\Middleware as MiddlewareInterface; | ||
|
||
class Middleware implements MiddlewareInterface | ||
{ | ||
public function wrap(DriverInterface $driver): DriverInterface | ||
{ | ||
return new Driver($driver); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\Middleware\Doctrine\Trace; | ||
|
||
use Doctrine\DBAL\Driver\Connection as ConnectionInterface; | ||
use Doctrine\DBAL\Driver\Middleware\AbstractConnectionMiddleware; | ||
use Doctrine\DBAL\Driver\Result; | ||
use Doctrine\DBAL\Driver\Statement as DriverStatement; | ||
use OpenTelemetry\API\Trace\SpanInterface; | ||
use OpenTelemetry\API\Trace\TracerInterface; | ||
use OpenTelemetry\Context\ScopeInterface; | ||
|
||
class Connection extends AbstractConnectionMiddleware | ||
{ | ||
public function __construct( | ||
private ConnectionInterface $connection, | ||
Check failure on line 16 in src/Middleware/Doctrine/Trace/Connection.php GitHub Actions / PHPStan (PHP 8.3)
|
||
private TracerInterface $tracer, | ||
private ?SpanInterface $span = null, | ||
private ?ScopeInterface $scope = null, | ||
) { | ||
parent::__construct($connection); | ||
} | ||
|
||
public function __destruct() | ||
{ | ||
if (null === $this->scope) { | ||
return; | ||
} | ||
$this->scope->detach(); | ||
|
||
if (null === $this->span) { | ||
return; | ||
} | ||
$this->span->end(); | ||
} | ||
|
||
public function prepare(string $sql): DriverStatement | ||
{ | ||
return new Statement( | ||
parent::prepare($sql), | ||
$this->tracer, | ||
$sql, | ||
); | ||
} | ||
|
||
public function query(string $sql): Result | ||
{ | ||
return parent::query($sql); | ||
} | ||
|
||
public function exec(string $sql): int | ||
{ | ||
return parent::exec($sql); | ||
} | ||
|
||
public function beginTransaction(): bool | ||
{ | ||
return parent::beginTransaction(); | ||
} | ||
|
||
public function commit(): bool | ||
{ | ||
return parent::commit(); | ||
} | ||
|
||
public function rollBack(): bool | ||
{ | ||
return parent::rollBack(); | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\Middleware\Doctrine\Trace; | ||
|
||
use Doctrine\DBAL\Connection as ConnectionInterface; | ||
use Doctrine\DBAL\Driver as DriverInterface; | ||
use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware; | ||
use Doctrine\DBAL\DriverManager; | ||
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform; | ||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Platforms\DB2Platform; | ||
use Doctrine\DBAL\Platforms\OraclePlatform; | ||
use Doctrine\DBAL\Platforms\PostgreSQLPlatform; | ||
use Doctrine\DBAL\Platforms\SqlitePlatform; | ||
use Doctrine\DBAL\Platforms\SQLServerPlatform; | ||
use OpenTelemetry\API\Trace\SpanKind; | ||
use OpenTelemetry\API\Trace\TracerInterface; | ||
use OpenTelemetry\Context\Context; | ||
use OpenTelemetry\SemConv\TraceAttributes; | ||
|
||
/** | ||
* Types extracted from \Doctrine\DBAL\DriverManager. | ||
* | ||
* @phpstan-type OverrideParams array{ | ||
* application_name?: string, | ||
* charset?: string, | ||
* dbname?: string, | ||
* default_dbname?: string, | ||
* driver?: key-of<DriverManager::DRIVER_MAP>, | ||
* driverClass?: class-string<DriverInterface>, | ||
* driverOptions?: array<int, mixed>, | ||
* host?: string, | ||
* password?: string, | ||
* path?: string, | ||
* persistent?: bool, | ||
* platform?: AbstractPlatform, | ||
* port?: int, | ||
* serverVersion?: string, | ||
* url?: string, | ||
* user?: string, | ||
* unix_socket?: string, | ||
* } | ||
* @phpstan-type Params array{ | ||
* application_name?: string, | ||
* charset?: string, | ||
* dbname?: string, | ||
* defaultTableOptions?: array<string, mixed>, | ||
* default_dbname?: string, | ||
* driver?: key-of<DriverManager::DRIVER_MAP>, | ||
* driverClass?: class-string<DriverInterface>, | ||
* driverOptions?: array<int, mixed>, | ||
* host?: string, | ||
* keepSlave?: bool, | ||
* keepReplica?: bool, | ||
* master?: OverrideParams, | ||
* memory?: bool, | ||
* password?: string, | ||
* path?: string, | ||
* persistent?: bool, | ||
* platform?: AbstractPlatform, | ||
* port?: int, | ||
* primary?: OverrideParams, | ||
* replica?: array<OverrideParams>, | ||
* serverVersion?: string, | ||
* sharding?: array<string,mixed>, | ||
* slaves?: array<OverrideParams>, | ||
* url?: string, | ||
* user?: string, | ||
* wrapperClass?: class-string<ConnectionInterface>, | ||
* unix_socket?: string, | ||
* } | ||
*/ | ||
final class Driver extends AbstractDriverMiddleware | ||
{ | ||
public function __construct( | ||
DriverInterface $driver, | ||
private TracerInterface $tracer | ||
) { | ||
parent::__construct($driver); | ||
} | ||
|
||
/** | ||
* @param Params $params | ||
*/ | ||
public function connect( | ||
Check failure on line 85 in src/Middleware/Doctrine/Trace/Driver.php GitHub Actions / PHPStan (PHP 8.3)
Check failure on line 85 in src/Middleware/Doctrine/Trace/Driver.php GitHub Actions / PHPStan (PHP 8.3)
Check failure on line 85 in src/Middleware/Doctrine/Trace/Driver.php GitHub Actions / PHPStan (PHP 8.3)
Check failure on line 85 in src/Middleware/Doctrine/Trace/Driver.php GitHub Actions / PHPStan (PHP 8.3)
Check failure on line 85 in src/Middleware/Doctrine/Trace/Driver.php GitHub Actions / PHPStan (PHP 8.3)
Check failure on line 85 in src/Middleware/Doctrine/Trace/Driver.php GitHub Actions / PHPStan (PHP 8.3)
|
||
#[\SensitiveParameter] | ||
array $params | ||
) { | ||
$databasePlatform = $this->getDatabasePlatform(); | ||
$spanBuilder = $this->tracer | ||
->spanBuilder('doctrine.dbal.middleware.driver') | ||
->setAttribute(TraceAttributes::DB_SYSTEM, $this->getSemanticDbSystem()) | ||
->setAttribute(TraceAttributes::DB_CONNECTION_STRING, $params['url']) | ||
->setAttribute(TraceAttributes::DB_NAME, $params['dbname']) | ||
->setAttribute(TraceAttributes::DB_USER, $params['user']) | ||
; | ||
|
||
$spanBuilder->setSpanKind(SpanKind::KIND_SERVER); | ||
|
||
$parent = Context::getCurrent(); | ||
|
||
$span = $spanBuilder->setParent($parent)->startSpan(); | ||
$scope = $span->storeInContext($parent)->activate(); | ||
|
||
return new Connection( | ||
parent::connect($params), | ||
$this->tracer, | ||
$span, | ||
$scope, | ||
); | ||
} | ||
|
||
private function getSemanticDbSystem(): string | ||
{ | ||
// https://github.com/open-telemetry/semantic-conventions/blob/main/docs/attributes-registry/db.md | ||
return match (get_class($this->getDatabasePlatform())) { | ||
AbstractMySQLPlatform::class => 'mysql', | ||
DB2Platform::class => 'db2', | ||
OraclePlatform::class => 'oracle', | ||
PostgreSQLPlatform::class => 'postgresql', | ||
SqlitePlatform::class => 'sqlite', | ||
SQLServerPlatform::class => 'mssql', | ||
default => 'other_sql', | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\Middleware\Doctrine\Trace; | ||
|
||
use Doctrine\DBAL\Driver as DriverInterface; | ||
use Doctrine\DBAL\Driver\Middleware as MiddlewareInterface; | ||
use OpenTelemetry\API\Trace\TracerInterface; | ||
|
||
final class Middleware implements MiddlewareInterface | ||
{ | ||
public function __construct(private readonly TracerInterface $tracer) | ||
{ | ||
} | ||
|
||
public function wrap(DriverInterface $driver): DriverInterface | ||
{ | ||
return new Driver($driver, $this->tracer); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\Middleware\Doctrine\Trace; | ||
|
||
use Doctrine\DBAL\Driver\Middleware\AbstractStatementMiddleware; | ||
use Doctrine\DBAL\Driver\Result; | ||
use Doctrine\DBAL\Driver\Statement as StatementInterface; | ||
use OpenTelemetry\API\Trace\TracerInterface; | ||
|
||
final class Statement extends AbstractStatementMiddleware | ||
{ | ||
public function __construct( | ||
private StatementInterface $statement, | ||
Check failure on line 13 in src/Middleware/Doctrine/Trace/Statement.php GitHub Actions / PHPStan (PHP 8.3)
|
||
private TracerInterface $tracer, | ||
Check failure on line 14 in src/Middleware/Doctrine/Trace/Statement.php GitHub Actions / PHPStan (PHP 8.3)
|
||
private string $sql, | ||
Check failure on line 15 in src/Middleware/Doctrine/Trace/Statement.php GitHub Actions / PHPStan (PHP 8.3)
|
||
) { | ||
parent::__construct($statement); | ||
} | ||
|
||
public function execute($params = null): Result | ||
{ | ||
return parent::execute($params); | ||
} | ||
} |
This file was deleted.
This file was deleted.
This file was deleted.