Passed
Push — master ( 16b058...517d96 )
by Dmitry
01:54
created

TracingTest::testParentSpanContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
use PHPUnit\Framework\TestCase;
6
7
use OpenTelemetry\Tracing\{Builder, SpanContext, Status, Tracer};
8
9
class TracingTest extends TestCase
10
{
11
    public function testContextGenerationAndRestore()
12
    {
13
        $spanContext = SpanContext::generate();
14
        $this->assertSame(strlen($spanContext->getTraceId()), 16);
15
        $this->assertSame(strlen($spanContext->getSpanId()), 8);
16
17
        $spanContext2 = SpanContext::generate();
18
        $this->assertNotSame($spanContext->getTraceId(), $spanContext2->getTraceId());
19
        $this->assertNotSame($spanContext->getSpanId(), $spanContext2->getSpanId());
20
21
        $spanContext3 = SpanContext::restore($spanContext->getTraceId(), $spanContext->getSpanId());
22
        $this->assertSame($spanContext3->getTraceId(), $spanContext->getTraceId());
23
        $this->assertSame($spanContext3->getSpanId(), $spanContext->getSpanId());
24
    }
25
26
    public function testTracerSpanContextRestore()
27
    {
28
        $tracer = new Tracer();
29
        $spanContext = $tracer->getActiveSpan()->getSpanContext();
30
31
        $spanContext2 = SpanContext::restore($spanContext->getTraceId(), $spanContext->getSpanId());
32
        $tracer2 = new Tracer($spanContext2);
33
34
        $this->assertSame($tracer->getActiveSpan()->getSpanContext()->getTraceId(), $tracer2->getActiveSpan()->getSpanContext()->getTraceId());
35
    }
36
37
    public function testSpanNameUpdate()
38
    {
39
        $database = (new Tracer)->createSpan('database');
40
        $this->assertSame($database->getName(), 'database');
41
        $database->setName('tarantool');
42
        $this->assertSame($database->getName(), 'tarantool');
43
    }
44
45
    public function testCreateSpan()
46
    {
47
        $tracer = new Tracer();
48
        $global = $tracer->getActiveSpan();
49
50
        $mysql = $tracer->createSpan('mysql');
51
        $this->assertSame($tracer->getActiveSpan(), $mysql);
52
        $this->assertSame($global->getSpanContext()->getTraceId(), $mysql->getSpanContext()->getTraceId());
53
        $this->assertSame($mysql->getParentSpanContext(), $global->getSpanContext());
54
        $this->assertNotNull($mysql->getStart());
55
        $this->assertTrue($mysql->isRecordingEvents());
56
        $this->assertNull($mysql->getDuration());
57
58
        $mysql->end();
59
        $this->assertFalse($mysql->isRecordingEvents());
60
        $this->assertNotNull($mysql->getDuration());
61
62
        $duration = $mysql->getDuration();
63
        $this->assertSame($duration, $mysql->getDuration());
64
        $mysql->end();
65
        $this->assertGreaterThan($duration, $mysql->getDuration());
66
67
        $this->assertTrue($mysql->getStatus()->isOk());
68
        
69
        // active span rolled back
70
        $this->assertSame($tracer->getActiveSpan(), $global);
71
        $this->assertNull($global->getStatus());
72
        
73
        // active span should be kept for global span
74
        $global->end();
75
        $this->assertSame($tracer->getActiveSpan(), $global);
76
        $this->assertTrue($global->getStatus()->isOk());
77
    }
78
79
    public function testStatusManipulation()
80
    {
81
        $tracer = new Tracer();
82
83
        $cancelled = $tracer->createSpan('cancelled');
84
        $cancelled->end(new Status(Status::CANCELLED));
85
        $this->assertFalse($cancelled->getStatus()->isOk());
86
        $this->assertSame($cancelled->getStatus()->getCanonicalCode(), Status::CANCELLED);
87
        $this->assertSame($cancelled->getStatus()->getDescription(), Status::DESCRIPTION[Status::CANCELLED]);
88
89
        $custom = $tracer->createSpan('custom');
90
        $custom->end(new Status(404, 'Not found'));
91
        $this->assertFalse($custom->getStatus()->isOk());
92
        $this->assertSame($custom->getStatus()->getCanonicalCode(), 404);
93
        $this->assertSame($custom->getStatus()->getDescription(), 'Not found');
94
95
        $noDescription = new Status(500);
96
        $this->assertNull($noDescription->getDescription());
97
98
        $custom->setStatus(new Status(Status::OK));
99
        $this->assertTrue($custom->getStatus()->isOk());
100
101
        $this->assertCount(3, $tracer->getSpans());
102
    }
103
104
    public function testSpanAttributesApi()
105
    {
106
        $span = (new Tracer())->getActiveSpan();
107
108
        // set attributes
109
        $span->setAttributes([ 'username' => 'nekufa' ]);
110
111
        // get attribute
112
        $this->assertSame($span->getAttribute('username'), 'nekufa');
113
        
114
        // otherwrite
115
        $span->setAttributes([ 'email' => '[email protected]', ]);
116
117
        // null attributes
118
        $this->assertNull($span->getAttribute('username'));
119
        $this->assertSame($span->getAttribute('email'), '[email protected]');
120
121
        // set attribute
122
        $span->setAttribute('username', 'nekufa');
123
        $this->assertSame($span->getAttribute('username'), 'nekufa');
124
        $this->assertSame($span->getAttributes(), [
125
            'email' => '[email protected]',
126
            'username' => 'nekufa',
127
        ]);
128
129
        // keep order
130
        $span->setAttributes([ 'a' => 1, 'b' => 2]);
131
        $this->assertSame(array_keys($span->getAttributes()), ['a', 'b']);
132
        $span->setAttributes([ 'b' => 2, 'a' => 1, ]);
133
        $this->assertSame(array_keys($span->getAttributes()), ['b', 'a']);
134
135
        // attribute update don't change the order
136
        $span->setAttribute('a', 3);
137
        $span->setAttribute('b', 4);
138
        $this->assertSame(array_keys($span->getAttributes()), ['b', 'a']);
139
140
        $this->expectExceptionMessage("Span is readonly");
141
        $span->end();
142
        $span->setAttribute('b', 5);
143
    }
144
145
    public function testEventRegistration()
146
    {
147
        $span = (new Tracer)->createSpan('database');
148
        $event = $span->addEvent('select', [
149
            'space' => 'guard.session',
150
            'id' => 67235
151
        ]);
152
        $this->assertSame($event->getName(), 'select');
153
        $this->assertSame($event->getAttributes(), [
154
            'space' => 'guard.session',
155
            'id' => 67235,
156
        ]);
157
        $this->assertSame($event->getAttribute('space'), 'guard.session');
158
        $this->assertNull($event->getAttribute('invalid-attribute'));
159
        $this->assertCount(1, $span->getEvents());
160
        $this->assertSame($span->getEvents(), [$event]);
161
        
162
        $span->addEvent('update')
163
            ->setAttribute('space', 'guard.session')
164
            ->setAttribute('id', 67235)
165
            ->setAttribute('active_at', time());
166
167
        $this->assertCount(2, $span->getEvents());
168
169
        $this->expectExceptionMessage("Span is readonly");
170
        $span->end();
171
        $span->addEvent('update');
172
    }
173
174
    public function testBuilder()
175
    {
176
        $spanContext = SpanContext::generate();
177
        $tracer = Builder::create()
178
            ->setSpanContext($spanContext)
179
            ->getTracer();
180
181
        $this->assertInstanceOf(Tracer::class, $tracer);
182
        $this->assertSame($tracer->getActiveSpan()->getSpanContext(), $spanContext);
183
    }
184
185
    public function testParentSpanContext()
186
    {
187
        $tracer = new Tracer;
188
        $global = $tracer->getActiveSpan();
189
        $request = $tracer->createSpan('request');
190
        $this->assertSame($request->getParentSpanContext()->getSpanId(), $global->getSpanContext()->getSpanId());
191
        $this->assertNull($global->getParentSpanContext());
192
        $this->assertNotNull($request->getParentSpanContext());
193
    }
194
}