diff --git a/src/LaravelFluentdLoggerServiceProvider.php b/src/LaravelFluentdLoggerServiceProvider.php index 1d7bfbd..7d73908 100644 --- a/src/LaravelFluentdLoggerServiceProvider.php +++ b/src/LaravelFluentdLoggerServiceProvider.php @@ -29,13 +29,15 @@ public function bootingPackage() $this->registerLogDriver(); } - private function initTracing() + private function initTracing(): void { - $traceId = (new RandomIdGenerator())->generateTraceId(); - +// $this->app->singleton(TraceIdStorage::class, function () { +// return new TraceIdStorage($this->app->make(RandomIdGenerator::class)); +// }); /** @var TraceIdStorage $traceIdStorage */ $traceIdStorage = $this->app->make(TraceIdStorage::class); - $traceIdStorage->setTraceId($traceId); + $traceIdStorage->startNewTrace(); + $traceIdStorage->startNewSpan(); /** @var MakeQueueTraceAwareAction $action */ $action = $this->app->make(MakeQueueTraceAwareAction::class); @@ -45,7 +47,7 @@ private function initTracing() $this->initDbQueryLog(); } - private function initQueueJobsFailsLog() + private function initQueueJobsFailsLog(): void { Queue::failing(function (JobFailed $event) { Log::error('Failed Job | ' . $event->job->resolveName(), [ @@ -74,7 +76,7 @@ private function initDbQueryLog() }); } - private function registerLogDriver() + private function registerLogDriver(): void { $log = $this->app->make(LoggerInterface::class); $log->extend('fluentd', function ($app, array $config) { diff --git a/src/Logs/FluentHandler.php b/src/Logs/FluentHandler.php index 76b020d..3239735 100644 --- a/src/Logs/FluentHandler.php +++ b/src/Logs/FluentHandler.php @@ -23,11 +23,11 @@ */ class FluentHandler extends AbstractProcessingHandler { - /** @var LoggerInterface */ - protected $logger; + protected LoggerInterface $logger; + private Repository $config; + private TraceIdStorage $traceIdStorage; - /** @var string */ - protected $tagFormat = '{{app_name}}.{{level_name}}'; + protected string $tagFormat = '{{app_name}}.{{level_name}}'; /** * @param LoggerInterface $logger @@ -64,6 +64,7 @@ protected function write(array $record): void $tag, [ '@trace_id' => $this->traceIdStorage->getTraceId(), + '@span_id' => $this->traceIdStorage->getSpanId(), '@level' => $record['level_name'], '@env' => $record['channel'], '@message' => $record['message'], @@ -137,10 +138,10 @@ protected function contextHasException($context): bool } /** - * Returns the entire exception trace as a string + * Returns the entire exception trace as array * * @param array $context - * @return string + * @return array */ protected function getContextExceptionTrace(array $context): array { diff --git a/src/Queue/MakeQueueTraceAwareAction.php b/src/Queue/MakeQueueTraceAwareAction.php index c85b7a3..5d18fdd 100644 --- a/src/Queue/MakeQueueTraceAwareAction.php +++ b/src/Queue/MakeQueueTraceAwareAction.php @@ -4,6 +4,7 @@ use Illuminate\Queue\Events\JobProcessing; use Illuminate\Queue\Events\JobRetryRequested; +use Vmorozov\LaravelFluentdLogger\Tracing\RandomIdGenerator; use Vmorozov\LaravelFluentdLogger\Tracing\TraceIdStorage; class MakeQueueTraceAwareAction @@ -28,7 +29,9 @@ protected function listenForJobsBeingQueued(): self app('queue')->createPayloadUsing(function ($connectionName, $queue, $payload) { $currentTraceId = $this->traceIdStorage->getTraceId(); if ($currentTraceId) { - return ['traceId' => $currentTraceId]; + return [ + 'traceId' => $currentTraceId + ]; } }); @@ -43,6 +46,7 @@ protected function listenForJobsBeingProcessed(): self } $this->traceIdStorage->setTraceId($event->job->payload()['traceId']); + $this->traceIdStorage->startNewSpan(); }); return $this; @@ -56,6 +60,7 @@ protected function listenForJobsRetryRequested(): self } $this->traceIdStorage->setTraceId($event->payload()['traceId']); + $this->traceIdStorage->startNewSpan(); }); return $this; diff --git a/src/Tracing/TraceIdStorage.php b/src/Tracing/TraceIdStorage.php index f7f69ff..becf481 100644 --- a/src/Tracing/TraceIdStorage.php +++ b/src/Tracing/TraceIdStorage.php @@ -4,8 +4,21 @@ class TraceIdStorage { - /** @var string */ - private static $traceId; + private RandomIdGenerator $randomIdGenerator; + + private static string $traceId; + private static string $spanId; + + public function __construct(RandomIdGenerator $randomIdGenerator) + { + $this->randomIdGenerator = $randomIdGenerator; + } + + public function startNewTrace(): string + { + $this->setTraceId($this->randomIdGenerator->generateTraceId()); + return $this->getTraceId(); + } public function getTraceId(): string { @@ -16,4 +29,24 @@ public function setTraceId(string $traceId): void { static::$traceId = $traceId; } + + public function startNewSpan(): string + { + if (!$this->getTraceId()) { + throw new \LogicException('Span can only be started after trace start.'); + } + + $this->setSpanId($this->randomIdGenerator->generateSpanId()); + return $this->getSpanId(); + } + + public function getSpanId(): string + { + return static::$spanId; + } + + public function setSpanId(string $spanId): void + { + static::$spanId = $spanId; + } }