Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
gaelreyrol committed Jan 4, 2024
1 parent 128b2b7 commit 6b81d51
Show file tree
Hide file tree
Showing 14 changed files with 264 additions and 66 deletions.
9 changes: 9 additions & 0 deletions src/Middleware/Doctrine/Metric/Connection.php
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
{
}
14 changes: 0 additions & 14 deletions src/Middleware/Doctrine/Metric/DoctrineMetricMiddleware.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware;

final class MetricConnectionDriverMiddleware extends AbstractDriverMiddleware
final class Driver extends AbstractDriverMiddleware
{
}
9 changes: 0 additions & 9 deletions src/Middleware/Doctrine/Metric/MetricResultDriver.php

This file was deleted.

14 changes: 14 additions & 0 deletions src/Middleware/Doctrine/Metric/Middleware.php
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
Expand Up @@ -4,6 +4,6 @@

use Doctrine\DBAL\Driver\Middleware\AbstractStatementMiddleware;

final class MetricStatementDriver extends AbstractStatementMiddleware
final class Statement extends AbstractStatementMiddleware
{
}
70 changes: 70 additions & 0 deletions src/Middleware/Doctrine/Trace/Connection.php
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

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.3)

Property FriendsOfOpenTelemetry\OpenTelemetryBundle\Middleware\Doctrine\Trace\Connection::$connection is never read, only written.

Check failure on line 16 in src/Middleware/Doctrine/Trace/Connection.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.3)

Property FriendsOfOpenTelemetry\OpenTelemetryBundle\Middleware\Doctrine\Trace\Connection::$connection is never read, only written.
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();
}
}
14 changes: 0 additions & 14 deletions src/Middleware/Doctrine/Trace/DoctrineTraceMiddleware.php

This file was deleted.

126 changes: 126 additions & 0 deletions src/Middleware/Doctrine/Trace/Driver.php
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

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.3)

Parameter #1 $params (array{application_name?: string, charset?: string, dbname?: string, defaultTableOptions?: array<string, mixed>, default_dbname?: string, driver?: 'ibm_db2'|'mysqli'|'oci8'|'pdo_mysql'|'pdo_oci'|'pdo_pgsql'|'pdo_sqlite'|'pdo_sqlsrv'|'pgsql'|'sqlite3'|'sqlsrv', driverClass?: class-string<Doctrine\DBAL\Driver>, driverOptions?: array<int, mixed>, ...}) of method FriendsOfOpenTelemetry\OpenTelemetryBundle\Middleware\Doctrine\Trace\Driver::connect() should be contravariant with parameter $params (array{application_name?: string, charset?: string, dbname?: string, defaultTableOptions?: array<string, mixed>, default_dbname?: string, driver?: 'ibm_db2'|'mysqli'|'oci8'|'pdo_mysql'|'pdo_oci'|'pdo_pgsql'|'pdo_sqlite'|'pdo_sqlsrv'|'pgsql'|'sqlite3'|'sqlsrv', driverClass?: class-string<Doctrine\DBAL\Driver>, driverOptions?: array, ...}) of method Doctrine\DBAL\Driver::connect()

Check failure on line 85 in src/Middleware/Doctrine/Trace/Driver.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.3)

Parameter #1 $params (array{application_name?: string, charset?: string, dbname?: string, defaultTableOptions?: array<string, mixed>, default_dbname?: string, driver?: 'ibm_db2'|'mysqli'|'oci8'|'pdo_mysql'|'pdo_oci'|'pdo_pgsql'|'pdo_sqlite'|'pdo_sqlsrv'|'pgsql'|'sqlite3'|'sqlsrv', driverClass?: class-string<Doctrine\DBAL\Driver>, driverOptions?: array<int, mixed>, ...}) of method FriendsOfOpenTelemetry\OpenTelemetryBundle\Middleware\Doctrine\Trace\Driver::connect() should be contravariant with parameter $params (array{application_name?: string, charset?: string, dbname?: string, defaultTableOptions?: array<string, mixed>, default_dbname?: string, driver?: 'ibm_db2'|'mysqli'|'oci8'|'pdo_mysql'|'pdo_oci'|'pdo_pgsql'|'pdo_sqlite'|'pdo_sqlsrv'|'pgsql'|'sqlite3'|'sqlsrv', driverClass?: class-string<Doctrine\DBAL\Driver>, driverOptions?: array, ...}) of method Doctrine\DBAL\Driver::connect()

