summaryrefslogtreecommitdiff
path: root/vendor/jonahgeorge/jaeger-client-php/tests/Jaeger/ConfigTest.php
blob: da1a9781e1702410266c0069b60583c8ebe073fd (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php

namespace Jaeger\Tests;

use Exception;
use Jaeger\Config;
use Jaeger\Reporter\ReporterInterface;
use Jaeger\Sampler\SamplerInterface;
use Jaeger\Tracer;
use OpenTracing\GlobalTracer;
use PHPUnit\Framework\TestCase;
use const Jaeger\SAMPLER_TYPE_CONST;

class ConfigTest extends TestCase
{
    /**
     * @var Config
     */
    private $config;

    /**
     * @var ReporterInterface
     */
    private $reporter;

    /**
     * @var SamplerInterface
     */
    private $sampler;

    /**
     * @var string
     */
    private $serviceName = 'test-service';

    function setUp(): void
    {
        $this->config = new Config([], $this->serviceName);
        $this->reporter = $this->createMock(ReporterInterface::class);
        $this->sampler = $this->createmock(SamplerInterface::class);
    }

    function testCreateTracer()
    {
        $tracer = $this->config->createTracer($this->reporter, $this->sampler);

        $this->assertEquals(Tracer::class, get_class($tracer));
        $this->assertEquals($this->serviceName, $tracer->getServiceName());
    }

    function testThrowExceptionWhenServiceNameIsNotDefined()
    {
        $this->expectException(Exception::class);
        $this->expectExceptionMessage('service_name required in the config or param.');

        new Config([]);
    }

    function testSetServiceNameFromConfig()
    {
        $config = new Config(['service_name' => 'test-service-name-from-config']);

        $serviceName = $config->getServiceName();

        $this->assertEquals('test-service-name-from-config', $serviceName);
    }

    /**
     * @test
     */
    public function shouldSetGlobalTracerAfterInitialize()
    {
        //given
        $config = new Config(['service_name' => 'test-service-name']);

        //when
        $config->initializeTracer();

        //then
        $tracer = GlobalTracer::get();
        $this->assertInstanceOf(Tracer::class, $tracer);
    }

    /** @test */
    public function shouldThrowExceptionWhenCreatingNotSupportedSampler()
    {
        $config = new Config(['service_name' => 'test-service-name', 'sampler' => ['type' => 'unsupportedSampler']]);

        $this->expectException(Exception::class);
        $this->expectExceptionMessage('Unknown sampler type unsupportedSampler');

        $config->initializeTracer();
    }

    /** @test */
    public function shouldThrowExceptionWhenCreatingRateLimitingSamplerWithoutCacheComponent()
    {
        $config = new Config([
            'service_name' => 'test-service-name',
            'sampler' => ['type' => \Jaeger\SAMPLER_TYPE_RATE_LIMITING]
        ]);

        $this->expectException(Exception::class);
        $this->expectExceptionMessage('You cannot use RateLimitingSampler without cache component');

        $config->initializeTracer();
    }

    /** @test  */
    public function shouldPassDifferentDispatchMode() {
        foreach (Config::getAvailableDispatchModes() as $dispatchMode) {
            $config = new Config(
                [
                    'sampler' => [
                        'type' => SAMPLER_TYPE_CONST,
                        'param' => true,
                    ],
                    'logging' => false,
                    "local_agent" => [
                        "reporting_host" => "localhost",
                    ],
                    'dispatch_mode' => $dispatchMode,
                ],
                'your-app-name'
            );
            $config->initializeTracer();
            $this->expectNotToPerformAssertions();
        }
    }

    /** @test */
    public function shouldPassConfiguredTagsToTracer()
    {
        $tags = [
            'bar' => 'a-value',
            'other.tag' => 'foo',
        ];

        $config = new Config([
            'sampler' => [
                'type' => SAMPLER_TYPE_CONST,
                'param' => true,
            ],
            'service_name' => 'test-service-name',
            'tags' => $tags,
        ]);

        $tracer = $config->initializeTracer();
        $span = $tracer->startSpan('test-span');
        $spanTags = $span->getTags();

        foreach ($tags as $name => $value) {
            $this->assertArrayHasKey($name, $spanTags, "Tag '$name' should be set on span");
            $this->assertEquals($value, $spanTags[$name]->value, "Tag '$name' should have configured value");
        }
    }

    /**
     * @test
     * @dataProvider shouldSetConfigPropertiesFromEnvVarsProvider
     */
    public function shouldSetConfigPropertiesFromEnvVars($varName, $varVal, $initialConfig, $valueGetter, $expectedVal)
    {
        $_ENV[$varName] = $varVal;

        $config = new Config([]);
        $configProperty = (new \ReflectionObject($config))->getProperty('config');
        $configProperty->setAccessible('true');
        $configArray = $configProperty->getValue($config);

        $this->assertSame($expectedVal, $valueGetter($configArray));
    }

    /**
     * @test
     * @dataProvider shouldSetConfigPropertiesFromEnvVarsProvider
     */
    public function shouldNotSetConfigPropertiesFromEnvVars($varName, $varVal, $initialConfig, $valueGetter, $expectedVal)
    {
        $_ENV[$varName] = $varVal;

        $config = new Config($initialConfig);
        $configProperty = (new \ReflectionObject($config))->getProperty('config');
        $configProperty->setAccessible('true');
        $configArray = $configProperty->getValue($config);

        $this->assertNotEquals($expectedVal, $valueGetter($configArray));
    }

    /**
     *  0 -> varName
     *  1 -> varVal
     *  2 -> initialConfig
     *  3 -> valueGetter
     *  4 -> expectedVal
     */
    public function shouldSetConfigPropertiesFromEnvVarsProvider() {
        return [
            [
                'JAEGER_SERVICE_NAME',
                'some-str',
                ['service_name' => 'some-other-str'],
                function ($a) { return $a['service_name']; },
                'some-str',
            ],
            [
                'JAEGER_TAGS',
                'some-str',
                ['tags' => 'some-other-str'],
                function ($a) { return $a['tags']; },
                'some-str',
            ],
            [
                'JAEGER_AGENT_HOST',
                'some-str',
                ['local_agent' => ['reporting_host' => 'some-other-str']],
                function ($a) { return $a['local_agent']['reporting_host'];},
                'some-str',
            ],
            [
                'JAEGER_AGENT_PORT',
                '2222',
                ['local_agent' => ['reporting_port' => 1111]],
                function ($a) { return $a['local_agent']['reporting_port']; },
                2222,
            ],
            [
                'JAEGER_REPORTER_LOG_SPANS',
                'true',
                ['logging' => false],
                function ($a) { return $a['logging']; },
                true,
            ],
            [
                'JAEGER_REPORTER_MAX_QUEUE_SIZE',
                '2222',
                ['max_buffer_length' => 1111],
                function ($a) { return $a['max_buffer_length']; },
                2222,
            ],
            [
                'JAEGER_SAMPLER_TYPE',
                'some-str',
                ['sampler' => ['type' => 'some-other-str']],
                function ($a) { return $a['sampler']['type']; },
                'some-str',
            ],
            [
                'JAEGER_SAMPLER_PARAM',
                'some-str',
                ['sampler' => ['param' => 'some-other-str']],
                function ($a) { return $a['sampler']['param']; },
                'some-str',
            ],
        ];
    }
}