auxmoney/opentracing-bundle-monolog
auxmoney/opentracing-bundle-core first via:
composer require auxmoney/opentracing-bundle-core
composer require auxmoney/opentracing-bundle-monolog
bundles.php:
Auxmoney\OpentracingMonologBundle\OpentracingMonologBundle::class => ['all' => true],
Log a message with OpenTracing context automatically injected:
$this->logger->info('Processing payment', [
'amount' => 100,
'currency' => 'EUR'
]);
Output (JSON-encoded in logs):
{"opentracing-context":"{\"UBER-TRACE-ID\":\"12345:67890:1:2\"}"}
config/packages/monolog.yaml (if customizing log handlers)src/Logger/Processor/OpenTracingProcessor.php (core logic)Span Context Injection:
The bundle adds a Monolog Processor that attaches the current OpenTracing span context to every log record. This happens automatically—no manual tagging required.
Log Context Propagation:
Tracer->startSpan() (e.g., in controllers) propagate their context to logs.Tracer->extract() to inject context from messages (e.g., Laravel Queues, Symfony Messenger).Log Handler Integration:
Ensure your Monolog handlers (e.g., monolog.handler.main) are configured to process records. Example:
monolog:
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
processors: [opentracing] # Auto-added by the bundle
| Scenario | Implementation Pattern |
|---|---|
| API Request Tracing | Start a span in the controller, log errors with context automatically included. |
| Queue Workers | Extract span context from the message payload before processing. |
| Database Operations | Wrap Doctrine queries in spans and log SQL with trace IDs. |
| Custom Services | Inject TracerInterface and LoggerInterface into services to correlate logs/spans. |
auxmoney/opentracing-bundle-core's Tracer facade in routes/services.Doctrine\DBAL\Connection to auto-start spans for queries.$message->setMetadata(['opentracing_context' => $span->context()]);
Missing Core Bundle:
Class 'Auxmoney\OpentracingBundle\TracerInterface' not found.auxmoney/opentracing-bundle-core first.Context Not Propagating:
Tracer->activeSpan() to check.Log Format Issues:
opentracing-context appears as a string instead of JSON.json_formatter is enabled in handlers.Performance Overhead:
debug channel):
monolog:
handlers:
debug:
processors: [] # Exclude opentracing
$span = \Auxmoney\OpentracingBundle\Tracer::activeSpan();
dump($span->context()->toTraceId()); // Verify trace ID exists
opentracing processor runs after other processors. Reorder in monolog.yaml if needed:
processors: [datetime, memory_usage, opentracing]
Custom Context Fields: Extend the processor to include additional span tags:
// src/Logger/Processor/CustomOpenTracingProcessor.php
class CustomOpenTracingProcessor extends OpenTracingProcessor
{
public function __invoke(array $record): array
{
$record = parent::__invoke($record);
$record['custom_tag'] = $this->tracer->activeSpan()->getBaggageItem('custom.key');
return $record;
}
}
Register it in services.yaml:
monolog.processor.opentracing: '@App\Logger\Processor\CustomOpenTracingProcessor'
Conditional Logging: Skip context injection for sensitive logs:
if (!$this->isSensitiveLog($record)) {
$record = $this->opentracingProcessor->__invoke($record);
}
bundles.php includes the bundle after MonologBundle.[Log]), ensure the processor runs before attribute handlers.UBER-TRACE-ID in your APM tool (e.g., Jaeger, Zipkin) to navigate logs within a trace.$record['context']['password'] = '[REDACTED]';
How can I help you explore Laravel packages today?