-
-
Notifications
You must be signed in to change notification settings - Fork 466
Expand file tree
/
Copy pathSpanTest.php
More file actions
190 lines (154 loc) · 5.16 KB
/
SpanTest.php
File metadata and controls
190 lines (154 loc) · 5.16 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
187
188
189
190
<?php
declare(strict_types=1);
namespace Sentry\Tests\Tracing;
use PHPUnit\Framework\TestCase;
use Sentry\Tracing\Span;
use Sentry\Tracing\SpanContext;
use Sentry\Tracing\SpanId;
use Sentry\Tracing\TraceId;
use Sentry\Tracing\Transaction;
use Sentry\Tracing\TransactionContext;
use Sentry\Util\ClockMock;
/**
* @group time-sensitive
*/
final class SpanTest extends TestCase
{
/**
* @dataProvider finishDataProvider
*/
public function testFinish(?float $currentTimestamp, ?float $endTimestamp, float $expectedEndTimestamp): void
{
ClockMock::withClockMock($currentTimestamp);
$span = new Span();
$span->finish($endTimestamp);
$this->assertSame($expectedEndTimestamp, $span->getEndTimestamp());
}
public static function finishDataProvider(): iterable
{
yield [
1598660006,
null,
1598660006,
];
yield [
1598660006,
1598660332,
1598660332,
];
}
public function testStartChild(): void
{
$spanContext2ParentSpanId = SpanId::generate();
$spanContext2TraceId = TraceId::generate();
$spanContext1 = (new SpanContext())
->setSampled(false)
->setSpanId(SpanId::generate())
->setTraceId(TraceId::generate());
$spanContext2 = SpanContext::make()
->setSampled(true)
->setParentSpanId($spanContext2ParentSpanId)
->setTraceId($spanContext2TraceId);
$span1 = new Span($spanContext1);
$span2 = $span1->startChild($spanContext2);
$this->assertSame($spanContext1->getSampled(), $span1->getSampled());
$this->assertSame($spanContext1->getSpanId(), $span1->getSpanId());
$this->assertSame($spanContext1->getTraceId(), $span1->getTraceId());
$this->assertSame($spanContext1->getSampled(), $span2->getSampled());
$this->assertSame($spanContext1->getSpanId(), $span2->getParentSpanId());
$this->assertSame($spanContext1->getTraceId(), $span2->getTraceId());
}
/**
* @dataProvider toTraceparentDataProvider
*/
public function testToTraceparent(?bool $sampled, string $expectedValue): void
{
$span = new Span();
$span->setSpanId(new SpanId('566e3688a61d4bc8'));
$span->setTraceId(new TraceId('566e3688a61d4bc888951642d6f14a19'));
$span->setSampled($sampled);
$this->assertSame($expectedValue, $span->toTraceparent());
}
public static function toTraceparentDataProvider(): iterable
{
yield [
null,
'566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8',
];
yield [
false,
'566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8-0',
];
yield [
true,
'566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8-1',
];
}
/**
* @dataProvider toBaggageDataProvider
*/
public function testToBaggage(string $baggageHeader, string $expectedValue): void
{
$context = TransactionContext::fromHeaders(
'566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8-1',
$baggageHeader
);
$transaction = new Transaction($context);
$this->assertSame($expectedValue, $transaction->toBaggage());
}
public static function toBaggageDataProvider(): iterable
{
yield [
'',
'',
];
yield [
'foo=bar,bar=baz',
'',
];
yield [
'sentry-public_key=public,sentry-trace_id=566e3688a61d4bc888951642d6f14a19,sentry-sample_rate=1',
'sentry-public_key=public,sentry-trace_id=566e3688a61d4bc888951642d6f14a19,sentry-sample_rate=1',
];
yield [
'sentry-public_key=public,sentry-trace_id=566e3688a61d4bc888951642d6f14a19,sentry-sample_rate=1,foo=bar,bar=baz',
'sentry-public_key=public,sentry-trace_id=566e3688a61d4bc888951642d6f14a19,sentry-sample_rate=1',
];
}
public function testDataGetter(): void
{
$span = new Span();
$initialData = [
'foo' => 'bar',
'baz' => 1,
];
$span->setData($initialData);
$this->assertSame($initialData, $span->getData());
$this->assertSame('bar', $span->getData('foo'));
$this->assertSame(1, $span->getData('baz'));
}
public function testDataIsMergedWhenSet(): void
{
$span = new Span();
$span->setData([
'foo' => 'bar',
'baz' => 1,
]);
$span->setData([
'baz' => 2,
]);
$this->assertSame(2, $span->getData('baz'));
$this->assertSame('bar', $span->getData('foo'));
$this->assertSame([
'foo' => 'bar',
'baz' => 2,
], $span->getData());
}
public function testOriginIsCopiedFromContext(): void
{
$context = SpanContext::make()->setOrigin('auto.testing');
$span = new Span($context);
$this->assertSame($context->getOrigin(), $span->getOrigin());
$this->assertSame($context->getOrigin(), $span->getTraceContext()['origin']);
}
}