Skip to content

Commit

Permalink
added spans usage
Browse files Browse the repository at this point in the history
  • Loading branch information
freezer278 committed Dec 9, 2022
1 parent f57645b commit d30b17c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 15 deletions.
14 changes: 8 additions & 6 deletions src/LaravelFluentdLoggerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(), [
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 7 additions & 6 deletions src/Logs/FluentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -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<string, mixed> $context
* @return string
* @return array
*/
protected function getContextExceptionTrace(array $context): array
{
Expand Down
7 changes: 6 additions & 1 deletion src/Queue/MakeQueueTraceAwareAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
];
}
});

Expand All @@ -43,6 +46,7 @@ protected function listenForJobsBeingProcessed(): self
}

$this->traceIdStorage->setTraceId($event->job->payload()['traceId']);
$this->traceIdStorage->startNewSpan();
});

return $this;
Expand All @@ -56,6 +60,7 @@ protected function listenForJobsRetryRequested(): self
}

$this->traceIdStorage->setTraceId($event->payload()['traceId']);
$this->traceIdStorage->startNewSpan();
});

return $this;
Expand Down
37 changes: 35 additions & 2 deletions src/Tracing/TraceIdStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
}
}

0 comments on commit d30b17c

Please sign in to comment.