Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 0 additions & 70 deletions src/Concerns/Recorders/PausableRecorder.php

This file was deleted.

9 changes: 0 additions & 9 deletions src/Recorders/CommandRecorder/CommandRecorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Spatie\FlareClient\AttributesProviders\PhpCommandAttributesProvider;
use Spatie\FlareClient\AttributesProviders\SymfonyInputCommandAttributesProvider;
use Spatie\FlareClient\Concerns\Recorders\PausableRecorder;
use Spatie\FlareClient\Contracts\CommandAttributesProvider;
use Spatie\FlareClient\Contracts\EntryPointHandlerProvider;
use Spatie\FlareClient\EntryPoint\EntryPointResolver;
Expand All @@ -19,8 +18,6 @@

class CommandRecorder extends SpansRecorder
{
use PausableRecorder;

/** @var array<int, string> */
protected array $ignoredCommands = [];

Expand Down Expand Up @@ -152,12 +149,6 @@ public function recordEnd(
int $exitCode = 0,
array $attributes = []
): ?Span {
if ($this->pausedTrace()) {
$this->resumeTrace();

return null;
}

return $this->endSpan(additionalAttributes: [
'process.exit_code' => $exitCode,
...$attributes,
Expand Down
15 changes: 0 additions & 15 deletions src/Recorders/JobRecorder/JobRecorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Spatie\FlareClient\Recorders\JobRecorder;

use Spatie\FlareClient\AttributesProviders\PhpJobAttributesProvider;
use Spatie\FlareClient\Concerns\Recorders\PausableRecorder;
use Spatie\FlareClient\Contracts\EntryPointHandlerProvider;
use Spatie\FlareClient\Contracts\JobAttributesProvider;
use Spatie\FlareClient\EntryPoint\EntryPoint;
Expand All @@ -25,8 +24,6 @@

class JobRecorder extends SpansRecorder
{
use PausableRecorder;

/** @var array<int, string> */
protected array $ignoredClasses = [];

Expand Down Expand Up @@ -125,12 +122,6 @@ public function recordStartFromJob(

public function recordEnd(array $attributes = []): ?Span
{
if ($this->pausedTrace()) {
$this->resumeTrace();

return null;
}

$span = $this->endSpan(
additionalAttributes: $attributes,
includeMemoryUsage: true,
Expand All @@ -147,12 +138,6 @@ public function recordFailed(
Throwable $exception,
array $attributes = [],
): ?Span {
if ($this->pausedTrace()) {
$this->resumeTrace();

return null;
}

$throwableClass = $exception::class;

$trackingUuid = $this->tracer->ids->uuid();
Expand Down
9 changes: 0 additions & 9 deletions src/Recorders/QueueRecorder/QueueRecorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Spatie\FlareClient\Recorders\QueueRecorder;

use Spatie\FlareClient\AttributesProviders\PhpJobAttributesProvider;
use Spatie\FlareClient\Concerns\Recorders\PausableRecorder;
use Spatie\FlareClient\Contracts\JobAttributesProvider;
use Spatie\FlareClient\Enums\RecorderType;
use Spatie\FlareClient\Enums\SpanType;
Expand All @@ -15,8 +14,6 @@

class QueueRecorder extends SpansRecorder
{
use PausableRecorder;

/** @var array<int, string> */
protected array $ignoredClasses = [];

Expand Down Expand Up @@ -74,12 +71,6 @@ public function recordStartFromQueuedJob(

public function recordEnd(array $attributes = []): ?Span
{
if ($this->pausedTrace()) {
$this->resumeTrace();

return null;
}

return $this->endSpan(additionalAttributes: $attributes);
}

Expand Down
65 changes: 61 additions & 4 deletions src/Recorders/SpansRecorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ abstract class SpansRecorder extends Recorder implements SpansRecorderContract
/** @var array<Span> */
protected array $stack = [];

protected static ?SpansRecorder $pauseOwner = null;

protected static int $pauseDepth = 0;

public function __construct(
protected Tracer $tracer,
protected BackTracer $backTracer,
Expand Down Expand Up @@ -69,6 +73,12 @@ final protected function startSpan(
throw new InvalidArgumentException('Either $nameAndAttributes must be set, or both $name and $attributes must be set.');
}

if (self::$pauseOwner === $this) {
self::$pauseDepth++;

return null;
}

$shouldTrace = $this->withTraces && $this->tracer->sampling;
$shouldReport = $this->shouldReport();

Expand Down Expand Up @@ -120,6 +130,10 @@ final protected function endSpan(
?Closure $spanCallback = null,
bool $includeMemoryUsage = false,
): ?Span {
if ($this->consumePauseEnd()) {
return null;
}

$span = array_pop($this->stack);

if ($span === null) {
Expand Down Expand Up @@ -183,17 +197,17 @@ final protected function span(
time: $start,
);

if ($span === null) {
return null;
}

$this->endSpan(
time: $end,
additionalAttributes: $additionalAttributes === null ? [] : $additionalAttributes,
spanCallback: $spanCallback,
includeMemoryUsage: $includeMemoryUsage,
);

if ($span === null) {
return null;
}

if ($this->withTraces && $this->tracer->sampling === true) {
$this->backtraceEntry($span);
}
Expand All @@ -205,4 +219,47 @@ final public function getSpans(): array
{
return $this->entries;
}

public static function resetPauseState(): void
{
self::$pauseOwner = null;
self::$pauseDepth = 0;
}

protected function pauseTrace(): void
{
if (self::$pauseOwner === $this) {
self::$pauseDepth++;

return;
}

if (self::$pauseOwner !== null) {
return;
}

self::$pauseOwner = $this;
self::$pauseDepth = 1;

$this->tracer->pauseSampling();
}

private function consumePauseEnd(): bool
{
if (self::$pauseOwner !== $this) {
return false;
}

self::$pauseDepth--;

if (self::$pauseDepth > 0) {
return true;
}

self::$pauseOwner = null;

$this->tracer->resumeSampling();

return true;
}
}
3 changes: 3 additions & 0 deletions src/Support/Lifecycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Spatie\FlareClient\Enums\SpanType;
use Spatie\FlareClient\Logger;
use Spatie\FlareClient\Memory\Memory;
use Spatie\FlareClient\Recorders\SpansRecorder;
use Spatie\FlareClient\Resources\Resource;
use Spatie\FlareClient\Spans\Span;
use Spatie\FlareClient\Time\Time;
Expand Down Expand Up @@ -391,6 +392,8 @@ public function flush(): void
$this->sentReports->clear();
$this->recorders->reset();

SpansRecorder::resetPauseState();

$this->applicationSpan = null;
$this->registeringSpan = null;
$this->bootingSpan = null;
Expand Down
2 changes: 2 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Spatie\FlareClient\Flare;
use Spatie\FlareClient\FlareConfig;
use Spatie\FlareClient\FlareProvider;
use Spatie\FlareClient\Recorders\SpansRecorder;
use Spatie\FlareClient\Support\Container;
use Spatie\FlareClient\Tests\Shared\FakeApi;
use Spatie\FlareClient\Tests\Shared\FakeIds;
Expand All @@ -21,6 +22,7 @@
FakeIds::reset();
FakeMemory::reset();
FakeSampler::reset();
SpansRecorder::resetPauseState();
})->in(__DIR__);

function makePathsRelative(string $text): string
Expand Down
Loading
Loading