Check failure on line 85 in src/Middleware/Doctrine/Trace/Driver.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.3)

Parameter #1 $params (array{application_name?: string, charset?: string, dbname?: string, defaultTableOptions?: array<string, mixed>, default_dbname?: string, driver?: 'ibm_db2'|'mysqli'|'oci8'|'pdo_mysql'|'pdo_oci'|'pdo_pgsql'|'pdo_sqlite'|'pdo_sqlsrv'|'pgsql'|'sqlite3'|'sqlsrv', driverClass?: class-string<Doctrine\DBAL\Driver>, driverOptions?: array<int, mixed>, ...}) of method FriendsOfOpenTelemetry\OpenTelemetryBundle\Middleware\Doctrine\Trace\Driver::connect() should be contravariant with parameter $params (array{application_name?: string, charset?: string, dbname?: string, defaultTableOptions?: array<string, mixed>, default_dbname?: string, driver?: 'ibm_db2'|'mysqli'|'oci8'|'pdo_mysql'|'pdo_oci'|'pdo_pgsql'|'pdo_sqlite'|'pdo_sqlsrv'|'pgsql'|'sqlite3'|'sqlsrv', driverClass?: class-string<Doctrine\DBAL\Driver>, driverOptions?: array, ...}) of method Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware::connect()

Check failure on line 85 in src/Middleware/Doctrine/Trace/Driver.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.3)

Parameter #1 $params (array{application_name?: string, charset?: string, dbname?: string, defaultTableOptions?: array<string, mixed>, default_dbname?: string, driver?: 'ibm_db2'|'mysqli'|'oci8'|'pdo_mysql'|'pdo_oci'|'pdo_pgsql'|'pdo_sqlite'|'pdo_sqlsrv'|'pgsql'|'sqlite3'|'sqlsrv', driverClass?: class-string<Doctrine\DBAL\Driver>, driverOptions?: array<int, mixed>, ...}) of method FriendsOfOpenTelemetry\OpenTelemetryBundle\Middleware\Doctrine\Trace\Driver::connect() should be contravariant with parameter $params (array{application_name?: string, charset?: string, dbname?: string, defaultTableOptions?: array<string, mixed>, default_dbname?: string, driver?: 'ibm_db2'|'mysqli'|'oci8'|'pdo_mysql'|'pdo_oci'|'pdo_pgsql'|'pdo_sqlite'|'pdo_sqlsrv'|'pgsql'|'sqlite3'|'sqlsrv', driverClass?: class-string<Doctrine\DBAL\Driver>, driverOptions?: array, ...}) of method Doctrine\DBAL\Driver::connect()

Check failure on line 85 in src/Middleware/Doctrine/Trace/Driver.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.3)

Parameter #1 $params (array{application_name?: string, charset?: string, dbname?: string, defaultTableOptions?: array<string, mixed>, default_dbname?: string, driver?: 'ibm_db2'|'mysqli'|'oci8'|'pdo_mysql'|'pdo_oci'|'pdo_pgsql'|'pdo_sqlite'|'pdo_sqlsrv'|'pgsql'|'sqlite3'|'sqlsrv', driverClass?: class-string<Doctrine\DBAL\Driver>, driverOptions?: array<int, mixed>, ...}) of method FriendsOfOpenTelemetry\OpenTelemetryBundle\Middleware\Doctrine\Trace\Driver::connect() should be contravariant with parameter $params (array{application_name?: string, charset?: string, dbname?: string, defaultTableOptions?: array<string, mixed>, default_dbname?: string, driver?: 'ibm_db2'|'mysqli'|'oci8'|'pdo_mysql'|'pdo_oci'|'pdo_pgsql'|'pdo_sqlite'|'pdo_sqlsrv'|'pgsql'|'sqlite3'|'sqlsrv', driverClass?: class-string<Doctrine\DBAL\Driver>, driverOptions?: array, ...}) of method Doctrine\DBAL\Driver::connect()

