class TwigTracingExtension in Raven: Sentry Integration 3.x
Provides Twig performance tracing.
Hierarchy
- class \Drupal\raven\Twig\TwigTracingExtension extends \Twig\Extension\AbstractExtension
Expanded class hierarchy of TwigTracingExtension
1 string reference to 'TwigTracingExtension'
1 service uses TwigTracingExtension
File
- src/
Twig/ TwigTracingExtension.php, line 17
Namespace
Drupal\raven\TwigView source
class TwigTracingExtension extends AbstractExtension {
/**
* Config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The currently active spans.
*
* @var \SplObjectStorage
*/
private $spans;
/**
* Extension constructor.
*/
public function __construct(ConfigFactoryInterface $config_factory) {
$this->configFactory = $config_factory;
$this->spans = new \SplObjectStorage();
}
/**
* This method is called before execution.
*
* @param \Twig\Profiler\Profile $profile
* The profiling data.
*/
public function enter(Profile $profile) : void {
if (!$this->configFactory
->get('raven.settings')
->get('twig_tracing') || !class_exists(SentrySdk::class)) {
return;
}
$transaction = SentrySdk::getCurrentHub()
->getTransaction();
if (NULL === $transaction) {
return;
}
$spanContext = new SpanContext();
$spanContext
->setOp('twig.render');
$spanContext
->setDescription($this
->getSpanDescription($profile));
$this->spans[$profile] = $transaction
->startChild($spanContext);
}
/**
* This method is called when execution is finished.
*
* @param \Twig\Profiler\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 \Twig\Profiler\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());
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
TwigTracingExtension:: |
protected | property | Config factory. | |
TwigTracingExtension:: |
private | property | The currently active spans. | |
TwigTracingExtension:: |
public | function | This method is called before execution. | |
TwigTracingExtension:: |
public | function | ||
TwigTracingExtension:: |
private | function | Gets a short description for the span. | |
TwigTracingExtension:: |
public | function | This method is called when execution is finished. | |
TwigTracingExtension:: |
public | function | Extension constructor. |