-
Notifications
You must be signed in to change notification settings - Fork 179
Implementation of optional sentry tracing with twig #430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ste93cry
merged 5 commits into
getsentry:develop
from
rjd22:feature/twig-tracing-support
Feb 22, 2021
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6d29db8
Implementation of optional sentry tracing with twig
rjd22 749dd2a
Add Psalm to pre-commit-check Make target
Jean85 29c0dd7
Add a static analysis stub for the Twig\Profiler\Profile class
Jean85 9ae4370
General improvements
ste93cry c353ce6
Ensure that the bundle works even without the symfony/twig-bundle pac…
ste93cry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,102 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sentry\SentryBundle\Tracing\Twig; | ||
|
||
use Sentry\State\HubInterface; | ||
use Sentry\Tracing\Span; | ||
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, Span> The currently active spans | ||
*/ | ||
private $spans; | ||
|
||
/** | ||
* @param HubInterface $hub The current hub | ||
*/ | ||
public function __construct(HubInterface $hub) | ||
{ | ||
$this->hub = $hub; | ||
$this->spans = new SplObjectStorage(); | ||
} | ||
|
||
/** | ||
* This method is called before the execution of a block, a macro or a | ||
* template. | ||
* | ||
* @param Profile $profile The profiling data | ||
*/ | ||
public function enter(Profile $profile): void | ||
{ | ||
$transaction = $this->hub->getTransaction(); | ||
|
||
if (null === $transaction) { | ||
return; | ||
} | ||
|
||
$spanContext = new SpanContext(); | ||
$spanContext->setOp('view.render'); | ||
$spanContext->setDescription($this->getSpanDescription($profile)); | ||
|
||
$this->spans[$profile] = $transaction->startChild($spanContext); | ||
} | ||
|
||
/** | ||
* This method is called when the execution of a block, a macro or a | ||
* template is finished. | ||
* | ||
* @param Profile $profile The profiling data | ||
*/ | ||
public function leave(Profile $profile): void | ||
{ | ||
if (!isset($this->spans[$profile])) { | ||
return; | ||
} | ||
|
||
$this->spans[$profile]->finish(); | ||
|
||
unset($this->spans[$profile]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getNodeVisitors(): array | ||
{ | ||
return [ | ||
new ProfilerNodeVisitor(self::class), | ||
]; | ||
} | ||
|
||
/** | ||
* Gets a short description for the span. | ||
* | ||
* @param Profile $profile The profiling data | ||
*/ | ||
private function getSpanDescription(Profile $profile): string | ||
{ | ||
switch (true) { | ||
case $profile->isRoot(): | ||
return $profile->getName(); | ||
|
||
case $profile->isTemplate(): | ||
return $profile->getTemplate(); | ||
|
||
default: | ||
return sprintf('%s::%s(%s)', $profile->getTemplate(), $profile->getType(), $profile->getName()); | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,10 @@ | ||
<?php | ||
|
||
namespace Twig\Profiler; | ||
|
||
/** | ||
* @template-implements \IteratorAggregate<Profile, Profile> | ||
*/ | ||
final class Profile implements \IteratorAggregate, \Serializable | ||
{ | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.