Check failure on line 85 in src/Middleware/Doctrine/Trace/Driver.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.3)

Parameter #1 $params (array{application_name?: string, charset?: string, dbname?: string, defaultTableOptions?: array<string, mixed>, default_dbname?: string, driver?: 'ibm_db2'|'mysqli'|'oci8'|'pdo_mysql'|'pdo_oci'|'pdo_pgsql'|'pdo_sqlite'|'pdo_sqlsrv'|'pgsql'|'sqlite3'|'sqlsrv', driverClass?: class-string<Doctrine\DBAL\Driver>, driverOptions?: array<int, mixed>, ...}) of method FriendsOfOpenTelemetry\OpenTelemetryBundle\Middleware\Doctrine\Trace\Driver::connect() should be contravariant with parameter $params (array{application_name?: string, charset?: string, dbname?: string, defaultTableOptions?: array<string, mixed>, default_dbname?: string, driver?: 'ibm_db2'|'mysqli'|'oci8'|'pdo_mysql'|'pdo_oci'|'pdo_pgsql'|'pdo_sqlite'|'pdo_sqlsrv'|'pgsql'|'sqlite3'|'sqlsrv', driverClass?: class-string<Doctrine\DBAL\Driver>, driverOptions?: array, ...}) of method Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware::connect()
#[\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',
};
}
}
19 changes: 19 additions & 0 deletions src/Middleware/Doctrine/Trace/Middleware.php
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);
}
}
24 changes: 24 additions & 0 deletions src/Middleware/Doctrine/Trace/Statement.php
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

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.3)

Property FriendsOfOpenTelemetry\OpenTelemetryBundle\Middleware\Doctrine\Trace\Statement::$statement is never read, only written.

Check failure on line 13 in src/Middleware/Doctrine/Trace/Statement.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.3)

Property FriendsOfOpenTelemetry\OpenTelemetryBundle\Middleware\Doctrine\Trace\Statement::$statement is never read, only written.
private TracerInterface $tracer,

Check failure on line 14 in src/Middleware/Doctrine/Trace/Statement.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.3)

Property FriendsOfOpenTelemetry\OpenTelemetryBundle\Middleware\Doctrine\Trace\Statement::$tracer is never read, only written.

Check failure on line 14 in src/Middleware/Doctrine/Trace/Statement.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.3)

Property FriendsOfOpenTelemetry\OpenTelemetryBundle\Middleware\Doctrine\Trace\Statement::$tracer is never read, only written.
private string $sql,

Check failure on line 15 in src/Middleware/Doctrine/Trace/Statement.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.3)

Property FriendsOfOpenTelemetry\OpenTelemetryBundle\Middleware\Doctrine\Trace\Statement::$sql is never read, only written.

Check failure on line 15 in src/Middleware/Doctrine/Trace/Statement.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.3)

Property FriendsOfOpenTelemetry\OpenTelemetryBundle\Middleware\Doctrine\Trace\Statement::$sql is never read, only written.
) {
parent::__construct($statement);
}

public function execute($params = null): Result
{
return parent::execute($params);
}
}
9 changes: 0 additions & 9 deletions src/Middleware/Doctrine/Trace/TraceConnectionDriver.php

This file was deleted.

9 changes: 0 additions & 9 deletions src/Middleware/Doctrine/Trace/TraceResultDriver.php

This file was deleted.

9 changes: 0 additions & 9 deletions src/Middleware/Doctrine/Trace/TraceStatementDriver.php

This file was deleted.

0 comments on commit 6b81d51

Please sign in to comment.