You are here

class TwigTracingExtension in Raven: Sentry Integration 3.x

Provides Twig performance tracing.

Hierarchy

Expanded class hierarchy of TwigTracingExtension

1 string reference to 'TwigTracingExtension'
raven.services.yml in ./raven.services.yml
raven.services.yml
1 service uses TwigTracingExtension
raven.twig_tracing_extension in ./raven.services.yml
Drupal\raven\Twig\TwigTracingExtension

File

src/Twig/TwigTracingExtension.php, line 17

Namespace

Drupal\raven\Twig
View 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

Namesort descending Modifiers Type Description Overrides
TwigTracingExtension::$configFactory protected property Config factory.
TwigTracingExtension::$spans private property The currently active spans.
TwigTracingExtension::enter public function This method is called before execution.
TwigTracingExtension::getNodeVisitors public function
TwigTracingExtension::getSpanDescription private function Gets a short description for the span.
TwigTracingExtension::leave public function This method is called when execution is finished.
TwigTracingExtension::__construct public function Extension constructor.