-
-
Notifications
You must be signed in to change notification settings - Fork 466
Expand file tree
/
Copy pathTransactionTest.php
More file actions
186 lines (154 loc) · 5.9 KB
/
TransactionTest.php
File metadata and controls
186 lines (154 loc) · 5.9 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
186
<?php
declare(strict_types=1);
namespace Sentry\Tests\Tracing;
use PHPUnit\Framework\TestCase;
use Sentry\ClientInterface;
use Sentry\Event;
use Sentry\EventId;
use Sentry\EventType;
use Sentry\Options;
use Sentry\State\Hub;
use Sentry\State\HubInterface;
use Sentry\Tracing\Span;
use Sentry\Tracing\SpanContext;
use Sentry\Tracing\Transaction;
use Sentry\Tracing\TransactionContext;
use Sentry\Util\ClockMock;
/**
* @group time-sensitive
*/
final class TransactionTest extends TestCase
{
public function testFinish(): void
{
ClockMock::withClockMock(1600640877);
$expectedEventId = null;
$transactionContext = TransactionContext::make()
->setTags(['ios_version' => '4.0'])
->setSampled(true)
->setStartTimestamp(1600640865);
$client = $this->createMock(ClientInterface::class);
$client->expects($this->once())
->method('getOptions')
->willReturn(new Options());
$hub = $this->createMock(HubInterface::class);
$hub->expects($this->once())
->method('getClient')
->willReturn($client);
$transaction = new Transaction($transactionContext, $hub);
$transaction->initSpanRecorder();
$span1 = $transaction->startChild(new SpanContext());
$span2 = $transaction->startChild(new SpanContext());
$span3 = $transaction->startChild(new SpanContext()); // This span isn't finished, so it should not be included in the event
$hub->expects($this->once())
->method('captureEvent')
->with($this->callback(function (Event $eventArg) use ($transactionContext, $span1, $span2): bool {
$this->assertSame(EventType::transaction(), $eventArg->getType());
$this->assertSame($transactionContext->getName(), $eventArg->getTransaction());
$this->assertSame($transactionContext->getStartTimestamp(), $eventArg->getStartTimestamp());
$this->assertSame(ClockMock::microtime(true), $eventArg->getTimestamp());
$this->assertSame($transactionContext->getTags(), $eventArg->getTags());
$this->assertSame([$span1, $span2], $eventArg->getSpans());
return true;
}))
->willReturnCallback(static function (Event $eventArg) use (&$expectedEventId): EventId {
$expectedEventId = $eventArg->getId();
return $expectedEventId;
});
$span1->finish();
$span2->finish();
$eventId = $transaction->finish();
$this->assertSame($expectedEventId, $eventId);
}
public function testFinishDoesNothingIfSampledFlagIsNotTrue(): void
{
$hub = $this->createMock(HubInterface::class);
$hub->expects($this->never())
->method('captureEvent');
$transaction = new Transaction(new TransactionContext(), $hub);
$transaction->finish();
}
public function testFluentApi(): void
{
$transaction = new Transaction(TransactionContext::make());
$tags = ['foo' => 'bar'];
$name = 'baz';
$transaction->setTags($tags)
->setName($name)
->finish();
$this->assertSame($tags, $transaction->getTags());
$this->assertSame($name, $transaction->getName());
}
/**
* @dataProvider parentTransactionContextDataProvider
*/
public function testTransactionIsSampledCorrectlyWhenTracingIsSetToZeroInOptions(TransactionContext $context, bool $expectedSampled): void
{
$client = $this->createMock(ClientInterface::class);
$client->expects($this->once())
->method('getOptions')
->willReturn(
new Options([
'traces_sampler' => null,
'traces_sample_rate' => 0,
])
);
$transaction = (new Hub($client))->startTransaction($context);
$this->assertSame($expectedSampled, $transaction->getSampled());
}
public static function parentTransactionContextDataProvider(): \Generator
{
yield [
new TransactionContext(TransactionContext::DEFAULT_NAME, true),
true,
];
yield [
new TransactionContext(TransactionContext::DEFAULT_NAME, false),
false,
];
yield [
TransactionContext::fromHeaders('566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8-1', ''),
true,
];
yield [
TransactionContext::fromHeaders('566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8-0', ''),
false,
];
}
/**
* @dataProvider parentTransactionContextDataProviderDisabled
*/
public function testTransactionIsNotSampledWhenTracingIsDisabledInOptions(TransactionContext $context, bool $expectedSampled): void
{
$client = $this->createMock(ClientInterface::class);
$client->expects($this->once())
->method('getOptions')
->willReturn(
new Options([
'traces_sampler' => null,
'traces_sample_rate' => null,
])
);
$transaction = (new Hub($client))->startTransaction($context);
$this->assertSame($expectedSampled, $transaction->getSampled());
}
public function parentTransactionContextDataProviderDisabled(): \Generator
{
yield [
new TransactionContext(TransactionContext::DEFAULT_NAME, true),
false,
];
yield [
new TransactionContext(TransactionContext::DEFAULT_NAME, false),
false,
];
yield [
TransactionContext::fromHeaders('566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8-1', ''),
false,
];
yield [
TransactionContext::fromHeaders('566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8-0', ''),
false,
];
}
}