vendor/sentry/sentry/src/Tracing/Transaction.php line 188

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sentry\Tracing;
  4. use Sentry\Event;
  5. use Sentry\EventId;
  6. use Sentry\Profiling\Profiler;
  7. use Sentry\SentrySdk;
  8. use Sentry\State\HubInterface;
  9. /**
  10.  * This class stores all the information about a Transaction.
  11.  */
  12. final class Transaction extends Span
  13. {
  14.     /**
  15.      * @var HubInterface The hub instance
  16.      */
  17.     private $hub;
  18.     /**
  19.      * @var string Name of the transaction
  20.      */
  21.     private $name;
  22.     /**
  23.      * @var Transaction The transaction
  24.      */
  25.     protected $transaction;
  26.     /**
  27.      * @var TransactionMetadata
  28.      */
  29.     protected $metadata;
  30.     /**
  31.      * @var Profiler|null Reference instance to the {@see Profiler}
  32.      */
  33.     protected $profiler null;
  34.     /**
  35.      * Span constructor.
  36.      *
  37.      * @param TransactionContext $context The context to create the transaction with
  38.      * @param HubInterface|null  $hub     Instance of a hub to flush the transaction
  39.      *
  40.      * @internal
  41.      */
  42.     public function __construct(TransactionContext $context, ?HubInterface $hub null)
  43.     {
  44.         parent::__construct($context);
  45.         $this->hub $hub ?? SentrySdk::getCurrentHub();
  46.         $this->name $context->getName();
  47.         $this->metadata $context->getMetadata();
  48.         $this->transaction $this;
  49.     }
  50.     /**
  51.      * Gets the name of this transaction.
  52.      */
  53.     public function getName(): string
  54.     {
  55.         return $this->name;
  56.     }
  57.     /**
  58.      * Sets the name of this transaction.
  59.      *
  60.      * @param string $name The name
  61.      */
  62.     public function setName(string $name): void
  63.     {
  64.         $this->name $name;
  65.     }
  66.     /**
  67.      * Gets the transaction metadata.
  68.      */
  69.     public function getMetadata(): TransactionMetadata
  70.     {
  71.         return $this->metadata;
  72.     }
  73.     /**
  74.      * Gets the transaction dynamic sampling context.
  75.      */
  76.     public function getDynamicSamplingContext(): DynamicSamplingContext
  77.     {
  78.         if (null !== $this->metadata->getDynamicSamplingContext()) {
  79.             return $this->metadata->getDynamicSamplingContext();
  80.         }
  81.         $samplingContext DynamicSamplingContext::fromTransaction($this->transaction$this->hub);
  82.         $this->getMetadata()->setDynamicSamplingContext($samplingContext);
  83.         return $samplingContext;
  84.     }
  85.     /**
  86.      * Attaches a {@see SpanRecorder} to the transaction itself.
  87.      *
  88.      * @param int $maxSpans The maximum number of spans that can be recorded
  89.      */
  90.     public function initSpanRecorder(int $maxSpans 1000): void
  91.     {
  92.         if (null === $this->spanRecorder) {
  93.             $this->spanRecorder = new SpanRecorder($maxSpans);
  94.         }
  95.         $this->spanRecorder->add($this);
  96.     }
  97.     public function detachSpanRecorder(): void
  98.     {
  99.         $this->spanRecorder null;
  100.     }
  101.     public function initProfiler(): void
  102.     {
  103.         if (null === $this->profiler) {
  104.             $client $this->hub->getClient();
  105.             $options null !== $client $client->getOptions() : null;
  106.             $this->profiler = new Profiler($options);
  107.         }
  108.     }
  109.     public function getProfiler(): ?Profiler
  110.     {
  111.         return $this->profiler;
  112.     }
  113.     public function detachProfiler(): void
  114.     {
  115.         $this->profiler null;
  116.     }
  117.     /**
  118.      * {@inheritdoc}
  119.      */
  120.     public function finish(?float $endTimestamp null): ?EventId
  121.     {
  122.         if (null !== $this->profiler) {
  123.             $this->profiler->stop();
  124.         }
  125.         if (null !== $this->endTimestamp) {
  126.             // Transaction was already finished once and we don't want to re-flush it
  127.             return null;
  128.         }
  129.         parent::finish($endTimestamp);
  130.         if (true !== $this->sampled) {
  131.             return null;
  132.         }
  133.         $finishedSpans = [];
  134.         if (null !== $this->spanRecorder) {
  135.             foreach ($this->spanRecorder->getSpans() as $span) {
  136.                 if ($span->getSpanId() !== $this->getSpanId() && null !== $span->getEndTimestamp()) {
  137.                     $finishedSpans[] = $span;
  138.                 }
  139.             }
  140.         }
  141.         $event Event::createTransaction();
  142.         $event->setSpans($finishedSpans);
  143.         $event->setStartTimestamp($this->startTimestamp);
  144.         $event->setTimestamp($this->endTimestamp);
  145.         $event->setTags($this->tags);
  146.         $event->setTransaction($this->name);
  147.         $event->setContext('trace'$this->getTraceContext());
  148.         $event->setSdkMetadata('dynamic_sampling_context'$this->getDynamicSamplingContext());
  149.         $event->setSdkMetadata('transaction_metadata'$this->getMetadata());
  150.         if (null !== $this->profiler) {
  151.             $profile $this->profiler->getProfile();
  152.             if (null !== $profile) {
  153.                 $event->setSdkMetadata('profile'$profile);
  154.             }
  155.         }
  156.         return $this->hub->captureEvent($event);
  157.     }
  158. }