auxmoney/opentracing-bundle-doctrine-dbal
Prerequisite: Install the core auxmoney/opentracing-bundle via Composer:
composer require auxmoney/opentracing-bundle
Add this bundle:
composer require auxmoney/opentracing-bundle-doctrine-dbal
For Symfony 4+, enable it in config/bundles.php:
Auxmoney\OpentracingDoctrineDBALBundle\OpentracingDoctrineDBALBundle::class => ['all' => true],
For Symfony 3.x, add to AppKernel.php.
Verify Configuration: Ensure OpentracingBundle is properly configured (e.g., tracer setup in config/packages/opentracing.yaml).
Run a DBAL query to see automatic tracing:
use Doctrine\DBAL\Connection;
// In a controller or service:
$connection = $this->getDoctrine()->getConnection();
$stmt = $connection->executeQuery('SELECT * FROM users WHERE id = :id', ['id' => 1]);
$results = $stmt->fetchAll();
Check your tracing backend (e.g., Jaeger, Zipkin) for a new span with tags like db.statement and db.parameters.
Automatic Span Creation:
Doctrine\DBAL\Connection and Doctrine\DBAL\Driver\Statement to inject OpenTracing spans.executeQuery()executeStatement()fetchAll(), fetchOne(), etc.Tagging Strategy:
Configure via environment variables or config/packages/opentracing.yaml:
auxmoney_opentracing:
doctrine:
tag_full_statement: true
tag_parameters: true
tag_row_count: false # Use cautiously (see Limitations)
tag_user: true
Tags added to spans:
db.statement: Raw SQL (if enabled).db.parameters: Bound parameters (sanitized).db.row_count: Affected rows (driver-dependent).db.user: Database username.Integration with ORM:
EntityManager::createQuery()).Transaction Handling:
Custom Span Naming: Override span names via a subscriber or event listener:
use Auxmoney\OpentracingDoctrineDBALBundle\Event\SpanEvent;
$eventDispatcher->addListener(SpanEvent::NAME, function (SpanEvent $event) {
$event->setSpanName('custom.query.name');
});
Conditional Tracing: Disable tracing for specific queries:
$connection->getWrappedConnection()->setTracingEnabled(false);
$stmt = $connection->executeQuery('SELECT * FROM sensitive_data');
Multi-Database Setups:
Configure per-connection tracing via ConnectionOptions:
$connection = DriverManager::getConnection([
'url' => 'mysql://user:pass@localhost/db',
'wrapperClass' => \Auxmoney\OpentracingDoctrineDBALBundle\DBAL\Connection::class,
'wrapperOptions' => [
'tracing' => [
'tag_full_statement' => false,
],
],
]);
Row Count Limitations:
db.row_count may return incorrect values for SELECT queries (driver-dependent).tag_row_count: false) or handle manually:
$rowCount = $stmt->rowCount();
$span->setTag('db.row_count', $rowCount);
Parameter Sanitization:
ParameterConverter or filter parameters:
$event->setParameters(array_filter($event->getParameters(), fn($key) => !str_starts_with($key, 'password')));
Performance Overhead:
Symfony 6+ Compatibility:
OpentracingBundle v1.x is used (see #23).Missing Spans:
OpentracingBundle is active and configured.var/log/dev.log or stderr.Incorrect Tags:
bin/console debug:container auxmoney_opentracing.doctrine to verify configuration.Driver-Specific Issues:
PDO and native drivers (e.g., pdo_mysql, pdo_pgsql).SQLite, rowCount() may return 0 for SELECT queries (expected behavior).Custom Tagging:
Extend TracingStatement to add domain-specific tags:
use Auxmoney\OpentracingDoctrineDBALBundle\DBAL\TracingStatement;
class CustomTracingStatement extends TracingStatement {
public function __construct(Statement $statement, array $options) {
parent::__construct($statement, $options);
$this->addTag('custom.tag', 'value');
}
}
Register via DI or override the bundle’s factory.
Span Lifecycle Control:
Use SpanEvent to hook into span creation/finishing:
$dispatcher->addListener(SpanEvent::START, function (SpanEvent $event) {
$event->getSpan()->setBaggageItem('custom.baggage', 'value');
});
Testing: Mock the tracer in tests:
$this->container->set('opentracing.tracer', $this->createMock(TracerInterface::class));
Use OpentracingTestTrait from the core bundle for assertions.
Environment Variables:
Prefix with AUXMONEY_OPENTRACING_DOCTRINE_ (e.g., AUXMONEY_OPENTRACING_DOCTRINE_TAG_FULL_STATEMENT=true).
Note: Boolean values accept true/false, on/off, or 1/0.
Flex Auto-Configuration:
If using Symfony Flex, ensure config/packages/opentracing.yaml exists or the bundle will auto-configure with defaults.
Doctrine Event Listeners: Avoid conflicts with other DBAL listeners (e.g., logging, profiling). Order matters:
# config/packages/doctrine.yaml
dbal:
event_listeners:
tracing: [auxmoney_opentracing_dbal_listener, 16] # Lower priority = earlier execution
How can I help you explore Laravel packages today?