-
Notifications
You must be signed in to change notification settings - Fork 169
/
TwigTracingExtension.php
101 lines (84 loc) · 2.37 KB
/
TwigTracingExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
declare(strict_types=1);
namespace Sentry\SentryBundle\Tracing\Twig;
use Sentry\State\HubInterface;
use Sentry\Tracing\Span;
use Sentry\Tracing\SpanContext;
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());
}
}
}