|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2018 OpenCensus Authors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +namespace OpenCensus\Tests\Integration\Trace\Exporter; |
| 19 | + |
| 20 | +use GuzzleHttp\Client; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | + |
| 23 | +class JaegerExporterTest extends TestCase |
| 24 | +{ |
| 25 | + private static $jaegerClient; |
| 26 | + |
| 27 | + public static function setUpBeforeClass() |
| 28 | + { |
| 29 | + parent::setUpBeforeClass(); |
| 30 | + |
| 31 | + $jaegerHost = getenv('JAEGER_HOST') ?: 'localhost'; |
| 32 | + $jaegerPort = (int)(getenv('JAEGER_PORT') ?: 16686); |
| 33 | + self::$jaegerClient = new Client([ |
| 34 | + 'base_uri' => sprintf('http://%s:%d/', $jaegerHost, $jaegerPort) |
| 35 | + ]); |
| 36 | + } |
| 37 | + |
| 38 | + public function testReportsTraceToJaeger() |
| 39 | + { |
| 40 | + $rand = mt_rand(); |
| 41 | + $client = new Client(['base_uri' => 'http://localhost:9000']); |
| 42 | + $response = $client->request('GET', '/', [ |
| 43 | + 'query' => [ |
| 44 | + 'rand' => $rand |
| 45 | + ] |
| 46 | + ]); |
| 47 | + $this->assertEquals(200, $response->getStatusCode()); |
| 48 | + $this->assertEquals('Hello world!', $response->getBody()->getContents()); |
| 49 | + |
| 50 | + $response = $this->findTraces($rand); |
| 51 | + $this->assertEquals(200, $response->getStatusCode()); |
| 52 | + $data = json_decode($response->getBody()->getContents(), true); |
| 53 | + $this->assertCount(1, $data['data']); |
| 54 | + $trace = $data['data'][0]; |
| 55 | + $this->assertCount(2, $trace['spans']); |
| 56 | + } |
| 57 | + |
| 58 | + public function testCanReachJaegerServer() |
| 59 | + { |
| 60 | + $response = self::$jaegerClient->request('GET', '/search'); |
| 61 | + $this->assertEquals(200, $response->getStatusCode()); |
| 62 | + } |
| 63 | + |
| 64 | + private function findTraces($rand) |
| 65 | + { |
| 66 | + return self::$jaegerClient->request('GET', '/api/traces', [ |
| 67 | + 'query' => [ |
| 68 | + 'service' => 'integration-test', |
| 69 | + 'operation' => "/?rand=$rand", |
| 70 | + 'limit' => 20, |
| 71 | + 'lookback' => '1h' |
| 72 | + ] |
| 73 | + ]); |
| 74 | + } |
| 75 | +} |
0 commit comments