Skip to content

Commit

Permalink
fix: Move Trace-Propagation to Terminate Event (#88)
Browse files Browse the repository at this point in the history
contributors: @L3tum, @MollocH
  • Loading branch information
L3tum authored Jul 22, 2022
1 parent 4ac6860 commit 56e9abe
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text eol=lf
16 changes: 16 additions & 0 deletions EventListener/FinishRootSpanSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static function getSubscribedEvents(): array
{
return [
'kernel.finish_request' => ['onFinishRequest', -16],
'kernel.terminate' => ['onTerminate', -16],
];
}

Expand All @@ -44,6 +45,21 @@ public function onFinishRequest(KernelEvent $event): void
}

$this->tracing->finishActiveSpan();
}

public function onTerminate(KernelEvent $event): void
{
# TODO: when Symfony 4.4 is unmaintained (November 2023), remove outer if-block in favor of isMainRequest()
if (method_exists($event, 'isMainRequest')) {
if (!$event->isMainRequest()) {
return;
}
} elseif (method_exists($event, 'isMasterRequest')) {
if (!$event->isMasterRequest()) {
return;
}
}

$this->persistence->flush();
}
}
25 changes: 24 additions & 1 deletion Tests/EventListener/FinishRootSpanSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function setUp(): void
public function testGetSubscribedEvents(): void
{
self::assertArrayHasKey('kernel.finish_request', $this->subject::getSubscribedEvents());
self::assertArrayHasKey('kernel.terminate', $this->subject::getSubscribedEvents());
}

public function testOnFinishRequestIsMainRequest(): void
Expand All @@ -45,7 +46,7 @@ public function testOnFinishRequestIsMainRequest(): void
$event = new KernelEvent($this->kernel->reveal(), $this->request->reveal(), HttpKernelInterface::MASTER_REQUEST);

$this->tracing->finishActiveSpan()->shouldBeCalledOnce();
$this->persistence->flush()->shouldBeCalledOnce();
$this->persistence->flush()->shouldNotBeCalled();

$this->subject->onFinishRequest($event);
}
Expand All @@ -59,4 +60,26 @@ public function testOnFinishRequestIsNotMainRequest(): void

$this->subject->onFinishRequest($event);
}

public function testOnTerminateIsMainRequest(): void
{
# TODO: when Symfony 4.4 is unmaintained (November 2023), replace HttpKernelInterface::MASTER_REQUEST with HttpKernelInterface::MAIN_REQUEST
$event = new KernelEvent($this->kernel->reveal(), $this->request->reveal(), HttpKernelInterface::MASTER_REQUEST);

$this->tracing->finishActiveSpan()->shouldNotBeCalled();
$this->persistence->flush()->shouldBeCalledOnce();

$this->subject->onTerminate($event);
}

public function testOnTerminateIsNotMainRequest(): void
{
# TODO: when Symfony 4.4 is unmaintained (November 2023), replace HttpKernelInterface::MASTER_REQUEST with HttpKernelInterface::MAIN_REQUEST
$event = new KernelEvent($this->kernel->reveal(), $this->request->reveal(), HttpKernelInterface::SUB_REQUEST);

$this->tracing->finishActiveSpan()->shouldNotBeCalled();
$this->persistence->flush()->shouldNotBeCalled();

$this->subject->onTerminate($event);
}
}

0 comments on commit 56e9abe

Please sign in to comment.