summaryrefslogtreecommitdiff
path: root/vendor/jonahgeorge/jaeger-client-php/tests/Jaeger/Codec/TextCodecTest.php
blob: 79a1dd96f04a257ff5099456f17e171891e56242 (plain)
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
<?php

namespace Jaeger\Tests\Codec;

use Exception;
use const Jaeger\BAGGAGE_HEADER_PREFIX;
use Jaeger\Codec\TextCodec;
use const Jaeger\DEBUG_ID_HEADER_KEY;
use Jaeger\SpanContext;
use const Jaeger\TRACE_ID_HEADER;
use PHPUnit\Framework\TestCase;

class TextCodecTest extends TestCase
{
    /** @var TextCodec */
    private $textCodec;

    public function setUp(): void
    {
        $this->textCodec = new TextCodec();
    }

    public function testCanInjectSimpleContextInCarrier(): void
    {
        $context = new SpanContext('trace-id', 'span-id', null, null);
        $carrier = [];

        $this->textCodec->inject($context, $carrier);

        $this->assertCount(1 , $carrier);
        $this->assertArrayHasKey(TRACE_ID_HEADER, $carrier);
    }

    /**
     * @dataProvider contextDataProvider
     * @param bool $urlEncode
     * @param $baggage
     */
    public function testCanInjectContextBaggageInCarrier(bool $urlEncode, $baggage, $injectedBaggage): void
    {
        $carrier = [];

        $context = new SpanContext('trace-id', 'span-id', null, null, $baggage);
        $textCodec = new TextCodec($urlEncode);
        $textCodec->inject($context, $carrier);

        $this->assertCount(1 + count($baggage) , $carrier);
        $this->assertArrayHasKey(TRACE_ID_HEADER, $carrier);
        foreach ($injectedBaggage as $key => $value) {
            $this->assertArrayHasKey(BAGGAGE_HEADER_PREFIX . $key, $carrier);
            $this->assertEquals($carrier[BAGGAGE_HEADER_PREFIX . $key], $value);
        }
    }

    public function contextDataProvider()
    {
        return [
            [false, ['baggage-1' => 'baggage value'], ['baggage-1' => 'baggage value']],
            [false, ['baggage-1' => 'https://testdomain.sk'], ['baggage-1' => 'https://testdomain.sk']],
            [true, ['baggage-1' => 'https://testdomain.sk'], ['baggage-1' => 'https%3A%2F%2Ftestdomain.sk']],
        ];
    }

    /**
     * @dataProvider carrierDataProvider
     * @param $urlEncode
     * @param $carrier
     * @param $traceId
     * @param $spanId
     * @param $parentId
     * @param $flags
     * @param $baggage
     * @throws \Exception
     */
    public function testSpanContextParsingFromHeader($urlEncode, $carrier, $traceId, $spanId, $parentId, $flags, $baggage): void
    {
        $textCodec = new TextCodec($urlEncode);
        $spanContext = $textCodec->extract($carrier);

        $this->assertEquals($traceId, $spanContext->getTraceId());
        $this->assertEquals($spanId, $spanContext->getSpanId());
        $this->assertEquals($parentId, $spanContext->getParentId());
        $this->assertEquals($flags, $spanContext->getFlags());
        $this->assertCount(count($baggage), $spanContext->getBaggage() ? $spanContext->getBaggage() : []);
        foreach ($baggage as $key => $value) {
            $this->assertEquals($value, $spanContext->getBaggageItem($key));
        }
    }

    public function carrierDataProvider(): array
    {
        return [
            [
                false,
                [
                    TRACE_ID_HEADER => '32834e4115071776:f7802330248418d:f123456789012345:1'
                ],
                "3639838965278119798",
                "1114643325879075213",
                "-1070935975401544891",
                1,
                []
            ],
            [
                false,
                [
                    TRACE_ID_HEADER => '32834e4115071776:f7802330248418d:f123456789012345:1',
                    BAGGAGE_HEADER_PREFIX . 'baggage-1' => 'https://testdomain.sk',
                ],
                "3639838965278119798",
                "1114643325879075213",
                "-1070935975401544891",
                1,
                ['baggage-1' => 'https://testdomain.sk']
            ],
            [
                true,
                [
                    TRACE_ID_HEADER => '32834e4115071776:f7802330248418d:f123456789012345:1',
                    BAGGAGE_HEADER_PREFIX . 'baggage-1' => 'https%3A%2F%2Ftestdomain.sk',
                ],
                "3639838965278119798",
                "1114643325879075213",
                "-1070935975401544891",
                1,
                ['baggage-1' => 'https://testdomain.sk']
            ]
        ];
    }

    public function testBaggageWithoutTraceContext(): void
    {
        $carrier = [BAGGAGE_HEADER_PREFIX.'test' => 'some data'];

        $this->expectException(Exception::class);
        $this->expectExceptionMessage('baggage without trace ctx');

        $this->textCodec->extract($carrier);
    }

    public function testInvalidSpanContextParsingFromHeader(): void
    {
        $carrier = [TRACE_ID_HEADER => 'invalid_data'];

        $this->expectException(Exception::class);
        $this->expectExceptionMessage('Malformed tracer state string.');

        $this->textCodec->extract($carrier);
    }

    public function testExtractDebugSpanContext(): void
    {
        $carrier = [DEBUG_ID_HEADER_KEY => 'debugId'];

        $spanContext = $this->textCodec->extract($carrier);

        $this->assertEquals('debugId', $spanContext->getDebugId());
        $this->assertNull($spanContext->getTraceId());
        $this->assertNull($spanContext->getSpanId());
        $this->assertNull($spanContext->getParentId());
        $this->assertNull($spanContext->getFlags());
    }


    public function testExtractEmptySpanContext(): void
    {
        $spanContext = $this->textCodec->extract([]);
        $this->assertNull($spanContext);
    }
}