summaryrefslogtreecommitdiff
path: root/vendor/jonahgeorge/jaeger-client-php/tests/Jaeger/Mapper/SpanToJaegerMapperTest.php
blob: d536cb63a6c5aabd415acb79d5616a1d70680bbe (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
<?php

use Jaeger\Mapper\SpanToJaegerMapper;
use Jaeger\Reporter\NullReporter;
use Jaeger\Sampler\ConstSampler;
use Jaeger\Span;
use Jaeger\SpanContext;
use Jaeger\Thrift\TagType;
use Jaeger\Tracer;
use const Jaeger\SAMPLED_FLAG;
use const OpenTracing\Tags\COMPONENT;
use const OpenTracing\Tags\PEER_HOST_IPV4;
use const OpenTracing\Tags\PEER_PORT;
use const OpenTracing\Tags\PEER_SERVICE;
use const OpenTracing\Tags\SPAN_KIND;
use const OpenTracing\Tags\SPAN_KIND_RPC_CLIENT;

class SpanToJaegerMapperTest extends \PHPUnit\Framework\TestCase
{
    private $serviceName = "test-service";
    /**
     * @var Tracer
     */
    private $tracer;

    /**
     * @var SpanContext
     */
    private $context;

    /**
     * {@inheritdoc}
     */
    public function setUp(): void
    {
        $this->tracer = new Tracer($this->serviceName, new NullReporter, new ConstSampler);
        $this->context = new SpanContext(0, 0, 0, SAMPLED_FLAG);
    }

    /**
     * {@inheritdoc}
     */
    protected function tearDown(): void
    {
        $this->tracer = null;
        $this->context = null;
    }

    /** @test */
    public function shouldProperlyInitializeAtConstructTime(): void
    {
        $span = new Span($this->context, $this->tracer, 'test-operation');
        $span->setTags([
            "tag-bool1" => true,
            "tag-bool2" => false,
            "tag-int" => 1234567,
            "tag-float" => 1.23456,
            "tag-string" => "hello-world"
        ]);

        $mapper = new SpanToJaegerMapper();
        $thriftSpan = $mapper->mapSpanToJaeger($span);

        $index = 0;
        $this->assertEquals($thriftSpan->tags[$index]->key, "component");
        $this->assertEquals($thriftSpan->tags[$index]->vType, TagType::STRING);
        $this->assertEquals($thriftSpan->tags[$index]->vStr, $this->serviceName);
        $index++;

        $this->assertEquals($thriftSpan->tags[$index]->key, "tag-bool1");
        $this->assertEquals($thriftSpan->tags[$index]->vType, TagType::BOOL);
        $this->assertEquals($thriftSpan->tags[$index]->vBool, true);
        $index++;

        $this->assertEquals($thriftSpan->tags[$index]->key, "tag-bool2");
        $this->assertEquals($thriftSpan->tags[$index]->vType, TagType::BOOL);
        $this->assertEquals($thriftSpan->tags[$index]->vBool, false);
        $index++;

        $this->assertEquals($thriftSpan->tags[$index]->key, "tag-int");
        $this->assertEquals($thriftSpan->tags[$index]->vType, TagType::LONG);
        $this->assertEquals($thriftSpan->tags[$index]->vLong, 1234567);
        $index++;

        $this->assertEquals($thriftSpan->tags[$index]->key, "tag-float");
        $this->assertEquals($thriftSpan->tags[$index]->vType, TagType::DOUBLE);
        $this->assertEquals($thriftSpan->tags[$index]->vDouble, 1.23456);
        $index++;

        $this->assertEquals($thriftSpan->tags[$index]->key, "tag-string");
        $this->assertEquals($thriftSpan->tags[$index]->vType, TagType::STRING);
        $this->assertEquals($thriftSpan->tags[$index]->vStr, "hello-world");
        $index++;
    }

    /**
     * @dataProvider specialTagProvider
     * @param array<string, mixed> $tags
     * @return void
     */
    public function testSpecialTagsAreAdded(array $tags): void
    {
        $span = new Span($this->context, $this->tracer, 'test-operation');
        $span->setTags($tags);

        // The component tag is always added, even if it's not specified in tags
        $expectedTagValues = array_merge([COMPONENT => $this->serviceName], $tags);

        $mapper = new SpanToJaegerMapper();
        $thriftSpan = $mapper->mapSpanToJaeger($span);

        $foundTags = [];

        foreach ($thriftSpan->tags as $tag) {
            $foundTags[] = $tag->key;

            switch ($tag->key) {
                case PEER_SERVICE:
                case PEER_HOST_IPV4:
                case SPAN_KIND:
                case COMPONENT:
                    $this->assertEquals(TagType::STRING, $tag->vType, 'Incorrect tag value type');
                    $this->assertEquals($expectedTagValues[$tag->key], $tag->vStr, 'Incorrect tag value');
                    break;
                case PEER_PORT:
                    $this->assertEquals(TagType::LONG, $tag->vType, 'Incorrect tag value type');
                    $this->assertEquals($expectedTagValues[$tag->key], $tag->vLong, 'Incorrect tag value');
                    break;
            }
        }

        $this->assertEqualsCanonicalizing(array_keys($expectedTagValues), $foundTags, 'Some of the tags are missing');
    }

    public function specialTagProvider(): array
    {
        return [
            [
                [
                    'bool_tag' => true,
                    PEER_SERVICE => 'my_service',
                    PEER_HOST_IPV4 => '127.0.0.1',
                    PEER_PORT => 443,
                    SPAN_KIND => SPAN_KIND_RPC_CLIENT,
                    COMPONENT => 'grpc',
                ],
            ],
            [
                [
                    'int_tag' => 5,
                    PEER_HOST_IPV4 => '192.168.0.1',
                    PEER_PORT => 80,
                ],
            ],
            [
                [
                    'string_tag' => 'testing-tag',
                    PEER_PORT => 80,
                    COMPONENT => 'grpc',
                ],
            ],
            [
                [
                    'string_tag' => 'testing-tag',
                ],
            ],
        ];
    }
}