-
-
Notifications
You must be signed in to change notification settings - Fork 474
Expand file tree
/
Copy pathTransaction.php
More file actions
185 lines (152 loc) · 4.46 KB
/
Copy pathTransaction.php
File metadata and controls
185 lines (152 loc) · 4.46 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
declare(strict_types=1);
namespace Sentry\Tracing;
use Sentry\Event;
use Sentry\EventId;
use Sentry\Options;
use Sentry\Profiling\Profiler;
use Sentry\SentrySdk;
/**
* This class stores all the information about a Transaction.
*/
final class Transaction extends Span
{
/**
* @var string Name of the transaction
*/
private $name;
/**
* @var Transaction The transaction
*/
protected $transaction;
/**
* @var TransactionMetadata
*/
protected $metadata;
/**
* @var Profiler|null Reference instance to the {@see Profiler}
*/
protected $profiler;
/**
* Span constructor.
*
* @param TransactionContext $context The context to create the transaction with
*
* @internal
*/
public function __construct(TransactionContext $context)
{
parent::__construct($context);
$this->name = $context->getName();
$this->metadata = $context->getMetadata();
$this->transaction = $this;
}
/**
* Gets the name of this transaction.
*/
public function getName(): string
{
return $this->name;
}
/**
* Sets the name of this transaction.
*
* @param string $name The name
*
* @return $this
*/
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
* Gets the transaction metadata.
*/
public function getMetadata(): TransactionMetadata
{
return $this->metadata;
}
/**
* Gets the transaction dynamic sampling context.
*/
public function getDynamicSamplingContext(): DynamicSamplingContext
{
if ($this->metadata->getDynamicSamplingContext() !== null) {
return $this->metadata->getDynamicSamplingContext();
}
$samplingContext = DynamicSamplingContext::fromTransaction($this->transaction, SentrySdk::getClient());
$this->getMetadata()->setDynamicSamplingContext($samplingContext);
return $samplingContext;
}
/**
* Attaches a {@see SpanRecorder} to the transaction itself.
*
* @param int $maxSpans The maximum number of spans that can be recorded
*/
public function initSpanRecorder(int $maxSpans = 1000): self
{
if ($this->spanRecorder === null) {
$this->spanRecorder = new SpanRecorder($maxSpans);
}
$this->spanRecorder->add($this);
return $this;
}
public function initProfiler(?Options $options = null): Profiler
{
if ($this->profiler === null) {
$this->profiler = new Profiler($options ?? SentrySdk::getClient()->getOptions());
}
return $this->profiler;
}
public function getProfiler(): ?Profiler
{
return $this->profiler;
}
public function detachProfiler(): self
{
$this->profiler = null;
return $this;
}
/**
* {@inheritdoc}
*/
public function finish(?float $endTimestamp = null): ?EventId
{
if ($this->profiler !== null) {
$this->profiler->stop();
}
if ($this->endTimestamp !== null) {
// Transaction was already finished once and we don't want to re-flush it
return null;
}
parent::finish($endTimestamp);
if ($this->sampled !== true) {
return null;
}
$finishedSpans = [];
if ($this->spanRecorder !== null) {
foreach ($this->spanRecorder->getSpans() as $span) {
if ($span->getSpanId() !== $this->getSpanId() && $span->getEndTimestamp() !== null) {
$finishedSpans[] = $span;
}
}
}
$event = Event::createTransaction();
$event->setSpans($finishedSpans);
$event->setStartTimestamp($this->startTimestamp);
$event->setTimestamp($this->endTimestamp);
$event->setTags($this->tags);
$event->setTransaction($this->name);
$event->setContext('trace', $this->getTraceContext());
$event->setSdkMetadata('dynamic_sampling_context', $this->getDynamicSamplingContext());
$event->setSdkMetadata('transaction_metadata', $this->getMetadata());
if ($this->profiler !== null) {
$profile = $this->profiler->getProfile();
if ($profile !== null) {
$event->setSdkMetadata('profile', $profile);
}
}
return \Sentry\captureEvent($event);
}
}