-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of optional sentry tracing with twig
- Loading branch information
Showing
17 changed files
with
271 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sentry\SentryBundle\Tracing\Twig; | ||
|
||
use Sentry\State\HubInterface; | ||
use Sentry\Tracing\SpanContext; | ||
use SplObjectStorage; | ||
use Twig\Extension\AbstractExtension; | ||
use Twig\Profiler\NodeVisitor\ProfilerNodeVisitor; | ||
use Twig\Profiler\Profile; | ||
|
||
final class TwigTracingExtension extends AbstractExtension | ||
{ | ||
/** | ||
* @var HubInterface The current hub | ||
*/ | ||
private $hub; | ||
|
||
/** | ||
* @var SplObjectStorage<object, \Sentry\Tracing\Span> | ||
*/ | ||
private $events; | ||
|
||
/** | ||
* @param HubInterface $hub The current hub | ||
*/ | ||
public function __construct(HubInterface $hub) | ||
{ | ||
$this->hub = $hub; | ||
$this->events = new SplObjectStorage(); | ||
} | ||
|
||
public function enter(Profile $profile): void | ||
{ | ||
$transaction = $this->hub->getTransaction(); | ||
|
||
if (null === $transaction || !$profile->isTemplate()) { | ||
return; | ||
} | ||
|
||
$spanContext = new SpanContext(); | ||
$spanContext->setOp('twig.template'); | ||
$spanContext->setDescription($profile->getName()); | ||
|
||
$this->events[$profile] = $transaction->startChild($spanContext); | ||
} | ||
|
||
public function leave(Profile $profile): void | ||
{ | ||
if (empty($this->events[$profile]) || !$profile->isTemplate()) { | ||
return; | ||
} | ||
|
||
$this->events[$profile]->finish(); | ||
unset($this->events[$profile]); | ||
} | ||
|
||
public function getNodeVisitors(): array | ||
{ | ||
return [new ProfilerNodeVisitor(static::class)]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,5 +47,8 @@ | |
'enabled' => false, | ||
'connections' => ['default'], | ||
], | ||
'twig' => [ | ||
'enabled' => false, | ||
], | ||
], | ||
]); |
14 changes: 14 additions & 0 deletions
14
tests/DependencyInjection/Fixtures/php/twig_tracing_enabled.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** @var ContainerBuilder $container */ | ||
$container->loadFromExtension('sentry', [ | ||
'tracing' => [ | ||
'twig' => [ | ||
'enabled' => true, | ||
], | ||
], | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
tests/DependencyInjection/Fixtures/xml/twig_tracing_enabled.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:sentry="https://sentry.io/schema/dic/sentry-symfony" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd | ||
https://sentry.io/schema/dic/sentry-symfony https://sentry.io/schema/dic/sentry-symfony/sentry-1.0.xsd"> | ||
|
||
<sentry:config> | ||
<sentry:tracing> | ||
<sentry:twig enabled="true" /> | ||
</sentry:tracing> | ||
</sentry:config> | ||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,3 +42,5 @@ sentry: | |
enabled: false | ||
connections: | ||
- enabled | ||
twig: | ||
enabled: false |
4 changes: 4 additions & 0 deletions
4
tests/DependencyInjection/Fixtures/yml/twig_tracing_enabled.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
sentry: | ||
tracing: | ||
twig: | ||
enabled: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sentry\SentryBundle\Tests\Tracing\Twig; | ||
|
||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Sentry\SentryBundle\Tracing\Twig\TwigTracingExtension; | ||
use Sentry\State\HubInterface; | ||
use Sentry\Tracing\Transaction; | ||
use Sentry\Tracing\TransactionContext; | ||
use Twig\Profiler\Profile; | ||
|
||
final class TwigTracingExtensionTest extends TestCase | ||
{ | ||
/** | ||
* @var MockObject&HubInterface | ||
*/ | ||
private $hub; | ||
|
||
/** | ||
* @var TwigTracingExtension | ||
*/ | ||
private $listener; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->hub = $this->createMock(HubInterface::class); | ||
$this->listener = new TwigTracingExtension($this->hub); | ||
} | ||
|
||
public function testThatTwigEnterProfileIgnoresTracingWhenTransactionIsNotStarted(): void | ||
{ | ||
$this->hub->expects($this->once()) | ||
->method('getTransaction') | ||
->willReturn(null); | ||
|
||
$this->listener->enter(new Profile('main', Profile::TEMPLATE)); | ||
} | ||
|
||
public function testThatTwigEnterProfileIgnoresTracingWhenNotATemplate(): void | ||
{ | ||
$this->hub->expects($this->once()) | ||
->method('getTransaction') | ||
->willReturn(new Transaction(new TransactionContext())); | ||
|
||
$this->listener->enter(new Profile('main', Profile::ROOT)); | ||
} | ||
|
||
public function testThatTwigLeaveProfileIgnoresTracingWhenTransactionIsNotStarted(): void | ||
{ | ||
$this->hub->expects($this->once()) | ||
->method('getTransaction') | ||
->willReturn(null); | ||
|
||
$profile = new Profile('main', Profile::TEMPLATE); | ||
|
||
$this->listener->enter($profile); | ||
$this->listener->leave($profile); | ||
} | ||
|
||
public function testThatTwigLeaveProfileIgnoresTracingWhenNotATemplate(): void | ||
{ | ||
$this->hub->expects($this->once()) | ||
->method('getTransaction') | ||
->willReturn(new Transaction(new TransactionContext())); | ||
|
||
$profile = new Profile('main', Profile::ROOT); | ||
|
||
$this->listener->enter($profile); | ||
$this->listener->leave($profile); | ||
} | ||
|
||
public function testThatTwigEnterProfileAttachesAChildSpanWhenTransactionStarted(): void | ||
{ | ||
$transaction = new Transaction(new TransactionContext()); | ||
$transaction->initSpanRecorder(); | ||
|
||
$this->hub->expects($this->once()) | ||
->method('getTransaction') | ||
->willReturn($transaction); | ||
|
||
$this->listener->enter(new Profile('main', Profile::TEMPLATE)); | ||
|
||
$spans = $transaction->getSpanRecorder()->getSpans(); | ||
|
||
$this->assertCount(2, $spans); | ||
$this->assertNull($spans[1]->getEndTimestamp()); | ||
} | ||
|
||
public function testThatTwigLeaveProfileFinishesTheChildSpanWhenChildSpanStarted(): void | ||
{ | ||
$transaction = new Transaction(new TransactionContext()); | ||
$transaction->initSpanRecorder(); | ||
|
||
$this->hub->expects($this->once()) | ||
->method('getTransaction') | ||
->willReturn($transaction); | ||
|
||
$profile = new Profile('main', Profile::TEMPLATE); | ||
|
||
$this->listener->enter($profile); | ||
$this->listener->leave($profile); | ||
|
||
$spans = $transaction->getSpanRecorder()->getSpans(); | ||
|
||
$this->assertCount(2, $spans); | ||
$this->assertNotNull($spans[1]->getEndTimestamp()); | ||
} | ||
} |