From 4d33273ba00872322ff632541831b8c2a1266990 Mon Sep 17 00:00:00 2001 From: "742161455@qq.com" <742161455@qq.com> Date: Thu, 27 Aug 2020 00:46:31 +0800 Subject: [PATCH 01/32] todo list --- README.md | 106 +++--------------------------------------------------- 1 file changed, 5 insertions(+), 101 deletions(-) diff --git a/README.md b/README.md index 03b4421..e3855f4 100644 --- a/README.md +++ b/README.md @@ -1,103 +1,7 @@ -[![Build Status](https://travis-ci.com/jukylin/jaeger-php.svg?branch=master)](https://travis-ci.com/jukylin/jaeger-php) -[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%205.6-8892BF.svg)](https://php.net/) -[![License](https://img.shields.io/github/license/jukylin/jaeger-php.svg)](https://github.com/jukylin/jaeger-php/blob/master/LICENSE) -[![Coverage Status](https://coveralls.io/repos/github/jukylin/jaeger-php/badge.svg?branch=master)](https://coveralls.io/github/jukylin/jaeger-php?branch=master) - # jaeger-php -## Install - -Install via composer. - -``` -composer config minimum-stability dev -composer require jukylin/jaeger-php -``` - -## Init Jaeger-php - -```php -$config = Config::getInstance(); -$tracer = $config->initTracer('example', '0.0.0.0:6831'); -``` - -## 128bit - -```php -$config->gen128bit(); -``` - -## Extract from Superglobals - -```php -$spanContext = $tracer->extract(Formats\TEXT_MAP, $_SERVER); -``` - -## Start Span - -```php -$serverSpan = $tracer->startSpan('example HTTP', ['child_of' => $spanContext]); -``` - -## Distributed context propagation -```php -$serverSpan->addBaggageItem("version", "2.0.0"); -``` - -## Inject into Superglobals - -```php -$clientTrace->inject($clientSpan1->spanContext, Formats\TEXT_MAP, $_SERVER); -``` - -## Tags and Log - -```php -// tags are searchable in Jaeger UI -$span->setTag('http.status', '200'); - -// log record -$span->log(['error' => 'HTTP request timeout']); -``` - -## Close Tracer - -```php -$config->setDisabled(true); -``` - -## Zipkin B3 Propagation - -*no support for* `Distributed context propagation` - -```php -$config::$propagator = \Jaeger\Constants\PROPAGATOR_ZIPKIN; -``` - -## Finish span and flush Tracer - -```php -$span->finish(); -$config->flush(); -``` - -## More example - -- [HTTP](https://github.com/jukylin/jaeger-php/blob/master/example/HTTP.php) -- [Hprose](https://github.com/jukylin/blog/blob/master/Uber%E5%88%86%E5%B8%83%E5%BC%8F%E8%BF%BD%E8%B8%AA%E7%B3%BB%E7%BB%9FJaeger%E4%BD%BF%E7%94%A8%E4%BB%8B%E7%BB%8D%E5%92%8C%E6%A1%88%E4%BE%8B%E3%80%90PHP%20%20%20Hprose%20%20%20Go%E3%80%91.md#跨语言调用案例) -- [Istio](https://github.com/jukylin/jaeger-php/blob/master/example/README.md) - -## Features - -- Transports - - via Thrift over UDP - -- Sampling - - ConstSampler - - ProbabilisticSampler - -## Reference - -[OpenTracing](https://opentracing.io/) - -[Jaeger](https://uber.github.io/jaeger/) +## TODO +- Implement the latest opentracing/opentracing +- Improve documentation +- Refactor thrift +- Improve unit test coverage From d21c078ebbda61939a7adefda00b8ed1df03877d Mon Sep 17 00:00:00 2001 From: Xiangxuan Liu Date: Fri, 25 Dec 2020 21:40:29 +0800 Subject: [PATCH 02/32] Migrate from travis to GitHub Actions --- .github/workflows/test.yaml | 41 +++++++++++++++++++++++++++++++++++++ .travis.yml | 18 ---------------- 2 files changed, 41 insertions(+), 18 deletions(-) create mode 100644 .github/workflows/test.yaml delete mode 100644 .travis.yml diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..b1e87ad --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,41 @@ +name: Tests + +on: [push, pull_request] + +jobs: + test: + runs-on: ubuntu-latest + strategy: + fail-fast: true + matrix: + php: + - '5.6' + - '7.0' + - '7.1' + - '7.2' + - '7.3' + - '7.4' + dependency-version: [prefer-stable] + name: ${{ matrix.php }} - ${{ matrix.dependency-version }} + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Cache dependencies + uses: actions/cache@v2 + with: + path: ~/.composer/cache/files + key: dependencies-laravel-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + coverage: none + + - name: Install dependencies + run: | + composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction + - name: Execute tests + run: ./vendor/bin/phpunit \ No newline at end of file diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index f3562fe..0000000 --- a/.travis.yml +++ /dev/null @@ -1,18 +0,0 @@ -language: php - -php: - - '5.6' - - '7.0' - - '7.1' - - '7.2' - - '7.3' - - -install: - - composer install - -script: - - composer test - -after_success: - - travis_retry php vendor/bin/coveralls -v \ No newline at end of file From e0b75d87a25d6b8317463b2f11215396a49c990b Mon Sep 17 00:00:00 2001 From: Xiangxuan Liu Date: Fri, 25 Dec 2020 23:48:50 +0800 Subject: [PATCH 03/32] Support opentracing v1.0 --- composer.json | 9 +++++---- example/HTTP.php | 4 ++-- src/Jaeger/Config.php | 5 +++-- src/Jaeger/Jaeger.php | 22 ++++++++++++---------- src/Jaeger/Scope.php | 4 ++-- src/Jaeger/ScopeManager.php | 17 +++++------------ src/Jaeger/Span.php | 26 +++++++++++++------------- src/Jaeger/SpanContext.php | 9 ++++++--- tests/JaegerTest.php | 20 ++++++++++---------- tests/SpanContextTest.php | 8 +++++--- 10 files changed, 63 insertions(+), 61 deletions(-) diff --git a/composer.json b/composer.json index 0216a99..ef6a2ad 100644 --- a/composer.json +++ b/composer.json @@ -5,9 +5,9 @@ "license": "Apache-2.0", "minimum-stability": "stable", "require": { - "php": ">=5.6.0", - "packaged/thrift" : "0.10.0", - "opentracing/opentracing" : "1.0.0-beta5" + "php": ">=7.1", + "packaged/thrift" : "~0.10.0", + "opentracing/opentracing" : "^1.0.0" }, "authors": [ { @@ -28,6 +28,7 @@ }, "require-dev": { "phpunit/phpunit": "^5", - "php-coveralls/php-coveralls": "^1.0" + "php-coveralls/php-coveralls": "^1.0", + "phpstan/phpstan": "^0.12.64" } } diff --git a/example/HTTP.php b/example/HTTP.php index 07c185a..6200d25 100644 --- a/example/HTTP.php +++ b/example/HTTP.php @@ -65,8 +65,8 @@ $spanContext = $clientTracer->extract(Formats\TEXT_MAP, $_SERVER); $clientSpan2 = $clientTracer->startSpan('HTTP2', ['references' => [ - Reference::create(Reference::FOLLOWS_FROM, $clientSpan1->spanContext), - Reference::create(Reference::CHILD_OF, $spanContext) + Reference::createForSpan(Reference::FOLLOWS_FROM, $clientSpan1->spanContext), + Reference::createForSpan(Reference::CHILD_OF, $spanContext) ]]); $clientTracer->inject($clientSpan2->spanContext, Formats\TEXT_MAP, $injectTarget2); diff --git a/src/Jaeger/Config.php b/src/Jaeger/Config.php index cba4c79..5cc7086 100644 --- a/src/Jaeger/Config.php +++ b/src/Jaeger/Config.php @@ -23,6 +23,7 @@ use Jaeger\Sampler\ConstSampler; use Jaeger\Propagator\JaegerPropagator; use Jaeger\Propagator\ZipkinPropagator; +use OpenTracing\Tracer; class Config { @@ -74,10 +75,10 @@ public static function getInstance() * @return Jaeger|null * @throws \Exception */ - public function initTracer($serverName, $agentHostPort = ''){ + public function initTracer($serverName, $agentHostPort = ''): Tracer{ if(self::$disabled){ - return NoopTracer::create(); + return new NoopTracer(); } if($serverName == ''){ diff --git a/src/Jaeger/Jaeger.php b/src/Jaeger/Jaeger.php index f317246..b3cd699 100644 --- a/src/Jaeger/Jaeger.php +++ b/src/Jaeger/Jaeger.php @@ -17,6 +17,7 @@ use Jaeger\Sampler\Sampler; use OpenTracing\Exceptions\UnsupportedFormat; +use OpenTracing\ScopeManager; use OpenTracing\SpanContext; use OpenTracing\Formats; use OpenTracing\Tracer; @@ -24,6 +25,7 @@ use OpenTracing\StartSpanOptions; use OpenTracing\Reference; use Jaeger\Propagator\Propagator; +use OpenTracing\UnsupportedFormatException; class Jaeger implements Tracer{ @@ -84,7 +86,7 @@ public function setTags(array $tags = []){ * @param array $options * @return Span */ - public function startSpan($operationName, $options = []){ + public function startSpan(string $operationName, $options = []): \OpenTracing\Span { if (!($options instanceof StartSpanOptions)) { $options = StartSpanOptions::create($options); @@ -135,11 +137,11 @@ public function setPropagator(Propagator $propagator){ * @param string $format * @param $carrier */ - public function inject(SpanContext $spanContext, $format, &$carrier){ + public function inject(SpanContext $spanContext, string $format, &$carrier): void { if($format == Formats\TEXT_MAP){ $this->propagator->inject($spanContext, $format, $carrier); }else{ - throw UnsupportedFormat::forFormat($format); + throw UnsupportedFormatException::forFormat($format); } } @@ -149,11 +151,11 @@ public function inject(SpanContext $spanContext, $format, &$carrier){ * @param string $format * @param $carrier */ - public function extract($format, $carrier){ + public function extract(string $format, $carrier): ?SpanContext{ if($format == Formats\TEXT_MAP){ return $this->propagator->extract($format, $carrier); }else{ - throw UnsupportedFormat::forFormat($format); + throw UnsupportedFormatException::forFormat($format); } } @@ -171,12 +173,12 @@ public function reportSpan(){ } - public function getScopeManager(){ + public function getScopeManager(): ScopeManager{ return $this->scopeManager; } - public function getActiveSpan(){ + public function getActiveSpan(): ?\OpenTracing\Span { $activeScope = $this->getScopeManager()->getActive(); if ($activeScope === null) { return null; @@ -186,7 +188,7 @@ public function getActiveSpan(){ } - public function startActiveSpan($operationName, $options = []){ + public function startActiveSpan(string $operationName, $options = []): \OpenTracing\Scope { if (!$options instanceof StartSpanOptions) { $options = StartSpanOptions::create($options); } @@ -209,7 +211,7 @@ private function getParentSpanContext(StartSpanOptions $options) $parentSpan = null; foreach ($references as $ref) { - $parentSpan = $ref->getContext(); + $parentSpan = $ref->getSpanContext(); if ($ref->isType(Reference::CHILD_OF)) { return $parentSpan; } @@ -250,7 +252,7 @@ public function gen128bit(){ /** * 结束,发送信息到jaeger */ - public function flush(){ + public function flush(): void{ $this->reportSpan(); $this->reporter->close(); } diff --git a/src/Jaeger/Scope.php b/src/Jaeger/Scope.php index e885d2d..654c812 100644 --- a/src/Jaeger/Scope.php +++ b/src/Jaeger/Scope.php @@ -35,7 +35,7 @@ public function __construct(ScopeManager $scopeManager, \OpenTracing\Span $span, } - public function close(){ + public function close(): void{ if ($this->finishSpanOnClose) { $this->span->finish(); } @@ -44,7 +44,7 @@ public function close(){ } - public function getSpan(){ + public function getSpan(): \OpenTracing\Span { return $this->span; } } diff --git a/src/Jaeger/ScopeManager.php b/src/Jaeger/ScopeManager.php index 3d11a06..932fed8 100644 --- a/src/Jaeger/ScopeManager.php +++ b/src/Jaeger/ScopeManager.php @@ -7,14 +7,10 @@ class ScopeManager implements \OpenTracing\ScopeManager{ private $scopes = []; - /** - * append scope - * @param \OpenTracing\Span $span - * @param bool $finishSpanOnClose - * @return Scope + * @inheritDoc */ - public function activate(\OpenTracing\Span $span, $finishSpanOnClose){ + public function activate(\OpenTracing\Span $span, bool $finishSpanOnClose = self::DEFAULT_FINISH_SPAN_ON_CLOSE): \OpenTracing\Scope{ $scope = new Scope($this, $span, $finishSpanOnClose); $this->scopes[] = $scope; return $scope; @@ -22,10 +18,9 @@ public function activate(\OpenTracing\Span $span, $finishSpanOnClose){ /** - * get last scope - * @return mixed|null + * @inheritDoc */ - public function getActive(){ + public function getActive(): ?Scope{ if (empty($this->scopes)) { return null; } @@ -35,9 +30,7 @@ public function getActive(){ /** - * del scope - * @param Scope $scope - * @return bool + * @inheritDoc */ public function delActive(Scope $scope){ $scopeLength = count($this->scopes); diff --git a/src/Jaeger/Span.php b/src/Jaeger/Span.php index 8fee8fd..d254aa2 100644 --- a/src/Jaeger/Span.php +++ b/src/Jaeger/Span.php @@ -16,6 +16,8 @@ namespace Jaeger; +use OpenTracing\SpanContext; + class Span implements \OpenTracing\Span{ private $operationName = ''; @@ -46,24 +48,21 @@ public function __construct($operationName, \OpenTracing\SpanContext $spanContex /** * @return string */ - public function getOperationName(){ + public function getOperationName(): string { return $this->operationName; } /** * @return SpanContext */ - public function getContext(){ + public function getContext(): SpanContext{ return $this->spanContext; } /** - * @param float|int|\DateTimeInterface|null $finishTime if passing float or int - * it should represent the timestamp (including as many decimal places as you need) - * @param array $logRecords - * @return mixed + * @inheritDoc */ - public function finish($finishTime = null, array $logRecords = []){ + public function finish($finishTime = null): void{ $this->finishTime = $finishTime == null ? $this->microtimeToInt() : $finishTime; $this->duration = $this->finishTime - $this->startTime; } @@ -71,12 +70,12 @@ public function finish($finishTime = null, array $logRecords = []){ /** * @param string $newOperationName */ - public function overwriteOperationName($newOperationName){ + public function overwriteOperationName(string $newOperationName): void { $this->operationName = $newOperationName; } - public function setTag($key, $value){ + public function setTag(string $key, $value): void{ $this->tags[$key] = $value; } @@ -88,7 +87,7 @@ public function setTag($key, $value){ * @param int|float|\DateTimeInterface $timestamp * @throws SpanAlreadyFinished if the span is already finished */ - public function log(array $fields = [], $timestamp = null){ + public function log(array $fields = [], $timestamp = null): void { $log['timestamp'] = $timestamp ? $timestamp : $this->microtimeToInt(); $log['fields'] = $fields; $this->logs[] = $log; @@ -102,20 +101,21 @@ public function log(array $fields = [], $timestamp = null){ * @param string $value * @throws SpanAlreadyFinished if the span is already finished */ - public function addBaggageItem($key, $value){ + public function addBaggageItem(string $key, string $value): void{ $this->log([ 'event' => 'baggage', 'key' => $key, 'value' => $value, ]); - return $this->spanContext->withBaggageItem($key, $value); + + $this->spanContext->withBaggageItem($key, $value); } /** * @param string $key * @return string|null */ - public function getBaggageItem($key){ + public function getBaggageItem(string $key): ?string{ return $this->spanContext->getBaggageItem($key); } diff --git a/src/Jaeger/SpanContext.php b/src/Jaeger/SpanContext.php index 955d023..247023d 100644 --- a/src/Jaeger/SpanContext.php +++ b/src/Jaeger/SpanContext.php @@ -53,14 +53,17 @@ public function __construct($spanId, $parentId, $flags, $baggage = null, $debugI } - public function getBaggageItem($key){ + public function getBaggageItem(string $key): ?string { return isset($this->baggage[$key]) ? $this->baggage[$key] : null; } - public function withBaggageItem($key, $value){ + public function withBaggageItem(string $key, string $value): ?\OpenTracing\SpanContext + { $this->baggage[$key] = $value; - return true; + + // TODO + return null; } public function getIterator() diff --git a/tests/JaegerTest.php b/tests/JaegerTest.php index 7f2eaa0..bca0615 100644 --- a/tests/JaegerTest.php +++ b/tests/JaegerTest.php @@ -82,7 +82,7 @@ public function testInjectUnSupportFormat(){ $Jaeger->setPropagator(new JaegerPropagator()); $context = new SpanContext(1, 1, 1, null, 1); - $this->expectExceptionMessage('The format \'http_headers\' is not supported.'); + $this->expectExceptionMessage('The format "http_headers" is not supported.'); $Jaeger->inject($context, Formats\HTTP_HEADERS, $_SERVER); } @@ -106,7 +106,7 @@ public function testExtractUnSupportFormat(){ $Jaeger->setPropagator(new JaegerPropagator()); $_SERVER[strtoupper(Constants\Tracer_State_Header_Name)] = '1:1:1:1'; - $this->expectExceptionMessage('The format \'http_headers\' is not supported.'); + $this->expectExceptionMessage('The format "http_headers" is not supported.'); $Jaeger->extract(Formats\HTTP_HEADERS, $_SERVER); } @@ -125,17 +125,17 @@ public function testStartSpanWithFollowsFromTypeRef() $jaeger = $this->getJaeger(); $rootSpan = $jaeger->startSpan('root-a'); $childSpan = $jaeger->startSpan('span-a', [ - 'references' => Reference::create(Reference::FOLLOWS_FROM, $rootSpan), + 'references' => Reference::createForSpan(Reference::FOLLOWS_FROM, $rootSpan), ]); $this->assertSame($childSpan->spanContext->traceIdLow, $rootSpan->spanContext->traceIdLow); - $this->assertSame(current($childSpan->references)->getContext(), $rootSpan->spanContext); + $this->assertSame(current($childSpan->references)->getSpanContext(), $rootSpan->spanContext); $otherRootSpan = $jaeger->startSpan('root-a'); $childSpan = $jaeger->startSpan('span-b', [ 'references' => [ - Reference::create(Reference::FOLLOWS_FROM, $rootSpan), - Reference::create(Reference::FOLLOWS_FROM, $otherRootSpan), + Reference::createForSpan(Reference::FOLLOWS_FROM, $rootSpan), + Reference::createForSpan(Reference::FOLLOWS_FROM, $otherRootSpan), ], ]); @@ -150,8 +150,8 @@ public function testStartSpanWithChildOfTypeRef() $otherRootSpan = $jaeger->startSpan('root-b'); $childSpan = $jaeger->startSpan('span-a', [ 'references' => [ - Reference::create(Reference::CHILD_OF, $rootSpan), - Reference::create(Reference::CHILD_OF, $otherRootSpan), + Reference::createForSpan(Reference::CHILD_OF, $rootSpan), + Reference::createForSpan(Reference::CHILD_OF, $otherRootSpan), ], ]); @@ -175,8 +175,8 @@ public function testStartSpanWithAllRefType() $otherRootSpan = $jaeger->startSpan('root-b'); $childSpan = $jaeger->startSpan('span-a', [ 'references' => [ - Reference::create(Reference::FOLLOWS_FROM, $rootSpan), - Reference::create(Reference::CHILD_OF, $otherRootSpan), + Reference::createForSpan(Reference::FOLLOWS_FROM, $rootSpan), + Reference::createForSpan(Reference::CHILD_OF, $otherRootSpan), ], ]); diff --git a/tests/SpanContextTest.php b/tests/SpanContextTest.php index 8f7dae2..13c3308 100644 --- a/tests/SpanContextTest.php +++ b/tests/SpanContextTest.php @@ -36,16 +36,18 @@ public function testNew(){ public function testWithBaggageItem(){ $spanContext = $this->getSpanContext(); $res = $spanContext->withBaggageItem('version', '2.0.0'); - $this->assertTrue($res); + // TODO + // $this->assertTrue($res); } public function testGetBaggageItem(){ $spanContext = $this->getSpanContext(); - $res = $spanContext->withBaggageItem('version', '2.0.0'); + $spanContext->withBaggageItem('version', '2.0.0'); $version = $spanContext->getBaggageItem('version'); - $this->assertTrue($res == $version); + + $this->assertTrue($version === '2.0.0'); $service = $spanContext->getBaggageItem('service'); $this->assertNull($service); From c3bdb70e953aa2e66f5f5bc6b12c62108d656ffb Mon Sep 17 00:00:00 2001 From: Xiangxuan Liu Date: Fri, 25 Dec 2020 23:51:24 +0800 Subject: [PATCH 04/32] Change CI php version --- .github/workflows/test.yaml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index b1e87ad..f2ed9b5 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -9,12 +9,11 @@ jobs: fail-fast: true matrix: php: - - '5.6' - - '7.0' - '7.1' - '7.2' - '7.3' - '7.4' + - '8.0' dependency-version: [prefer-stable] name: ${{ matrix.php }} - ${{ matrix.dependency-version }} From e30d6a450b59bd974d5dc7abf49452251ef5dfda Mon Sep 17 00:00:00 2001 From: Xiangxuan Liu Date: Fri, 25 Dec 2020 23:53:48 +0800 Subject: [PATCH 05/32] Fix phpunit tests --- src/Jaeger/ScopeManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Jaeger/ScopeManager.php b/src/Jaeger/ScopeManager.php index 932fed8..915193f 100644 --- a/src/Jaeger/ScopeManager.php +++ b/src/Jaeger/ScopeManager.php @@ -20,7 +20,7 @@ public function activate(\OpenTracing\Span $span, bool $finishSpanOnClose = self /** * @inheritDoc */ - public function getActive(): ?Scope{ + public function getActive(): ?\OpenTracing\Scope{ if (empty($this->scopes)) { return null; } From 11db65466bbf7fd55f99f59fcc4971438b1e5f20 Mon Sep 17 00:00:00 2001 From: Xiangxuan Liu Date: Fri, 25 Dec 2020 23:59:34 +0800 Subject: [PATCH 06/32] Update README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 03b4421..b115f46 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ [![Build Status](https://travis-ci.com/jukylin/jaeger-php.svg?branch=master)](https://travis-ci.com/jukylin/jaeger-php) -[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%205.6-8892BF.svg)](https://php.net/) +[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%207.1-8892BF.svg)](https://php.net/) [![License](https://img.shields.io/github/license/jukylin/jaeger-php.svg)](https://github.com/jukylin/jaeger-php/blob/master/LICENSE) [![Coverage Status](https://coveralls.io/repos/github/jukylin/jaeger-php/badge.svg?branch=master)](https://coveralls.io/github/jukylin/jaeger-php?branch=master) From b1dc43fbfbe3fb9d79da75a91d80866675f59f2f Mon Sep 17 00:00:00 2001 From: Xiangxuan Liu Date: Sat, 26 Dec 2020 00:30:21 +0800 Subject: [PATCH 07/32] Bump PHPStan level 4 --- composer.json | 2 +- phpstan.neon | 9 +++++++++ src/Jaeger/Config.php | 16 ++++++--------- src/Jaeger/Jaeger.php | 27 +++++++++++++------------- src/Jaeger/Propagator/Propagator.php | 4 ++-- src/Jaeger/Scope.php | 10 +++++----- src/Jaeger/ScopeManager.php | 5 +---- src/Jaeger/Span.php | 2 -- src/Jaeger/SpanContext.php | 1 + src/Jaeger/Thrift/JaegerThriftSpan.php | 2 ++ src/Jaeger/Transport/TransportUdp.php | 2 +- src/Jaeger/UdpClient.php | 3 ++- tests/ScopeMangerTest.php | 14 ++++++------- tests/SpanContextTest.php | 2 +- 14 files changed, 51 insertions(+), 48 deletions(-) create mode 100644 phpstan.neon diff --git a/composer.json b/composer.json index ef6a2ad..9407a8c 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ "minimum-stability": "stable", "require": { "php": ">=7.1", - "packaged/thrift" : "~0.10.0", + "packaged/thrift" : "~0.13.0", "opentracing/opentracing" : "^1.0.0" }, "authors": [ diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..284c978 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,9 @@ +parameters: + level: 4 + paths: + - src + - tests + ignoreErrors: + - + message: '#Constant#' + path: src/Jaeger/Constants.php diff --git a/src/Jaeger/Config.php b/src/Jaeger/Config.php index 5cc7086..0d984b2 100644 --- a/src/Jaeger/Config.php +++ b/src/Jaeger/Config.php @@ -70,19 +70,19 @@ public static function getInstance() /** * init jaeger, return can use flush buffers - * @param $serviceName + * @param string $serverName * @param string $agentHostPort - * @return Jaeger|null - * @throws \Exception + * @return Tracer + * @throws \RuntimeException */ - public function initTracer($serverName, $agentHostPort = ''): Tracer{ + public function initTracer(string $serverName, $agentHostPort = ''): Tracer{ if(self::$disabled){ return new NoopTracer(); } if($serverName == ''){ - throw new \Exception("serverName require"); + throw new \RuntimeException("serverName require"); } if(isset(self::$tracer[$serverName]) && !empty(self::$tracer[$serverName])){ @@ -126,11 +126,7 @@ public function initTracer($serverName, $agentHostPort = ''): Tracer{ } - /** - * close tracer - * @param $disabled - */ - public function setDisabled($disabled){ + public function setDisabled(bool $disabled){ self::$disabled = $disabled; return $this; diff --git a/src/Jaeger/Jaeger.php b/src/Jaeger/Jaeger.php index b3cd699..3082dcf 100644 --- a/src/Jaeger/Jaeger.php +++ b/src/Jaeger/Jaeger.php @@ -81,10 +81,7 @@ public function setTags(array $tags = []){ /** - * init span info - * @param string $operationName - * @param array $options - * @return Span + * @inheritDoc */ public function startSpan(string $operationName, $options = []): \OpenTracing\Span { @@ -135,7 +132,7 @@ public function setPropagator(Propagator $propagator){ * 注入 * @param SpanContext $spanContext * @param string $format - * @param $carrier + * @param mixed $carrier */ public function inject(SpanContext $spanContext, string $format, &$carrier): void { if($format == Formats\TEXT_MAP){ @@ -149,7 +146,7 @@ public function inject(SpanContext $spanContext, string $format, &$carrier): voi /** * 提取 * @param string $format - * @param $carrier + * @param mixed $carrier */ public function extract(string $format, $carrier): ?SpanContext{ if($format == Formats\TEXT_MAP){ @@ -208,21 +205,23 @@ private function getParentSpanContext(StartSpanOptions $options) { $references = $options->getReferences(); - $parentSpan = null; + $parentSpanContext = null; foreach ($references as $ref) { - $parentSpan = $ref->getSpanContext(); + $parentSpanContext = $ref->getSpanContext(); if ($ref->isType(Reference::CHILD_OF)) { - return $parentSpan; + return $parentSpanContext; } } - if ($parentSpan) { - if (($parentSpan->isValid() - || (!$parentSpan->isTraceIdValid() && $parentSpan->debugId) - || count($parentSpan->baggage) > 0) + if ($parentSpanContext) { + assert($parentSpanContext instanceof \Jaeger\SpanContext); + + if (($parentSpanContext->isValid() + || (!$parentSpanContext->isTraceIdValid() && $parentSpanContext->debugId) + || count($parentSpanContext->baggage) > 0) ) { - return $parentSpan; + return $parentSpanContext; } } diff --git a/src/Jaeger/Propagator/Propagator.php b/src/Jaeger/Propagator/Propagator.php index d094e4d..d0aeb13 100644 --- a/src/Jaeger/Propagator/Propagator.php +++ b/src/Jaeger/Propagator/Propagator.php @@ -23,7 +23,7 @@ interface Propagator{ * 注入 * @param SpanContext $spanContext * @param string $format - * @param $carrier + * @param mixed $carrier */ public function inject(SpanContext $spanContext, $format, &$carrier); @@ -31,7 +31,7 @@ public function inject(SpanContext $spanContext, $format, &$carrier); /** * 提取 * @param string $format - * @param $carrier + * @param mixed $carrier * @return SpanContext|null */ public function extract($format, $carrier); diff --git a/src/Jaeger/Scope.php b/src/Jaeger/Scope.php index 654c812..89c74bd 100644 --- a/src/Jaeger/Scope.php +++ b/src/Jaeger/Scope.php @@ -7,12 +7,12 @@ class Scope implements \OpenTracing\Scope{ /** - * @var MockScopeManager + * @var ScopeManager */ private $scopeManager = null; /** - * @var span + * @var Span */ private $span = null; @@ -25,10 +25,10 @@ class Scope implements \OpenTracing\Scope{ /** * Scope constructor. * @param ScopeManager $scopeManager - * @param \OpenTracing\Span $span + * @param Span $span * @param bool $finishSpanOnClose */ - public function __construct(ScopeManager $scopeManager, \OpenTracing\Span $span, $finishSpanOnClose){ + public function __construct(ScopeManager $scopeManager, Span $span, $finishSpanOnClose){ $this->scopeManager = $scopeManager; $this->span = $span; $this->finishSpanOnClose = $finishSpanOnClose; @@ -40,7 +40,7 @@ public function close(): void{ $this->span->finish(); } - $this->scopeManager->delActive($this); + $this->scopeManager->deactivate($this); } diff --git a/src/Jaeger/ScopeManager.php b/src/Jaeger/ScopeManager.php index 915193f..cf8b353 100644 --- a/src/Jaeger/ScopeManager.php +++ b/src/Jaeger/ScopeManager.php @@ -29,10 +29,7 @@ public function getActive(): ?\OpenTracing\Scope{ } - /** - * @inheritDoc - */ - public function delActive(Scope $scope){ + public function deactivate(Scope $scope){ $scopeLength = count($this->scopes); if($scopeLength <= 0){ diff --git a/src/Jaeger/Span.php b/src/Jaeger/Span.php index d254aa2..0fa2526 100644 --- a/src/Jaeger/Span.php +++ b/src/Jaeger/Span.php @@ -85,7 +85,6 @@ public function setTag(string $key, $value): void{ * * @param array $fields [key => val] * @param int|float|\DateTimeInterface $timestamp - * @throws SpanAlreadyFinished if the span is already finished */ public function log(array $fields = [], $timestamp = null): void { $log['timestamp'] = $timestamp ? $timestamp : $this->microtimeToInt(); @@ -99,7 +98,6 @@ public function log(array $fields = [], $timestamp = null): void { * * @param string $key * @param string $value - * @throws SpanAlreadyFinished if the span is already finished */ public function addBaggageItem(string $key, string $value): void{ $this->log([ diff --git a/src/Jaeger/SpanContext.php b/src/Jaeger/SpanContext.php index 247023d..cf28f61 100644 --- a/src/Jaeger/SpanContext.php +++ b/src/Jaeger/SpanContext.php @@ -68,6 +68,7 @@ public function withBaggageItem(string $key, string $value): ?\OpenTracing\SpanC public function getIterator() { + // @phpstan-ignore-next-line // TODO: Implement getIterator() method. } diff --git a/src/Jaeger/Thrift/JaegerThriftSpan.php b/src/Jaeger/Thrift/JaegerThriftSpan.php index 446fdd4..2fdd8d5 100644 --- a/src/Jaeger/Thrift/JaegerThriftSpan.php +++ b/src/Jaeger/Thrift/JaegerThriftSpan.php @@ -98,6 +98,8 @@ private function buildReferences($references){ $type = SpanRefType::CHILD_OF; }else if($ref->isType(Reference::FOLLOWS_FROM)){ $type = SpanRefType::FOLLOWS_FROM; + } else { + throw new \LogicException("Unsupported reference type"); } $ctx = $ref->getContext(); $spanRef[] = [ diff --git a/src/Jaeger/Transport/TransportUdp.php b/src/Jaeger/Transport/TransportUdp.php index c3e6464..57eec1f 100644 --- a/src/Jaeger/Transport/TransportUdp.php +++ b/src/Jaeger/Transport/TransportUdp.php @@ -137,7 +137,7 @@ public function resetBuffer() /** * 获取序列化后的thrift和计算序列化后的thrift字符长度 * @param TStruct $ts - * @param $serializedThrift + * @param mixed $serializedThrift * @return mixed */ private function getAndCalcSizeOfSerializedThrift(TStruct $ts, &$serializedThrift) diff --git a/src/Jaeger/UdpClient.php b/src/Jaeger/UdpClient.php index 8e47dfe..3bc41f6 100644 --- a/src/Jaeger/UdpClient.php +++ b/src/Jaeger/UdpClient.php @@ -50,8 +50,9 @@ public function isOpen(){ /** * send thrift - * @param $batch + * @param mixed $batch * @return bool + * @throws \Exception */ public function emitBatch($batch){ $buildThrift = $this->agentClient->buildThrift($batch); diff --git a/tests/ScopeMangerTest.php b/tests/ScopeMangerTest.php index b1d515e..69ab5e7 100644 --- a/tests/ScopeMangerTest.php +++ b/tests/ScopeMangerTest.php @@ -52,7 +52,7 @@ public function testDelActive(){ $scopeManager = new ScopeManager(); $scope = $scopeManager->activate($span, true); - $res = $scopeManager->delActive($scope); + $res = $scopeManager->deactivate($scope); $this->assertTrue($res == true); $getRes = $scopeManager->getActive(); @@ -71,17 +71,17 @@ public function testDelActiveNestedScopes() { $active = $scopeManager->getActive(); $this->assertTrue($active === $scope3); - $res = $scopeManager->delActive($scope3); + $res = $scopeManager->deactivate($scope3); $this->assertTrue($res == true); $active = $scopeManager->getActive(); $this->assertTrue($active === $scope2); - $res = $scopeManager->delActive($scope2); + $res = $scopeManager->deactivate($scope2); $this->assertTrue($res == true); $active = $scopeManager->getActive(); $this->assertTrue($active === $scope1); - $res = $scopeManager->delActive($scope1); + $res = $scopeManager->deactivate($scope1); $this->assertTrue($res == true); $active = $scopeManager->getActive(); $this->assertTrue($active === null); @@ -98,7 +98,7 @@ public function testDelActiveReNestScopes() { $this->assertTrue($active === $scope2); // Remove scope2 so that scope1 is active - $scopeManager->delActive($scope2); + $scopeManager->deactivate($scope2); $active = $scopeManager->getActive(); $this->assertTrue($active === $scope1); @@ -109,11 +109,11 @@ public function testDelActiveReNestScopes() { $this->assertTrue($active === $scope3); // Delete active scope3 - $scopeManager->delActive($scope3); + $scopeManager->deactivate($scope3); $active = $scopeManager->getActive(); $this->assertTrue($active === $scope1); - $scopeManager->delActive($scope1); + $scopeManager->deactivate($scope1); $active = $scopeManager->getActive(); $this->assertTrue($active === null); } diff --git a/tests/SpanContextTest.php b/tests/SpanContextTest.php index 13c3308..ed7db28 100644 --- a/tests/SpanContextTest.php +++ b/tests/SpanContextTest.php @@ -93,7 +93,7 @@ public function testTraceIdToString(){ $spanContext->traceIdHigh = null; $spanContext->traceIdToString('1b207'); $this->assertTrue($spanContext->traceIdLow == '111111'); - $this->assertTrue(!$spanContext->traceIdHigh); + $this->assertTrue($spanContext->traceIdHigh === null); } From 19db2aca6a39cb3238b2685deb31314f6f3ee795 Mon Sep 17 00:00:00 2001 From: Xiangxuan Liu Date: Sat, 26 Dec 2020 00:50:59 +0800 Subject: [PATCH 08/32] Disable test on PHP 8 --- .github/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index f2ed9b5..c8ee95d 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -13,7 +13,7 @@ jobs: - '7.2' - '7.3' - '7.4' - - '8.0' +# - '8.0' dependency-version: [prefer-stable] name: ${{ matrix.php }} - ${{ matrix.dependency-version }} From 8a67a8fa8588fd82a343ae674f0c60a320f2bed3 Mon Sep 17 00:00:00 2001 From: Xiangxuan Liu Date: Sat, 26 Dec 2020 15:32:11 +0800 Subject: [PATCH 09/32] Add php-cs-fixer to format code --- .gitignore | 1 + .php_cs | 21 ++++ composer.json | 8 +- example/HTTP.php | 13 +- example/HTTP2.php | 15 +-- example/Hprose.php | 14 +-- example/Istio1.php | 12 +- example/Istio2.php | 8 +- example/Istio3.php | 6 +- src/Jaeger/Config.php | 91 +++++++------- src/Jaeger/Constants.php | 28 ++--- src/Jaeger/Jaeger.php | 131 ++++++++++---------- src/Jaeger/Propagator/JaegerPropagator.php | 58 ++++----- src/Jaeger/Propagator/Propagator.php | 18 +-- src/Jaeger/Propagator/ZipkinPropagator.php | 30 ++--- src/Jaeger/Reporter/RemoteReporter.php | 7 +- src/Jaeger/Reporter/Reporter.php | 4 +- src/Jaeger/Sampler/ConstSampler.php | 21 ++-- src/Jaeger/Sampler/ProbabilisticSampler.php | 25 ++-- src/Jaeger/Sampler/Sampler.php | 3 +- src/Jaeger/Scope.php | 21 ++-- src/Jaeger/ScopeManager.php | 25 ++-- src/Jaeger/Span.php | 66 +++++----- src/Jaeger/SpanContext.php | 65 +++++----- src/Jaeger/Thrift/AgentClient.php | 20 ++- src/Jaeger/Thrift/JaegerThriftSpan.php | 44 ++++--- src/Jaeger/Thrift/Process.php | 32 +++-- src/Jaeger/Thrift/Span.php | 48 +++---- src/Jaeger/Thrift/SpanRefType.php | 5 +- src/Jaeger/Thrift/TStruct.php | 6 +- src/Jaeger/Thrift/Tags.php | 72 +++++------ src/Jaeger/Thrift/Types.php | 50 ++++---- src/Jaeger/Transport/Transport.php | 5 +- src/Jaeger/Transport/TransportUdp.php | 31 ++--- src/Jaeger/UdpClient.php | 42 ++++--- tests/ConfigTest.php | 19 ++- tests/JaegerTest.php | 79 ++++++------ tests/Propagator/JaegerPropagatorTest.php | 105 ++++++++-------- tests/Propagator/ZipkinPropagatorTest.php | 68 +++++----- tests/Reporter/RemoteReporterTest.php | 16 +-- tests/Sampler/ConstSamplerTest.php | 15 +-- tests/Sampler/ProbabilisticSamplerTest.php | 15 +-- tests/ScopeMangerTest.php | 39 +++--- tests/ScopeTest.php | 18 +-- tests/SpanContextTest.php | 68 +++++----- tests/SpanTest.php | 39 +++--- tests/Transport/TransportUdpTest.php | 15 +-- tests/UdpClientTest.php | 36 +++--- 48 files changed, 754 insertions(+), 824 deletions(-) create mode 100644 .php_cs diff --git a/.gitignore b/.gitignore index ed8581a..67fe1ad 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ .buildpath .project .idea +.php_cs.cache composer.lock vendor/* build diff --git a/.php_cs b/.php_cs new file mode 100644 index 0000000..82a2b41 --- /dev/null +++ b/.php_cs @@ -0,0 +1,21 @@ +exclude('vendor') + ->in(__DIR__); + +return PhpCsFixer\Config::create() + ->setRiskyAllowed(true) + ->setRules([ + '@Symfony' => true, + ]) + ->setFinder($finder); \ No newline at end of file diff --git a/composer.json b/composer.json index 9407a8c..ef3b374 100644 --- a/composer.json +++ b/composer.json @@ -24,11 +24,13 @@ ] }, "scripts": { - "test": "./vendor/bin/phpunit" + "test": "./vendor/bin/phpunit", + "phpcs": "./vendor/bin/php-cs-fixer fix --ansi" }, "require-dev": { "phpunit/phpunit": "^5", - "php-coveralls/php-coveralls": "^1.0", - "phpstan/phpstan": "^0.12.64" + "php-coveralls/php-coveralls": "^v2.4.3", + "phpstan/phpstan": "^0.12.64", + "friendsofphp/php-cs-fixer": "^2.17" } } diff --git a/example/HTTP.php b/example/HTTP.php index 6200d25..0b734a4 100644 --- a/example/HTTP.php +++ b/example/HTTP.php @@ -15,14 +15,13 @@ require_once dirname(dirname(dirname(dirname(__FILE__)))).'/autoload.php'; -use Jaeger\Config; use GuzzleHttp\Client; +use Jaeger\Config; use OpenTracing\Formats; use OpenTracing\Reference; unset($_SERVER['argv']); - //init server span start $config = Config::getInstance(); $config->gen128bit(); @@ -34,7 +33,7 @@ $injectTarget = []; $spanContext = $tracer->extract(Formats\TEXT_MAP, $_SERVER); $serverSpan = $tracer->startSpan('example HTTP', ['child_of' => $spanContext]); -$serverSpan->addBaggageItem("version", "1.8.9"); +$serverSpan->addBaggageItem('version', '1.8.9'); print_r($serverSpan->getContext()); $tracer->inject($serverSpan->getContext(), Formats\TEXT_MAP, $_SERVER); @@ -50,13 +49,13 @@ $method = 'GET'; $url = 'https://github.com/'; $client = new Client(); -$res = $client->request($method, $url,['headers' => $injectTarget1]); +$res = $client->request($method, $url, ['headers' => $injectTarget1]); $clientSpan1->setTag('http.status_code', 200); $clientSpan1->setTag('http.method', 'GET'); $clientSpan1->setTag('http.url', $url); -$clientSpan1->log(['message' => "HTTP1 ". $method .' '. $url .' end !']); +$clientSpan1->log(['message' => 'HTTP1 '.$method.' '.$url.' end !']); $clientSpan1->finish(); //client span1 end @@ -66,7 +65,7 @@ $clientSpan2 = $clientTracer->startSpan('HTTP2', ['references' => [ Reference::createForSpan(Reference::FOLLOWS_FROM, $clientSpan1->spanContext), - Reference::createForSpan(Reference::CHILD_OF, $spanContext) + Reference::createForSpan(Reference::CHILD_OF, $spanContext), ]]); $clientTracer->inject($clientSpan2->spanContext, Formats\TEXT_MAP, $injectTarget2); @@ -80,7 +79,7 @@ $clientSpan2->setTag('http.method', 'GET'); $clientSpan2->setTag('http.url', $url); -$clientSpan2->log(['message' => "HTTP2 ". $method .' '. $url .' end !']); +$clientSpan2->log(['message' => 'HTTP2 '.$method.' '.$url.' end !']); $clientSpan2->finish(); //client span2 end diff --git a/example/HTTP2.php b/example/HTTP2.php index c217498..5c7051f 100644 --- a/example/HTTP2.php +++ b/example/HTTP2.php @@ -3,13 +3,9 @@ require_once dirname(dirname(dirname(dirname(__FILE__)))).'/autoload.php'; use Jaeger\Config; -use GuzzleHttp\Client; -use OpenTracing\Formats; -use OpenTracing\Reference; unset($_SERVER['argv']); - //init server span start $config = Config::getInstance(); @@ -20,22 +16,21 @@ $third = $tracer->startActiveSpan('level third'); $num = 0; -for ($i = 0; $i < 10; $i++){ - $num += 1; +for ($i = 0; $i < 10; ++$i) { + ++$num; } -$third->getSpan()->setTag("num", $num); +$third->getSpan()->setTag('num', $num); sleep(1); $third->close(); $num = 0; -for ($i = 0; $i < 10; $i++){ +for ($i = 0; $i < 10; ++$i) { $num += 2; } -$third->getSpan()->setTag("num", $num); +$third->getSpan()->setTag('num', $num); sleep(1); $second->close(); - $top->close(); //trace flush diff --git a/example/Hprose.php b/example/Hprose.php index e1112af..0babc3a 100644 --- a/example/Hprose.php +++ b/example/Hprose.php @@ -19,7 +19,6 @@ use Jaeger\Config; use OpenTracing\Formats; - unset($_SERVER['argv']); //init server span start @@ -37,33 +36,30 @@ $header = []; $spanContext = $clientTracer->extract(Formats\TEXT_MAP, $_SERVER); $clientSpan = $clientTracer->startSpan('get', ['child_of' => $spanContext]); -$clientSpan->addBaggageItem("version", "2.0.0"); +$clientSpan->addBaggageItem('version', '2.0.0'); $clientTracer->inject($clientSpan->spanContext, Formats\TEXT_MAP, $header); $url = 'http://0.0.0.0:8080/main'; $client = Client::create($url, false); -if($header){ - foreach($header as $key => $val){ +if ($header) { + foreach ($header as $key => $val) { $client->setHeader($key, $val); } } $clientSpan->setTag('http.url', $url); -$clientSpan->setTag('http.method' , 'POST'); +$clientSpan->setTag('http.method', 'POST'); -$result = $client->get("Hprose"); +$result = $client->get('Hprose'); $clientSpan->log(['http.result' => $result]); $clientSpan->finish(); //client span end - //server span end $serverSpan->finish(); //trace flush $config->flush(); echo "success\r\n"; - - diff --git a/example/Istio1.php b/example/Istio1.php index e30196d..aad6ec5 100644 --- a/example/Istio1.php +++ b/example/Istio1.php @@ -15,11 +15,11 @@ require_once dirname(dirname(dirname(dirname(__FILE__)))).'/autoload.php'; +use GuzzleHttp\Client; use Jaeger\Config; use OpenTracing\Formats; -use GuzzleHttp\Client; -$http = new swoole_http_server("0.0.0.0", 8000); +$http = new swoole_http_server('0.0.0.0', 8000); $http->on('request', function ($request, $response) { unset($_SERVER['argv']); $config = Config::getInstance(); @@ -40,9 +40,9 @@ $clientTracer->inject($clientSpan->spanContext, Formats\TEXT_MAP, $injectTarget); $client = new Client(); - $clientSpan->setTag("http.url", "Istio2:8001"); - $res = $client->request('GET', 'Istio2:8001' ,['headers' => $injectTarget]); - $clientSpan->setTag("http.status_code", $res->getStatusCode()); + $clientSpan->setTag('http.url', 'Istio2:8001'); + $res = $client->request('GET', 'Istio2:8001', ['headers' => $injectTarget]); + $clientSpan->setTag('http.status_code', $res->getStatusCode()); //client span1 end //server span end @@ -50,6 +50,6 @@ //trace flush $config->flush(); - $response->end("Hello Istio1"); + $response->end('Hello Istio1'); }); $http->start(); diff --git a/example/Istio2.php b/example/Istio2.php index 3e6a6a6..40c8276 100644 --- a/example/Istio2.php +++ b/example/Istio2.php @@ -18,7 +18,7 @@ use Jaeger\Config; use OpenTracing\Formats; -$http = new swoole_http_server("0.0.0.0", 8001); +$http = new swoole_http_server('0.0.0.0', 8001); $http->on('request', function ($request, $response) { unset($_SERVER['argv']); $config = Config::getInstance(); @@ -38,9 +38,9 @@ $clientTracer->inject($clientSpan->spanContext, Formats\TEXT_MAP, $injectTarget); $client = new \GuzzleHttp\Client(); - $clientSpan->setTag("http.url", "Istio3:8002"); + $clientSpan->setTag('http.url', 'Istio3:8002'); $res = $client->request('GET', 'Istio3:8002', ['headers' => $injectTarget]); - $clientSpan->setTag("http.status_code", $res->getStatusCode()); + $clientSpan->setTag('http.status_code', $res->getStatusCode()); //client span1 end //server span end @@ -48,6 +48,6 @@ //trace flush $config->flush(); - $response->end("Hello Istio2"); + $response->end('Hello Istio2'); }); $http->start(); diff --git a/example/Istio3.php b/example/Istio3.php index 9fdd6b1..81a41ae 100644 --- a/example/Istio3.php +++ b/example/Istio3.php @@ -18,7 +18,7 @@ use Jaeger\Config; use OpenTracing\Formats; -$http = new swoole_http_server("0.0.0.0", 8002); +$http = new swoole_http_server('0.0.0.0', 8002); $http->on('request', function ($request, $response) { unset($_SERVER['argv']); $config = Config::getInstance(); @@ -38,7 +38,7 @@ $clientSpan = $clientTracer->startSpan('Istio3', ['child_of' => $spanContext]); $sum = 0; - for($i = 0; $i < 10; $i++){ + for ($i = 0; $i < 10; ++$i) { $sum += $i; } $clientSpan->log(['message' => 'result:'.$sum]); @@ -51,6 +51,6 @@ //trace flush $config->flush(); - $response->end("Hello Istio3"); + $response->end('Hello Istio3'); }); $http->start(); diff --git a/src/Jaeger/Config.php b/src/Jaeger/Config.php index 0d984b2..7ad6898 100644 --- a/src/Jaeger/Config.php +++ b/src/Jaeger/Config.php @@ -15,18 +15,18 @@ namespace Jaeger; +use Jaeger\Propagator\JaegerPropagator; +use Jaeger\Propagator\ZipkinPropagator; use Jaeger\Reporter\RemoteReporter; use Jaeger\Reporter\Reporter; +use Jaeger\Sampler\ConstSampler; +use Jaeger\Sampler\Sampler; use Jaeger\Transport\TransportUdp; use OpenTracing\NoopTracer; -use Jaeger\Sampler\Sampler; -use Jaeger\Sampler\ConstSampler; -use Jaeger\Propagator\JaegerPropagator; -use Jaeger\Propagator\ZipkinPropagator; use OpenTracing\Tracer; -class Config { - +class Config +{ private $transport = null; private $reporter = null; @@ -47,123 +47,116 @@ class Config { public static $propagator = \Jaeger\Constants\PROPAGATOR_JAEGER; - - private function __construct(){ - + private function __construct() + { } - - private function __clone(){ - + private function __clone() + { } - public static function getInstance() { - if(! (self::$instance instanceof self) ) - { + if (!(self::$instance instanceof self)) { self::$instance = new self(); } + return self::$instance; } - /** - * init jaeger, return can use flush buffers - * @param string $serverName + * init jaeger, return can use flush buffers. + * * @param string $agentHostPort - * @return Tracer + * * @throws \RuntimeException */ - public function initTracer(string $serverName, $agentHostPort = ''): Tracer{ - - if(self::$disabled){ + public function initTracer(string $serverName, $agentHostPort = ''): Tracer + { + if (self::$disabled) { return new NoopTracer(); } - if($serverName == ''){ - throw new \RuntimeException("serverName require"); + if ('' == $serverName) { + throw new \RuntimeException('serverName require'); } - if(isset(self::$tracer[$serverName]) && !empty(self::$tracer[$serverName])){ + if (isset(self::$tracer[$serverName]) && !empty(self::$tracer[$serverName])) { return self::$tracer[$serverName]; } - - if($this->transport == null){ + if (null == $this->transport) { $this->transport = new TransportUdp($agentHostPort); } - if($this->reporter == null) { + if (null == $this->reporter) { $this->reporter = new RemoteReporter($this->transport); } - if($this->sampler == null){ + if (null == $this->sampler) { $this->sampler = new ConstSampler(true); } - if($this->scopeManager == null){ + if (null == $this->scopeManager) { $this->scopeManager = new ScopeManager(); } $tracer = new Jaeger($serverName, $this->reporter, $this->sampler, $this->scopeManager); - if($this->gen128bit == true){ + if (true == $this->gen128bit) { $tracer->gen128bit(); } - if(self::$propagator == \Jaeger\Constants\PROPAGATOR_ZIPKIN){ + if (\Jaeger\Constants\PROPAGATOR_ZIPKIN == self::$propagator) { $tracer->setPropagator(new ZipkinPropagator()); - }else{ + } else { $tracer->setPropagator(new JaegerPropagator()); } - self::$tracer[$serverName] = $tracer; - return $tracer; } - - public function setDisabled(bool $disabled){ + public function setDisabled(bool $disabled) + { self::$disabled = $disabled; return $this; } - - public function setTransport(Transport\Transport $transport){ + public function setTransport(Transport\Transport $transport) + { $this->transport = $transport; return $this; } - - public function setReporter(Reporter $reporter){ + public function setReporter(Reporter $reporter) + { $this->reporter = $reporter; return $this; } - - public function setSampler(Sampler $sampler){ + public function setSampler(Sampler $sampler) + { $this->sampler = $sampler; return $this; } - - public function gen128bit(){ + public function gen128bit() + { $this->gen128bit = true; return $this; } - - public function flush(){ - if(count(self::$tracer) > 0) { - foreach(self::$tracer as $tracer){ + public function flush() + { + if (count(self::$tracer) > 0) { + foreach (self::$tracer as $tracer) { $tracer->reportSpan(); } $this->reporter->close(); diff --git a/src/Jaeger/Constants.php b/src/Jaeger/Constants.php index d83b4b8..4960b4e 100644 --- a/src/Jaeger/Constants.php +++ b/src/Jaeger/Constants.php @@ -21,7 +21,7 @@ const Trace_Baggage_Header_Prefix = 'uberctx-'; -const Jaeger_Debug_Header = "jaeger-debug-id"; +const Jaeger_Debug_Header = 'jaeger-debug-id'; const EMIT_BATCH_OVER_HEAD = 30; @@ -43,28 +43,28 @@ const X_B3_SAMPLED = 'x-b3-sampled'; -const CLIENT_SEND = "cs"; +const CLIENT_SEND = 'cs'; -const CLIENT_RECV = "cr"; +const CLIENT_RECV = 'cr'; -const SERVER_SEND = "ss"; +const SERVER_SEND = 'ss'; -const SERVER_RECV = "sr"; +const SERVER_RECV = 'sr'; -const WIRE_SEND = "ws"; +const WIRE_SEND = 'ws'; -const WIRE_RECV = "wr"; +const WIRE_RECV = 'wr'; -const CLIENT_SEND_FRAGMENT = "csf"; +const CLIENT_SEND_FRAGMENT = 'csf'; -const CLIENT_RECV_FRAGMENT = "crf"; +const CLIENT_RECV_FRAGMENT = 'crf'; -const SERVER_SEND_FRAGMENT = "ssf"; +const SERVER_SEND_FRAGMENT = 'ssf'; -const SERVER_RECV_FRAGMENT = "srf"; +const SERVER_RECV_FRAGMENT = 'srf'; -const LOCAL_COMPONENT = "lc"; +const LOCAL_COMPONENT = 'lc'; -const CLIENT_ADDR = "ca"; +const CLIENT_ADDR = 'ca'; -const SERVER_ADDR = "sa"; \ No newline at end of file +const SERVER_ADDR = 'sa'; diff --git a/src/Jaeger/Jaeger.php b/src/Jaeger/Jaeger.php index 3082dcf..882117d 100644 --- a/src/Jaeger/Jaeger.php +++ b/src/Jaeger/Jaeger.php @@ -15,20 +15,19 @@ namespace Jaeger; +use Jaeger\Propagator\Propagator; +use Jaeger\Reporter\Reporter; use Jaeger\Sampler\Sampler; -use OpenTracing\Exceptions\UnsupportedFormat; +use OpenTracing\Formats; +use OpenTracing\Reference; use OpenTracing\ScopeManager; use OpenTracing\SpanContext; -use OpenTracing\Formats; -use OpenTracing\Tracer; -use Jaeger\Reporter\Reporter; use OpenTracing\StartSpanOptions; -use OpenTracing\Reference; -use Jaeger\Propagator\Propagator; +use OpenTracing\Tracer; use OpenTracing\UnsupportedFormatException; -class Jaeger implements Tracer{ - +class Jaeger implements Tracer +{ private $reporter = null; private $sampler = null; @@ -51,8 +50,8 @@ class Jaeger implements Tracer{ public $propagator = null; public function __construct($serverName = '', Reporter $reporter, Sampler $sampler, - ScopeManager $scopeManager){ - + ScopeManager $scopeManager) + { $this->reporter = $reporter; $this->sampler = $sampler; @@ -62,145 +61,143 @@ public function __construct($serverName = '', Reporter $reporter, Sampler $sampl $this->setTags($this->sampler->getTags()); $this->setTags($this->getEnvTags()); - if($serverName == '') { + if ('' == $serverName) { $this->serverName = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'unknow server'; - }else{ + } else { $this->serverName = $serverName; } } - /** - * @param array $tags key => value + * @param array $tags key => value */ - public function setTags(array $tags = []){ - if(!empty($tags)) { + public function setTags(array $tags = []) + { + if (!empty($tags)) { $this->tags = array_merge($this->tags, $tags); } } - /** - * @inheritDoc + * {@inheritDoc} */ - public function startSpan(string $operationName, $options = []): \OpenTracing\Span { - + public function startSpan(string $operationName, $options = []): \OpenTracing\Span + { if (!($options instanceof StartSpanOptions)) { $options = StartSpanOptions::create($options); } $parentSpan = $this->getParentSpanContext($options); - if($parentSpan == null || !$parentSpan->traceIdLow){ + if (null == $parentSpan || !$parentSpan->traceIdLow) { $low = $this->generateId(); $spanId = $low; $flags = $this->sampler->IsSampled(); $spanContext = new \Jaeger\SpanContext($spanId, 0, $flags, null, 0); $spanContext->traceIdLow = $low; - if($this->gen128bit == true){ + if (true == $this->gen128bit) { $spanContext->traceIdHigh = $this->generateId(); } - }else{ + } else { $spanContext = new \Jaeger\SpanContext($this->generateId(), $parentSpan->spanId, $parentSpan->flags, $parentSpan->baggage, 0); $spanContext->traceIdLow = $parentSpan->traceIdLow; - if($parentSpan->traceIdHigh){ + if ($parentSpan->traceIdHigh) { $spanContext->traceIdHigh = $parentSpan->traceIdHigh; } } $startTime = $options->getStartTime() ? intval($options->getStartTime() * 1000000) : null; $span = new Span($operationName, $spanContext, $options->getReferences(), $startTime); - if(!empty($options->getTags())) { + if (!empty($options->getTags())) { foreach ($options->getTags() as $k => $tag) { $span->setTag($k, $tag); } } - if($spanContext->isSampled() == 1) { + if (1 == $spanContext->isSampled()) { $this->spans[] = $span; } return $span; } - - public function setPropagator(Propagator $propagator){ + public function setPropagator(Propagator $propagator) + { $this->propagator = $propagator; } - /** - * 注入 - * @param SpanContext $spanContext - * @param string $format + * 注入. + * * @param mixed $carrier */ - public function inject(SpanContext $spanContext, string $format, &$carrier): void { - if($format == Formats\TEXT_MAP){ + public function inject(SpanContext $spanContext, string $format, &$carrier): void + { + if (Formats\TEXT_MAP == $format) { $this->propagator->inject($spanContext, $format, $carrier); - }else{ + } else { throw UnsupportedFormatException::forFormat($format); } } - /** - * 提取 - * @param string $format + * 提取. + * * @param mixed $carrier */ - public function extract(string $format, $carrier): ?SpanContext{ - if($format == Formats\TEXT_MAP){ + public function extract(string $format, $carrier): ?SpanContext + { + if (Formats\TEXT_MAP == $format) { return $this->propagator->extract($format, $carrier); - }else{ + } else { throw UnsupportedFormatException::forFormat($format); } } - - public function getSpans(){ + public function getSpans() + { return $this->spans; } - - public function reportSpan(){ - if($this->spans) { + public function reportSpan() + { + if ($this->spans) { $this->reporter->report($this); $this->spans = []; } } - - public function getScopeManager(): ScopeManager{ + public function getScopeManager(): ScopeManager + { return $this->scopeManager; } - - public function getActiveSpan(): ?\OpenTracing\Span { + public function getActiveSpan(): ?\OpenTracing\Span + { $activeScope = $this->getScopeManager()->getActive(); - if ($activeScope === null) { + if (null === $activeScope) { return null; } return $activeScope->getSpan(); } - - public function startActiveSpan(string $operationName, $options = []): \OpenTracing\Scope { + public function startActiveSpan(string $operationName, $options = []): \OpenTracing\Scope + { if (!$options instanceof StartSpanOptions) { $options = StartSpanOptions::create($options); } $parentSpan = $this->getParentSpanContext($options); - if ($parentSpan === null && $this->getActiveSpan() !== null) { + if (null === $parentSpan && null !== $this->getActiveSpan()) { $parentContext = $this->getActiveSpan()->getContext(); $options = $options->withParent($parentContext); } $span = $this->startSpan($operationName, $options); + return $this->getScopeManager()->activate($span, $options->shouldFinishSpanOnClose()); } - private function getParentSpanContext(StartSpanOptions $options) { $references = $options->getReferences(); @@ -228,12 +225,12 @@ private function getParentSpanContext(StartSpanOptions $options) return null; } - - public function getEnvTags(){ + public function getEnvTags() + { $tags = []; - if(isset($_SERVER['JAEGER_TAGS']) && $_SERVER['JAEGER_TAGS'] != ''){ + if (isset($_SERVER['JAEGER_TAGS']) && '' != $_SERVER['JAEGER_TAGS']) { $envTags = explode(',', $_SERVER['JAEGER_TAGS']); - foreach ($envTags as $envK => $envTag){ + foreach ($envTags as $envK => $envTag) { list($key, $value) = explode('=', $envTag); $tags[$key] = $value; } @@ -242,22 +239,22 @@ public function getEnvTags(){ return $tags; } - - public function gen128bit(){ + public function gen128bit() + { $this->gen128bit = true; } - /** - * 结束,发送信息到jaeger + * 结束,发送信息到jaeger. */ - public function flush(): void{ + public function flush(): void + { $this->reportSpan(); $this->reporter->close(); } - - private function generateId(){ - return microtime(true) * 10000 . rand(10000, 99999); + private function generateId() + { + return microtime(true) * 10000 .rand(10000, 99999); } } diff --git a/src/Jaeger/Propagator/JaegerPropagator.php b/src/Jaeger/Propagator/JaegerPropagator.php index 763435f..e3a3481 100644 --- a/src/Jaeger/Propagator/JaegerPropagator.php +++ b/src/Jaeger/Propagator/JaegerPropagator.php @@ -15,59 +15,58 @@ namespace Jaeger\Propagator; -use Jaeger\SpanContext; use Jaeger\Constants; +use Jaeger\SpanContext; -class JaegerPropagator implements Propagator{ - - public function inject(SpanContext $spanContext, $format, &$carrier){ +class JaegerPropagator implements Propagator +{ + public function inject(SpanContext $spanContext, $format, &$carrier) + { $carrier[strtoupper(Constants\Tracer_State_Header_Name)] = $spanContext->buildString(); - if($spanContext->baggage) { + if ($spanContext->baggage) { foreach ($spanContext->baggage as $k => $v) { - $carrier[strtoupper(Constants\Trace_Baggage_Header_Prefix . $k)] = $v; + $carrier[strtoupper(Constants\Trace_Baggage_Header_Prefix.$k)] = $v; } } } - - public function extract($format, $carrier){ + public function extract($format, $carrier) + { $spanContext = null; - + $carrier = array_change_key_case($carrier, CASE_LOWER); - foreach ($carrier as $k => $v){ - - if(!in_array($k, [Constants\Tracer_State_Header_Name, - Constants\Jaeger_Debug_Header, Constants\Jaeger_Baggage_Header]) && - stripos($k, Constants\Trace_Baggage_Header_Prefix) === false){ + foreach ($carrier as $k => $v) { + if (!in_array($k, [Constants\Tracer_State_Header_Name, + Constants\Jaeger_Debug_Header, Constants\Jaeger_Baggage_Header, ]) && + false === stripos($k, Constants\Trace_Baggage_Header_Prefix)) { continue; } - if($spanContext === null){ + if (null === $spanContext) { $spanContext = new SpanContext(0, 0, 0, null, 0); } - - if(is_array($v)){ + + if (is_array($v)) { $v = urldecode(current($v)); - }else { + } else { $v = urldecode($v); } - if($k == Constants\Tracer_State_Header_Name){ - list($traceId, $spanId, $parentId,$flags) = explode(':', $v); + if (Constants\Tracer_State_Header_Name == $k) { + list($traceId, $spanId, $parentId, $flags) = explode(':', $v); $spanContext->spanId = $spanContext->hexToSignedInt($spanId); $spanContext->parentId = $spanContext->hexToSignedInt($parentId); $spanContext->flags = $flags; $spanContext->traceIdToString($traceId); - - }elseif(stripos($k, Constants\Trace_Baggage_Header_Prefix) !== false){ - $safeKey = str_replace(Constants\Trace_Baggage_Header_Prefix, "", $k); - if($safeKey != "") { + } elseif (false !== stripos($k, Constants\Trace_Baggage_Header_Prefix)) { + $safeKey = str_replace(Constants\Trace_Baggage_Header_Prefix, '', $k); + if ('' != $safeKey) { $spanContext->withBaggageItem($safeKey, $v); } - }elseif($k == Constants\Jaeger_Debug_Header){ + } elseif (Constants\Jaeger_Debug_Header == $k) { $spanContext->debugId = $v; - }elseif($k == Constants\Jaeger_Baggage_Header){ + } elseif (Constants\Jaeger_Baggage_Header == $k) { // Converts a comma separated key value pair list into a map // e.g. key1=value1, key2=value2, key3 = value3 // is converted to array { "key1" : "value1", @@ -75,19 +74,16 @@ public function extract($format, $carrier){ // "key3" : "value3" } $parseVal = explode(',', $v); foreach ($parseVal as $val) { - if(stripos($v, '=') !== false) { + if (false !== stripos($v, '=')) { $kv = explode('=', trim($val)); - if (count($kv) == 2) { + if (2 == count($kv)) { $spanContext->withBaggageItem($kv[0], $kv[1]); } } } - } } - return $spanContext; } - } diff --git a/src/Jaeger/Propagator/Propagator.php b/src/Jaeger/Propagator/Propagator.php index d0aeb13..34c1549 100644 --- a/src/Jaeger/Propagator/Propagator.php +++ b/src/Jaeger/Propagator/Propagator.php @@ -17,23 +17,23 @@ use Jaeger\SpanContext; -interface Propagator{ - +interface Propagator +{ /** - * 注入 - * @param SpanContext $spanContext + * 注入. + * * @param string $format - * @param mixed $carrier + * @param mixed $carrier */ public function inject(SpanContext $spanContext, $format, &$carrier); - /** - * 提取 + * 提取. + * * @param string $format - * @param mixed $carrier + * @param mixed $carrier + * * @return SpanContext|null */ public function extract($format, $carrier); - } diff --git a/src/Jaeger/Propagator/ZipkinPropagator.php b/src/Jaeger/Propagator/ZipkinPropagator.php index 7dfcb41..c1eae1d 100644 --- a/src/Jaeger/Propagator/ZipkinPropagator.php +++ b/src/Jaeger/Propagator/ZipkinPropagator.php @@ -15,50 +15,50 @@ namespace Jaeger\Propagator; -use Jaeger\SpanContext; use Jaeger\Constants; +use Jaeger\SpanContext; -class ZipkinPropagator implements Propagator{ - - public function inject(SpanContext $spanContext, $format, &$carrier){ - $carrier[Constants\X_B3_TRACEID] = $spanContext ->traceIdLowToString(); +class ZipkinPropagator implements Propagator +{ + public function inject(SpanContext $spanContext, $format, &$carrier) + { + $carrier[Constants\X_B3_TRACEID] = $spanContext->traceIdLowToString(); $carrier[Constants\X_B3_PARENT_SPANID] = $spanContext->parentIdToString(); $carrier[Constants\X_B3_SPANID] = $spanContext->spanIdToString(); $carrier[Constants\X_B3_SAMPLED] = $spanContext->flagsToString(); } - - public function extract($format, $carrier){ + public function extract($format, $carrier) + { $spanContext = null; foreach ($carrier as $k => $val) { if (in_array($k, [Constants\X_B3_TRACEID, - Constants\X_B3_PARENT_SPANID, Constants\X_B3_SPANID, Constants\X_B3_SAMPLED]) + Constants\X_B3_PARENT_SPANID, Constants\X_B3_SPANID, Constants\X_B3_SAMPLED, ]) ) { - if($spanContext === null){ + if (null === $spanContext) { $spanContext = new SpanContext(0, 0, 0, null, 0); } continue; } } - - if(isset($carrier[Constants\X_B3_TRACEID]) && $carrier[Constants\X_B3_TRACEID]){ + + if (isset($carrier[Constants\X_B3_TRACEID]) && $carrier[Constants\X_B3_TRACEID]) { $spanContext->traceIdToString($carrier[Constants\X_B3_TRACEID]); } - if(isset($carrier[Constants\X_B3_PARENT_SPANID]) && $carrier[Constants\X_B3_PARENT_SPANID]){ + if (isset($carrier[Constants\X_B3_PARENT_SPANID]) && $carrier[Constants\X_B3_PARENT_SPANID]) { $spanContext->parentId = $spanContext->hexToSignedInt($carrier[Constants\X_B3_PARENT_SPANID]); } - if(isset($carrier[Constants\X_B3_SPANID]) && $carrier[Constants\X_B3_SPANID]){ + if (isset($carrier[Constants\X_B3_SPANID]) && $carrier[Constants\X_B3_SPANID]) { $spanContext->spanId = $spanContext->hexToSignedInt($carrier[Constants\X_B3_SPANID]); } - if(isset($carrier[Constants\X_B3_SAMPLED]) && $carrier[Constants\X_B3_SAMPLED]){ + if (isset($carrier[Constants\X_B3_SAMPLED]) && $carrier[Constants\X_B3_SAMPLED]) { $spanContext->flags = $carrier[Constants\X_B3_SAMPLED]; } - return $spanContext; } } diff --git a/src/Jaeger/Reporter/RemoteReporter.php b/src/Jaeger/Reporter/RemoteReporter.php index 8cfadd2..4a495dc 100644 --- a/src/Jaeger/Reporter/RemoteReporter.php +++ b/src/Jaeger/Reporter/RemoteReporter.php @@ -18,8 +18,8 @@ use Jaeger\Jaeger; use Jaeger\Transport\Transport; -class RemoteReporter implements Reporter{ - +class RemoteReporter implements Reporter +{ public $tran = null; public function __construct(Transport $tran) @@ -32,9 +32,8 @@ public function report(Jaeger $jaeger) $this->tran->append($jaeger); } - public function close() { $this->tran->flush(); } -} \ No newline at end of file +} diff --git a/src/Jaeger/Reporter/Reporter.php b/src/Jaeger/Reporter/Reporter.php index a480b40..4e7f6bd 100644 --- a/src/Jaeger/Reporter/Reporter.php +++ b/src/Jaeger/Reporter/Reporter.php @@ -17,8 +17,8 @@ use Jaeger\Jaeger; -interface Reporter{ - +interface Reporter +{ public function report(Jaeger $jaeger); public function close(); diff --git a/src/Jaeger/Sampler/ConstSampler.php b/src/Jaeger/Sampler/ConstSampler.php index 515c56a..6f0622c 100644 --- a/src/Jaeger/Sampler/ConstSampler.php +++ b/src/Jaeger/Sampler/ConstSampler.php @@ -15,32 +15,33 @@ namespace Jaeger\Sampler; - use Jaeger\Constants; -class ConstSampler implements Sampler{ - +class ConstSampler implements Sampler +{ private $decision = ''; private $tags = []; - public function __construct($decision = true){ + public function __construct($decision = true) + { $this->decision = $decision; $this->tags[Constants\SAMPLER_TYPE_TAG_KEY] = 'const'; $this->tags[Constants\SAMPLER_PARAM_TAG_KEY] = $decision; } - public function IsSampled(){ + public function IsSampled() + { return $this->decision; } - - public function Close(){ + public function Close() + { //nothing to do } - - public function getTags(){ + public function getTags() + { return $this->tags; } -} \ No newline at end of file +} diff --git a/src/Jaeger/Sampler/ProbabilisticSampler.php b/src/Jaeger/Sampler/ProbabilisticSampler.php index 112ece6..420cb0d 100644 --- a/src/Jaeger/Sampler/ProbabilisticSampler.php +++ b/src/Jaeger/Sampler/ProbabilisticSampler.php @@ -12,40 +12,41 @@ * or implied. See the License for the specific language governing permissions and limitations under * the License. */ + namespace Jaeger\Sampler; use Jaeger\Constants; -class ProbabilisticSampler implements Sampler{ - +class ProbabilisticSampler implements Sampler +{ // min 0, max 1 private $rate = 0; private $tags = []; - - public function __construct($rate = 0.0001){ + public function __construct($rate = 0.0001) + { $this->rate = $rate; $this->tags[Constants\SAMPLER_TYPE_TAG_KEY] = 'probabilistic'; $this->tags[Constants\SAMPLER_PARAM_TAG_KEY] = $rate; } - - public function IsSampled(){ - if(mt_rand(1, 1 / $this->rate) == 1){ + public function IsSampled() + { + if (1 == mt_rand(1, 1 / $this->rate)) { return true; - }else{ + } else { return false; } } - - public function Close(){ + public function Close() + { //nothing to do } - - public function getTags(){ + public function getTags() + { return $this->tags; } } diff --git a/src/Jaeger/Sampler/Sampler.php b/src/Jaeger/Sampler/Sampler.php index 1a5c3ed..1dd85a8 100644 --- a/src/Jaeger/Sampler/Sampler.php +++ b/src/Jaeger/Sampler/Sampler.php @@ -15,7 +15,6 @@ namespace Jaeger\Sampler; - interface Sampler { public function IsSampled(); @@ -23,4 +22,4 @@ public function IsSampled(); public function Close(); public function getTags(); -} \ No newline at end of file +} diff --git a/src/Jaeger/Scope.php b/src/Jaeger/Scope.php index 89c74bd..b0afad7 100644 --- a/src/Jaeger/Scope.php +++ b/src/Jaeger/Scope.php @@ -2,10 +2,8 @@ namespace Jaeger; - - -class Scope implements \OpenTracing\Scope{ - +class Scope implements \OpenTracing\Scope +{ /** * @var ScopeManager */ @@ -21,21 +19,20 @@ class Scope implements \OpenTracing\Scope{ */ private $finishSpanOnClose; - /** * Scope constructor. - * @param ScopeManager $scopeManager - * @param Span $span + * * @param bool $finishSpanOnClose */ - public function __construct(ScopeManager $scopeManager, Span $span, $finishSpanOnClose){ + public function __construct(ScopeManager $scopeManager, Span $span, $finishSpanOnClose) + { $this->scopeManager = $scopeManager; $this->span = $span; $this->finishSpanOnClose = $finishSpanOnClose; } - - public function close(): void{ + public function close(): void + { if ($this->finishSpanOnClose) { $this->span->finish(); } @@ -43,8 +40,8 @@ public function close(): void{ $this->scopeManager->deactivate($this); } - - public function getSpan(): \OpenTracing\Span { + public function getSpan(): \OpenTracing\Span + { return $this->span; } } diff --git a/src/Jaeger/ScopeManager.php b/src/Jaeger/ScopeManager.php index cf8b353..638124a 100644 --- a/src/Jaeger/ScopeManager.php +++ b/src/Jaeger/ScopeManager.php @@ -2,25 +2,26 @@ namespace Jaeger; - -class ScopeManager implements \OpenTracing\ScopeManager{ - +class ScopeManager implements \OpenTracing\ScopeManager +{ private $scopes = []; /** - * @inheritDoc + * {@inheritDoc} */ - public function activate(\OpenTracing\Span $span, bool $finishSpanOnClose = self::DEFAULT_FINISH_SPAN_ON_CLOSE): \OpenTracing\Scope{ + public function activate(\OpenTracing\Span $span, bool $finishSpanOnClose = self::DEFAULT_FINISH_SPAN_ON_CLOSE): \OpenTracing\Scope + { $scope = new Scope($this, $span, $finishSpanOnClose); $this->scopes[] = $scope; + return $scope; } - /** - * @inheritDoc + * {@inheritDoc} */ - public function getActive(): ?\OpenTracing\Scope{ + public function getActive(): ?\OpenTracing\Scope + { if (empty($this->scopes)) { return null; } @@ -28,15 +29,15 @@ public function getActive(): ?\OpenTracing\Scope{ return $this->scopes[count($this->scopes) - 1]; } - - public function deactivate(Scope $scope){ + public function deactivate(Scope $scope) + { $scopeLength = count($this->scopes); - if($scopeLength <= 0){ + if ($scopeLength <= 0) { return false; } - for ($i = 0; $i < $scopeLength; $i++) { + for ($i = 0; $i < $scopeLength; ++$i) { if ($scope === $this->scopes[$i]) { array_splice($this->scopes, $i, 1); } diff --git a/src/Jaeger/Span.php b/src/Jaeger/Span.php index 0fa2526..4e6030a 100644 --- a/src/Jaeger/Span.php +++ b/src/Jaeger/Span.php @@ -15,11 +15,10 @@ namespace Jaeger; - use OpenTracing\SpanContext; -class Span implements \OpenTracing\Span{ - +class Span implements \OpenTracing\Span +{ private $operationName = ''; public $startTime = ''; @@ -38,55 +37,51 @@ class Span implements \OpenTracing\Span{ public $references = []; - public function __construct($operationName, \OpenTracing\SpanContext $spanContext, $references, $startTime = null){ + public function __construct($operationName, SpanContext $spanContext, $references, $startTime = null) + { $this->operationName = $operationName; - $this->startTime = $startTime == null ? $this->microtimeToInt() : $startTime; + $this->startTime = null == $startTime ? $this->microtimeToInt() : $startTime; $this->spanContext = $spanContext; $this->references = $references; } - /** - * @return string - */ - public function getOperationName(): string { + public function getOperationName(): string + { return $this->operationName; } - /** - * @return SpanContext - */ - public function getContext(): SpanContext{ + public function getContext(): SpanContext + { return $this->spanContext; } /** - * @inheritDoc + * {@inheritDoc} */ - public function finish($finishTime = null): void{ - $this->finishTime = $finishTime == null ? $this->microtimeToInt() : $finishTime; + public function finish($finishTime = null): void + { + $this->finishTime = null == $finishTime ? $this->microtimeToInt() : $finishTime; $this->duration = $this->finishTime - $this->startTime; } - /** - * @param string $newOperationName - */ - public function overwriteOperationName(string $newOperationName): void { + public function overwriteOperationName(string $newOperationName): void + { $this->operationName = $newOperationName; } - - public function setTag(string $key, $value): void{ + public function setTag(string $key, $value): void + { $this->tags[$key] = $value; } - /** - * Adds a log record to the span + * Adds a log record to the span. * - * @param array $fields [key => val] + * @param array $fields [key => val] * @param int|float|\DateTimeInterface $timestamp */ - public function log(array $fields = [], $timestamp = null): void { + public function log(array $fields = [], $timestamp = null): void + { $log['timestamp'] = $timestamp ? $timestamp : $this->microtimeToInt(); $log['fields'] = $fields; $this->logs[] = $log; @@ -95,11 +90,9 @@ public function log(array $fields = [], $timestamp = null): void { /** * Adds a baggage item to the SpanContext which is immutable so it is required to use SpanContext::withBaggageItem * to get a new one. - * - * @param string $key - * @param string $value */ - public function addBaggageItem(string $key, string $value): void{ + public function addBaggageItem(string $key, string $value): void + { $this->log([ 'event' => 'baggage', 'key' => $key, @@ -109,16 +102,13 @@ public function addBaggageItem(string $key, string $value): void{ $this->spanContext->withBaggageItem($key, $value); } - /** - * @param string $key - * @return string|null - */ - public function getBaggageItem(string $key): ?string{ + public function getBaggageItem(string $key): ?string + { return $this->spanContext->getBaggageItem($key); } - - private function microtimeToInt(){ + private function microtimeToInt() + { return intval(microtime(true) * 1000000); } -} \ No newline at end of file +} diff --git a/src/Jaeger/SpanContext.php b/src/Jaeger/SpanContext.php index cf28f61..c5cea21 100644 --- a/src/Jaeger/SpanContext.php +++ b/src/Jaeger/SpanContext.php @@ -15,15 +15,14 @@ namespace Jaeger; - -class SpanContext implements \OpenTracing\SpanContext{ +class SpanContext implements \OpenTracing\SpanContext +{ // traceID represents globally unique ID of the trace. // Usually generated as a random number. public $traceIdLow; public $traceIdHigh; - // spanID represents span ID that must be unique within its trace, // but does not have to be globally unique. public $spanId; @@ -43,8 +42,8 @@ class SpanContext implements \OpenTracing\SpanContext{ // extracted from a TextMap carrier. public $debugId; - - public function __construct($spanId, $parentId, $flags, $baggage = null, $debugId = 0){ + public function __construct($spanId, $parentId, $flags, $baggage = null, $debugId = 0) + { $this->spanId = $spanId; $this->parentId = $parentId; $this->flags = $flags; @@ -52,12 +51,11 @@ public function __construct($spanId, $parentId, $flags, $baggage = null, $debugI $this->debugId = $debugId; } - - public function getBaggageItem(string $key): ?string { + public function getBaggageItem(string $key): ?string + { return isset($this->baggage[$key]) ? $this->baggage[$key] : null; } - public function withBaggageItem(string $key, string $value): ?\OpenTracing\SpanContext { $this->baggage[$key] = $value; @@ -72,60 +70,60 @@ public function getIterator() // TODO: Implement getIterator() method. } - - public function buildString(){ - if($this->traceIdHigh){ - return sprintf("%x%016x:%x:%x:%x", $this->traceIdHigh, $this->traceIdLow, + public function buildString() + { + if ($this->traceIdHigh) { + return sprintf('%x%016x:%x:%x:%x', $this->traceIdHigh, $this->traceIdLow, $this->spanId, $this->parentId, $this->flags); } - return sprintf("%x:%x:%x:%x", $this->traceIdLow, $this->spanId, $this->parentId, $this->flags); + return sprintf('%x:%x:%x:%x', $this->traceIdLow, $this->spanId, $this->parentId, $this->flags); } - - public function spanIdToString(){ - return sprintf("%x", $this->spanId); + public function spanIdToString() + { + return sprintf('%x', $this->spanId); } - - public function parentIdToString(){ - return sprintf("%x", $this->parentId); + public function parentIdToString() + { + return sprintf('%x', $this->parentId); } - - public function traceIdLowToString(){ + public function traceIdLowToString() + { if ($this->traceIdHigh) { - return sprintf("%x%016x", $this->traceIdHigh, $this->traceIdLow); + return sprintf('%x%016x', $this->traceIdHigh, $this->traceIdLow); } - return sprintf("%x", $this->traceIdLow); + return sprintf('%x', $this->traceIdLow); } - - public function flagsToString(){ - return sprintf("%x", $this->flags); + public function flagsToString() + { + return sprintf('%x', $this->flags); } - /** - * 是否取样 + * 是否取样. + * * @return mixed */ - public function isSampled(){ + public function isSampled() + { return $this->flags; } - public function hexToSignedInt($hex) { //Avoid pure Arabic numerals eg:1 - if (gettype($hex) != "string") { + if ('string' != gettype($hex)) { $hex .= ''; } $hexStrLen = strlen($hex); $dec = 0; - for ($i = 0; $i < $hexStrLen; $i++) { + for ($i = 0; $i < $hexStrLen; ++$i) { $hexByteStr = $hex[$i]; if (ctype_xdigit($hexByteStr)) { $decByte = hexdec($hex[$i]); @@ -136,7 +134,6 @@ public function hexToSignedInt($hex) return $dec; } - public function traceIdToString($traceId) { $len = strlen($traceId); @@ -148,7 +145,6 @@ public function traceIdToString($traceId) } } - /** * @return bool */ @@ -157,7 +153,6 @@ public function isValid() return $this->isTraceIdValid() && $this->spanId; } - /** * @return bool */ diff --git a/src/Jaeger/Thrift/AgentClient.php b/src/Jaeger/Thrift/AgentClient.php index 25b0e07..d2c9255 100644 --- a/src/Jaeger/Thrift/AgentClient.php +++ b/src/Jaeger/Thrift/AgentClient.php @@ -15,15 +15,14 @@ namespace Jaeger\Thrift; -use Thrift\Transport\TMemoryBuffer; +use Jaeger\Constants; use Thrift\Protocol\TCompactProtocol; +use Thrift\Transport\TMemoryBuffer; use Thrift\Type\TMessageType; use Thrift\Type\TType; -use Jaeger\Constants; class AgentClient { - public static $tptl = null; public function buildThrift($batch) @@ -45,12 +44,11 @@ public function buildThrift($batch) return ['len' => $batchLen, 'thriftStr' => $batchThriftStr]; } - private function handleBatch($batch) { - self::$tptl->writeFieldBegin("batch", TType::STRUCT, 1); + self::$tptl->writeFieldBegin('batch', TType::STRUCT, 1); - self::$tptl->writeStructBegin("Batch"); + self::$tptl->writeStructBegin('Batch'); $this->handleThriftProcess($batch['thriftProcess']); $this->handleThriftSpans($batch['thriftSpans']); @@ -60,14 +58,13 @@ private function handleBatch($batch) self::$tptl->writeFieldEnd(); } - private function handleThriftSpans($thriftSpans) { - self::$tptl->writeFieldBegin("spans", TType::LST, 2); + self::$tptl->writeFieldBegin('spans', TType::LST, 2); self::$tptl->writeListBegin(TType::STRUCT, count($thriftSpans)); $agentSpan = Span::getInstance(); - foreach ($thriftSpans as $thriftSpan){ + foreach ($thriftSpans as $thriftSpan) { $agentSpan->setThriftSpan($thriftSpan); $agentSpan->write(self::$tptl); } @@ -76,13 +73,10 @@ private function handleThriftSpans($thriftSpans) self::$tptl->writeFieldEnd(); } - private function handleThriftProcess($thriftProcess) { - self::$tptl->writeFieldBegin("process", TType::STRUCT, 1); + self::$tptl->writeFieldBegin('process', TType::STRUCT, 1); (new Process($thriftProcess))->write(self::$tptl); self::$tptl->writeFieldEnd(); } - - } diff --git a/src/Jaeger/Thrift/JaegerThriftSpan.php b/src/Jaeger/Thrift/JaegerThriftSpan.php index 2fdd8d5..293e42d 100644 --- a/src/Jaeger/Thrift/JaegerThriftSpan.php +++ b/src/Jaeger/Thrift/JaegerThriftSpan.php @@ -19,10 +19,10 @@ use Jaeger\Span; use OpenTracing\Reference; -class JaegerThriftSpan{ - - - public function buildJaegerProcessThrift(Jaeger $jaeger){ +class JaegerThriftSpan +{ + public function buildJaegerProcessThrift(Jaeger $jaeger) + { $tags = []; $ip = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : '0.0.0.0'; $tags['peer.ipv4'] = $ip; @@ -40,12 +40,11 @@ public function buildJaegerProcessThrift(Jaeger $jaeger){ 'tags' => $thriftTags, ]; - return $processThrift; } - public function buildJaegerSpanThrift(Span $span){ - + public function buildJaegerSpanThrift(Span $span) + { $spContext = $span->spanContext; $thriftSpan = [ 'traceIdLow' => $spContext->traceIdLow, @@ -58,15 +57,14 @@ public function buildJaegerSpanThrift(Span $span){ 'duration' => $span->duration, 'tags' => $this->buildTags($span->tags), 'logs' => $this->buildLogs($span->logs), - 'references' => $this->buildReferences($span->references) + 'references' => $this->buildReferences($span->references), ]; return $thriftSpan; } - - - private function buildTags($tags){ + private function buildTags($tags) + { $tagsObj = Tags::getInstance(); $tagsObj->setTags($tags); $resultTags = $tagsObj->buildTags(); @@ -74,32 +72,32 @@ private function buildTags($tags){ return $resultTags; } - - private function buildLogs($logs){ + private function buildLogs($logs) + { $resultLogs = []; $tagsObj = Tags::getInstance(); - foreach($logs as $log){ + foreach ($logs as $log) { $tagsObj->setTags($log['fields']); $fields = $tagsObj->buildTags(); $resultLogs[] = [ - "timestamp" => $log['timestamp'], - "fields" => $fields, + 'timestamp' => $log['timestamp'], + 'fields' => $fields, ]; } return $resultLogs; } - - private function buildReferences($references){ + private function buildReferences($references) + { $spanRef = []; - foreach ($references as $ref){ - if($ref->isType(Reference::CHILD_OF)){ + foreach ($references as $ref) { + if ($ref->isType(Reference::CHILD_OF)) { $type = SpanRefType::CHILD_OF; - }else if($ref->isType(Reference::FOLLOWS_FROM)){ + } elseif ($ref->isType(Reference::FOLLOWS_FROM)) { $type = SpanRefType::FOLLOWS_FROM; } else { - throw new \LogicException("Unsupported reference type"); + throw new \LogicException('Unsupported reference type'); } $ctx = $ref->getContext(); $spanRef[] = [ @@ -112,4 +110,4 @@ private function buildReferences($references){ return $spanRef; } -} \ No newline at end of file +} diff --git a/src/Jaeger/Thrift/Process.php b/src/Jaeger/Thrift/Process.php index 4aaa24e..9081fab 100644 --- a/src/Jaeger/Thrift/Process.php +++ b/src/Jaeger/Thrift/Process.php @@ -18,8 +18,8 @@ use Thrift\Protocol\TProtocol; use Thrift\Type\TType; -class Process implements TStruct{ - +class Process implements TStruct +{ public static $tptl = null; public static $serverName = ''; @@ -35,42 +35,37 @@ public function __construct($processThrift) self::$wrote = isset($processThrift['wrote']) ? $processThrift['wrote'] : ''; } - - public function write(TProtocol $t){ + public function write(TProtocol $t) + { self::$tptl = $t; - if(self::$wrote){ + if (self::$wrote) { $tran = self::$tptl->getTransport(); $tran->write(self::$wrote); } else { - - self::$tptl->writeStructBegin("Process"); + self::$tptl->writeStructBegin('Process'); $this->handleProcessSName(); $this->handleProcessTags(); - + self::$tptl->writeFieldStop(); self::$tptl->writeStructEnd(); } - - } - private function handleProcessSName() { - self::$tptl->writeFieldBegin("serverName", TType::STRING, 1); + self::$tptl->writeFieldBegin('serverName', TType::STRING, 1); self::$tptl->writeString(self::$serverName); self::$tptl->writeFieldEnd(); } - private function handleProcessTags() { - if(count(self::$thriftTags) > 0) { - self::$tptl->writeFieldBegin("tags", TType::LST, 2); + if (count(self::$thriftTags) > 0) { + self::$tptl->writeFieldBegin('tags', TType::LST, 2); self::$tptl->writeListBegin(TType::STRUCT, count(self::$thriftTags)); $tagsObj = Tags::getInstance(); @@ -82,6 +77,7 @@ private function handleProcessTags() } } - - public function read(TProtocol $t){} -} \ No newline at end of file + public function read(TProtocol $t) + { + } +} diff --git a/src/Jaeger/Thrift/Span.php b/src/Jaeger/Thrift/Span.php index 93f00ec..4695ced 100644 --- a/src/Jaeger/Thrift/Span.php +++ b/src/Jaeger/Thrift/Span.php @@ -18,8 +18,8 @@ use Thrift\Protocol\TProtocol; use Thrift\Type\TType; -class Span implements TStruct{ - +class Span implements TStruct +{ public static $thriftSpan = null; public static $tptl = null; @@ -30,35 +30,33 @@ private function __construct() { } - private function __clone() { } - - public static function getInstance(){ - if(! (self::$instance instanceof self) ) - { + public static function getInstance() + { + if (!(self::$instance instanceof self)) { self::$instance = new self(); } + return self::$instance; } - - public function setThriftSpan($thriftSpan = []){ + public function setThriftSpan($thriftSpan = []) + { self::$thriftSpan = $thriftSpan; } - - public function getThriftSpan(){ + public function getThriftSpan() + { return self::$thriftSpan; } - public function write(TProtocol $t) { self::$tptl = $t; - if(isset(self::$thriftSpan['wrote']) && self::$thriftSpan['wrote']){ + if (isset(self::$thriftSpan['wrote']) && self::$thriftSpan['wrote']) { $tran = self::$tptl->getTransport(); $tran->write(self::$thriftSpan['wrote']); } else { @@ -66,16 +64,14 @@ public function write(TProtocol $t) } } - public function read(TProtocol $t) { // TODO: Implement read() method. } - private function handleSpan($span) { - self::$tptl->writeStructBegin("Span"); + self::$tptl->writeStructBegin('Span'); self::$tptl->writeFieldBegin('traceIdLow', TType::I64, 1); self::$tptl->writeI64($span['traceIdLow']); @@ -125,7 +121,6 @@ private function handleSpan($span) self::$tptl->writeStructEnd(); } - private function handleSpanLogs($logs) { self::$tptl->writeFieldBegin('logs', TType::LST, 11); @@ -139,10 +134,9 @@ private function handleSpanLogs($logs) self::$tptl->writeFieldEnd(); } - private function handleLog($log) { - self::$tptl->writeStructBegin("Log"); + self::$tptl->writeStructBegin('Log'); self::$tptl->writeFieldBegin('timestamp', TType::I64, 1); self::$tptl->writeI64($log['timestamp']); @@ -154,7 +148,6 @@ private function handleLog($log) self::$tptl->writeStructEnd(); } - private function handleLogFields($fields) { self::$tptl->writeFieldBegin('fields', TType::LST, 2); @@ -168,7 +161,6 @@ private function handleLogFields($fields) self::$tptl->writeFieldEnd(); } - private function handleSpanTags($tags) { self::$tptl->writeFieldBegin('tags', TType::LST, 10); @@ -182,7 +174,6 @@ private function handleSpanTags($tags) self::$tptl->writeFieldEnd(); } - private function handleSpanRefes($references) { self::$tptl->writeFieldBegin('references', TType::LST, 6); @@ -196,28 +187,27 @@ private function handleSpanRefes($references) self::$tptl->writeFieldEnd(); } - private function handleSpanRefe($refe) { - self::$tptl->writeStructBegin("SpanRef"); + self::$tptl->writeStructBegin('SpanRef'); - self::$tptl->writeFieldBegin("refType", TType::I32, 1); + self::$tptl->writeFieldBegin('refType', TType::I32, 1); self::$tptl->writeI32($refe['refType']); self::$tptl->writeFieldEnd(); - self::$tptl->writeFieldBegin("traceIdLow", TType::I64, 2); + self::$tptl->writeFieldBegin('traceIdLow', TType::I64, 2); self::$tptl->writeI64($refe['traceIdLow']); self::$tptl->writeFieldEnd(); - self::$tptl->writeFieldBegin("traceIdHigh", TType::I64, 3); + self::$tptl->writeFieldBegin('traceIdHigh', TType::I64, 3); self::$tptl->writeI64($refe['traceIdHigh']); self::$tptl->writeFieldEnd(); - self::$tptl->writeFieldBegin("spanId", TType::I64, 4); + self::$tptl->writeFieldBegin('spanId', TType::I64, 4); self::$tptl->writeI64($refe['spanId']); self::$tptl->writeFieldEnd(); self::$tptl->writeFieldStop(); self::$tptl->writeStructEnd(); } -} \ No newline at end of file +} diff --git a/src/Jaeger/Thrift/SpanRefType.php b/src/Jaeger/Thrift/SpanRefType.php index 6b48f6a..accf2ce 100644 --- a/src/Jaeger/Thrift/SpanRefType.php +++ b/src/Jaeger/Thrift/SpanRefType.php @@ -15,10 +15,9 @@ namespace Jaeger\Thrift; -final class SpanRefType { - +final class SpanRefType +{ const CHILD_OF = 0; const FOLLOWS_FROM = 1; - } diff --git a/src/Jaeger/Thrift/TStruct.php b/src/Jaeger/Thrift/TStruct.php index f12a408..7a4361c 100644 --- a/src/Jaeger/Thrift/TStruct.php +++ b/src/Jaeger/Thrift/TStruct.php @@ -17,9 +17,9 @@ use Thrift\Protocol\TProtocol; -interface TStruct { - +interface TStruct +{ public function write(TProtocol $t); public function read(TProtocol $t); -} \ No newline at end of file +} diff --git a/src/Jaeger/Thrift/Tags.php b/src/Jaeger/Thrift/Tags.php index 86c7520..813add0 100644 --- a/src/Jaeger/Thrift/Tags.php +++ b/src/Jaeger/Thrift/Tags.php @@ -18,8 +18,8 @@ use Thrift\Protocol\TProtocol; use Thrift\Type\TType; -class Tags implements TStruct{ - +class Tags implements TStruct +{ public static $tptl = null; public static $instance = null; @@ -28,40 +28,36 @@ class Tags implements TStruct{ public $thriftTags = null; - - private function __construct(){ - + private function __construct() + { } - - private function __clone(){ - + private function __clone() + { } - - public static function getInstance(){ - if(!(self::$instance instanceof self)){ + public static function getInstance() + { + if (!(self::$instance instanceof self)) { self::$instance = new self(); } return self::$instance; } - - public function write(TProtocol $t){ - + public function write(TProtocol $t) + { self::$tptl = $t; - if(empty($this->thriftTags)){ + if (empty($this->thriftTags)) { return false; } - foreach($this->thriftTags as $tag) { - - self::$tptl->writeStructBegin("Tag"); + foreach ($this->thriftTags as $tag) { + self::$tptl->writeStructBegin('Tag'); if (isset($tag['key'])) { - self::$tptl->writeFieldBegin("key", TType::STRING, 1); + self::$tptl->writeFieldBegin('key', TType::STRING, 1); self::$tptl->writeString(strval($tag['key'])); self::$tptl->writeFieldEnd(); } @@ -106,64 +102,61 @@ public function write(TProtocol $t){ self::$tptl->writeStructEnd(); } - return true; } - - public function read(TProtocol $t){ - + public function read(TProtocol $t) + { } - - public function setThriftTags($thriftTags){ + public function setThriftTags($thriftTags) + { $this->thriftTags = $thriftTags; } - - public function setTags($tags){ + public function setTags($tags) + { $this->tags = $tags; } - - public function buildTags(){ - + public function buildTags() + { $thriftTags = []; - if(empty($this->tags)){ + if (empty($this->tags)) { return $thriftTags; } - foreach ($this->tags as $k => $v){ - switch(gettype($v)){ - case "string": + foreach ($this->tags as $k => $v) { + switch (gettype($v)) { + case 'string': $thriftTags[] = [ 'key' => $k, 'vType' => 'STRING', 'vStr' => $v, ]; break; - case "boolean": + case 'boolean': $thriftTags[] = [ 'key' => $k, 'vType' => 'BOOL', 'vBool' => $v, ]; break; - case "double": + case 'double': $thriftTags[] = [ 'key' => $k, 'vType' => 'DOUBLE', 'vDouble' => $v, ]; break; - case "integer": + case 'integer': $thriftTags[] = [ 'key' => $k, 'vType' => 'DOUBLE', 'vDouble' => $v, ]; break; - case "array": + case 'array': $thriftTags[] = [ 'key' => $k, 'vType' => 'STRING', @@ -179,7 +172,6 @@ public function buildTags(){ } } - return $thriftTags; } -} \ No newline at end of file +} diff --git a/src/Jaeger/Thrift/Types.php b/src/Jaeger/Thrift/Types.php index 38e97de..a465efe 100644 --- a/src/Jaeger/Thrift/Types.php +++ b/src/Jaeger/Thrift/Types.php @@ -15,45 +15,47 @@ namespace Jaeger\Thrift; -class Types { - +class Types +{ const TAG_TYPE_STRING = 0; const TAG_TYPE_DOUBLE = 1; - const TAG_TYPE_BOOL = 2; - const TAG_TYPE_LONG = 3; - const TAG_TYPE_BINARY = 4; - + const TAG_TYPE_BOOL = 2; + const TAG_TYPE_LONG = 3; + const TAG_TYPE_BINARY = 4; - public static function stringToTagType($string){ - switch($string){ - case "STRING": + public static function stringToTagType($string) + { + switch ($string) { + case 'STRING': return self::TAG_TYPE_STRING; - case "DOUBLE": + case 'DOUBLE': return self::TAG_TYPE_DOUBLE; - case "BOOL": + case 'BOOL': return self::TAG_TYPE_BOOL; - case "LONG": + case 'LONG': return self::TAG_TYPE_LONG; - case "BINARY": + case 'BINARY': return self::TAG_TYPE_BINARY; } - return "not a valid TagType string"; - } + return 'not a valid TagType string'; + } - public static function tagTypeToString($tagType){ - switch($tagType){ + public static function tagTypeToString($tagType) + { + switch ($tagType) { case self::TAG_TYPE_STRING: - return "STRING"; + return 'STRING'; case self::TAG_TYPE_DOUBLE: - return "DOUBLE"; + return 'DOUBLE'; case self::TAG_TYPE_BOOL: - return "BOOL"; + return 'BOOL'; case self::TAG_TYPE_LONG: - return "LONG"; + return 'LONG'; case self::TAG_TYPE_BINARY: - return "BINARY"; + return 'BINARY'; } - return "UNSET"; + + return 'UNSET'; } -} \ No newline at end of file +} diff --git a/src/Jaeger/Transport/Transport.php b/src/Jaeger/Transport/Transport.php index ab928dc..b11e47c 100644 --- a/src/Jaeger/Transport/Transport.php +++ b/src/Jaeger/Transport/Transport.php @@ -17,8 +17,9 @@ use Jaeger\Jaeger; -interface Transport { +interface Transport +{ public function append(Jaeger $jaeger); public function flush(); -} \ No newline at end of file +} diff --git a/src/Jaeger/Transport/TransportUdp.php b/src/Jaeger/Transport/TransportUdp.php index 57eec1f..cc90602 100644 --- a/src/Jaeger/Transport/TransportUdp.php +++ b/src/Jaeger/Transport/TransportUdp.php @@ -15,6 +15,7 @@ namespace Jaeger\Transport; +use Jaeger\Constants; use Jaeger\Jaeger; use Jaeger\Thrift\AgentClient; use Jaeger\Thrift\JaegerThriftSpan; @@ -22,13 +23,11 @@ use Jaeger\Thrift\Span; use Jaeger\Thrift\TStruct; use Jaeger\UdpClient; -use Thrift\Transport\TMemoryBuffer; use Thrift\Protocol\TCompactProtocol; -use Jaeger\Constants; +use Thrift\Transport\TMemoryBuffer; class TransportUdp implements Transport { - private $tran = null; public static $hostPort = ''; @@ -50,12 +49,12 @@ class TransportUdp implements Transport public function __construct($hostport = '', $maxPacketSize = '') { - if ($hostport == "") { + if ('' == $hostport) { $hostport = $this->agentServerHostPort; } self::$hostPort = $hostport; - if ($maxPacketSize == 0) { + if (0 == $maxPacketSize) { $maxPacketSize = stristr(PHP_OS, 'DAR') ? self::MAC_UDP_MAX_SIZE : Constants\UDP_PACKET_MAX_LENGTH; } @@ -65,7 +64,6 @@ public function __construct($hostport = '', $maxPacketSize = '') $this->thriftProtocol = new TCompactProtocol($this->tran); } - public function buildAndCalcSizeOfProcessThrift(Jaeger $jaeger) { $jaeger->processThrift = (new JaegerThriftSpan())->buildJaegerProcessThrift($jaeger); @@ -74,23 +72,20 @@ public function buildAndCalcSizeOfProcessThrift(Jaeger $jaeger) $this->bufferSize += $this->procesSize; } - /** - * 收集将要发送的追踪信息 - * @param Jaeger $jaeger + * 收集将要发送的追踪信息. + * * @return bool */ public function append(Jaeger $jaeger) { - - if ($jaeger->process == null) { + if (null == $jaeger->process) { $this->buildAndCalcSizeOfProcessThrift($jaeger); } $thriftSpansBuffer = []; // Uncommitted span used to temporarily store shards foreach ($jaeger->spans as $span) { - $spanThrift = (new JaegerThriftSpan())->buildJaegerSpanThrift($span); $agentSpan = Span::getInstance(); @@ -126,23 +121,21 @@ public function append(Jaeger $jaeger) return true; } - public function resetBuffer() { $this->bufferSize = $this->procesSize; self::$batchs = []; } - /** - * 获取序列化后的thrift和计算序列化后的thrift字符长度 - * @param TStruct $ts + * 获取序列化后的thrift和计算序列化后的thrift字符长度. + * * @param mixed $serializedThrift + * * @return mixed */ private function getAndCalcSizeOfSerializedThrift(TStruct $ts, &$serializedThrift) { - $ts->write($this->thriftProtocol); $serThriftStrlen = $this->tran->available(); //获取后buf清空 @@ -151,7 +144,6 @@ private function getAndCalcSizeOfSerializedThrift(TStruct $ts, &$serializedThrif return $serThriftStrlen; } - /** * @return int */ @@ -176,9 +168,8 @@ public function flush() return $spanNum; } - public function getBatchs() { return self::$batchs; } -} \ No newline at end of file +} diff --git a/src/Jaeger/UdpClient.php b/src/Jaeger/UdpClient.php index 3bc41f6..e5ae4ec 100644 --- a/src/Jaeger/UdpClient.php +++ b/src/Jaeger/UdpClient.php @@ -19,12 +19,10 @@ /** * send thrift to jaeger-agent - * Class UdpClient - * @package Jaeger + * Class UdpClient. */ - -class UdpClient{ - +class UdpClient +{ private $host = ''; private $post = ''; @@ -33,46 +31,50 @@ class UdpClient{ private $agentClient = null; - public function __construct($hostPost, AgentClient $agentClient){ - list($this->host, $this->post) = explode(":", $hostPost); + public function __construct($hostPost, AgentClient $agentClient) + { + list($this->host, $this->post) = explode(':', $hostPost); $this->agentClient = $agentClient; $this->socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); } - /** * @return bool */ - public function isOpen(){ - return $this->socket !== null; + public function isOpen() + { + return null !== $this->socket; } - /** - * send thrift + * send thrift. + * * @param mixed $batch + * * @return bool + * * @throws \Exception */ - public function emitBatch($batch){ + public function emitBatch($batch) + { $buildThrift = $this->agentClient->buildThrift($batch); - if(isset($buildThrift['len']) && $buildThrift['len'] && $this->isOpen()) { + if (isset($buildThrift['len']) && $buildThrift['len'] && $this->isOpen()) { $len = $buildThrift['len']; $enitThrift = $buildThrift['thriftStr']; $res = socket_sendto($this->socket, $enitThrift, $len, 0, $this->host, $this->post); - if($res === false) { - throw new \Exception("emit failse"); + if (false === $res) { + throw new \Exception('emit failse'); } return true; - }else{ + } else { return false; } } - - public function close(){ + public function close() + { socket_close($this->socket); $this->socket = null; } -} \ No newline at end of file +} diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 2dc39e1..9051b4f 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -12,32 +12,29 @@ * or implied. See the License for the specific language governing permissions and limitations under * the License. */ + namespace tests; -use PHPUnit\Framework\TestCase; use Jaeger\Config; use OpenTracing\NoopTracer; +use PHPUnit\Framework\TestCase; class ConfigTest extends TestCase { - - public function testSetDisabled(){ + public function testSetDisabled() + { $config = Config::getInstance(); $config->setDisabled(true); - $this->assertTrue($config::$disabled == true); + $this->assertTrue(true == $config::$disabled); } - - public function testNoopTracer(){ - + public function testNoopTracer() + { $config = Config::getInstance(); $config->setDisabled(true); $trace = $config->initTracer('test'); $this->assertTrue($trace instanceof NoopTracer); } - - - -} \ No newline at end of file +} diff --git a/tests/JaegerTest.php b/tests/JaegerTest.php index bca0615..1c3096c 100644 --- a/tests/JaegerTest.php +++ b/tests/JaegerTest.php @@ -15,25 +15,23 @@ namespace tests; +use Jaeger\Constants; use Jaeger\Jaeger; +use Jaeger\Propagator\JaegerPropagator; use Jaeger\Reporter\RemoteReporter; use Jaeger\Sampler\ConstSampler; use Jaeger\ScopeManager; use Jaeger\Span; +use Jaeger\SpanContext; use Jaeger\Transport\TransportUdp; +use OpenTracing\Formats; use OpenTracing\Reference; use PHPUnit\Framework\TestCase; -use OpenTracing\Formats; -use Jaeger\SpanContext; -use Jaeger\Constants; -use Jaeger\Propagator\JaegerPropagator; class JaegerTest extends TestCase { - - - public function getJaeger(){ - + public function getJaeger() + { $tranSport = new TransportUdp(); $reporter = new RemoteReporter($tranSport); $sampler = new ConstSampler(); @@ -42,14 +40,14 @@ public function getJaeger(){ return new Jaeger('jaeger', $reporter, $sampler, $scopeManager); } - - public function testNew(){ + public function testNew() + { $Jaeger = $this->getJaeger(); $this->assertInstanceOf(Jaeger::class, $Jaeger); } - public function testGetEnvTags(){ - + public function testGetEnvTags() + { $_SERVER['JAEGER_TAGS'] = 'a=b,c=d'; $Jaeger = $this->getJaeger(); $tags = $Jaeger->getEnvTags(); @@ -57,16 +55,16 @@ public function testGetEnvTags(){ $this->assertTrue(count($tags) > 0); } - - public function testSetTags(){ + public function testSetTags() + { $Jaeger = $this->getJaeger(); $Jaeger->setTags(['version' => '2.0.0']); - $this->assertTrue($Jaeger->tags['version'] == '2.0.0'); + $this->assertTrue('2.0.0' == $Jaeger->tags['version']); } - - public function testInject(){ + public function testInject() + { $Jaeger = $this->getJaeger(); $Jaeger->setPropagator(new JaegerPropagator()); @@ -76,8 +74,8 @@ public function testInject(){ $this->assertTrue('0:1:1:1' == $_SERVER[strtoupper(Constants\Tracer_State_Header_Name)]); } - - public function testInjectUnSupportFormat(){ + public function testInjectUnSupportFormat() + { $Jaeger = $this->getJaeger(); $Jaeger->setPropagator(new JaegerPropagator()); @@ -87,21 +85,21 @@ public function testInjectUnSupportFormat(){ $Jaeger->inject($context, Formats\HTTP_HEADERS, $_SERVER); } - - public function testExtract(){ + public function testExtract() + { $Jaeger = $this->getJaeger(); $Jaeger->setPropagator(new JaegerPropagator()); $carrier[strtoupper(Constants\Tracer_State_Header_Name)] = '1:1:1:1'; $spanContext = $Jaeger->extract(Formats\TEXT_MAP, $carrier); - $this->assertTrue($spanContext->parentId == 1); - $this->assertTrue($spanContext->traceIdLow == 1); - $this->assertTrue($spanContext->flags == 1); - $this->assertTrue($spanContext->spanId == 1); + $this->assertTrue(1 == $spanContext->parentId); + $this->assertTrue(1 == $spanContext->traceIdLow); + $this->assertTrue(1 == $spanContext->flags); + $this->assertTrue(1 == $spanContext->spanId); } - - public function testExtractUnSupportFormat(){ + public function testExtractUnSupportFormat() + { $Jaeger = $this->getJaeger(); $Jaeger->setPropagator(new JaegerPropagator()); @@ -111,15 +109,14 @@ public function testExtractUnSupportFormat(){ $Jaeger->extract(Formats\HTTP_HEADERS, $_SERVER); } - - public function testStartSpan(){ + public function testStartSpan() + { $Jaeger = $this->getJaeger(); $span = $Jaeger->startSpan('test'); $this->assertNotEmpty($span->startTime); $this->assertNotEmpty($Jaeger->getSpans()); } - public function testStartSpanWithFollowsFromTypeRef() { $jaeger = $this->getJaeger(); @@ -142,7 +139,6 @@ public function testStartSpanWithFollowsFromTypeRef() $this->assertSame($childSpan->spanContext->traceIdLow, $otherRootSpan->spanContext->traceIdLow); } - public function testStartSpanWithChildOfTypeRef() { $jaeger = $this->getJaeger(); @@ -158,7 +154,6 @@ public function testStartSpanWithChildOfTypeRef() $this->assertSame($childSpan->spanContext->traceIdLow, $rootSpan->spanContext->traceIdLow); } - public function testStartSpanWithCustomStartTime() { $jaeger = $this->getJaeger(); @@ -167,7 +162,6 @@ public function testStartSpanWithCustomStartTime() $this->assertSame(1499355363123456, $span->startTime); } - public function testStartSpanWithAllRefType() { $jaeger = $this->getJaeger(); @@ -183,23 +177,24 @@ public function testStartSpanWithAllRefType() $this->assertSame($childSpan->spanContext->traceIdLow, $otherRootSpan->spanContext->traceIdLow); } - - public function testReportSpan(){ + public function testReportSpan() + { $Jaeger = $this->getJaeger(); $Jaeger->startSpan('test'); $Jaeger->reportSpan(); $this->assertEmpty($Jaeger->getSpans()); } - public function testStartActiveSpan(){ + public function testStartActiveSpan() + { $Jaeger = $this->getJaeger(); $Jaeger->startActiveSpan('test'); $this->assertNotEmpty($Jaeger->getSpans()); } - - public function testGetActiveSpan(){ + public function testGetActiveSpan() + { $Jaeger = $this->getJaeger(); $Jaeger->startActiveSpan('test'); @@ -208,16 +203,16 @@ public function testGetActiveSpan(){ $this->assertInstanceOf(Span::class, $span); } - - public function testFlush(){ + public function testFlush() + { $Jaeger = $this->getJaeger(); $Jaeger->startSpan('test'); $Jaeger->flush(); $this->assertEmpty($Jaeger->getSpans()); } - - public function testNestedSpanBaggage(){ + public function testNestedSpanBaggage() + { $tracer = $this->getJaeger(); $parent = $tracer->startSpan('parent'); diff --git a/tests/Propagator/JaegerPropagatorTest.php b/tests/Propagator/JaegerPropagatorTest.php index 937a4e7..4738bc3 100644 --- a/tests/Propagator/JaegerPropagatorTest.php +++ b/tests/Propagator/JaegerPropagatorTest.php @@ -15,31 +15,32 @@ namespace tests; -use PHPUnit\Framework\TestCase; -use OpenTracing\Formats; use Jaeger\Constants; -use Jaeger\SpanContext; use Jaeger\Propagator\JaegerPropagator; +use Jaeger\SpanContext; +use OpenTracing\Formats; +use PHPUnit\Framework\TestCase; -class JaegerPropagatorTest extends TestCase{ - - public function getSpanContext(){ +class JaegerPropagatorTest extends TestCase +{ + public function getSpanContext() + { return new SpanContext(1562237095801441413, 0, 1, null, 1); } - - public function testInject(){ + public function testInject() + { $context = $this->getSpanContext(); $context->traceIdLow = 1562237095801441413; $jaeger = new JaegerPropagator(); $carrier = []; $jaeger->inject($context, Formats\TEXT_MAP, $carrier); - $this->assertTrue($carrier[strtoupper(Constants\Tracer_State_Header_Name)] == '15ae2e5c8e2ecc85:15ae2e5c8e2ecc85:0:1'); + $this->assertTrue('15ae2e5c8e2ecc85:15ae2e5c8e2ecc85:0:1' == $carrier[strtoupper(Constants\Tracer_State_Header_Name)]); } - - public function testInject128Bit(){ + public function testInject128Bit() + { $context = $this->getSpanContext(); $context->traceIdLow = 1562289663898779811; $context->traceIdHigh = 1562289663898881723; @@ -48,67 +49,65 @@ public function testInject128Bit(){ $carrier = []; $jaeger->inject($context, Formats\TEXT_MAP, $carrier); - $this->assertTrue($carrier[strtoupper(Constants\Tracer_State_Header_Name)] - == '15ae5e2c04f50ebb15ae5e2c04f380a3:15ae2e5c8e2ecc85:0:1'); + $this->assertTrue('15ae5e2c04f50ebb15ae5e2c04f380a3:15ae2e5c8e2ecc85:0:1' + == $carrier[strtoupper(Constants\Tracer_State_Header_Name)]); } - - public function testExtract(){ - + public function testExtract() + { $jaeger = new JaegerPropagator(); $carrier = []; $carrier[strtoupper(Constants\Tracer_State_Header_Name)] = '15ae2e5c8e2ecc85:15ae2e5c8e2ecc85:0:1'; $context = $jaeger->extract(Formats\TEXT_MAP, $carrier); - $this->assertTrue($context->traceIdLow == 1562237095801441413); - $this->assertTrue($context->parentId == 0); - $this->assertTrue($context->spanId == 1562237095801441413); - $this->assertTrue($context->flags == 1); + $this->assertTrue(1562237095801441413 == $context->traceIdLow); + $this->assertTrue(0 == $context->parentId); + $this->assertTrue(1562237095801441413 == $context->spanId); + $this->assertTrue(1 == $context->flags); } - - public function testExtractDebugId(){ - + public function testExtractDebugId() + { $jaeger = new JaegerPropagator(); - $carrier[Constants\Trace_Baggage_Header_Prefix . 'baggage'] = 2; + $carrier[Constants\Trace_Baggage_Header_Prefix.'baggage'] = 2; $context = $jaeger->extract(Formats\TEXT_MAP, $carrier); - $this->assertTrue($context->debugId == 0); + $this->assertTrue(0 == $context->debugId); $carrier[Constants\Jaeger_Debug_Header] = 1; $context = $jaeger->extract(Formats\TEXT_MAP, $carrier); - $this->assertTrue($context->debugId == 1); + $this->assertTrue(1 == $context->debugId); } - - public function testExtractUberctx(){ + public function testExtractUberctx() + { $jaeger = new JaegerPropagator(); $carrier[Constants\Trace_Baggage_Header_Prefix] = '2.0.0'; $carrier[Constants\Jaeger_Debug_Header] = true; $context = $jaeger->extract(Formats\TEXT_MAP, $carrier); - $this->assertTrue($context->baggage == null); + $this->assertTrue(null == $context->baggage); $carrier = []; $carrier[Constants\Trace_Baggage_Header_Prefix.'version'] = '2.0.0'; $context = $jaeger->extract(Formats\TEXT_MAP, $carrier); - $this->assertTrue($context->getBaggageItem('version') == '2.0.0'); + $this->assertTrue('2.0.0' == $context->getBaggageItem('version')); } - - public function testExtractBaggageHeader(){ + public function testExtractBaggageHeader() + { $jaeger = new JaegerPropagator(); $carrier = []; $carrier[Constants\Jaeger_Baggage_Header] = 'version=2.0.0,os=1'; $context = $jaeger->extract(Formats\TEXT_MAP, $carrier); - $this->assertTrue($context->getBaggageItem('version') == '2.0.0'); - $this->assertTrue($context->getBaggageItem('os') == '1'); + $this->assertTrue('2.0.0' == $context->getBaggageItem('version')); + $this->assertTrue('1' == $context->getBaggageItem('os')); } - - public function testExtractBadBaggageHeader(){ + public function testExtractBadBaggageHeader() + { $jaeger = new JaegerPropagator(); $carrier = []; @@ -116,41 +115,39 @@ public function testExtractBadBaggageHeader(){ $carrier[Constants\Jaeger_Baggage_Header] = 'version'; $carrier[Constants\Jaeger_Debug_Header] = true; $context = $jaeger->extract(Formats\TEXT_MAP, $carrier); - $this->assertTrue($context->baggage == null); + $this->assertTrue(null == $context->baggage); } - - public function testExtract128Bit(){ - + public function testExtract128Bit() + { $jaeger = new JaegerPropagator(); $carrier = []; $carrier[strtoupper(Constants\Tracer_State_Header_Name)] = '15ae5e2c04f50ebb15ae5e2c04f380a3:15ae2e5c8e2ecc85:0:1'; $context = $jaeger->extract(Formats\TEXT_MAP, $carrier); - $this->assertTrue($context->traceIdLow == 1562289663898779811); - $this->assertTrue($context->traceIdHigh == 1562289663898881723); - $this->assertTrue($context->parentId == 0); - $this->assertTrue($context->spanId == 1562237095801441413); - $this->assertTrue($context->flags == 1); + $this->assertTrue(1562289663898779811 == $context->traceIdLow); + $this->assertTrue(1562289663898881723 == $context->traceIdHigh); + $this->assertTrue(0 == $context->parentId); + $this->assertTrue(1562237095801441413 == $context->spanId); + $this->assertTrue(1 == $context->flags); } - - public function testExtractPsr7(){ - + public function testExtractPsr7() + { $jaeger = new JaegerPropagator(); $carrier = []; $carrier[] = [strtoupper(Constants\Tracer_State_Header_Name) => '15ae2e5c8e2ecc85:15ae2e5c8e2ecc85:0:1']; $context = $jaeger->extract(Formats\TEXT_MAP, $carrier); - $this->assertTrue($context->traceIdLow == 1562237095801441413); - $this->assertTrue($context->parentId == 0); - $this->assertTrue($context->spanId == 1562237095801441413); - $this->assertTrue($context->flags == 1); + $this->assertTrue(1562237095801441413 == $context->traceIdLow); + $this->assertTrue(0 == $context->parentId); + $this->assertTrue(1562237095801441413 == $context->spanId); + $this->assertTrue(1 == $context->flags); } - - public function testExtractReturnsNull(){ + public function testExtractReturnsNull() + { $jaeger = new JaegerPropagator(); $carrier = []; diff --git a/tests/Propagator/ZipkinPropagatorTest.php b/tests/Propagator/ZipkinPropagatorTest.php index 94a3c28..60396b6 100644 --- a/tests/Propagator/ZipkinPropagatorTest.php +++ b/tests/Propagator/ZipkinPropagatorTest.php @@ -15,20 +15,21 @@ namespace tests; -use PHPUnit\Framework\TestCase; -use OpenTracing\Formats; use Jaeger\Constants; -use Jaeger\SpanContext; use Jaeger\Propagator\ZipkinPropagator; +use Jaeger\SpanContext; +use OpenTracing\Formats; +use PHPUnit\Framework\TestCase; -class ZipkinPropagatorTest extends TestCase{ - - public function getSpanContext(){ +class ZipkinPropagatorTest extends TestCase +{ + public function getSpanContext() + { return new SpanContext(1562237095801441413, 0, 1, null, 1); } - - public function testInject(){ + public function testInject() + { $context = $this->getSpanContext(); $context->traceIdLow = 1562237095801441413; $zipkin = new ZipkinPropagator(); @@ -36,14 +37,14 @@ public function testInject(){ $zipkin->inject($context, Formats\TEXT_MAP, $carrier); - $this->assertTrue($carrier[Constants\X_B3_TRACEID] == '15ae2e5c8e2ecc85'); - $this->assertTrue($carrier[Constants\X_B3_PARENT_SPANID] == 0); - $this->assertTrue($carrier[Constants\X_B3_SPANID] == '15ae2e5c8e2ecc85'); - $this->assertTrue($carrier[Constants\X_B3_SAMPLED] == 1); + $this->assertTrue('15ae2e5c8e2ecc85' == $carrier[Constants\X_B3_TRACEID]); + $this->assertTrue(0 == $carrier[Constants\X_B3_PARENT_SPANID]); + $this->assertTrue('15ae2e5c8e2ecc85' == $carrier[Constants\X_B3_SPANID]); + $this->assertTrue(1 == $carrier[Constants\X_B3_SAMPLED]); } - - public function testInject128Bit(){ + public function testInject128Bit() + { $context = $this->getSpanContext(); $context->traceIdLow = 1562289663898779811; $context->traceIdHigh = 1562289663898881723; @@ -53,14 +54,14 @@ public function testInject128Bit(){ $zipkin->inject($context, Formats\TEXT_MAP, $carrier); - $this->assertTrue($carrier[Constants\X_B3_TRACEID] == '15ae5e2c04f50ebb15ae5e2c04f380a3'); - $this->assertTrue($carrier[Constants\X_B3_PARENT_SPANID] == 0); - $this->assertTrue($carrier[Constants\X_B3_SPANID] == '15ae2e5c8e2ecc85'); - $this->assertTrue($carrier[Constants\X_B3_SAMPLED] == 1); + $this->assertTrue('15ae5e2c04f50ebb15ae5e2c04f380a3' == $carrier[Constants\X_B3_TRACEID]); + $this->assertTrue(0 == $carrier[Constants\X_B3_PARENT_SPANID]); + $this->assertTrue('15ae2e5c8e2ecc85' == $carrier[Constants\X_B3_SPANID]); + $this->assertTrue(1 == $carrier[Constants\X_B3_SAMPLED]); } - public function testExtract(){ - + public function testExtract() + { $zipkin = new ZipkinPropagator(); $carrier = []; $carrier[Constants\X_B3_TRACEID] = '15ae2e5c8e2ecc85'; @@ -69,15 +70,14 @@ public function testExtract(){ $carrier[Constants\X_B3_SAMPLED] = 1; $context = $zipkin->extract(Formats\TEXT_MAP, $carrier); - $this->assertTrue($context->traceIdLow == '1562237095801441413'); - $this->assertTrue($context->parentId == 1); - $this->assertTrue($context->spanId == '1562237095801441413'); - $this->assertTrue($context->flags == 1); + $this->assertTrue('1562237095801441413' == $context->traceIdLow); + $this->assertTrue(1 == $context->parentId); + $this->assertTrue('1562237095801441413' == $context->spanId); + $this->assertTrue(1 == $context->flags); } - - public function testExtract128Bit(){ - + public function testExtract128Bit() + { $zipkin = new ZipkinPropagator(); $carrier = []; $carrier[Constants\X_B3_TRACEID] = '15ae5e2c04f50ebb15ae5e2c04f380a3'; @@ -86,15 +86,15 @@ public function testExtract128Bit(){ $carrier[Constants\X_B3_SAMPLED] = 1; $context = $zipkin->extract(Formats\TEXT_MAP, $carrier); - $this->assertTrue($context->traceIdLow == 1562289663898779811); - $this->assertTrue($context->traceIdHigh == 1562289663898881723); - $this->assertTrue($context->parentId == 0); - $this->assertTrue($context->spanId == 1562289663898779811); - $this->assertTrue($context->flags == 1); + $this->assertTrue(1562289663898779811 == $context->traceIdLow); + $this->assertTrue(1562289663898881723 == $context->traceIdHigh); + $this->assertTrue(0 == $context->parentId); + $this->assertTrue(1562289663898779811 == $context->spanId); + $this->assertTrue(1 == $context->flags); } - - public function testExtractReturnsNull(){ + public function testExtractReturnsNull() + { $jaeger = new ZipkinPropagator(); $carrier = []; diff --git a/tests/Reporter/RemoteReporterTest.php b/tests/Reporter/RemoteReporterTest.php index 4b841fa..9d96877 100644 --- a/tests/Reporter/RemoteReporterTest.php +++ b/tests/Reporter/RemoteReporterTest.php @@ -15,18 +15,18 @@ namespace tests; -use PHPUnit\Framework\TestCase; use Jaeger\SpanContext; +use PHPUnit\Framework\TestCase; -class RemoteReporterTest extends TestCase{ - - public function getSpanContext(){ +class RemoteReporterTest extends TestCase +{ + public function getSpanContext() + { return new SpanContext(1562237095801441413, 0, 1, null, 1); } - - public function testClose(){ + public function testClose() + { $this->assertTrue(true); } - -} \ No newline at end of file +} diff --git a/tests/Sampler/ConstSamplerTest.php b/tests/Sampler/ConstSamplerTest.php index d4f5487..d90e3b4 100644 --- a/tests/Sampler/ConstSamplerTest.php +++ b/tests/Sampler/ConstSamplerTest.php @@ -15,21 +15,22 @@ namespace tests; +use Jaeger\Constants; use Jaeger\Sampler\ConstSampler; use PHPUnit\Framework\TestCase; -use Jaeger\Constants; class ConstSamplerTest extends TestCase { - - public function testConstSampler(){ + public function testConstSampler() + { $sample = new ConstSampler(true); - $this->assertTrue($sample->IsSampled() == true); + $this->assertTrue(true == $sample->IsSampled()); } - public function testConstSamplerGetTag(){ + public function testConstSamplerGetTag() + { $sample = new ConstSampler(true); $tags = $sample->getTags(); - $this->assertTrue($tags[Constants\SAMPLER_TYPE_TAG_KEY] == 'const'); + $this->assertTrue('const' == $tags[Constants\SAMPLER_TYPE_TAG_KEY]); } -} \ No newline at end of file +} diff --git a/tests/Sampler/ProbabilisticSamplerTest.php b/tests/Sampler/ProbabilisticSamplerTest.php index 48646db..68d4b1f 100644 --- a/tests/Sampler/ProbabilisticSamplerTest.php +++ b/tests/Sampler/ProbabilisticSamplerTest.php @@ -15,21 +15,22 @@ namespace tests; +use Jaeger\Constants; use Jaeger\Sampler\ProbabilisticSampler; use PHPUnit\Framework\TestCase; -use Jaeger\Constants; class ProbabilisticSamplerTest extends TestCase { - - public function testProbabilisticSampler(){ + public function testProbabilisticSampler() + { $sample = new ProbabilisticSampler(0.0001); - $this->assertTrue($sample->IsSampled() !== null); + $this->assertTrue(null !== $sample->IsSampled()); } - public function testConstSamplerGetTag(){ + public function testConstSamplerGetTag() + { $sample = new ProbabilisticSampler(0.0001); $tags = $sample->getTags(); - $this->assertTrue($tags[Constants\SAMPLER_TYPE_TAG_KEY] == 'probabilistic'); + $this->assertTrue('probabilistic' == $tags[Constants\SAMPLER_TYPE_TAG_KEY]); } -} \ No newline at end of file +} diff --git a/tests/ScopeMangerTest.php b/tests/ScopeMangerTest.php index 69ab5e7..331aaaa 100644 --- a/tests/ScopeMangerTest.php +++ b/tests/ScopeMangerTest.php @@ -15,15 +15,15 @@ namespace tests; -use OpenTracing\NoopSpanContext; +use Jaeger\ScopeManager; use Jaeger\Span; +use OpenTracing\NoopSpanContext; use PHPUnit\Framework\TestCase; -use Jaeger\ScopeManager; class ScopeMangerTest extends TestCase { - - public function testActivate(){ + public function testActivate() + { $span1 = new Span('test', new NoopSpanContext(), []); $scopeManager = new ScopeManager(); @@ -33,8 +33,8 @@ public function testActivate(){ $this->assertTrue($span1 === $span2); } - public function testGetActive(){ - + public function testGetActive() + { $span = new Span('test', new NoopSpanContext(), []); $scopeManager = new ScopeManager(); @@ -44,22 +44,22 @@ public function testGetActive(){ $this->assertTrue($scope1 === $scope2); } - - public function testDelActive(){ - + public function testDelActive() + { $span = new Span('test', new NoopSpanContext(), []); $scopeManager = new ScopeManager(); $scope = $scopeManager->activate($span, true); $res = $scopeManager->deactivate($scope); - $this->assertTrue($res == true); + $this->assertTrue(true == $res); $getRes = $scopeManager->getActive(); - $this->assertTrue($getRes === null); + $this->assertTrue(null === $getRes); } - public function testDelActiveNestedScopes() { + public function testDelActiveNestedScopes() + { $scopeManager = new ScopeManager(); $span1 = new Span('Z', new NoopSpanContext(), []); $scope1 = $scopeManager->activate($span1, true); @@ -72,22 +72,23 @@ public function testDelActiveNestedScopes() { $this->assertTrue($active === $scope3); $res = $scopeManager->deactivate($scope3); - $this->assertTrue($res == true); + $this->assertTrue(true == $res); $active = $scopeManager->getActive(); $this->assertTrue($active === $scope2); $res = $scopeManager->deactivate($scope2); - $this->assertTrue($res == true); + $this->assertTrue(true == $res); $active = $scopeManager->getActive(); $this->assertTrue($active === $scope1); $res = $scopeManager->deactivate($scope1); - $this->assertTrue($res == true); + $this->assertTrue(true == $res); $active = $scopeManager->getActive(); - $this->assertTrue($active === null); + $this->assertTrue(null === $active); } - public function testDelActiveReNestScopes() { + public function testDelActiveReNestScopes() + { $scopeManager = new ScopeManager(); $span1 = new Span('A', new NoopSpanContext(), []); $scope1 = $scopeManager->activate($span1, true); @@ -115,6 +116,6 @@ public function testDelActiveReNestScopes() { $scopeManager->deactivate($scope1); $active = $scopeManager->getActive(); - $this->assertTrue($active === null); + $this->assertTrue(null === $active); } -} \ No newline at end of file +} diff --git a/tests/ScopeTest.php b/tests/ScopeTest.php index 1e182b2..03a3131 100644 --- a/tests/ScopeTest.php +++ b/tests/ScopeTest.php @@ -15,31 +15,31 @@ namespace tests; -use OpenTracing\NoopSpanContext; +use Jaeger\ScopeManager; use Jaeger\Span; +use OpenTracing\NoopSpanContext; use PHPUnit\Framework\TestCase; -use Jaeger\ScopeManager; class ScopeTest extends TestCase { - - public function testClose(){ + public function testClose() + { $span1 = new Span('test', new NoopSpanContext(), []); $scopeManager = new ScopeManager(); $scope = $scopeManager->activate($span1, true); $scope->close(); - $this->assertTrue($scopeManager->getActive() === null); + $this->assertTrue(null === $scopeManager->getActive()); } - - public function testGetSpan(){ + public function testGetSpan() + { $span1 = new Span('test', new NoopSpanContext(), []); $scopeManager = new ScopeManager(); $scope = $scopeManager->activate($span1, true); - $this->assertTrue($scope->getSpan() !== null); + $this->assertTrue(null !== $scope->getSpan()); } -} \ No newline at end of file +} diff --git a/tests/SpanContextTest.php b/tests/SpanContextTest.php index ed7db28..2dff2fe 100644 --- a/tests/SpanContextTest.php +++ b/tests/SpanContextTest.php @@ -15,87 +15,83 @@ namespace tests; -use PHPUnit\Framework\TestCase; use Jaeger\SpanContext; +use PHPUnit\Framework\TestCase; class SpanContextTest extends TestCase { - - - public function getSpanContext(){ + public function getSpanContext() + { return new SpanContext(1, 1, 1, null, 1); } - - public function testNew(){ + public function testNew() + { $spanContext = $this->getSpanContext(); $this->assertInstanceOf(SpanContext::class, $spanContext); } - - public function testWithBaggageItem(){ + public function testWithBaggageItem() + { $spanContext = $this->getSpanContext(); $res = $spanContext->withBaggageItem('version', '2.0.0'); // TODO // $this->assertTrue($res); } - - public function testGetBaggageItem(){ + public function testGetBaggageItem() + { $spanContext = $this->getSpanContext(); $spanContext->withBaggageItem('version', '2.0.0'); $version = $spanContext->getBaggageItem('version'); - $this->assertTrue($version === '2.0.0'); + $this->assertTrue('2.0.0' === $version); $service = $spanContext->getBaggageItem('service'); $this->assertNull($service); } - - public function testBuildString(){ + public function testBuildString() + { $spanContext = $this->getSpanContext(); $spanContext->traceIdLow = 1; - $this->assertTrue($spanContext->buildString() == '1:1:1:1'); + $this->assertTrue('1:1:1:1' == $spanContext->buildString()); $spanContext->traceIdHigh = 1; - $this->assertTrue($spanContext->buildString() == '10000000000000001:1:1:1'); + $this->assertTrue('10000000000000001:1:1:1' == $spanContext->buildString()); } - - public function testSpanIdToString(){ + public function testSpanIdToString() + { $spanContext = $this->getSpanContext(); - $this->assertTrue($spanContext->spanIdToString() == '1'); + $this->assertTrue('1' == $spanContext->spanIdToString()); - $spanContext->spanId = "111111"; - $this->assertTrue($spanContext->spanIdToString() == '1b207'); + $spanContext->spanId = '111111'; + $this->assertTrue('1b207' == $spanContext->spanIdToString()); } - - public function testTraceIdLowToString(){ + public function testTraceIdLowToString() + { $spanContext = $this->getSpanContext(); - $spanContext->traceIdLow = "111111"; - $this->assertTrue($spanContext->traceIdLowToString() == '1b207'); + $spanContext->traceIdLow = '111111'; + $this->assertTrue('1b207' == $spanContext->traceIdLowToString()); - $spanContext->traceIdHigh = "111111"; - $this->assertTrue($spanContext->traceIdLowToString() == '1b207000000000001b207'); + $spanContext->traceIdHigh = '111111'; + $this->assertTrue('1b207000000000001b207' == $spanContext->traceIdLowToString()); } - - public function testTraceIdToString(){ + public function testTraceIdToString() + { $spanContext = $this->getSpanContext(); $spanContext->traceIdToString('1b207000000000001b207'); - $this->assertTrue($spanContext->traceIdLow == '111111'); - $this->assertTrue($spanContext->traceIdHigh == '1954685383581106176'); + $this->assertTrue('111111' == $spanContext->traceIdLow); + $this->assertTrue('1954685383581106176' == $spanContext->traceIdHigh); $spanContext->traceIdLow = null; $spanContext->traceIdHigh = null; $spanContext->traceIdToString('1b207'); - $this->assertTrue($spanContext->traceIdLow == '111111'); - $this->assertTrue($spanContext->traceIdHigh === null); + $this->assertTrue('111111' == $spanContext->traceIdLow); + $this->assertTrue(null === $spanContext->traceIdHigh); } - - - -} \ No newline at end of file +} diff --git a/tests/SpanTest.php b/tests/SpanTest.php index d3fd48a..deab711 100644 --- a/tests/SpanTest.php +++ b/tests/SpanTest.php @@ -15,63 +15,62 @@ namespace tests; +use Jaeger\Span; use Jaeger\SpanContext; use OpenTracing\NoopSpanContext; -use Jaeger\Span; use PHPUnit\Framework\TestCase; class SpanTest extends TestCase { - - public function testOverwriteOperationName(){ + public function testOverwriteOperationName() + { $span = new Span('test', new NoopSpanContext(), []); $span->overwriteOperationName('test2'); - $this->assertTrue($span->getOperationName() == 'test2'); + $this->assertTrue('test2' == $span->getOperationName()); } - - public function testAddTags(){ + public function testAddTags() + { $span = new Span('test', new NoopSpanContext(), []); $span->setTag('test', 'test'); - $this->assertTrue((isset($span->tags['test']) && $span->tags['test'] == 'test')); + $this->assertTrue((isset($span->tags['test']) && 'test' == $span->tags['test'])); } - - public function testFinish(){ + public function testFinish() + { $span = new Span('test', new NoopSpanContext(), []); $span->setTag('test', 'test'); $span->finish(); $this->assertTrue(!empty($span->finishTime) && !empty($span->duration)); } - - public function testGetContext(){ + public function testGetContext() + { $span = new Span('test', new NoopSpanContext(), []); $spanContext = $span->getContext(); $this->assertInstanceOf(NoopSpanContext::class, $spanContext); } - - - public function testLog(){ + public function testLog() + { $span = new Span('test', new NoopSpanContext(), []); $logs = [ 'msg' => 'is test', - 'msg2' => 'is msg 2' + 'msg2' => 'is msg 2', ]; $span->log($logs); - $this->assertTrue(count($span->logs) == 1); + $this->assertTrue(1 == count($span->logs)); } - - public function testGetBaggageItem(){ + public function testGetBaggageItem() + { $span = new Span('test', new SpanContext(0, 0, 0), []); $span->addBaggageItem('version', '2.0.0'); - $version = $span->getBaggageItem('version'); + $version = $span->getBaggageItem('version'); $this->assertEquals('2.0.0', $version); $service = $span->getBaggageItem('service'); $this->assertNull($service); } -} \ No newline at end of file +} diff --git a/tests/Transport/TransportUdpTest.php b/tests/Transport/TransportUdpTest.php index ece8d08..66e97fa 100644 --- a/tests/Transport/TransportUdpTest.php +++ b/tests/Transport/TransportUdpTest.php @@ -15,28 +15,25 @@ namespace tests; -use PHPUnit\Framework\TestCase; use Jaeger\Transport\TransportUdp; +use PHPUnit\Framework\TestCase; class TransportUdpTest extends TestCase { - public $tran = null; - public function setUp(){ + public function setUp() + { $this->tran = new TransportUdp('localhost:6831'); } - // public function testFlush(){ // $this->tran->append(); // } - - public function testResetBuffer(){ + public function testResetBuffer() + { $this->tran->resetBuffer(); $this->assertCount(0, $this->tran->getBatchs()); - } - -} \ No newline at end of file +} diff --git a/tests/UdpClientTest.php b/tests/UdpClientTest.php index 20db1f7..8be2a13 100644 --- a/tests/UdpClientTest.php +++ b/tests/UdpClientTest.php @@ -15,9 +15,9 @@ namespace tests; -use PHPUnit\Framework\TestCase; -use Jaeger\UdpClient; use Jaeger\Thrift\AgentClient; +use Jaeger\UdpClient; +use PHPUnit\Framework\TestCase; class UdpClientTest extends TestCase { @@ -25,31 +25,29 @@ class UdpClientTest extends TestCase public $agentClient = null; - public function setUp(){ + public function setUp() + { $this->agentClient = $this->createMock(AgentClient::class); $this->udpClient = new UdpClient('localhost:6831', $this->agentClient); } - - public function testIsOpen(){ + public function testIsOpen() + { $this->assertTrue($this->udpClient->isOpen()); } - - public function testEmitBatch(){ - + public function testEmitBatch() + { $this->agentClient->expects($this->once())->method('buildThrift') - ->willReturn(['len'=> 3 , 'thriftStr' => 123]); - $batch = ['thriftProcess' => '' - , 'thriftSpans' => '']; + ->willReturn(['len' => 3, 'thriftStr' => 123]); + $batch = ['thriftProcess' => '', 'thriftSpans' => '']; $this->assertTrue($this->udpClient->emitBatch($batch)); } - - public function testEmitBatchFalse(){ - $batch = ['thriftProcess' => '' - , 'thriftSpans' => '']; + public function testEmitBatchFalse() + { + $batch = ['thriftProcess' => '', 'thriftSpans' => '']; $this->agentClient->expects($this->any())->method('buildThrift') ->willReturn(['thriftStr' => 123]); @@ -58,14 +56,14 @@ public function testEmitBatchFalse(){ $this->udpClient->close(); $this->agentClient->expects($this->any())->method('buildThrift') - ->willReturn(['len'=> 3 , 'thriftStr' => 123]); - + ->willReturn(['len' => 3, 'thriftStr' => 123]); $this->assertFalse($this->udpClient->emitBatch($batch)); } - public function testClose(){ + public function testClose() + { $this->udpClient->close(); $this->assertFalse($this->udpClient->isOpen()); } -} \ No newline at end of file +} From 9ee1ce584ad5ad70a4693528cde5907b41819c43 Mon Sep 17 00:00:00 2001 From: Xiangxuan Liu Date: Sat, 26 Dec 2020 16:15:58 +0800 Subject: [PATCH 10/32] PHP 7.1 syntax --- example/HTTP.php | 2 +- example/HTTP2.php | 2 +- example/Hprose.php | 2 +- example/Istio1.php | 2 +- example/Istio2.php | 2 +- example/Istio3.php | 2 +- src/Jaeger/Jaeger.php | 6 +++--- src/Jaeger/Propagator/JaegerPropagator.php | 2 +- src/Jaeger/Sampler/ProbabilisticSampler.php | 2 +- src/Jaeger/SpanContext.php | 2 +- src/Jaeger/Thrift/JaegerThriftSpan.php | 4 ++-- src/Jaeger/Thrift/Process.php | 6 +++--- src/Jaeger/UdpClient.php | 2 +- 13 files changed, 18 insertions(+), 18 deletions(-) diff --git a/example/HTTP.php b/example/HTTP.php index 0b734a4..9dc3e17 100644 --- a/example/HTTP.php +++ b/example/HTTP.php @@ -13,7 +13,7 @@ * the License. */ -require_once dirname(dirname(dirname(dirname(__FILE__)))).'/autoload.php'; +require_once dirname(__FILE__, 4).'/autoload.php'; use GuzzleHttp\Client; use Jaeger\Config; diff --git a/example/HTTP2.php b/example/HTTP2.php index 5c7051f..d9b248c 100644 --- a/example/HTTP2.php +++ b/example/HTTP2.php @@ -1,6 +1,6 @@ setTags($this->getEnvTags()); if ('' == $serverName) { - $this->serverName = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'unknow server'; + $this->serverName = $_SERVER['SERVER_NAME'] ?? 'unknow server'; } else { $this->serverName = $serverName; } @@ -231,7 +231,7 @@ public function getEnvTags() if (isset($_SERVER['JAEGER_TAGS']) && '' != $_SERVER['JAEGER_TAGS']) { $envTags = explode(',', $_SERVER['JAEGER_TAGS']); foreach ($envTags as $envK => $envTag) { - list($key, $value) = explode('=', $envTag); + [$key, $value] = explode('=', $envTag); $tags[$key] = $value; } } @@ -255,6 +255,6 @@ public function flush(): void private function generateId() { - return microtime(true) * 10000 .rand(10000, 99999); + return microtime(true) * 10000 .random_int(10000, 99999); } } diff --git a/src/Jaeger/Propagator/JaegerPropagator.php b/src/Jaeger/Propagator/JaegerPropagator.php index e3a3481..adb1964 100644 --- a/src/Jaeger/Propagator/JaegerPropagator.php +++ b/src/Jaeger/Propagator/JaegerPropagator.php @@ -53,7 +53,7 @@ public function extract($format, $carrier) $v = urldecode($v); } if (Constants\Tracer_State_Header_Name == $k) { - list($traceId, $spanId, $parentId, $flags) = explode(':', $v); + [$traceId, $spanId, $parentId, $flags] = explode(':', $v); $spanContext->spanId = $spanContext->hexToSignedInt($spanId); $spanContext->parentId = $spanContext->hexToSignedInt($parentId); diff --git a/src/Jaeger/Sampler/ProbabilisticSampler.php b/src/Jaeger/Sampler/ProbabilisticSampler.php index 420cb0d..5e17f6b 100644 --- a/src/Jaeger/Sampler/ProbabilisticSampler.php +++ b/src/Jaeger/Sampler/ProbabilisticSampler.php @@ -33,7 +33,7 @@ public function __construct($rate = 0.0001) public function IsSampled() { - if (1 == mt_rand(1, 1 / $this->rate)) { + if (1 == random_int(1, 1 / $this->rate)) { return true; } else { return false; diff --git a/src/Jaeger/SpanContext.php b/src/Jaeger/SpanContext.php index c5cea21..e15e58b 100644 --- a/src/Jaeger/SpanContext.php +++ b/src/Jaeger/SpanContext.php @@ -53,7 +53,7 @@ public function __construct($spanId, $parentId, $flags, $baggage = null, $debugI public function getBaggageItem(string $key): ?string { - return isset($this->baggage[$key]) ? $this->baggage[$key] : null; + return $this->baggage[$key] ?? null; } public function withBaggageItem(string $key, string $value): ?\OpenTracing\SpanContext diff --git a/src/Jaeger/Thrift/JaegerThriftSpan.php b/src/Jaeger/Thrift/JaegerThriftSpan.php index 293e42d..6c6058d 100644 --- a/src/Jaeger/Thrift/JaegerThriftSpan.php +++ b/src/Jaeger/Thrift/JaegerThriftSpan.php @@ -24,10 +24,10 @@ class JaegerThriftSpan public function buildJaegerProcessThrift(Jaeger $jaeger) { $tags = []; - $ip = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : '0.0.0.0'; + $ip = $_SERVER['SERVER_ADDR'] ?? '0.0.0.0'; $tags['peer.ipv4'] = $ip; - $port = isset($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : '80'; + $port = $_SERVER['SERVER_PORT'] ?? '80'; $tags['peer.port'] = $port; $tags = array_merge($tags, $jaeger->tags); diff --git a/src/Jaeger/Thrift/Process.php b/src/Jaeger/Thrift/Process.php index 9081fab..1b08b9d 100644 --- a/src/Jaeger/Thrift/Process.php +++ b/src/Jaeger/Thrift/Process.php @@ -30,9 +30,9 @@ class Process implements TStruct public function __construct($processThrift) { - self::$serverName = isset($processThrift['serverName']) ? $processThrift['serverName'] : ''; - self::$thriftTags = isset($processThrift['tags']) ? $processThrift['tags'] : ''; - self::$wrote = isset($processThrift['wrote']) ? $processThrift['wrote'] : ''; + self::$serverName = $processThrift['serverName'] ?? ''; + self::$thriftTags = $processThrift['tags'] ?? ''; + self::$wrote = $processThrift['wrote'] ?? ''; } public function write(TProtocol $t) diff --git a/src/Jaeger/UdpClient.php b/src/Jaeger/UdpClient.php index e5ae4ec..f37b76b 100644 --- a/src/Jaeger/UdpClient.php +++ b/src/Jaeger/UdpClient.php @@ -33,7 +33,7 @@ class UdpClient public function __construct($hostPost, AgentClient $agentClient) { - list($this->host, $this->post) = explode(':', $hostPost); + [$this->host, $this->post] = explode(':', $hostPost); $this->agentClient = $agentClient; $this->socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); } From b92a7db6147eb2a502eb82ba490894bebaee192f Mon Sep 17 00:00:00 2001 From: Xiangxuan Liu Date: Sat, 26 Dec 2020 16:21:49 +0800 Subject: [PATCH 11/32] Add GitHub Action Lint job --- .github/workflows/lint.yaml | 44 +++++++++++++++++++++++++++++++++++++ .github/workflows/test.yaml | 3 ++- composer.json | 3 ++- 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/lint.yaml diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml new file mode 100644 index 0000000..68c79cd --- /dev/null +++ b/.github/workflows/lint.yaml @@ -0,0 +1,44 @@ +name: Lint + +on: [push, pull_request] + +jobs: + lint: + runs-on: ubuntu-latest + strategy: + fail-fast: true + matrix: + php: + - '7.1' + - '7.2' + - '7.3' + - '7.4' + # - '8.0' + dependency-version: [prefer-stable] + name: ${{ matrix.php }} - ${{ matrix.dependency-version }} + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Cache dependencies + uses: actions/cache@v2 + with: + path: ~/.composer/cache/files + key: dependencies-laravel-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + coverage: none + + - name: Install dependencies + run: | + composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction + + - name: Execute PHPStan + run: vendor/bin/phpstan analyse src tests + + - name: PHP CS Fixer Check + run: vendor/bin/php-cs-fixer fix --dry-run --diff 1>&2 diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index c8ee95d..823bc36 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -36,5 +36,6 @@ jobs: - name: Install dependencies run: | composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction + - name: Execute tests - run: ./vendor/bin/phpunit \ No newline at end of file + run: ./vendor/bin/phpunit diff --git a/composer.json b/composer.json index ef3b374..5daf591 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,8 @@ }, "scripts": { "test": "./vendor/bin/phpunit", - "phpcs": "./vendor/bin/php-cs-fixer fix --ansi" + "phpcs": "./vendor/bin/php-cs-fixer fix --ansi", + "phpstan": "./vendor/bin/phpstan analyse src tests" }, "require-dev": { "phpunit/phpunit": "^5", From 936d373e90729f40d07dbceb8686d7d9f9408229 Mon Sep 17 00:00:00 2001 From: Xiangxuan Liu Date: Sat, 26 Dec 2020 16:47:08 +0800 Subject: [PATCH 12/32] Improve PHPDoc --- src/Jaeger/Jaeger.php | 65 ++++++++++++---------- src/Jaeger/Propagator/JaegerPropagator.php | 6 ++ src/Jaeger/Propagator/ZipkinPropagator.php | 6 ++ src/Jaeger/Scope.php | 6 ++ src/Jaeger/ScopeManager.php | 4 ++ src/Jaeger/Span.php | 23 ++++++-- src/Jaeger/SpanContext.php | 9 +++ 7 files changed, 83 insertions(+), 36 deletions(-) diff --git a/src/Jaeger/Jaeger.php b/src/Jaeger/Jaeger.php index d990128..46a9391 100644 --- a/src/Jaeger/Jaeger.php +++ b/src/Jaeger/Jaeger.php @@ -18,15 +18,8 @@ use Jaeger\Propagator\Propagator; use Jaeger\Reporter\Reporter; use Jaeger\Sampler\Sampler; -use OpenTracing\Formats; -use OpenTracing\Reference; -use OpenTracing\ScopeManager; -use OpenTracing\SpanContext; -use OpenTracing\StartSpanOptions; -use OpenTracing\Tracer; -use OpenTracing\UnsupportedFormatException; - -class Jaeger implements Tracer + +class Jaeger implements \OpenTracing\Tracer { private $reporter = null; @@ -34,6 +27,9 @@ class Jaeger implements Tracer private $gen128bit = false; + /** + * @var \Jaeger\ScopeManager + */ private $scopeManager; public $spans = []; @@ -49,7 +45,7 @@ class Jaeger implements Tracer /** @var Propagator|null */ public $propagator = null; - public function __construct($serverName = '', Reporter $reporter, Sampler $sampler, + public function __construct(string $serverName, Reporter $reporter, Sampler $sampler, ScopeManager $scopeManager) { $this->reporter = $reporter; @@ -83,8 +79,8 @@ public function setTags(array $tags = []) */ public function startSpan(string $operationName, $options = []): \OpenTracing\Span { - if (!($options instanceof StartSpanOptions)) { - $options = StartSpanOptions::create($options); + if (!($options instanceof \OpenTracing\StartSpanOptions)) { + $options = \OpenTracing\StartSpanOptions::create($options); } $parentSpan = $this->getParentSpanContext($options); @@ -126,30 +122,26 @@ public function setPropagator(Propagator $propagator) } /** - * 注入. - * - * @param mixed $carrier + * {@inheritDoc} */ - public function inject(SpanContext $spanContext, string $format, &$carrier): void + public function inject(\OpenTracing\SpanContext $spanContext, string $format, &$carrier): void { - if (Formats\TEXT_MAP == $format) { + if (\OpenTracing\Formats\TEXT_MAP == $format) { $this->propagator->inject($spanContext, $format, $carrier); } else { - throw UnsupportedFormatException::forFormat($format); + throw \OpenTracing\UnsupportedFormatException::forFormat($format); } } /** - * 提取. - * - * @param mixed $carrier + * {@inheritDoc} */ - public function extract(string $format, $carrier): ?SpanContext + public function extract(string $format, $carrier): ?\OpenTracing\SpanContext { - if (Formats\TEXT_MAP == $format) { + if (\OpenTracing\Formats\TEXT_MAP == $format) { return $this->propagator->extract($format, $carrier); } else { - throw UnsupportedFormatException::forFormat($format); + throw \OpenTracing\UnsupportedFormatException::forFormat($format); } } @@ -166,11 +158,19 @@ public function reportSpan() } } - public function getScopeManager(): ScopeManager + /** + * {@inheritDoc} + * + * @return \Jaeger\ScopeManager + */ + public function getScopeManager(): \OpenTracing\ScopeManager { return $this->scopeManager; } + /** + * {@inheritDoc} + */ public function getActiveSpan(): ?\OpenTracing\Span { $activeScope = $this->getScopeManager()->getActive(); @@ -181,10 +181,15 @@ public function getActiveSpan(): ?\OpenTracing\Span return $activeScope->getSpan(); } + /** + * {@inheritDoc} + * + * @return Scope + */ public function startActiveSpan(string $operationName, $options = []): \OpenTracing\Scope { - if (!$options instanceof StartSpanOptions) { - $options = StartSpanOptions::create($options); + if (!$options instanceof \OpenTracing\StartSpanOptions) { + $options = \OpenTracing\StartSpanOptions::create($options); } $parentSpan = $this->getParentSpanContext($options); @@ -198,7 +203,7 @@ public function startActiveSpan(string $operationName, $options = []): \OpenTrac return $this->getScopeManager()->activate($span, $options->shouldFinishSpanOnClose()); } - private function getParentSpanContext(StartSpanOptions $options) + private function getParentSpanContext(\OpenTracing\StartSpanOptions $options) { $references = $options->getReferences(); @@ -206,7 +211,7 @@ private function getParentSpanContext(StartSpanOptions $options) foreach ($references as $ref) { $parentSpanContext = $ref->getSpanContext(); - if ($ref->isType(Reference::CHILD_OF)) { + if ($ref->isType(\OpenTracing\Reference::CHILD_OF)) { return $parentSpanContext; } } @@ -245,7 +250,7 @@ public function gen128bit() } /** - * 结束,发送信息到jaeger. + * {@inheritDoc} */ public function flush(): void { diff --git a/src/Jaeger/Propagator/JaegerPropagator.php b/src/Jaeger/Propagator/JaegerPropagator.php index adb1964..3b7051f 100644 --- a/src/Jaeger/Propagator/JaegerPropagator.php +++ b/src/Jaeger/Propagator/JaegerPropagator.php @@ -20,6 +20,9 @@ class JaegerPropagator implements Propagator { + /** + * {@inheritDoc} + */ public function inject(SpanContext $spanContext, $format, &$carrier) { $carrier[strtoupper(Constants\Tracer_State_Header_Name)] = $spanContext->buildString(); @@ -30,6 +33,9 @@ public function inject(SpanContext $spanContext, $format, &$carrier) } } + /** + * {@inheritDoc} + */ public function extract($format, $carrier) { $spanContext = null; diff --git a/src/Jaeger/Propagator/ZipkinPropagator.php b/src/Jaeger/Propagator/ZipkinPropagator.php index c1eae1d..882fefc 100644 --- a/src/Jaeger/Propagator/ZipkinPropagator.php +++ b/src/Jaeger/Propagator/ZipkinPropagator.php @@ -20,6 +20,9 @@ class ZipkinPropagator implements Propagator { + /** + * {@inheritDoc} + */ public function inject(SpanContext $spanContext, $format, &$carrier) { $carrier[Constants\X_B3_TRACEID] = $spanContext->traceIdLowToString(); @@ -28,6 +31,9 @@ public function inject(SpanContext $spanContext, $format, &$carrier) $carrier[Constants\X_B3_SAMPLED] = $spanContext->flagsToString(); } + /** + * {@inheritDoc} + */ public function extract($format, $carrier) { $spanContext = null; diff --git a/src/Jaeger/Scope.php b/src/Jaeger/Scope.php index b0afad7..2d80ed0 100644 --- a/src/Jaeger/Scope.php +++ b/src/Jaeger/Scope.php @@ -31,6 +31,9 @@ public function __construct(ScopeManager $scopeManager, Span $span, $finishSpanO $this->finishSpanOnClose = $finishSpanOnClose; } + /** + * {@inheritDoc} + */ public function close(): void { if ($this->finishSpanOnClose) { @@ -40,6 +43,9 @@ public function close(): void $this->scopeManager->deactivate($this); } + /** + * {@inheritDoc} + */ public function getSpan(): \OpenTracing\Span { return $this->span; diff --git a/src/Jaeger/ScopeManager.php b/src/Jaeger/ScopeManager.php index 638124a..a4c9e41 100644 --- a/src/Jaeger/ScopeManager.php +++ b/src/Jaeger/ScopeManager.php @@ -8,6 +8,8 @@ class ScopeManager implements \OpenTracing\ScopeManager /** * {@inheritDoc} + * + * @return Scope */ public function activate(\OpenTracing\Span $span, bool $finishSpanOnClose = self::DEFAULT_FINISH_SPAN_ON_CLOSE): \OpenTracing\Scope { @@ -19,6 +21,8 @@ public function activate(\OpenTracing\Span $span, bool $finishSpanOnClose = self /** * {@inheritDoc} + * + * @return Scope */ public function getActive(): ?\OpenTracing\Scope { diff --git a/src/Jaeger/Span.php b/src/Jaeger/Span.php index 4e6030a..d7d85be 100644 --- a/src/Jaeger/Span.php +++ b/src/Jaeger/Span.php @@ -45,11 +45,17 @@ public function __construct($operationName, SpanContext $spanContext, $reference $this->references = $references; } + /** + * {@inheritDoc} + */ public function getOperationName(): string { return $this->operationName; } + /** + * {@inheritDoc} + */ public function getContext(): SpanContext { return $this->spanContext; @@ -64,21 +70,24 @@ public function finish($finishTime = null): void $this->duration = $this->finishTime - $this->startTime; } + /** + * {@inheritDoc} + */ public function overwriteOperationName(string $newOperationName): void { $this->operationName = $newOperationName; } + /** + * {@inheritDoc} + */ public function setTag(string $key, $value): void { $this->tags[$key] = $value; } /** - * Adds a log record to the span. - * - * @param array $fields [key => val] - * @param int|float|\DateTimeInterface $timestamp + * {@inheritDoc} */ public function log(array $fields = [], $timestamp = null): void { @@ -88,8 +97,7 @@ public function log(array $fields = [], $timestamp = null): void } /** - * Adds a baggage item to the SpanContext which is immutable so it is required to use SpanContext::withBaggageItem - * to get a new one. + * {@inheritDoc} */ public function addBaggageItem(string $key, string $value): void { @@ -102,6 +110,9 @@ public function addBaggageItem(string $key, string $value): void $this->spanContext->withBaggageItem($key, $value); } + /** + * {@inheritDoc} + */ public function getBaggageItem(string $key): ?string { return $this->spanContext->getBaggageItem($key); diff --git a/src/Jaeger/SpanContext.php b/src/Jaeger/SpanContext.php index e15e58b..0126adc 100644 --- a/src/Jaeger/SpanContext.php +++ b/src/Jaeger/SpanContext.php @@ -51,11 +51,17 @@ public function __construct($spanId, $parentId, $flags, $baggage = null, $debugI $this->debugId = $debugId; } + /** + * {@inheritDoc} + */ public function getBaggageItem(string $key): ?string { return $this->baggage[$key] ?? null; } + /** + * {@inheritDoc} + */ public function withBaggageItem(string $key, string $value): ?\OpenTracing\SpanContext { $this->baggage[$key] = $value; @@ -64,6 +70,9 @@ public function withBaggageItem(string $key, string $value): ?\OpenTracing\SpanC return null; } + /** + * {@inheritDoc} + */ public function getIterator() { // @phpstan-ignore-next-line From 34d6051d3b79fbe18ad0ff08343d9d65ca109f63 Mon Sep 17 00:00:00 2001 From: Xiangxuan Liu Date: Sat, 26 Dec 2020 17:00:50 +0800 Subject: [PATCH 13/32] Add span property type doc --- src/Jaeger/Span.php | 37 +++++++++++++++++++++----- src/Jaeger/Thrift/JaegerThriftSpan.php | 2 ++ tests/JaegerTest.php | 2 +- tests/SpanTest.php | 2 +- 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/Jaeger/Span.php b/src/Jaeger/Span.php index d7d85be..df34ee6 100644 --- a/src/Jaeger/Span.php +++ b/src/Jaeger/Span.php @@ -15,29 +15,54 @@ namespace Jaeger; -use OpenTracing\SpanContext; - class Span implements \OpenTracing\Span { + /** + * @var string + */ private $operationName = ''; - public $startTime = ''; + /** + * @var int|null + */ + public $startTime = null; - public $finishTime = ''; + /** + * @var int|null + */ + public $finishTime = null; + /** + * @var string + */ public $spanKind = ''; + /** + * @var \OpenTracing\SpanContext|null + */ public $spanContext = null; + /** + * @var int + */ public $duration = 0; + /** + * @var array + */ public $logs = []; + /** + * @var array + */ public $tags = []; + /** + * @var array + */ public $references = []; - public function __construct($operationName, SpanContext $spanContext, $references, $startTime = null) + public function __construct($operationName, \OpenTracing\SpanContext $spanContext, $references, $startTime = null) { $this->operationName = $operationName; $this->startTime = null == $startTime ? $this->microtimeToInt() : $startTime; @@ -56,7 +81,7 @@ public function getOperationName(): string /** * {@inheritDoc} */ - public function getContext(): SpanContext + public function getContext(): \OpenTracing\SpanContext { return $this->spanContext; } diff --git a/src/Jaeger/Thrift/JaegerThriftSpan.php b/src/Jaeger/Thrift/JaegerThriftSpan.php index 6c6058d..f42f7fe 100644 --- a/src/Jaeger/Thrift/JaegerThriftSpan.php +++ b/src/Jaeger/Thrift/JaegerThriftSpan.php @@ -46,6 +46,8 @@ public function buildJaegerProcessThrift(Jaeger $jaeger) public function buildJaegerSpanThrift(Span $span) { $spContext = $span->spanContext; + assert($spContext instanceof \Jaeger\SpanContext); + $thriftSpan = [ 'traceIdLow' => $spContext->traceIdLow, 'traceIdHigh' => $spContext->traceIdHigh, diff --git a/tests/JaegerTest.php b/tests/JaegerTest.php index 1c3096c..301cdbd 100644 --- a/tests/JaegerTest.php +++ b/tests/JaegerTest.php @@ -113,7 +113,7 @@ public function testStartSpan() { $Jaeger = $this->getJaeger(); $span = $Jaeger->startSpan('test'); - $this->assertNotEmpty($span->startTime); + $this->assertNotNull($span->startTime); $this->assertNotEmpty($Jaeger->getSpans()); } diff --git a/tests/SpanTest.php b/tests/SpanTest.php index deab711..9a86d9a 100644 --- a/tests/SpanTest.php +++ b/tests/SpanTest.php @@ -41,7 +41,7 @@ public function testFinish() $span = new Span('test', new NoopSpanContext(), []); $span->setTag('test', 'test'); $span->finish(); - $this->assertTrue(!empty($span->finishTime) && !empty($span->duration)); + $this->assertTrue(null !== $span->finishTime && null !== $span->duration); } public function testGetContext() From e151e519bbed7dd1b4f59d48d17f7d4a8e3d5fde Mon Sep 17 00:00:00 2001 From: Xiangxuan Liu <9570112+nauxliu@users.noreply.github.com> Date: Sat, 26 Dec 2020 17:44:50 +0800 Subject: [PATCH 14/32] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0fce5eb..aab905d 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ [![Coverage Status](https://coveralls.io/repos/github/jukylin/jaeger-php/badge.svg?branch=master)](https://coveralls.io/github/jukylin/jaeger-php?branch=master) ## TODO -- [] Implement the latest opentracing/opentracing -- [] Improve documentation -- [] Refactor thrift -- [] Improve unit test coverage +- [x] Implement the latest opentracing/opentracing +- [ ] Improve documentation +- [ ] Refactor thrift +- [ ] Improve unit test coverage From 4294caa820255c09094cc10ec62c4fb32903e742 Mon Sep 17 00:00:00 2001 From: Xiangxuan Liu Date: Sat, 26 Dec 2020 20:06:25 +0800 Subject: [PATCH 15/32] Fix call to undefined on Reference --- src/Jaeger/Thrift/JaegerThriftSpan.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Jaeger/Thrift/JaegerThriftSpan.php b/src/Jaeger/Thrift/JaegerThriftSpan.php index f42f7fe..2626d58 100644 --- a/src/Jaeger/Thrift/JaegerThriftSpan.php +++ b/src/Jaeger/Thrift/JaegerThriftSpan.php @@ -90,6 +90,11 @@ private function buildLogs($logs) return $resultLogs; } + /** + * @param array $references + * + * @return array + */ private function buildReferences($references) { $spanRef = []; @@ -101,7 +106,10 @@ private function buildReferences($references) } else { throw new \LogicException('Unsupported reference type'); } - $ctx = $ref->getContext(); + + $ctx = $ref->getSpanContext(); + assert($ctx instanceof \Jaeger\SpanContext); + $spanRef[] = [ 'refType' => $type, 'traceIdLow' => $ctx->traceIdLow, From 3e44d9aa9dde25a28efbfa4a94821ebce79282ea Mon Sep 17 00:00:00 2001 From: Xiangxuan Liu Date: Sat, 26 Dec 2020 20:24:31 +0800 Subject: [PATCH 16/32] Require ext-json --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index 5daf591..2d9c9db 100644 --- a/composer.json +++ b/composer.json @@ -6,6 +6,7 @@ "minimum-stability": "stable", "require": { "php": ">=7.1", + "ext-json": "*", "packaged/thrift" : "~0.13.0", "opentracing/opentracing" : "^1.0.0" }, From 7a9ddf8a7f7380e2ed908524ddbc5627e49f66da Mon Sep 17 00:00:00 2001 From: Xiangxuan Liu Date: Sat, 26 Dec 2020 20:24:50 +0800 Subject: [PATCH 17/32] Code style improvements --- src/Jaeger/Thrift/JaegerThriftSpan.php | 3 +-- src/Jaeger/Thrift/Process.php | 12 ++++++++++++ src/Jaeger/Thrift/Tags.php | 8 +------- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/Jaeger/Thrift/JaegerThriftSpan.php b/src/Jaeger/Thrift/JaegerThriftSpan.php index 2626d58..a31124e 100644 --- a/src/Jaeger/Thrift/JaegerThriftSpan.php +++ b/src/Jaeger/Thrift/JaegerThriftSpan.php @@ -69,9 +69,8 @@ private function buildTags($tags) { $tagsObj = Tags::getInstance(); $tagsObj->setTags($tags); - $resultTags = $tagsObj->buildTags(); - return $resultTags; + return $tagsObj->buildTags(); } private function buildLogs($logs) diff --git a/src/Jaeger/Thrift/Process.php b/src/Jaeger/Thrift/Process.php index 1b08b9d..7ce98b0 100644 --- a/src/Jaeger/Thrift/Process.php +++ b/src/Jaeger/Thrift/Process.php @@ -20,12 +20,24 @@ class Process implements TStruct { + /** + * @var TProtocol + */ public static $tptl = null; + /** + * @var string + */ public static $serverName = ''; + /** + * @var array + */ public static $thriftTags = []; + /** + * @var string + */ public static $wrote = ''; public function __construct($processThrift) diff --git a/src/Jaeger/Thrift/Tags.php b/src/Jaeger/Thrift/Tags.php index 813add0..34b0b64 100644 --- a/src/Jaeger/Thrift/Tags.php +++ b/src/Jaeger/Thrift/Tags.php @@ -142,14 +142,8 @@ public function buildTags() 'vBool' => $v, ]; break; - case 'double': - $thriftTags[] = [ - 'key' => $k, - 'vType' => 'DOUBLE', - 'vDouble' => $v, - ]; - break; case 'integer': + case 'double': $thriftTags[] = [ 'key' => $k, 'vType' => 'DOUBLE', From c00b24378cb9797a038594494b18506ec7428a39 Mon Sep 17 00:00:00 2001 From: "742161455@qq.com" <742161455@qq.com> Date: Tue, 29 Dec 2020 17:45:49 +0800 Subject: [PATCH 18/32] Refactor thrift --- README.md | 2 +- example/HTTP.php | 26 +- example/HTTP2.php | 2 +- src/Jaeger/Config.php | 14 +- src/Jaeger/Jaeger.php | 14 +- src/Jaeger/JaegerThrift.php | 164 +++++ src/Jaeger/Propagator/JaegerPropagator.php | 2 +- src/Jaeger/Propagator/Propagator.php | 2 +- src/Jaeger/Reporter/RemoteReporter.php | 3 + src/Jaeger/Thrift/Agent/AgentClient.php | 86 +++ src/Jaeger/Thrift/Agent/AgentIf.php | 29 + .../Thrift/Agent/Agent_emitBatch_args.php | 99 ++++ .../Agent/Agent_emitZipkinBatch_args.php | 116 ++++ .../Agent/AggregationValidatorClient.php | 91 +++ .../Thrift/Agent/AggregationValidatorIf.php | 26 + ...ggregationValidator_validateTrace_args.php | 94 +++ ...regationValidator_validateTrace_result.php | 99 ++++ .../Thrift/Agent/BaggageRestriction.php | 118 ++++ .../Agent/BaggageRestrictionManagerClient.php | 91 +++ .../Agent/BaggageRestrictionManagerIf.php | 30 + ...ionManager_getBaggageRestrictions_args.php | 94 +++ ...nManager_getBaggageRestrictions_result.php | 116 ++++ src/Jaeger/Thrift/Agent/Dependencies.php | 116 ++++ src/Jaeger/Thrift/Agent/DependencyClient.php | 118 ++++ src/Jaeger/Thrift/Agent/DependencyIf.php | 30 + src/Jaeger/Thrift/Agent/DependencyLink.php | 142 +++++ ...ependency_getDependenciesForTrace_args.php | 94 +++ ...endency_getDependenciesForTrace_result.php | 99 ++++ .../Dependency_saveDependencies_args.php | 99 ++++ .../Agent/OperationSamplingStrategy.php | 123 ++++ .../Agent/PerOperationSamplingStrategies.php | 188 ++++++ .../Agent/ProbabilisticSamplingStrategy.php | 94 +++ .../Agent/RateLimitingSamplingStrategy.php | 94 +++ .../Thrift/Agent/SamplingManagerClient.php | 91 +++ src/Jaeger/Thrift/Agent/SamplingManagerIf.php | 26 + ...mplingManager_getSamplingStrategy_args.php | 94 +++ ...lingManager_getSamplingStrategy_result.php | 99 ++++ .../Thrift/Agent/SamplingStrategyResponse.php | 181 ++++++ .../Thrift/Agent/SamplingStrategyType.php | 30 + .../Thrift/Agent/ValidateTraceResponse.php | 118 ++++ src/Jaeger/Thrift/Agent/Zipkin/Annotation.php | 159 +++++ .../Thrift/Agent/Zipkin/AnnotationType.php | 45 ++ .../Thrift/Agent/Zipkin/BinaryAnnotation.php | 193 ++++++ src/Jaeger/Thrift/Agent/Zipkin/Constant.php | 261 ++++++++ src/Jaeger/Thrift/Agent/Zipkin/Endpoint.php | 191 ++++++ src/Jaeger/Thrift/Agent/Zipkin/Response.php | 94 +++ src/Jaeger/Thrift/Agent/Zipkin/Span.php | 397 +++++++++++++ .../Agent/Zipkin/ZipkinCollectorClient.php | 91 +++ .../Thrift/Agent/Zipkin/ZipkinCollectorIf.php | 26 + ...ZipkinCollector_submitZipkinBatch_args.php | 116 ++++ ...pkinCollector_submitZipkinBatch_result.php | 116 ++++ src/Jaeger/Thrift/AgentClient.php | 82 --- src/Jaeger/Thrift/Batch.php | 198 +++++++ src/Jaeger/Thrift/BatchSubmitResponse.php | 94 +++ src/Jaeger/Thrift/ClientStats.php | 142 +++++ src/Jaeger/Thrift/CollectorClient.php | 91 +++ src/Jaeger/Thrift/CollectorIf.php | 26 + .../Thrift/Collector_submitBatches_args.php | 116 ++++ .../Thrift/Collector_submitBatches_result.php | 116 ++++ src/Jaeger/Thrift/Crossdock/Downstream.php | 219 +++++++ .../Thrift/Crossdock/JoinTraceRequest.php | 123 ++++ src/Jaeger/Thrift/Crossdock/ObservedSpan.php | 142 +++++ .../Thrift/Crossdock/StartTraceRequest.php | 171 ++++++ src/Jaeger/Thrift/Crossdock/TraceResponse.php | 158 +++++ .../Thrift/Crossdock/TracedServiceClient.php | 150 +++++ .../Thrift/Crossdock/TracedServiceIf.php | 39 ++ .../TracedService_joinTrace_args.php | 99 ++++ .../TracedService_joinTrace_result.php | 99 ++++ .../TracedService_startTrace_args.php | 99 ++++ .../TracedService_startTrace_result.php | 99 ++++ src/Jaeger/Thrift/Crossdock/Transport.php | 33 ++ src/Jaeger/Thrift/JaegerThriftSpan.php | 122 ---- src/Jaeger/Thrift/Log.php | 140 +++++ src/Jaeger/Thrift/Process.php | 181 +++--- src/Jaeger/Thrift/Span.php | 561 ++++++++++++------ src/Jaeger/Thrift/SpanRef.php | 166 ++++++ src/Jaeger/Thrift/SpanRefType.php | 33 +- src/Jaeger/Thrift/TStruct.php | 25 - src/Jaeger/Thrift/Tag.php | 238 ++++++++ src/Jaeger/Thrift/TagType.php | 39 ++ src/Jaeger/Thrift/Tags.php | 171 ------ src/Jaeger/Thrift/Types.php | 61 -- src/Jaeger/Transport/TransportUdp.php | 135 +++-- src/Jaeger/UdpClient.php | 32 +- tests/Transport/TransportUdpTest.php | 53 +- tests/UdpClientTest.php | 99 +++- 86 files changed, 8339 insertions(+), 856 deletions(-) create mode 100644 src/Jaeger/JaegerThrift.php create mode 100644 src/Jaeger/Thrift/Agent/AgentClient.php create mode 100644 src/Jaeger/Thrift/Agent/AgentIf.php create mode 100644 src/Jaeger/Thrift/Agent/Agent_emitBatch_args.php create mode 100644 src/Jaeger/Thrift/Agent/Agent_emitZipkinBatch_args.php create mode 100644 src/Jaeger/Thrift/Agent/AggregationValidatorClient.php create mode 100644 src/Jaeger/Thrift/Agent/AggregationValidatorIf.php create mode 100644 src/Jaeger/Thrift/Agent/AggregationValidator_validateTrace_args.php create mode 100644 src/Jaeger/Thrift/Agent/AggregationValidator_validateTrace_result.php create mode 100644 src/Jaeger/Thrift/Agent/BaggageRestriction.php create mode 100644 src/Jaeger/Thrift/Agent/BaggageRestrictionManagerClient.php create mode 100644 src/Jaeger/Thrift/Agent/BaggageRestrictionManagerIf.php create mode 100644 src/Jaeger/Thrift/Agent/BaggageRestrictionManager_getBaggageRestrictions_args.php create mode 100644 src/Jaeger/Thrift/Agent/BaggageRestrictionManager_getBaggageRestrictions_result.php create mode 100644 src/Jaeger/Thrift/Agent/Dependencies.php create mode 100644 src/Jaeger/Thrift/Agent/DependencyClient.php create mode 100644 src/Jaeger/Thrift/Agent/DependencyIf.php create mode 100644 src/Jaeger/Thrift/Agent/DependencyLink.php create mode 100644 src/Jaeger/Thrift/Agent/Dependency_getDependenciesForTrace_args.php create mode 100644 src/Jaeger/Thrift/Agent/Dependency_getDependenciesForTrace_result.php create mode 100644 src/Jaeger/Thrift/Agent/Dependency_saveDependencies_args.php create mode 100644 src/Jaeger/Thrift/Agent/OperationSamplingStrategy.php create mode 100644 src/Jaeger/Thrift/Agent/PerOperationSamplingStrategies.php create mode 100644 src/Jaeger/Thrift/Agent/ProbabilisticSamplingStrategy.php create mode 100644 src/Jaeger/Thrift/Agent/RateLimitingSamplingStrategy.php create mode 100644 src/Jaeger/Thrift/Agent/SamplingManagerClient.php create mode 100644 src/Jaeger/Thrift/Agent/SamplingManagerIf.php create mode 100644 src/Jaeger/Thrift/Agent/SamplingManager_getSamplingStrategy_args.php create mode 100644 src/Jaeger/Thrift/Agent/SamplingManager_getSamplingStrategy_result.php create mode 100644 src/Jaeger/Thrift/Agent/SamplingStrategyResponse.php create mode 100644 src/Jaeger/Thrift/Agent/SamplingStrategyType.php create mode 100644 src/Jaeger/Thrift/Agent/ValidateTraceResponse.php create mode 100644 src/Jaeger/Thrift/Agent/Zipkin/Annotation.php create mode 100644 src/Jaeger/Thrift/Agent/Zipkin/AnnotationType.php create mode 100644 src/Jaeger/Thrift/Agent/Zipkin/BinaryAnnotation.php create mode 100644 src/Jaeger/Thrift/Agent/Zipkin/Constant.php create mode 100644 src/Jaeger/Thrift/Agent/Zipkin/Endpoint.php create mode 100644 src/Jaeger/Thrift/Agent/Zipkin/Response.php create mode 100644 src/Jaeger/Thrift/Agent/Zipkin/Span.php create mode 100644 src/Jaeger/Thrift/Agent/Zipkin/ZipkinCollectorClient.php create mode 100644 src/Jaeger/Thrift/Agent/Zipkin/ZipkinCollectorIf.php create mode 100644 src/Jaeger/Thrift/Agent/Zipkin/ZipkinCollector_submitZipkinBatch_args.php create mode 100644 src/Jaeger/Thrift/Agent/Zipkin/ZipkinCollector_submitZipkinBatch_result.php delete mode 100644 src/Jaeger/Thrift/AgentClient.php create mode 100644 src/Jaeger/Thrift/Batch.php create mode 100644 src/Jaeger/Thrift/BatchSubmitResponse.php create mode 100644 src/Jaeger/Thrift/ClientStats.php create mode 100644 src/Jaeger/Thrift/CollectorClient.php create mode 100644 src/Jaeger/Thrift/CollectorIf.php create mode 100644 src/Jaeger/Thrift/Collector_submitBatches_args.php create mode 100644 src/Jaeger/Thrift/Collector_submitBatches_result.php create mode 100644 src/Jaeger/Thrift/Crossdock/Downstream.php create mode 100644 src/Jaeger/Thrift/Crossdock/JoinTraceRequest.php create mode 100644 src/Jaeger/Thrift/Crossdock/ObservedSpan.php create mode 100644 src/Jaeger/Thrift/Crossdock/StartTraceRequest.php create mode 100644 src/Jaeger/Thrift/Crossdock/TraceResponse.php create mode 100644 src/Jaeger/Thrift/Crossdock/TracedServiceClient.php create mode 100644 src/Jaeger/Thrift/Crossdock/TracedServiceIf.php create mode 100644 src/Jaeger/Thrift/Crossdock/TracedService_joinTrace_args.php create mode 100644 src/Jaeger/Thrift/Crossdock/TracedService_joinTrace_result.php create mode 100644 src/Jaeger/Thrift/Crossdock/TracedService_startTrace_args.php create mode 100644 src/Jaeger/Thrift/Crossdock/TracedService_startTrace_result.php create mode 100644 src/Jaeger/Thrift/Crossdock/Transport.php delete mode 100644 src/Jaeger/Thrift/JaegerThriftSpan.php create mode 100644 src/Jaeger/Thrift/Log.php create mode 100644 src/Jaeger/Thrift/SpanRef.php delete mode 100644 src/Jaeger/Thrift/TStruct.php create mode 100644 src/Jaeger/Thrift/Tag.php create mode 100644 src/Jaeger/Thrift/TagType.php delete mode 100644 src/Jaeger/Thrift/Tags.php delete mode 100644 src/Jaeger/Thrift/Types.php diff --git a/README.md b/README.md index aab905d..910458b 100644 --- a/README.md +++ b/README.md @@ -8,5 +8,5 @@ ## TODO - [x] Implement the latest opentracing/opentracing - [ ] Improve documentation -- [ ] Refactor thrift +- [x] Refactor thrift - [ ] Improve unit test coverage diff --git a/example/HTTP.php b/example/HTTP.php index 9dc3e17..1cecd19 100644 --- a/example/HTTP.php +++ b/example/HTTP.php @@ -13,7 +13,7 @@ * the License. */ -require_once dirname(__FILE__, 4).'/autoload.php'; +require_once dirname(__FILE__, 2).'/vendor/autoload.php'; use GuzzleHttp\Client; use Jaeger\Config; @@ -24,17 +24,23 @@ //init server span start $config = Config::getInstance(); + $config->gen128bit(); -$config::$propagator = Jaeger\Constants\PROPAGATOR_ZIPKIN; +$config::$propagator = Jaeger\Constants\PROPAGATOR_JAEGER; -$tracer = $config->initTracer('example', '0.0.0.0:6831'); +$tracer = $config->initTracer('example', 'localhost:6831'); $injectTarget = []; $spanContext = $tracer->extract(Formats\TEXT_MAP, $_SERVER); -$serverSpan = $tracer->startSpan('example HTTP', ['child_of' => $spanContext]); -$serverSpan->addBaggageItem('version', '1.8.9'); -print_r($serverSpan->getContext()); + +$options = []; +if($spanContext != null) { + $options = ['child_of' => $spanContext]; +} + +$serverSpan = $tracer->startSpan('HTTP', $options); +//$serverSpan->addBaggageItem('version', '1.8.9'); $tracer->inject($serverSpan->getContext(), Formats\TEXT_MAP, $_SERVER); //init server span end @@ -55,7 +61,7 @@ $clientSpan1->setTag('http.method', 'GET'); $clientSpan1->setTag('http.url', $url); -$clientSpan1->log(['message' => 'HTTP1 '.$method.' '.$url.' end !']); +//$clientSpan1->log(['message' => 'HTTP1 '.$method.' '.$url.' end !']); $clientSpan1->finish(); //client span1 end @@ -64,8 +70,8 @@ $spanContext = $clientTracer->extract(Formats\TEXT_MAP, $_SERVER); $clientSpan2 = $clientTracer->startSpan('HTTP2', ['references' => [ - Reference::createForSpan(Reference::FOLLOWS_FROM, $clientSpan1->spanContext), - Reference::createForSpan(Reference::CHILD_OF, $spanContext), + //Reference::createForSpan(Reference::FOLLOWS_FROM, $clientSpan1), + Reference::createForSpan(Reference::CHILD_OF, $clientSpan1), ]]); $clientTracer->inject($clientSpan2->spanContext, Formats\TEXT_MAP, $injectTarget2); @@ -79,7 +85,7 @@ $clientSpan2->setTag('http.method', 'GET'); $clientSpan2->setTag('http.url', $url); -$clientSpan2->log(['message' => 'HTTP2 '.$method.' '.$url.' end !']); +//$clientSpan2->log(['message' => 'HTTP2 '.$method.' '.$url.' end !']); $clientSpan2->finish(); //client span2 end diff --git a/example/HTTP2.php b/example/HTTP2.php index d9b248c..0229d84 100644 --- a/example/HTTP2.php +++ b/example/HTTP2.php @@ -1,6 +1,6 @@ transport) { @@ -101,7 +101,7 @@ public function initTracer(string $serverName, $agentHostPort = ''): Tracer $this->scopeManager = new ScopeManager(); } - $tracer = new Jaeger($serverName, $this->reporter, $this->sampler, $this->scopeManager); + $tracer = new Jaeger($serviceName, $this->reporter, $this->sampler, $this->scopeManager); if (true == $this->gen128bit) { $tracer->gen128bit(); @@ -113,7 +113,7 @@ public function initTracer(string $serverName, $agentHostPort = ''): Tracer $tracer->setPropagator(new JaegerPropagator()); } - self::$tracer[$serverName] = $tracer; + self::$tracer[$serviceName] = $tracer; return $tracer; } diff --git a/src/Jaeger/Jaeger.php b/src/Jaeger/Jaeger.php index 46a9391..1335b26 100644 --- a/src/Jaeger/Jaeger.php +++ b/src/Jaeger/Jaeger.php @@ -36,16 +36,12 @@ class Jaeger implements \OpenTracing\Tracer public $tags = []; - public $process = null; - - public $serverName = ''; - - public $processThrift = ''; + public $serviceName = ''; /** @var Propagator|null */ public $propagator = null; - public function __construct(string $serverName, Reporter $reporter, Sampler $sampler, + public function __construct(string $serviceName, Reporter $reporter, Sampler $sampler, ScopeManager $scopeManager) { $this->reporter = $reporter; @@ -57,10 +53,10 @@ public function __construct(string $serverName, Reporter $reporter, Sampler $sam $this->setTags($this->sampler->getTags()); $this->setTags($this->getEnvTags()); - if ('' == $serverName) { - $this->serverName = $_SERVER['SERVER_NAME'] ?? 'unknow server'; + if ('' == $serviceName) { + $this->serviceName = $_SERVER['SERVER_NAME'] ?? 'unknow server'; } else { - $this->serverName = $serverName; + $this->serviceName = $serviceName; } } diff --git a/src/Jaeger/JaegerThrift.php b/src/Jaeger/JaegerThrift.php new file mode 100644 index 0000000..47895f8 --- /dev/null +++ b/src/Jaeger/JaegerThrift.php @@ -0,0 +1,164 @@ +tags); + + $processThrift = new Process([ + 'serviceName' => $jaeger->serviceName, + 'tags' => $this->buildTags($tags), + ]); + + return $processThrift; + } + + public function buildSpanThrift(jspan $span): Span + { + $spContext = $span->spanContext; + assert($spContext instanceof \Jaeger\SpanContext); + + $spanVals = [ + 'traceIdLow' => intval($spContext->traceIdLow), + 'traceIdHigh' => intval($spContext->traceIdHigh), + 'spanId' => intval($spContext->spanId), + 'parentSpanId' => intval($spContext->parentId), + 'operationName' => $span->getOperationName(), + 'flags' => intval($spContext->flags), + 'startTime' => $span->startTime, + 'duration' => $span->duration, + 'tags' => $this->buildTags($span->tags), + 'logs' => $this->buildLogs($span->logs), + ]; + + if($spContext->parentId != 0) { + $spanVals['references'] = $this->buildReferences($span->references); + } + + $thriftSpan = new Span($spanVals); + + return $thriftSpan; + } + + private function buildLogs(array $logs): array + { + $resultLogs = []; + foreach ($logs as $log) { + $resultLogs[] = new Log([ + 'timestamp' => $log['timestamp'], + 'fields' => $this->buildTags($log['fields']), + ]); + } + + return $resultLogs; + } + + /** + * @param array $references + * + * @return array + */ + private function buildReferences(array $references): array + { + $spanRef = []; + foreach ($references as $ref) { + if ($ref->isType(Reference::CHILD_OF)) { + $type = SpanRefType::CHILD_OF; + } elseif ($ref->isType(Reference::FOLLOWS_FROM)) { + $type = SpanRefType::FOLLOWS_FROM; + } else { + throw new \LogicException('Unsupported reference type'); + } + + $ctx = $ref->getSpanContext(); + assert($ctx instanceof \Jaeger\SpanContext); + + $spanRef[] = new SpanRef([ + 'refType' => $type, + 'traceIdLow' => intval($ctx->traceIdLow), + 'traceIdHigh' => intval($ctx->traceIdHigh), + 'spanId' => intval($ctx->spanId), + ]); + } + + return $spanRef; + } + + + public function buildTags(array $tags): array + { + $jTags = []; + if (empty($tags)) { + return $jTags; + } + + foreach ($tags as $k => $v) { + switch (gettype($v)) { + case 'boolean': + $jTags[] = new Tag([ + 'key' => $k, + 'vType' => TagType::BOOL, + 'vBool' => $v, + ]); + break; + case 'integer': + $jTags[] = new Tag([ + 'key' => $k, + 'vType' => TagType::LONG, + 'vDouble' => $v, + ]); + break; + case 'double': + $jTags[] = new Tag([ + 'key' => $k, + 'vType' => TagType::DOUBLE, + 'vDouble' => $v, + ]); + break; + case 'array': + $v = json_encode($v, JSON_UNESCAPED_UNICODE); + default: + $jTags[] = new Tag([ + 'key' => $k, + 'vType' => TagType::STRING, + 'vStr' => $v, + ]); + } + } + + return $jTags; + } +} diff --git a/src/Jaeger/Propagator/JaegerPropagator.php b/src/Jaeger/Propagator/JaegerPropagator.php index 3b7051f..ea19ea8 100644 --- a/src/Jaeger/Propagator/JaegerPropagator.php +++ b/src/Jaeger/Propagator/JaegerPropagator.php @@ -23,7 +23,7 @@ class JaegerPropagator implements Propagator /** * {@inheritDoc} */ - public function inject(SpanContext $spanContext, $format, &$carrier) + public function inject(\OpenTracing\SpanContext $spanContext, $format, &$carrier) { $carrier[strtoupper(Constants\Tracer_State_Header_Name)] = $spanContext->buildString(); if ($spanContext->baggage) { diff --git a/src/Jaeger/Propagator/Propagator.php b/src/Jaeger/Propagator/Propagator.php index 34c1549..4ffb0b1 100644 --- a/src/Jaeger/Propagator/Propagator.php +++ b/src/Jaeger/Propagator/Propagator.php @@ -25,7 +25,7 @@ interface Propagator * @param string $format * @param mixed $carrier */ - public function inject(SpanContext $spanContext, $format, &$carrier); + public function inject(\OpenTracing\SpanContext $spanContext, $format, &$carrier); /** * 提取. diff --git a/src/Jaeger/Reporter/RemoteReporter.php b/src/Jaeger/Reporter/RemoteReporter.php index 4a495dc..3bec330 100644 --- a/src/Jaeger/Reporter/RemoteReporter.php +++ b/src/Jaeger/Reporter/RemoteReporter.php @@ -20,6 +20,9 @@ class RemoteReporter implements Reporter { + /** + * @var Transport|null + */ public $tran = null; public function __construct(Transport $tran) diff --git a/src/Jaeger/Thrift/Agent/AgentClient.php b/src/Jaeger/Thrift/Agent/AgentClient.php new file mode 100644 index 0000000..ff7c4b1 --- /dev/null +++ b/src/Jaeger/Thrift/Agent/AgentClient.php @@ -0,0 +1,86 @@ +input_ = $input; + $this->output_ = $output ? $output : $input; + } + + + public function emitZipkinBatch(array $spans) + { + $this->send_emitZipkinBatch($spans); + } + + public function send_emitZipkinBatch(array $spans) + { + $args = new \Jaeger\Thrift\Agent\Agent_emitZipkinBatch_args(); + $args->spans = $spans; + $bin_accel = ($this->output_ instanceof TBinaryProtocolAccelerated) && function_exists('thrift_protocol_write_binary'); + if ($bin_accel) { + thrift_protocol_write_binary( + $this->output_, + 'emitZipkinBatch', + TMessageType::ONEWAY, + $args, + $this->seqid_, + $this->output_->isStrictWrite() + ); + } else { + $this->output_->writeMessageBegin('emitZipkinBatch', TMessageType::ONEWAY, $this->seqid_); + $args->write($this->output_); + $this->output_->writeMessageEnd(); + $this->output_->getTransport()->flush(); + } + } + + public function emitBatch(\Jaeger\Thrift\Batch $batch) + { + $this->send_emitBatch($batch); + } + + public function send_emitBatch(\Jaeger\Thrift\Batch $batch) + { + $args = new \Jaeger\Thrift\Agent\Agent_emitBatch_args(); + $args->batch = $batch; + $bin_accel = ($this->output_ instanceof TBinaryProtocolAccelerated) && function_exists('thrift_protocol_write_binary'); + if ($bin_accel) { + thrift_protocol_write_binary( + $this->output_, + 'emitBatch', + TMessageType::ONEWAY, + $args, + $this->seqid_, + $this->output_->isStrictWrite() + ); + } else { + $this->output_->writeMessageBegin('emitBatch', TMessageType::ONEWAY, $this->seqid_); + $args->write($this->output_); + $this->output_->writeMessageEnd(); + $this->output_->getTransport()->flush(); + } + } +} diff --git a/src/Jaeger/Thrift/Agent/AgentIf.php b/src/Jaeger/Thrift/Agent/AgentIf.php new file mode 100644 index 0000000..b022128 --- /dev/null +++ b/src/Jaeger/Thrift/Agent/AgentIf.php @@ -0,0 +1,29 @@ + array( + 'var' => 'batch', + 'isRequired' => false, + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Batch', + ), + ); + + /** + * @var \Jaeger\Thrift\Batch + */ + public $batch = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['batch'])) { + $this->batch = $vals['batch']; + } + } + } + + public function getName() + { + return 'Agent_emitBatch_args'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::STRUCT) { + $this->batch = new \Jaeger\Thrift\Batch(); + $xfer += $this->batch->read($input); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('Agent_emitBatch_args'); + if ($this->batch !== null) { + if (!is_object($this->batch)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('batch', TType::STRUCT, 1); + $xfer += $this->batch->write($output); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Agent/Agent_emitZipkinBatch_args.php b/src/Jaeger/Thrift/Agent/Agent_emitZipkinBatch_args.php new file mode 100644 index 0000000..b91d2c2 --- /dev/null +++ b/src/Jaeger/Thrift/Agent/Agent_emitZipkinBatch_args.php @@ -0,0 +1,116 @@ + array( + 'var' => 'spans', + 'isRequired' => false, + 'type' => TType::LST, + 'etype' => TType::STRUCT, + 'elem' => array( + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Agent\Zipkin\Span', + ), + ), + ); + + /** + * @var \Jaeger\Thrift\Agent\Zipkin\Span[] + */ + public $spans = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['spans'])) { + $this->spans = $vals['spans']; + } + } + } + + public function getName() + { + return 'Agent_emitZipkinBatch_args'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::LST) { + $this->spans = array(); + $_size0 = 0; + $_etype3 = 0; + $xfer += $input->readListBegin($_etype3, $_size0); + for ($_i4 = 0; $_i4 < $_size0; ++$_i4) { + $elem5 = null; + $elem5 = new \Jaeger\Thrift\Agent\Zipkin\Span(); + $xfer += $elem5->read($input); + $this->spans []= $elem5; + } + $xfer += $input->readListEnd(); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('Agent_emitZipkinBatch_args'); + if ($this->spans !== null) { + if (!is_array($this->spans)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('spans', TType::LST, 1); + $output->writeListBegin(TType::STRUCT, count($this->spans)); + foreach ($this->spans as $iter6) { + $xfer += $iter6->write($output); + } + $output->writeListEnd(); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Agent/AggregationValidatorClient.php b/src/Jaeger/Thrift/Agent/AggregationValidatorClient.php new file mode 100644 index 0000000..4b3a1b2 --- /dev/null +++ b/src/Jaeger/Thrift/Agent/AggregationValidatorClient.php @@ -0,0 +1,91 @@ +input_ = $input; + $this->output_ = $output ? $output : $input; + } + + + public function validateTrace($traceId) + { + $this->send_validateTrace($traceId); + return $this->recv_validateTrace(); + } + + public function send_validateTrace($traceId) + { + $args = new \Jaeger\Thrift\Agent\AggregationValidator_validateTrace_args(); + $args->traceId = $traceId; + $bin_accel = ($this->output_ instanceof TBinaryProtocolAccelerated) && function_exists('thrift_protocol_write_binary'); + if ($bin_accel) { + thrift_protocol_write_binary( + $this->output_, + 'validateTrace', + TMessageType::CALL, + $args, + $this->seqid_, + $this->output_->isStrictWrite() + ); + } else { + $this->output_->writeMessageBegin('validateTrace', TMessageType::CALL, $this->seqid_); + $args->write($this->output_); + $this->output_->writeMessageEnd(); + $this->output_->getTransport()->flush(); + } + } + + public function recv_validateTrace() + { + $bin_accel = ($this->input_ instanceof TBinaryProtocolAccelerated) && function_exists('thrift_protocol_read_binary'); + if ($bin_accel) { + $result = thrift_protocol_read_binary( + $this->input_, + '\Jaeger\Thrift\Agent\AggregationValidator_validateTrace_result', + $this->input_->isStrictRead() + ); + } else { + $rseqid = 0; + $fname = null; + $mtype = 0; + + $this->input_->readMessageBegin($fname, $mtype, $rseqid); + if ($mtype == TMessageType::EXCEPTION) { + $x = new TApplicationException(); + $x->read($this->input_); + $this->input_->readMessageEnd(); + throw $x; + } + $result = new \Jaeger\Thrift\Agent\AggregationValidator_validateTrace_result(); + $result->read($this->input_); + $this->input_->readMessageEnd(); + } + if ($result->success !== null) { + return $result->success; + } + throw new \Exception("validateTrace failed: unknown result"); + } +} diff --git a/src/Jaeger/Thrift/Agent/AggregationValidatorIf.php b/src/Jaeger/Thrift/Agent/AggregationValidatorIf.php new file mode 100644 index 0000000..de54419 --- /dev/null +++ b/src/Jaeger/Thrift/Agent/AggregationValidatorIf.php @@ -0,0 +1,26 @@ + array( + 'var' => 'traceId', + 'isRequired' => true, + 'type' => TType::STRING, + ), + ); + + /** + * @var string + */ + public $traceId = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['traceId'])) { + $this->traceId = $vals['traceId']; + } + } + } + + public function getName() + { + return 'AggregationValidator_validateTrace_args'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->traceId); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('AggregationValidator_validateTrace_args'); + if ($this->traceId !== null) { + $xfer += $output->writeFieldBegin('traceId', TType::STRING, 1); + $xfer += $output->writeString($this->traceId); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Agent/AggregationValidator_validateTrace_result.php b/src/Jaeger/Thrift/Agent/AggregationValidator_validateTrace_result.php new file mode 100644 index 0000000..cecf4ff --- /dev/null +++ b/src/Jaeger/Thrift/Agent/AggregationValidator_validateTrace_result.php @@ -0,0 +1,99 @@ + array( + 'var' => 'success', + 'isRequired' => false, + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Agent\ValidateTraceResponse', + ), + ); + + /** + * @var \Jaeger\Thrift\Agent\ValidateTraceResponse + */ + public $success = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['success'])) { + $this->success = $vals['success']; + } + } + } + + public function getName() + { + return 'AggregationValidator_validateTrace_result'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 0: + if ($ftype == TType::STRUCT) { + $this->success = new \Jaeger\Thrift\Agent\ValidateTraceResponse(); + $xfer += $this->success->read($input); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('AggregationValidator_validateTrace_result'); + if ($this->success !== null) { + if (!is_object($this->success)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('success', TType::STRUCT, 0); + $xfer += $this->success->write($output); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Agent/BaggageRestriction.php b/src/Jaeger/Thrift/Agent/BaggageRestriction.php new file mode 100644 index 0000000..1e535b4 --- /dev/null +++ b/src/Jaeger/Thrift/Agent/BaggageRestriction.php @@ -0,0 +1,118 @@ + array( + 'var' => 'baggageKey', + 'isRequired' => true, + 'type' => TType::STRING, + ), + 2 => array( + 'var' => 'maxValueLength', + 'isRequired' => true, + 'type' => TType::I32, + ), + ); + + /** + * @var string + */ + public $baggageKey = null; + /** + * @var int + */ + public $maxValueLength = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['baggageKey'])) { + $this->baggageKey = $vals['baggageKey']; + } + if (isset($vals['maxValueLength'])) { + $this->maxValueLength = $vals['maxValueLength']; + } + } + } + + public function getName() + { + return 'BaggageRestriction'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->baggageKey); + } else { + $xfer += $input->skip($ftype); + } + break; + case 2: + if ($ftype == TType::I32) { + $xfer += $input->readI32($this->maxValueLength); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('BaggageRestriction'); + if ($this->baggageKey !== null) { + $xfer += $output->writeFieldBegin('baggageKey', TType::STRING, 1); + $xfer += $output->writeString($this->baggageKey); + $xfer += $output->writeFieldEnd(); + } + if ($this->maxValueLength !== null) { + $xfer += $output->writeFieldBegin('maxValueLength', TType::I32, 2); + $xfer += $output->writeI32($this->maxValueLength); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Agent/BaggageRestrictionManagerClient.php b/src/Jaeger/Thrift/Agent/BaggageRestrictionManagerClient.php new file mode 100644 index 0000000..29bf690 --- /dev/null +++ b/src/Jaeger/Thrift/Agent/BaggageRestrictionManagerClient.php @@ -0,0 +1,91 @@ +input_ = $input; + $this->output_ = $output ? $output : $input; + } + + + public function getBaggageRestrictions($serviceName) + { + $this->send_getBaggageRestrictions($serviceName); + return $this->recv_getBaggageRestrictions(); + } + + public function send_getBaggageRestrictions($serviceName) + { + $args = new \Jaeger\Thrift\Agent\BaggageRestrictionManager_getBaggageRestrictions_args(); + $args->serviceName = $serviceName; + $bin_accel = ($this->output_ instanceof TBinaryProtocolAccelerated) && function_exists('thrift_protocol_write_binary'); + if ($bin_accel) { + thrift_protocol_write_binary( + $this->output_, + 'getBaggageRestrictions', + TMessageType::CALL, + $args, + $this->seqid_, + $this->output_->isStrictWrite() + ); + } else { + $this->output_->writeMessageBegin('getBaggageRestrictions', TMessageType::CALL, $this->seqid_); + $args->write($this->output_); + $this->output_->writeMessageEnd(); + $this->output_->getTransport()->flush(); + } + } + + public function recv_getBaggageRestrictions() + { + $bin_accel = ($this->input_ instanceof TBinaryProtocolAccelerated) && function_exists('thrift_protocol_read_binary'); + if ($bin_accel) { + $result = thrift_protocol_read_binary( + $this->input_, + '\Jaeger\Thrift\Agent\BaggageRestrictionManager_getBaggageRestrictions_result', + $this->input_->isStrictRead() + ); + } else { + $rseqid = 0; + $fname = null; + $mtype = 0; + + $this->input_->readMessageBegin($fname, $mtype, $rseqid); + if ($mtype == TMessageType::EXCEPTION) { + $x = new TApplicationException(); + $x->read($this->input_); + $this->input_->readMessageEnd(); + throw $x; + } + $result = new \Jaeger\Thrift\Agent\BaggageRestrictionManager_getBaggageRestrictions_result(); + $result->read($this->input_); + $this->input_->readMessageEnd(); + } + if ($result->success !== null) { + return $result->success; + } + throw new \Exception("getBaggageRestrictions failed: unknown result"); + } +} diff --git a/src/Jaeger/Thrift/Agent/BaggageRestrictionManagerIf.php b/src/Jaeger/Thrift/Agent/BaggageRestrictionManagerIf.php new file mode 100644 index 0000000..66dea4a --- /dev/null +++ b/src/Jaeger/Thrift/Agent/BaggageRestrictionManagerIf.php @@ -0,0 +1,30 @@ + array( + 'var' => 'serviceName', + 'isRequired' => false, + 'type' => TType::STRING, + ), + ); + + /** + * @var string + */ + public $serviceName = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['serviceName'])) { + $this->serviceName = $vals['serviceName']; + } + } + } + + public function getName() + { + return 'BaggageRestrictionManager_getBaggageRestrictions_args'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->serviceName); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('BaggageRestrictionManager_getBaggageRestrictions_args'); + if ($this->serviceName !== null) { + $xfer += $output->writeFieldBegin('serviceName', TType::STRING, 1); + $xfer += $output->writeString($this->serviceName); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Agent/BaggageRestrictionManager_getBaggageRestrictions_result.php b/src/Jaeger/Thrift/Agent/BaggageRestrictionManager_getBaggageRestrictions_result.php new file mode 100644 index 0000000..f7e226a --- /dev/null +++ b/src/Jaeger/Thrift/Agent/BaggageRestrictionManager_getBaggageRestrictions_result.php @@ -0,0 +1,116 @@ + array( + 'var' => 'success', + 'isRequired' => false, + 'type' => TType::LST, + 'etype' => TType::STRUCT, + 'elem' => array( + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Agent\BaggageRestriction', + ), + ), + ); + + /** + * @var \Jaeger\Thrift\Agent\BaggageRestriction[] + */ + public $success = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['success'])) { + $this->success = $vals['success']; + } + } + } + + public function getName() + { + return 'BaggageRestrictionManager_getBaggageRestrictions_result'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 0: + if ($ftype == TType::LST) { + $this->success = array(); + $_size0 = 0; + $_etype3 = 0; + $xfer += $input->readListBegin($_etype3, $_size0); + for ($_i4 = 0; $_i4 < $_size0; ++$_i4) { + $elem5 = null; + $elem5 = new \Jaeger\Thrift\Agent\BaggageRestriction(); + $xfer += $elem5->read($input); + $this->success []= $elem5; + } + $xfer += $input->readListEnd(); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('BaggageRestrictionManager_getBaggageRestrictions_result'); + if ($this->success !== null) { + if (!is_array($this->success)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('success', TType::LST, 0); + $output->writeListBegin(TType::STRUCT, count($this->success)); + foreach ($this->success as $iter6) { + $xfer += $iter6->write($output); + } + $output->writeListEnd(); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Agent/Dependencies.php b/src/Jaeger/Thrift/Agent/Dependencies.php new file mode 100644 index 0000000..739399c --- /dev/null +++ b/src/Jaeger/Thrift/Agent/Dependencies.php @@ -0,0 +1,116 @@ + array( + 'var' => 'links', + 'isRequired' => true, + 'type' => TType::LST, + 'etype' => TType::STRUCT, + 'elem' => array( + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Agent\DependencyLink', + ), + ), + ); + + /** + * @var \Jaeger\Thrift\Agent\DependencyLink[] + */ + public $links = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['links'])) { + $this->links = $vals['links']; + } + } + } + + public function getName() + { + return 'Dependencies'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::LST) { + $this->links = array(); + $_size0 = 0; + $_etype3 = 0; + $xfer += $input->readListBegin($_etype3, $_size0); + for ($_i4 = 0; $_i4 < $_size0; ++$_i4) { + $elem5 = null; + $elem5 = new \Jaeger\Thrift\Agent\DependencyLink(); + $xfer += $elem5->read($input); + $this->links []= $elem5; + } + $xfer += $input->readListEnd(); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('Dependencies'); + if ($this->links !== null) { + if (!is_array($this->links)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('links', TType::LST, 1); + $output->writeListBegin(TType::STRUCT, count($this->links)); + foreach ($this->links as $iter6) { + $xfer += $iter6->write($output); + } + $output->writeListEnd(); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Agent/DependencyClient.php b/src/Jaeger/Thrift/Agent/DependencyClient.php new file mode 100644 index 0000000..a650ec5 --- /dev/null +++ b/src/Jaeger/Thrift/Agent/DependencyClient.php @@ -0,0 +1,118 @@ +input_ = $input; + $this->output_ = $output ? $output : $input; + } + + + public function getDependenciesForTrace($traceId) + { + $this->send_getDependenciesForTrace($traceId); + return $this->recv_getDependenciesForTrace(); + } + + public function send_getDependenciesForTrace($traceId) + { + $args = new \Jaeger\Thrift\Agent\Dependency_getDependenciesForTrace_args(); + $args->traceId = $traceId; + $bin_accel = ($this->output_ instanceof TBinaryProtocolAccelerated) && function_exists('thrift_protocol_write_binary'); + if ($bin_accel) { + thrift_protocol_write_binary( + $this->output_, + 'getDependenciesForTrace', + TMessageType::CALL, + $args, + $this->seqid_, + $this->output_->isStrictWrite() + ); + } else { + $this->output_->writeMessageBegin('getDependenciesForTrace', TMessageType::CALL, $this->seqid_); + $args->write($this->output_); + $this->output_->writeMessageEnd(); + $this->output_->getTransport()->flush(); + } + } + + public function recv_getDependenciesForTrace() + { + $bin_accel = ($this->input_ instanceof TBinaryProtocolAccelerated) && function_exists('thrift_protocol_read_binary'); + if ($bin_accel) { + $result = thrift_protocol_read_binary( + $this->input_, + '\Jaeger\Thrift\Agent\Dependency_getDependenciesForTrace_result', + $this->input_->isStrictRead() + ); + } else { + $rseqid = 0; + $fname = null; + $mtype = 0; + + $this->input_->readMessageBegin($fname, $mtype, $rseqid); + if ($mtype == TMessageType::EXCEPTION) { + $x = new TApplicationException(); + $x->read($this->input_); + $this->input_->readMessageEnd(); + throw $x; + } + $result = new \Jaeger\Thrift\Agent\Dependency_getDependenciesForTrace_result(); + $result->read($this->input_); + $this->input_->readMessageEnd(); + } + if ($result->success !== null) { + return $result->success; + } + throw new \Exception("getDependenciesForTrace failed: unknown result"); + } + + public function saveDependencies(\Jaeger\Thrift\Agent\Dependencies $dependencies) + { + $this->send_saveDependencies($dependencies); + } + + public function send_saveDependencies(\Jaeger\Thrift\Agent\Dependencies $dependencies) + { + $args = new \Jaeger\Thrift\Agent\Dependency_saveDependencies_args(); + $args->dependencies = $dependencies; + $bin_accel = ($this->output_ instanceof TBinaryProtocolAccelerated) && function_exists('thrift_protocol_write_binary'); + if ($bin_accel) { + thrift_protocol_write_binary( + $this->output_, + 'saveDependencies', + TMessageType::ONEWAY, + $args, + $this->seqid_, + $this->output_->isStrictWrite() + ); + } else { + $this->output_->writeMessageBegin('saveDependencies', TMessageType::ONEWAY, $this->seqid_); + $args->write($this->output_); + $this->output_->writeMessageEnd(); + $this->output_->getTransport()->flush(); + } + } +} diff --git a/src/Jaeger/Thrift/Agent/DependencyIf.php b/src/Jaeger/Thrift/Agent/DependencyIf.php new file mode 100644 index 0000000..118f2ae --- /dev/null +++ b/src/Jaeger/Thrift/Agent/DependencyIf.php @@ -0,0 +1,30 @@ + array( + 'var' => 'parent', + 'isRequired' => true, + 'type' => TType::STRING, + ), + 2 => array( + 'var' => 'child', + 'isRequired' => true, + 'type' => TType::STRING, + ), + 4 => array( + 'var' => 'callCount', + 'isRequired' => true, + 'type' => TType::I64, + ), + ); + + /** + * @var string + */ + public $parent = null; + /** + * @var string + */ + public $child = null; + /** + * @var int + */ + public $callCount = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['parent'])) { + $this->parent = $vals['parent']; + } + if (isset($vals['child'])) { + $this->child = $vals['child']; + } + if (isset($vals['callCount'])) { + $this->callCount = $vals['callCount']; + } + } + } + + public function getName() + { + return 'DependencyLink'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->parent); + } else { + $xfer += $input->skip($ftype); + } + break; + case 2: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->child); + } else { + $xfer += $input->skip($ftype); + } + break; + case 4: + if ($ftype == TType::I64) { + $xfer += $input->readI64($this->callCount); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('DependencyLink'); + if ($this->parent !== null) { + $xfer += $output->writeFieldBegin('parent', TType::STRING, 1); + $xfer += $output->writeString($this->parent); + $xfer += $output->writeFieldEnd(); + } + if ($this->child !== null) { + $xfer += $output->writeFieldBegin('child', TType::STRING, 2); + $xfer += $output->writeString($this->child); + $xfer += $output->writeFieldEnd(); + } + if ($this->callCount !== null) { + $xfer += $output->writeFieldBegin('callCount', TType::I64, 4); + $xfer += $output->writeI64($this->callCount); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Agent/Dependency_getDependenciesForTrace_args.php b/src/Jaeger/Thrift/Agent/Dependency_getDependenciesForTrace_args.php new file mode 100644 index 0000000..409c56b --- /dev/null +++ b/src/Jaeger/Thrift/Agent/Dependency_getDependenciesForTrace_args.php @@ -0,0 +1,94 @@ + array( + 'var' => 'traceId', + 'isRequired' => true, + 'type' => TType::STRING, + ), + ); + + /** + * @var string + */ + public $traceId = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['traceId'])) { + $this->traceId = $vals['traceId']; + } + } + } + + public function getName() + { + return 'Dependency_getDependenciesForTrace_args'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->traceId); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('Dependency_getDependenciesForTrace_args'); + if ($this->traceId !== null) { + $xfer += $output->writeFieldBegin('traceId', TType::STRING, 1); + $xfer += $output->writeString($this->traceId); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Agent/Dependency_getDependenciesForTrace_result.php b/src/Jaeger/Thrift/Agent/Dependency_getDependenciesForTrace_result.php new file mode 100644 index 0000000..229b4d2 --- /dev/null +++ b/src/Jaeger/Thrift/Agent/Dependency_getDependenciesForTrace_result.php @@ -0,0 +1,99 @@ + array( + 'var' => 'success', + 'isRequired' => false, + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Agent\Dependencies', + ), + ); + + /** + * @var \Jaeger\Thrift\Agent\Dependencies + */ + public $success = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['success'])) { + $this->success = $vals['success']; + } + } + } + + public function getName() + { + return 'Dependency_getDependenciesForTrace_result'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 0: + if ($ftype == TType::STRUCT) { + $this->success = new \Jaeger\Thrift\Agent\Dependencies(); + $xfer += $this->success->read($input); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('Dependency_getDependenciesForTrace_result'); + if ($this->success !== null) { + if (!is_object($this->success)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('success', TType::STRUCT, 0); + $xfer += $this->success->write($output); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Agent/Dependency_saveDependencies_args.php b/src/Jaeger/Thrift/Agent/Dependency_saveDependencies_args.php new file mode 100644 index 0000000..3c34c5c --- /dev/null +++ b/src/Jaeger/Thrift/Agent/Dependency_saveDependencies_args.php @@ -0,0 +1,99 @@ + array( + 'var' => 'dependencies', + 'isRequired' => false, + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Agent\Dependencies', + ), + ); + + /** + * @var \Jaeger\Thrift\Agent\Dependencies + */ + public $dependencies = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['dependencies'])) { + $this->dependencies = $vals['dependencies']; + } + } + } + + public function getName() + { + return 'Dependency_saveDependencies_args'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::STRUCT) { + $this->dependencies = new \Jaeger\Thrift\Agent\Dependencies(); + $xfer += $this->dependencies->read($input); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('Dependency_saveDependencies_args'); + if ($this->dependencies !== null) { + if (!is_object($this->dependencies)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('dependencies', TType::STRUCT, 1); + $xfer += $this->dependencies->write($output); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Agent/OperationSamplingStrategy.php b/src/Jaeger/Thrift/Agent/OperationSamplingStrategy.php new file mode 100644 index 0000000..902fcfc --- /dev/null +++ b/src/Jaeger/Thrift/Agent/OperationSamplingStrategy.php @@ -0,0 +1,123 @@ + array( + 'var' => 'operation', + 'isRequired' => true, + 'type' => TType::STRING, + ), + 2 => array( + 'var' => 'probabilisticSampling', + 'isRequired' => true, + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Agent\ProbabilisticSamplingStrategy', + ), + ); + + /** + * @var string + */ + public $operation = null; + /** + * @var \Jaeger\Thrift\Agent\ProbabilisticSamplingStrategy + */ + public $probabilisticSampling = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['operation'])) { + $this->operation = $vals['operation']; + } + if (isset($vals['probabilisticSampling'])) { + $this->probabilisticSampling = $vals['probabilisticSampling']; + } + } + } + + public function getName() + { + return 'OperationSamplingStrategy'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->operation); + } else { + $xfer += $input->skip($ftype); + } + break; + case 2: + if ($ftype == TType::STRUCT) { + $this->probabilisticSampling = new \Jaeger\Thrift\Agent\ProbabilisticSamplingStrategy(); + $xfer += $this->probabilisticSampling->read($input); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('OperationSamplingStrategy'); + if ($this->operation !== null) { + $xfer += $output->writeFieldBegin('operation', TType::STRING, 1); + $xfer += $output->writeString($this->operation); + $xfer += $output->writeFieldEnd(); + } + if ($this->probabilisticSampling !== null) { + if (!is_object($this->probabilisticSampling)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('probabilisticSampling', TType::STRUCT, 2); + $xfer += $this->probabilisticSampling->write($output); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Agent/PerOperationSamplingStrategies.php b/src/Jaeger/Thrift/Agent/PerOperationSamplingStrategies.php new file mode 100644 index 0000000..59f9d9a --- /dev/null +++ b/src/Jaeger/Thrift/Agent/PerOperationSamplingStrategies.php @@ -0,0 +1,188 @@ + array( + 'var' => 'defaultSamplingProbability', + 'isRequired' => true, + 'type' => TType::DOUBLE, + ), + 2 => array( + 'var' => 'defaultLowerBoundTracesPerSecond', + 'isRequired' => true, + 'type' => TType::DOUBLE, + ), + 3 => array( + 'var' => 'perOperationStrategies', + 'isRequired' => true, + 'type' => TType::LST, + 'etype' => TType::STRUCT, + 'elem' => array( + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Agent\OperationSamplingStrategy', + ), + ), + 4 => array( + 'var' => 'defaultUpperBoundTracesPerSecond', + 'isRequired' => false, + 'type' => TType::DOUBLE, + ), + ); + + /** + * @var double + */ + public $defaultSamplingProbability = null; + /** + * @var double + */ + public $defaultLowerBoundTracesPerSecond = null; + /** + * @var \Jaeger\Thrift\Agent\OperationSamplingStrategy[] + */ + public $perOperationStrategies = null; + /** + * @var double + */ + public $defaultUpperBoundTracesPerSecond = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['defaultSamplingProbability'])) { + $this->defaultSamplingProbability = $vals['defaultSamplingProbability']; + } + if (isset($vals['defaultLowerBoundTracesPerSecond'])) { + $this->defaultLowerBoundTracesPerSecond = $vals['defaultLowerBoundTracesPerSecond']; + } + if (isset($vals['perOperationStrategies'])) { + $this->perOperationStrategies = $vals['perOperationStrategies']; + } + if (isset($vals['defaultUpperBoundTracesPerSecond'])) { + $this->defaultUpperBoundTracesPerSecond = $vals['defaultUpperBoundTracesPerSecond']; + } + } + } + + public function getName() + { + return 'PerOperationSamplingStrategies'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::DOUBLE) { + $xfer += $input->readDouble($this->defaultSamplingProbability); + } else { + $xfer += $input->skip($ftype); + } + break; + case 2: + if ($ftype == TType::DOUBLE) { + $xfer += $input->readDouble($this->defaultLowerBoundTracesPerSecond); + } else { + $xfer += $input->skip($ftype); + } + break; + case 3: + if ($ftype == TType::LST) { + $this->perOperationStrategies = array(); + $_size0 = 0; + $_etype3 = 0; + $xfer += $input->readListBegin($_etype3, $_size0); + for ($_i4 = 0; $_i4 < $_size0; ++$_i4) { + $elem5 = null; + $elem5 = new \Jaeger\Thrift\Agent\OperationSamplingStrategy(); + $xfer += $elem5->read($input); + $this->perOperationStrategies []= $elem5; + } + $xfer += $input->readListEnd(); + } else { + $xfer += $input->skip($ftype); + } + break; + case 4: + if ($ftype == TType::DOUBLE) { + $xfer += $input->readDouble($this->defaultUpperBoundTracesPerSecond); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('PerOperationSamplingStrategies'); + if ($this->defaultSamplingProbability !== null) { + $xfer += $output->writeFieldBegin('defaultSamplingProbability', TType::DOUBLE, 1); + $xfer += $output->writeDouble($this->defaultSamplingProbability); + $xfer += $output->writeFieldEnd(); + } + if ($this->defaultLowerBoundTracesPerSecond !== null) { + $xfer += $output->writeFieldBegin('defaultLowerBoundTracesPerSecond', TType::DOUBLE, 2); + $xfer += $output->writeDouble($this->defaultLowerBoundTracesPerSecond); + $xfer += $output->writeFieldEnd(); + } + if ($this->perOperationStrategies !== null) { + if (!is_array($this->perOperationStrategies)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('perOperationStrategies', TType::LST, 3); + $output->writeListBegin(TType::STRUCT, count($this->perOperationStrategies)); + foreach ($this->perOperationStrategies as $iter6) { + $xfer += $iter6->write($output); + } + $output->writeListEnd(); + $xfer += $output->writeFieldEnd(); + } + if ($this->defaultUpperBoundTracesPerSecond !== null) { + $xfer += $output->writeFieldBegin('defaultUpperBoundTracesPerSecond', TType::DOUBLE, 4); + $xfer += $output->writeDouble($this->defaultUpperBoundTracesPerSecond); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Agent/ProbabilisticSamplingStrategy.php b/src/Jaeger/Thrift/Agent/ProbabilisticSamplingStrategy.php new file mode 100644 index 0000000..a7fe556 --- /dev/null +++ b/src/Jaeger/Thrift/Agent/ProbabilisticSamplingStrategy.php @@ -0,0 +1,94 @@ + array( + 'var' => 'samplingRate', + 'isRequired' => true, + 'type' => TType::DOUBLE, + ), + ); + + /** + * @var double + */ + public $samplingRate = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['samplingRate'])) { + $this->samplingRate = $vals['samplingRate']; + } + } + } + + public function getName() + { + return 'ProbabilisticSamplingStrategy'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::DOUBLE) { + $xfer += $input->readDouble($this->samplingRate); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('ProbabilisticSamplingStrategy'); + if ($this->samplingRate !== null) { + $xfer += $output->writeFieldBegin('samplingRate', TType::DOUBLE, 1); + $xfer += $output->writeDouble($this->samplingRate); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Agent/RateLimitingSamplingStrategy.php b/src/Jaeger/Thrift/Agent/RateLimitingSamplingStrategy.php new file mode 100644 index 0000000..7d15c8d --- /dev/null +++ b/src/Jaeger/Thrift/Agent/RateLimitingSamplingStrategy.php @@ -0,0 +1,94 @@ + array( + 'var' => 'maxTracesPerSecond', + 'isRequired' => true, + 'type' => TType::I16, + ), + ); + + /** + * @var int + */ + public $maxTracesPerSecond = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['maxTracesPerSecond'])) { + $this->maxTracesPerSecond = $vals['maxTracesPerSecond']; + } + } + } + + public function getName() + { + return 'RateLimitingSamplingStrategy'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::I16) { + $xfer += $input->readI16($this->maxTracesPerSecond); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('RateLimitingSamplingStrategy'); + if ($this->maxTracesPerSecond !== null) { + $xfer += $output->writeFieldBegin('maxTracesPerSecond', TType::I16, 1); + $xfer += $output->writeI16($this->maxTracesPerSecond); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Agent/SamplingManagerClient.php b/src/Jaeger/Thrift/Agent/SamplingManagerClient.php new file mode 100644 index 0000000..bd5f646 --- /dev/null +++ b/src/Jaeger/Thrift/Agent/SamplingManagerClient.php @@ -0,0 +1,91 @@ +input_ = $input; + $this->output_ = $output ? $output : $input; + } + + + public function getSamplingStrategy($serviceName) + { + $this->send_getSamplingStrategy($serviceName); + return $this->recv_getSamplingStrategy(); + } + + public function send_getSamplingStrategy($serviceName) + { + $args = new \Jaeger\Thrift\Agent\SamplingManager_getSamplingStrategy_args(); + $args->serviceName = $serviceName; + $bin_accel = ($this->output_ instanceof TBinaryProtocolAccelerated) && function_exists('thrift_protocol_write_binary'); + if ($bin_accel) { + thrift_protocol_write_binary( + $this->output_, + 'getSamplingStrategy', + TMessageType::CALL, + $args, + $this->seqid_, + $this->output_->isStrictWrite() + ); + } else { + $this->output_->writeMessageBegin('getSamplingStrategy', TMessageType::CALL, $this->seqid_); + $args->write($this->output_); + $this->output_->writeMessageEnd(); + $this->output_->getTransport()->flush(); + } + } + + public function recv_getSamplingStrategy() + { + $bin_accel = ($this->input_ instanceof TBinaryProtocolAccelerated) && function_exists('thrift_protocol_read_binary'); + if ($bin_accel) { + $result = thrift_protocol_read_binary( + $this->input_, + '\Jaeger\Thrift\Agent\SamplingManager_getSamplingStrategy_result', + $this->input_->isStrictRead() + ); + } else { + $rseqid = 0; + $fname = null; + $mtype = 0; + + $this->input_->readMessageBegin($fname, $mtype, $rseqid); + if ($mtype == TMessageType::EXCEPTION) { + $x = new TApplicationException(); + $x->read($this->input_); + $this->input_->readMessageEnd(); + throw $x; + } + $result = new \Jaeger\Thrift\Agent\SamplingManager_getSamplingStrategy_result(); + $result->read($this->input_); + $this->input_->readMessageEnd(); + } + if ($result->success !== null) { + return $result->success; + } + throw new \Exception("getSamplingStrategy failed: unknown result"); + } +} diff --git a/src/Jaeger/Thrift/Agent/SamplingManagerIf.php b/src/Jaeger/Thrift/Agent/SamplingManagerIf.php new file mode 100644 index 0000000..9eca93f --- /dev/null +++ b/src/Jaeger/Thrift/Agent/SamplingManagerIf.php @@ -0,0 +1,26 @@ + array( + 'var' => 'serviceName', + 'isRequired' => false, + 'type' => TType::STRING, + ), + ); + + /** + * @var string + */ + public $serviceName = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['serviceName'])) { + $this->serviceName = $vals['serviceName']; + } + } + } + + public function getName() + { + return 'SamplingManager_getSamplingStrategy_args'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->serviceName); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('SamplingManager_getSamplingStrategy_args'); + if ($this->serviceName !== null) { + $xfer += $output->writeFieldBegin('serviceName', TType::STRING, 1); + $xfer += $output->writeString($this->serviceName); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Agent/SamplingManager_getSamplingStrategy_result.php b/src/Jaeger/Thrift/Agent/SamplingManager_getSamplingStrategy_result.php new file mode 100644 index 0000000..7f7aca3 --- /dev/null +++ b/src/Jaeger/Thrift/Agent/SamplingManager_getSamplingStrategy_result.php @@ -0,0 +1,99 @@ + array( + 'var' => 'success', + 'isRequired' => false, + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Agent\SamplingStrategyResponse', + ), + ); + + /** + * @var \Jaeger\Thrift\Agent\SamplingStrategyResponse + */ + public $success = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['success'])) { + $this->success = $vals['success']; + } + } + } + + public function getName() + { + return 'SamplingManager_getSamplingStrategy_result'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 0: + if ($ftype == TType::STRUCT) { + $this->success = new \Jaeger\Thrift\Agent\SamplingStrategyResponse(); + $xfer += $this->success->read($input); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('SamplingManager_getSamplingStrategy_result'); + if ($this->success !== null) { + if (!is_object($this->success)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('success', TType::STRUCT, 0); + $xfer += $this->success->write($output); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Agent/SamplingStrategyResponse.php b/src/Jaeger/Thrift/Agent/SamplingStrategyResponse.php new file mode 100644 index 0000000..14f4538 --- /dev/null +++ b/src/Jaeger/Thrift/Agent/SamplingStrategyResponse.php @@ -0,0 +1,181 @@ + array( + 'var' => 'strategyType', + 'isRequired' => true, + 'type' => TType::I32, + ), + 2 => array( + 'var' => 'probabilisticSampling', + 'isRequired' => false, + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Agent\ProbabilisticSamplingStrategy', + ), + 3 => array( + 'var' => 'rateLimitingSampling', + 'isRequired' => false, + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Agent\RateLimitingSamplingStrategy', + ), + 4 => array( + 'var' => 'operationSampling', + 'isRequired' => false, + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Agent\PerOperationSamplingStrategies', + ), + ); + + /** + * @var int + */ + public $strategyType = null; + /** + * @var \Jaeger\Thrift\Agent\ProbabilisticSamplingStrategy + */ + public $probabilisticSampling = null; + /** + * @var \Jaeger\Thrift\Agent\RateLimitingSamplingStrategy + */ + public $rateLimitingSampling = null; + /** + * @var \Jaeger\Thrift\Agent\PerOperationSamplingStrategies + */ + public $operationSampling = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['strategyType'])) { + $this->strategyType = $vals['strategyType']; + } + if (isset($vals['probabilisticSampling'])) { + $this->probabilisticSampling = $vals['probabilisticSampling']; + } + if (isset($vals['rateLimitingSampling'])) { + $this->rateLimitingSampling = $vals['rateLimitingSampling']; + } + if (isset($vals['operationSampling'])) { + $this->operationSampling = $vals['operationSampling']; + } + } + } + + public function getName() + { + return 'SamplingStrategyResponse'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::I32) { + $xfer += $input->readI32($this->strategyType); + } else { + $xfer += $input->skip($ftype); + } + break; + case 2: + if ($ftype == TType::STRUCT) { + $this->probabilisticSampling = new \Jaeger\Thrift\Agent\ProbabilisticSamplingStrategy(); + $xfer += $this->probabilisticSampling->read($input); + } else { + $xfer += $input->skip($ftype); + } + break; + case 3: + if ($ftype == TType::STRUCT) { + $this->rateLimitingSampling = new \Jaeger\Thrift\Agent\RateLimitingSamplingStrategy(); + $xfer += $this->rateLimitingSampling->read($input); + } else { + $xfer += $input->skip($ftype); + } + break; + case 4: + if ($ftype == TType::STRUCT) { + $this->operationSampling = new \Jaeger\Thrift\Agent\PerOperationSamplingStrategies(); + $xfer += $this->operationSampling->read($input); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('SamplingStrategyResponse'); + if ($this->strategyType !== null) { + $xfer += $output->writeFieldBegin('strategyType', TType::I32, 1); + $xfer += $output->writeI32($this->strategyType); + $xfer += $output->writeFieldEnd(); + } + if ($this->probabilisticSampling !== null) { + if (!is_object($this->probabilisticSampling)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('probabilisticSampling', TType::STRUCT, 2); + $xfer += $this->probabilisticSampling->write($output); + $xfer += $output->writeFieldEnd(); + } + if ($this->rateLimitingSampling !== null) { + if (!is_object($this->rateLimitingSampling)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('rateLimitingSampling', TType::STRUCT, 3); + $xfer += $this->rateLimitingSampling->write($output); + $xfer += $output->writeFieldEnd(); + } + if ($this->operationSampling !== null) { + if (!is_object($this->operationSampling)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('operationSampling', TType::STRUCT, 4); + $xfer += $this->operationSampling->write($output); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Agent/SamplingStrategyType.php b/src/Jaeger/Thrift/Agent/SamplingStrategyType.php new file mode 100644 index 0000000..0a7fa40 --- /dev/null +++ b/src/Jaeger/Thrift/Agent/SamplingStrategyType.php @@ -0,0 +1,30 @@ + 'PROBABILISTIC', + 1 => 'RATE_LIMITING', + ); +} + diff --git a/src/Jaeger/Thrift/Agent/ValidateTraceResponse.php b/src/Jaeger/Thrift/Agent/ValidateTraceResponse.php new file mode 100644 index 0000000..593bb93 --- /dev/null +++ b/src/Jaeger/Thrift/Agent/ValidateTraceResponse.php @@ -0,0 +1,118 @@ + array( + 'var' => 'ok', + 'isRequired' => true, + 'type' => TType::BOOL, + ), + 2 => array( + 'var' => 'traceCount', + 'isRequired' => true, + 'type' => TType::I64, + ), + ); + + /** + * @var bool + */ + public $ok = null; + /** + * @var int + */ + public $traceCount = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['ok'])) { + $this->ok = $vals['ok']; + } + if (isset($vals['traceCount'])) { + $this->traceCount = $vals['traceCount']; + } + } + } + + public function getName() + { + return 'ValidateTraceResponse'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::BOOL) { + $xfer += $input->readBool($this->ok); + } else { + $xfer += $input->skip($ftype); + } + break; + case 2: + if ($ftype == TType::I64) { + $xfer += $input->readI64($this->traceCount); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('ValidateTraceResponse'); + if ($this->ok !== null) { + $xfer += $output->writeFieldBegin('ok', TType::BOOL, 1); + $xfer += $output->writeBool($this->ok); + $xfer += $output->writeFieldEnd(); + } + if ($this->traceCount !== null) { + $xfer += $output->writeFieldBegin('traceCount', TType::I64, 2); + $xfer += $output->writeI64($this->traceCount); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Agent/Zipkin/Annotation.php b/src/Jaeger/Thrift/Agent/Zipkin/Annotation.php new file mode 100644 index 0000000..178c8bb --- /dev/null +++ b/src/Jaeger/Thrift/Agent/Zipkin/Annotation.php @@ -0,0 +1,159 @@ + array( + 'var' => 'timestamp', + 'isRequired' => false, + 'type' => TType::I64, + ), + 2 => array( + 'var' => 'value', + 'isRequired' => false, + 'type' => TType::STRING, + ), + 3 => array( + 'var' => 'host', + 'isRequired' => false, + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Agent\Zipkin\Endpoint', + ), + ); + + /** + * Microseconds from epoch. + * + * This value should use the most precise value possible. For example, + * gettimeofday or syncing nanoTime against a tick of currentTimeMillis. + * + * @var int + */ + public $timestamp = null; + /** + * @var string + */ + public $value = null; + /** + * Always the host that recorded the event. By specifying the host you allow + * rollup of all events (such as client requests to a service) by IP address. + * + * @var \Jaeger\Thrift\Agent\Zipkin\Endpoint + */ + public $host = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['timestamp'])) { + $this->timestamp = $vals['timestamp']; + } + if (isset($vals['value'])) { + $this->value = $vals['value']; + } + if (isset($vals['host'])) { + $this->host = $vals['host']; + } + } + } + + public function getName() + { + return 'Annotation'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::I64) { + $xfer += $input->readI64($this->timestamp); + } else { + $xfer += $input->skip($ftype); + } + break; + case 2: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->value); + } else { + $xfer += $input->skip($ftype); + } + break; + case 3: + if ($ftype == TType::STRUCT) { + $this->host = new \Jaeger\Thrift\Agent\Zipkin\Endpoint(); + $xfer += $this->host->read($input); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('Annotation'); + if ($this->timestamp !== null) { + $xfer += $output->writeFieldBegin('timestamp', TType::I64, 1); + $xfer += $output->writeI64($this->timestamp); + $xfer += $output->writeFieldEnd(); + } + if ($this->value !== null) { + $xfer += $output->writeFieldBegin('value', TType::STRING, 2); + $xfer += $output->writeString($this->value); + $xfer += $output->writeFieldEnd(); + } + if ($this->host !== null) { + if (!is_object($this->host)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('host', TType::STRUCT, 3); + $xfer += $this->host->write($output); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Agent/Zipkin/AnnotationType.php b/src/Jaeger/Thrift/Agent/Zipkin/AnnotationType.php new file mode 100644 index 0000000..39c5a16 --- /dev/null +++ b/src/Jaeger/Thrift/Agent/Zipkin/AnnotationType.php @@ -0,0 +1,45 @@ + 'BOOL', + 1 => 'BYTES', + 2 => 'I16', + 3 => 'I32', + 4 => 'I64', + 5 => 'DOUBLE', + 6 => 'STRING', + ); +} + diff --git a/src/Jaeger/Thrift/Agent/Zipkin/BinaryAnnotation.php b/src/Jaeger/Thrift/Agent/Zipkin/BinaryAnnotation.php new file mode 100644 index 0000000..39884d7 --- /dev/null +++ b/src/Jaeger/Thrift/Agent/Zipkin/BinaryAnnotation.php @@ -0,0 +1,193 @@ + array( + 'var' => 'key', + 'isRequired' => false, + 'type' => TType::STRING, + ), + 2 => array( + 'var' => 'value', + 'isRequired' => false, + 'type' => TType::STRING, + ), + 3 => array( + 'var' => 'annotation_type', + 'isRequired' => false, + 'type' => TType::I32, + ), + 4 => array( + 'var' => 'host', + 'isRequired' => false, + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Agent\Zipkin\Endpoint', + ), + ); + + /** + * @var string + */ + public $key = null; + /** + * @var string + */ + public $value = null; + /** + * @var int + */ + public $annotation_type = null; + /** + * The host that recorded tag, which allows you to differentiate between + * multiple tags with the same key. There are two exceptions to this. + * + * When the key is CLIENT_ADDR or SERVER_ADDR, host indicates the source or + * destination of an RPC. This exception allows zipkin to display network + * context of uninstrumented services, or clients such as web browsers. + * + * @var \Jaeger\Thrift\Agent\Zipkin\Endpoint + */ + public $host = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['key'])) { + $this->key = $vals['key']; + } + if (isset($vals['value'])) { + $this->value = $vals['value']; + } + if (isset($vals['annotation_type'])) { + $this->annotation_type = $vals['annotation_type']; + } + if (isset($vals['host'])) { + $this->host = $vals['host']; + } + } + } + + public function getName() + { + return 'BinaryAnnotation'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->key); + } else { + $xfer += $input->skip($ftype); + } + break; + case 2: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->value); + } else { + $xfer += $input->skip($ftype); + } + break; + case 3: + if ($ftype == TType::I32) { + $xfer += $input->readI32($this->annotation_type); + } else { + $xfer += $input->skip($ftype); + } + break; + case 4: + if ($ftype == TType::STRUCT) { + $this->host = new \Jaeger\Thrift\Agent\Zipkin\Endpoint(); + $xfer += $this->host->read($input); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('BinaryAnnotation'); + if ($this->key !== null) { + $xfer += $output->writeFieldBegin('key', TType::STRING, 1); + $xfer += $output->writeString($this->key); + $xfer += $output->writeFieldEnd(); + } + if ($this->value !== null) { + $xfer += $output->writeFieldBegin('value', TType::STRING, 2); + $xfer += $output->writeString($this->value); + $xfer += $output->writeFieldEnd(); + } + if ($this->annotation_type !== null) { + $xfer += $output->writeFieldBegin('annotation_type', TType::I32, 3); + $xfer += $output->writeI32($this->annotation_type); + $xfer += $output->writeFieldEnd(); + } + if ($this->host !== null) { + if (!is_object($this->host)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('host', TType::STRUCT, 4); + $xfer += $this->host->write($output); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Agent/Zipkin/Constant.php b/src/Jaeger/Thrift/Agent/Zipkin/Constant.php new file mode 100644 index 0000000..ade2f62 --- /dev/null +++ b/src/Jaeger/Thrift/Agent/Zipkin/Constant.php @@ -0,0 +1,261 @@ + array( + 'var' => 'ipv4', + 'isRequired' => false, + 'type' => TType::I32, + ), + 2 => array( + 'var' => 'port', + 'isRequired' => false, + 'type' => TType::I16, + ), + 3 => array( + 'var' => 'service_name', + 'isRequired' => false, + 'type' => TType::STRING, + ), + 4 => array( + 'var' => 'ipv6', + 'isRequired' => false, + 'type' => TType::STRING, + ), + ); + + /** + * IPv4 host address packed into 4 bytes. + * + * Ex for the ip 1.2.3.4, it would be (1 << 24) | (2 << 16) | (3 << 8) | 4 + * + * @var int + */ + public $ipv4 = null; + /** + * IPv4 port + * + * Note: this is to be treated as an unsigned integer, so watch for negatives. + * + * Conventionally, when the port isn't known, port = 0. + * + * @var int + */ + public $port = null; + /** + * Service name in lowercase, such as "memcache" or "zipkin-web" + * + * Conventionally, when the service name isn't known, service_name = "unknown". + * + * @var string + */ + public $service_name = null; + /** + * IPv6 host address packed into 16 bytes. Ex Inet6Address.getBytes() + * + * @var string + */ + public $ipv6 = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['ipv4'])) { + $this->ipv4 = $vals['ipv4']; + } + if (isset($vals['port'])) { + $this->port = $vals['port']; + } + if (isset($vals['service_name'])) { + $this->service_name = $vals['service_name']; + } + if (isset($vals['ipv6'])) { + $this->ipv6 = $vals['ipv6']; + } + } + } + + public function getName() + { + return 'Endpoint'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::I32) { + $xfer += $input->readI32($this->ipv4); + } else { + $xfer += $input->skip($ftype); + } + break; + case 2: + if ($ftype == TType::I16) { + $xfer += $input->readI16($this->port); + } else { + $xfer += $input->skip($ftype); + } + break; + case 3: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->service_name); + } else { + $xfer += $input->skip($ftype); + } + break; + case 4: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->ipv6); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('Endpoint'); + if ($this->ipv4 !== null) { + $xfer += $output->writeFieldBegin('ipv4', TType::I32, 1); + $xfer += $output->writeI32($this->ipv4); + $xfer += $output->writeFieldEnd(); + } + if ($this->port !== null) { + $xfer += $output->writeFieldBegin('port', TType::I16, 2); + $xfer += $output->writeI16($this->port); + $xfer += $output->writeFieldEnd(); + } + if ($this->service_name !== null) { + $xfer += $output->writeFieldBegin('service_name', TType::STRING, 3); + $xfer += $output->writeString($this->service_name); + $xfer += $output->writeFieldEnd(); + } + if ($this->ipv6 !== null) { + $xfer += $output->writeFieldBegin('ipv6', TType::STRING, 4); + $xfer += $output->writeString($this->ipv6); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Agent/Zipkin/Response.php b/src/Jaeger/Thrift/Agent/Zipkin/Response.php new file mode 100644 index 0000000..22ae6f3 --- /dev/null +++ b/src/Jaeger/Thrift/Agent/Zipkin/Response.php @@ -0,0 +1,94 @@ + array( + 'var' => 'ok', + 'isRequired' => true, + 'type' => TType::BOOL, + ), + ); + + /** + * @var bool + */ + public $ok = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['ok'])) { + $this->ok = $vals['ok']; + } + } + } + + public function getName() + { + return 'Response'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::BOOL) { + $xfer += $input->readBool($this->ok); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('Response'); + if ($this->ok !== null) { + $xfer += $output->writeFieldBegin('ok', TType::BOOL, 1); + $xfer += $output->writeBool($this->ok); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Agent/Zipkin/Span.php b/src/Jaeger/Thrift/Agent/Zipkin/Span.php new file mode 100644 index 0000000..5f90335 --- /dev/null +++ b/src/Jaeger/Thrift/Agent/Zipkin/Span.php @@ -0,0 +1,397 @@ + array( + 'var' => 'trace_id', + 'isRequired' => false, + 'type' => TType::I64, + ), + 3 => array( + 'var' => 'name', + 'isRequired' => false, + 'type' => TType::STRING, + ), + 4 => array( + 'var' => 'id', + 'isRequired' => false, + 'type' => TType::I64, + ), + 5 => array( + 'var' => 'parent_id', + 'isRequired' => false, + 'type' => TType::I64, + ), + 6 => array( + 'var' => 'annotations', + 'isRequired' => false, + 'type' => TType::LST, + 'etype' => TType::STRUCT, + 'elem' => array( + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Agent\Zipkin\Annotation', + ), + ), + 8 => array( + 'var' => 'binary_annotations', + 'isRequired' => false, + 'type' => TType::LST, + 'etype' => TType::STRUCT, + 'elem' => array( + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Agent\Zipkin\BinaryAnnotation', + ), + ), + 9 => array( + 'var' => 'debug', + 'isRequired' => false, + 'type' => TType::BOOL, + ), + 10 => array( + 'var' => 'timestamp', + 'isRequired' => false, + 'type' => TType::I64, + ), + 11 => array( + 'var' => 'duration', + 'isRequired' => false, + 'type' => TType::I64, + ), + 12 => array( + 'var' => 'trace_id_high', + 'isRequired' => false, + 'type' => TType::I64, + ), + ); + + /** + * @var int + */ + public $trace_id = null; + /** + * Span name in lowercase, rpc method for example + * + * Conventionally, when the span name isn't known, name = "unknown". + * + * @var string + */ + public $name = null; + /** + * @var int + */ + public $id = null; + /** + * @var int + */ + public $parent_id = null; + /** + * @var \Jaeger\Thrift\Agent\Zipkin\Annotation[] + */ + public $annotations = null; + /** + * @var \Jaeger\Thrift\Agent\Zipkin\BinaryAnnotation[] + */ + public $binary_annotations = null; + /** + * @var bool + */ + public $debug = false; + /** + * Microseconds from epoch of the creation of this span. + * + * This value should be set directly by instrumentation, using the most + * precise value possible. For example, gettimeofday or syncing nanoTime + * against a tick of currentTimeMillis. + * + * For compatibility with instrumentation that precede this field, collectors + * or span stores can derive this via Annotation.timestamp. + * For example, SERVER_RECV.timestamp or CLIENT_SEND.timestamp. + * + * This field is optional for compatibility with old data: first-party span + * stores are expected to support this at time of introduction. + * + * @var int + */ + public $timestamp = null; + /** + * Measurement of duration in microseconds, used to support queries. + * + * This value should be set directly, where possible. Doing so encourages + * precise measurement decoupled from problems of clocks, such as skew or NTP + * updates causing time to move backwards. + * + * For compatibility with instrumentation that precede this field, collectors + * or span stores can derive this by subtracting Annotation.timestamp. + * For example, SERVER_SEND.timestamp - SERVER_RECV.timestamp. + * + * If this field is persisted as unset, zipkin will continue to work, except + * duration query support will be implementation-specific. Similarly, setting + * this field non-atomically is implementation-specific. + * + * This field is i64 vs i32 to support spans longer than 35 minutes. + * + * @var int + */ + public $duration = null; + /** + * Optional unique 8-byte additional identifier for a trace. If non zero, this + * means the trace uses 128 bit traceIds instead of 64 bit. + * + * @var int + */ + public $trace_id_high = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['trace_id'])) { + $this->trace_id = $vals['trace_id']; + } + if (isset($vals['name'])) { + $this->name = $vals['name']; + } + if (isset($vals['id'])) { + $this->id = $vals['id']; + } + if (isset($vals['parent_id'])) { + $this->parent_id = $vals['parent_id']; + } + if (isset($vals['annotations'])) { + $this->annotations = $vals['annotations']; + } + if (isset($vals['binary_annotations'])) { + $this->binary_annotations = $vals['binary_annotations']; + } + if (isset($vals['debug'])) { + $this->debug = $vals['debug']; + } + if (isset($vals['timestamp'])) { + $this->timestamp = $vals['timestamp']; + } + if (isset($vals['duration'])) { + $this->duration = $vals['duration']; + } + if (isset($vals['trace_id_high'])) { + $this->trace_id_high = $vals['trace_id_high']; + } + } + } + + public function getName() + { + return 'Span'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::I64) { + $xfer += $input->readI64($this->trace_id); + } else { + $xfer += $input->skip($ftype); + } + break; + case 3: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->name); + } else { + $xfer += $input->skip($ftype); + } + break; + case 4: + if ($ftype == TType::I64) { + $xfer += $input->readI64($this->id); + } else { + $xfer += $input->skip($ftype); + } + break; + case 5: + if ($ftype == TType::I64) { + $xfer += $input->readI64($this->parent_id); + } else { + $xfer += $input->skip($ftype); + } + break; + case 6: + if ($ftype == TType::LST) { + $this->annotations = array(); + $_size0 = 0; + $_etype3 = 0; + $xfer += $input->readListBegin($_etype3, $_size0); + for ($_i4 = 0; $_i4 < $_size0; ++$_i4) { + $elem5 = null; + $elem5 = new \Jaeger\Thrift\Agent\Zipkin\Annotation(); + $xfer += $elem5->read($input); + $this->annotations []= $elem5; + } + $xfer += $input->readListEnd(); + } else { + $xfer += $input->skip($ftype); + } + break; + case 8: + if ($ftype == TType::LST) { + $this->binary_annotations = array(); + $_size6 = 0; + $_etype9 = 0; + $xfer += $input->readListBegin($_etype9, $_size6); + for ($_i10 = 0; $_i10 < $_size6; ++$_i10) { + $elem11 = null; + $elem11 = new \Jaeger\Thrift\Agent\Zipkin\BinaryAnnotation(); + $xfer += $elem11->read($input); + $this->binary_annotations []= $elem11; + } + $xfer += $input->readListEnd(); + } else { + $xfer += $input->skip($ftype); + } + break; + case 9: + if ($ftype == TType::BOOL) { + $xfer += $input->readBool($this->debug); + } else { + $xfer += $input->skip($ftype); + } + break; + case 10: + if ($ftype == TType::I64) { + $xfer += $input->readI64($this->timestamp); + } else { + $xfer += $input->skip($ftype); + } + break; + case 11: + if ($ftype == TType::I64) { + $xfer += $input->readI64($this->duration); + } else { + $xfer += $input->skip($ftype); + } + break; + case 12: + if ($ftype == TType::I64) { + $xfer += $input->readI64($this->trace_id_high); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('Span'); + if ($this->trace_id !== null) { + $xfer += $output->writeFieldBegin('trace_id', TType::I64, 1); + $xfer += $output->writeI64($this->trace_id); + $xfer += $output->writeFieldEnd(); + } + if ($this->name !== null) { + $xfer += $output->writeFieldBegin('name', TType::STRING, 3); + $xfer += $output->writeString($this->name); + $xfer += $output->writeFieldEnd(); + } + if ($this->id !== null) { + $xfer += $output->writeFieldBegin('id', TType::I64, 4); + $xfer += $output->writeI64($this->id); + $xfer += $output->writeFieldEnd(); + } + if ($this->parent_id !== null) { + $xfer += $output->writeFieldBegin('parent_id', TType::I64, 5); + $xfer += $output->writeI64($this->parent_id); + $xfer += $output->writeFieldEnd(); + } + if ($this->annotations !== null) { + if (!is_array($this->annotations)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('annotations', TType::LST, 6); + $output->writeListBegin(TType::STRUCT, count($this->annotations)); + foreach ($this->annotations as $iter12) { + $xfer += $iter12->write($output); + } + $output->writeListEnd(); + $xfer += $output->writeFieldEnd(); + } + if ($this->binary_annotations !== null) { + if (!is_array($this->binary_annotations)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('binary_annotations', TType::LST, 8); + $output->writeListBegin(TType::STRUCT, count($this->binary_annotations)); + foreach ($this->binary_annotations as $iter13) { + $xfer += $iter13->write($output); + } + $output->writeListEnd(); + $xfer += $output->writeFieldEnd(); + } + if ($this->debug !== null) { + $xfer += $output->writeFieldBegin('debug', TType::BOOL, 9); + $xfer += $output->writeBool($this->debug); + $xfer += $output->writeFieldEnd(); + } + if ($this->timestamp !== null) { + $xfer += $output->writeFieldBegin('timestamp', TType::I64, 10); + $xfer += $output->writeI64($this->timestamp); + $xfer += $output->writeFieldEnd(); + } + if ($this->duration !== null) { + $xfer += $output->writeFieldBegin('duration', TType::I64, 11); + $xfer += $output->writeI64($this->duration); + $xfer += $output->writeFieldEnd(); + } + if ($this->trace_id_high !== null) { + $xfer += $output->writeFieldBegin('trace_id_high', TType::I64, 12); + $xfer += $output->writeI64($this->trace_id_high); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Agent/Zipkin/ZipkinCollectorClient.php b/src/Jaeger/Thrift/Agent/Zipkin/ZipkinCollectorClient.php new file mode 100644 index 0000000..ee0bea1 --- /dev/null +++ b/src/Jaeger/Thrift/Agent/Zipkin/ZipkinCollectorClient.php @@ -0,0 +1,91 @@ +input_ = $input; + $this->output_ = $output ? $output : $input; + } + + + public function submitZipkinBatch(array $spans) + { + $this->send_submitZipkinBatch($spans); + return $this->recv_submitZipkinBatch(); + } + + public function send_submitZipkinBatch(array $spans) + { + $args = new \Jaeger\Thrift\Agent\Zipkin\ZipkinCollector_submitZipkinBatch_args(); + $args->spans = $spans; + $bin_accel = ($this->output_ instanceof TBinaryProtocolAccelerated) && function_exists('thrift_protocol_write_binary'); + if ($bin_accel) { + thrift_protocol_write_binary( + $this->output_, + 'submitZipkinBatch', + TMessageType::CALL, + $args, + $this->seqid_, + $this->output_->isStrictWrite() + ); + } else { + $this->output_->writeMessageBegin('submitZipkinBatch', TMessageType::CALL, $this->seqid_); + $args->write($this->output_); + $this->output_->writeMessageEnd(); + $this->output_->getTransport()->flush(); + } + } + + public function recv_submitZipkinBatch() + { + $bin_accel = ($this->input_ instanceof TBinaryProtocolAccelerated) && function_exists('thrift_protocol_read_binary'); + if ($bin_accel) { + $result = thrift_protocol_read_binary( + $this->input_, + '\Jaeger\Thrift\Agent\Zipkin\ZipkinCollector_submitZipkinBatch_result', + $this->input_->isStrictRead() + ); + } else { + $rseqid = 0; + $fname = null; + $mtype = 0; + + $this->input_->readMessageBegin($fname, $mtype, $rseqid); + if ($mtype == TMessageType::EXCEPTION) { + $x = new TApplicationException(); + $x->read($this->input_); + $this->input_->readMessageEnd(); + throw $x; + } + $result = new \Jaeger\Thrift\Agent\Zipkin\ZipkinCollector_submitZipkinBatch_result(); + $result->read($this->input_); + $this->input_->readMessageEnd(); + } + if ($result->success !== null) { + return $result->success; + } + throw new \Exception("submitZipkinBatch failed: unknown result"); + } +} diff --git a/src/Jaeger/Thrift/Agent/Zipkin/ZipkinCollectorIf.php b/src/Jaeger/Thrift/Agent/Zipkin/ZipkinCollectorIf.php new file mode 100644 index 0000000..ad07496 --- /dev/null +++ b/src/Jaeger/Thrift/Agent/Zipkin/ZipkinCollectorIf.php @@ -0,0 +1,26 @@ + array( + 'var' => 'spans', + 'isRequired' => false, + 'type' => TType::LST, + 'etype' => TType::STRUCT, + 'elem' => array( + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Agent\Zipkin\Span', + ), + ), + ); + + /** + * @var \Jaeger\Thrift\Agent\Zipkin\Span[] + */ + public $spans = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['spans'])) { + $this->spans = $vals['spans']; + } + } + } + + public function getName() + { + return 'ZipkinCollector_submitZipkinBatch_args'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::LST) { + $this->spans = array(); + $_size14 = 0; + $_etype17 = 0; + $xfer += $input->readListBegin($_etype17, $_size14); + for ($_i18 = 0; $_i18 < $_size14; ++$_i18) { + $elem19 = null; + $elem19 = new \Jaeger\Thrift\Agent\Zipkin\Span(); + $xfer += $elem19->read($input); + $this->spans []= $elem19; + } + $xfer += $input->readListEnd(); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('ZipkinCollector_submitZipkinBatch_args'); + if ($this->spans !== null) { + if (!is_array($this->spans)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('spans', TType::LST, 1); + $output->writeListBegin(TType::STRUCT, count($this->spans)); + foreach ($this->spans as $iter20) { + $xfer += $iter20->write($output); + } + $output->writeListEnd(); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Agent/Zipkin/ZipkinCollector_submitZipkinBatch_result.php b/src/Jaeger/Thrift/Agent/Zipkin/ZipkinCollector_submitZipkinBatch_result.php new file mode 100644 index 0000000..c04a8a1 --- /dev/null +++ b/src/Jaeger/Thrift/Agent/Zipkin/ZipkinCollector_submitZipkinBatch_result.php @@ -0,0 +1,116 @@ + array( + 'var' => 'success', + 'isRequired' => false, + 'type' => TType::LST, + 'etype' => TType::STRUCT, + 'elem' => array( + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Agent\Zipkin\Response', + ), + ), + ); + + /** + * @var \Jaeger\Thrift\Agent\Zipkin\Response[] + */ + public $success = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['success'])) { + $this->success = $vals['success']; + } + } + } + + public function getName() + { + return 'ZipkinCollector_submitZipkinBatch_result'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 0: + if ($ftype == TType::LST) { + $this->success = array(); + $_size21 = 0; + $_etype24 = 0; + $xfer += $input->readListBegin($_etype24, $_size21); + for ($_i25 = 0; $_i25 < $_size21; ++$_i25) { + $elem26 = null; + $elem26 = new \Jaeger\Thrift\Agent\Zipkin\Response(); + $xfer += $elem26->read($input); + $this->success []= $elem26; + } + $xfer += $input->readListEnd(); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('ZipkinCollector_submitZipkinBatch_result'); + if ($this->success !== null) { + if (!is_array($this->success)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('success', TType::LST, 0); + $output->writeListBegin(TType::STRUCT, count($this->success)); + foreach ($this->success as $iter27) { + $xfer += $iter27->write($output); + } + $output->writeListEnd(); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/AgentClient.php b/src/Jaeger/Thrift/AgentClient.php deleted file mode 100644 index d2c9255..0000000 --- a/src/Jaeger/Thrift/AgentClient.php +++ /dev/null @@ -1,82 +0,0 @@ -writeMessageBegin('emitBatch', TMessageType::ONEWAY, 1); - self::$tptl->writeStructBegin('emitBatch_args'); - - $this->handleBatch($batch); - self::$tptl->writeFieldStop(); - - self::$tptl->writeStructEnd(); - self::$tptl->writeMessageEnd(); - - $batchLen = $tran->available(); - $batchThriftStr = $tran->read(Constants\UDP_PACKET_MAX_LENGTH); - - return ['len' => $batchLen, 'thriftStr' => $batchThriftStr]; - } - - private function handleBatch($batch) - { - self::$tptl->writeFieldBegin('batch', TType::STRUCT, 1); - - self::$tptl->writeStructBegin('Batch'); - $this->handleThriftProcess($batch['thriftProcess']); - $this->handleThriftSpans($batch['thriftSpans']); - - self::$tptl->writeFieldStop(); - self::$tptl->writeStructEnd(); - - self::$tptl->writeFieldEnd(); - } - - private function handleThriftSpans($thriftSpans) - { - self::$tptl->writeFieldBegin('spans', TType::LST, 2); - self::$tptl->writeListBegin(TType::STRUCT, count($thriftSpans)); - - $agentSpan = Span::getInstance(); - foreach ($thriftSpans as $thriftSpan) { - $agentSpan->setThriftSpan($thriftSpan); - $agentSpan->write(self::$tptl); - } - - self::$tptl->writeListEnd(); - self::$tptl->writeFieldEnd(); - } - - private function handleThriftProcess($thriftProcess) - { - self::$tptl->writeFieldBegin('process', TType::STRUCT, 1); - (new Process($thriftProcess))->write(self::$tptl); - self::$tptl->writeFieldEnd(); - } -} diff --git a/src/Jaeger/Thrift/Batch.php b/src/Jaeger/Thrift/Batch.php new file mode 100644 index 0000000..311c656 --- /dev/null +++ b/src/Jaeger/Thrift/Batch.php @@ -0,0 +1,198 @@ + array( + 'var' => 'process', + 'isRequired' => true, + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Process', + ), + 2 => array( + 'var' => 'spans', + 'isRequired' => true, + 'type' => TType::LST, + 'etype' => TType::STRUCT, + 'elem' => array( + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Span', + ), + ), + 3 => array( + 'var' => 'seqNo', + 'isRequired' => false, + 'type' => TType::I64, + ), + 4 => array( + 'var' => 'stats', + 'isRequired' => false, + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\ClientStats', + ), + ); + + /** + * @var \Jaeger\Thrift\Process + */ + public $process = null; + /** + * @var \Jaeger\Thrift\Span[] + */ + public $spans = null; + /** + * @var int + */ + public $seqNo = null; + /** + * @var \Jaeger\Thrift\ClientStats + */ + public $stats = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['process'])) { + $this->process = $vals['process']; + } + if (isset($vals['spans'])) { + $this->spans = $vals['spans']; + } + if (isset($vals['seqNo'])) { + $this->seqNo = $vals['seqNo']; + } + if (isset($vals['stats'])) { + $this->stats = $vals['stats']; + } + } + } + + public function getName() + { + return 'Batch'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::STRUCT) { + $this->process = new \Jaeger\Thrift\Process(); + $xfer += $this->process->read($input); + } else { + $xfer += $input->skip($ftype); + } + break; + case 2: + if ($ftype == TType::LST) { + $this->spans = array(); + $_size35 = 0; + $_etype38 = 0; + $xfer += $input->readListBegin($_etype38, $_size35); + for ($_i39 = 0; $_i39 < $_size35; ++$_i39) { + $elem40 = null; + $elem40 = new \Jaeger\Thrift\Span(); + $xfer += $elem40->read($input); + $this->spans []= $elem40; + } + $xfer += $input->readListEnd(); + } else { + $xfer += $input->skip($ftype); + } + break; + case 3: + if ($ftype == TType::I64) { + $xfer += $input->readI64($this->seqNo); + } else { + $xfer += $input->skip($ftype); + } + break; + case 4: + if ($ftype == TType::STRUCT) { + $this->stats = new \Jaeger\Thrift\ClientStats(); + $xfer += $this->stats->read($input); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('Batch'); + if ($this->process !== null) { + if (!is_object($this->process)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('process', TType::STRUCT, 1); + $xfer += $this->process->write($output); + $xfer += $output->writeFieldEnd(); + } + if ($this->spans !== null) { + if (!is_array($this->spans)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('spans', TType::LST, 2); + $output->writeListBegin(TType::STRUCT, count($this->spans)); + foreach ($this->spans as $iter41) { + $xfer += $iter41->write($output); + } + $output->writeListEnd(); + $xfer += $output->writeFieldEnd(); + } + if ($this->seqNo !== null) { + $xfer += $output->writeFieldBegin('seqNo', TType::I64, 3); + $xfer += $output->writeI64($this->seqNo); + $xfer += $output->writeFieldEnd(); + } + if ($this->stats !== null) { + if (!is_object($this->stats)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('stats', TType::STRUCT, 4); + $xfer += $this->stats->write($output); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/BatchSubmitResponse.php b/src/Jaeger/Thrift/BatchSubmitResponse.php new file mode 100644 index 0000000..492e293 --- /dev/null +++ b/src/Jaeger/Thrift/BatchSubmitResponse.php @@ -0,0 +1,94 @@ + array( + 'var' => 'ok', + 'isRequired' => true, + 'type' => TType::BOOL, + ), + ); + + /** + * @var bool + */ + public $ok = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['ok'])) { + $this->ok = $vals['ok']; + } + } + } + + public function getName() + { + return 'BatchSubmitResponse'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::BOOL) { + $xfer += $input->readBool($this->ok); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('BatchSubmitResponse'); + if ($this->ok !== null) { + $xfer += $output->writeFieldBegin('ok', TType::BOOL, 1); + $xfer += $output->writeBool($this->ok); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/ClientStats.php b/src/Jaeger/Thrift/ClientStats.php new file mode 100644 index 0000000..e554c20 --- /dev/null +++ b/src/Jaeger/Thrift/ClientStats.php @@ -0,0 +1,142 @@ + array( + 'var' => 'fullQueueDroppedSpans', + 'isRequired' => true, + 'type' => TType::I64, + ), + 2 => array( + 'var' => 'tooLargeDroppedSpans', + 'isRequired' => true, + 'type' => TType::I64, + ), + 3 => array( + 'var' => 'failedToEmitSpans', + 'isRequired' => true, + 'type' => TType::I64, + ), + ); + + /** + * @var int + */ + public $fullQueueDroppedSpans = null; + /** + * @var int + */ + public $tooLargeDroppedSpans = null; + /** + * @var int + */ + public $failedToEmitSpans = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['fullQueueDroppedSpans'])) { + $this->fullQueueDroppedSpans = $vals['fullQueueDroppedSpans']; + } + if (isset($vals['tooLargeDroppedSpans'])) { + $this->tooLargeDroppedSpans = $vals['tooLargeDroppedSpans']; + } + if (isset($vals['failedToEmitSpans'])) { + $this->failedToEmitSpans = $vals['failedToEmitSpans']; + } + } + } + + public function getName() + { + return 'ClientStats'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::I64) { + $xfer += $input->readI64($this->fullQueueDroppedSpans); + } else { + $xfer += $input->skip($ftype); + } + break; + case 2: + if ($ftype == TType::I64) { + $xfer += $input->readI64($this->tooLargeDroppedSpans); + } else { + $xfer += $input->skip($ftype); + } + break; + case 3: + if ($ftype == TType::I64) { + $xfer += $input->readI64($this->failedToEmitSpans); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('ClientStats'); + if ($this->fullQueueDroppedSpans !== null) { + $xfer += $output->writeFieldBegin('fullQueueDroppedSpans', TType::I64, 1); + $xfer += $output->writeI64($this->fullQueueDroppedSpans); + $xfer += $output->writeFieldEnd(); + } + if ($this->tooLargeDroppedSpans !== null) { + $xfer += $output->writeFieldBegin('tooLargeDroppedSpans', TType::I64, 2); + $xfer += $output->writeI64($this->tooLargeDroppedSpans); + $xfer += $output->writeFieldEnd(); + } + if ($this->failedToEmitSpans !== null) { + $xfer += $output->writeFieldBegin('failedToEmitSpans', TType::I64, 3); + $xfer += $output->writeI64($this->failedToEmitSpans); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/CollectorClient.php b/src/Jaeger/Thrift/CollectorClient.php new file mode 100644 index 0000000..b8d385c --- /dev/null +++ b/src/Jaeger/Thrift/CollectorClient.php @@ -0,0 +1,91 @@ +input_ = $input; + $this->output_ = $output ? $output : $input; + } + + + public function submitBatches(array $batches) + { + $this->send_submitBatches($batches); + return $this->recv_submitBatches(); + } + + public function send_submitBatches(array $batches) + { + $args = new \Jaeger\Thrift\Collector_submitBatches_args(); + $args->batches = $batches; + $bin_accel = ($this->output_ instanceof TBinaryProtocolAccelerated) && function_exists('thrift_protocol_write_binary'); + if ($bin_accel) { + thrift_protocol_write_binary( + $this->output_, + 'submitBatches', + TMessageType::CALL, + $args, + $this->seqid_, + $this->output_->isStrictWrite() + ); + } else { + $this->output_->writeMessageBegin('submitBatches', TMessageType::CALL, $this->seqid_); + $args->write($this->output_); + $this->output_->writeMessageEnd(); + $this->output_->getTransport()->flush(); + } + } + + public function recv_submitBatches() + { + $bin_accel = ($this->input_ instanceof TBinaryProtocolAccelerated) && function_exists('thrift_protocol_read_binary'); + if ($bin_accel) { + $result = thrift_protocol_read_binary( + $this->input_, + '\Jaeger\Thrift\Collector_submitBatches_result', + $this->input_->isStrictRead() + ); + } else { + $rseqid = 0; + $fname = null; + $mtype = 0; + + $this->input_->readMessageBegin($fname, $mtype, $rseqid); + if ($mtype == TMessageType::EXCEPTION) { + $x = new TApplicationException(); + $x->read($this->input_); + $this->input_->readMessageEnd(); + throw $x; + } + $result = new \Jaeger\Thrift\Collector_submitBatches_result(); + $result->read($this->input_); + $this->input_->readMessageEnd(); + } + if ($result->success !== null) { + return $result->success; + } + throw new \Exception("submitBatches failed: unknown result"); + } +} diff --git a/src/Jaeger/Thrift/CollectorIf.php b/src/Jaeger/Thrift/CollectorIf.php new file mode 100644 index 0000000..66d35f2 --- /dev/null +++ b/src/Jaeger/Thrift/CollectorIf.php @@ -0,0 +1,26 @@ + array( + 'var' => 'batches', + 'isRequired' => false, + 'type' => TType::LST, + 'etype' => TType::STRUCT, + 'elem' => array( + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Batch', + ), + ), + ); + + /** + * @var \Jaeger\Thrift\Batch[] + */ + public $batches = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['batches'])) { + $this->batches = $vals['batches']; + } + } + } + + public function getName() + { + return 'Collector_submitBatches_args'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::LST) { + $this->batches = array(); + $_size42 = 0; + $_etype45 = 0; + $xfer += $input->readListBegin($_etype45, $_size42); + for ($_i46 = 0; $_i46 < $_size42; ++$_i46) { + $elem47 = null; + $elem47 = new \Jaeger\Thrift\Batch(); + $xfer += $elem47->read($input); + $this->batches []= $elem47; + } + $xfer += $input->readListEnd(); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('Collector_submitBatches_args'); + if ($this->batches !== null) { + if (!is_array($this->batches)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('batches', TType::LST, 1); + $output->writeListBegin(TType::STRUCT, count($this->batches)); + foreach ($this->batches as $iter48) { + $xfer += $iter48->write($output); + } + $output->writeListEnd(); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Collector_submitBatches_result.php b/src/Jaeger/Thrift/Collector_submitBatches_result.php new file mode 100644 index 0000000..1b179b7 --- /dev/null +++ b/src/Jaeger/Thrift/Collector_submitBatches_result.php @@ -0,0 +1,116 @@ + array( + 'var' => 'success', + 'isRequired' => false, + 'type' => TType::LST, + 'etype' => TType::STRUCT, + 'elem' => array( + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\BatchSubmitResponse', + ), + ), + ); + + /** + * @var \Jaeger\Thrift\BatchSubmitResponse[] + */ + public $success = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['success'])) { + $this->success = $vals['success']; + } + } + } + + public function getName() + { + return 'Collector_submitBatches_result'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 0: + if ($ftype == TType::LST) { + $this->success = array(); + $_size49 = 0; + $_etype52 = 0; + $xfer += $input->readListBegin($_etype52, $_size49); + for ($_i53 = 0; $_i53 < $_size49; ++$_i53) { + $elem54 = null; + $elem54 = new \Jaeger\Thrift\BatchSubmitResponse(); + $xfer += $elem54->read($input); + $this->success []= $elem54; + } + $xfer += $input->readListEnd(); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('Collector_submitBatches_result'); + if ($this->success !== null) { + if (!is_array($this->success)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('success', TType::LST, 0); + $output->writeListBegin(TType::STRUCT, count($this->success)); + foreach ($this->success as $iter55) { + $xfer += $iter55->write($output); + } + $output->writeListEnd(); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Crossdock/Downstream.php b/src/Jaeger/Thrift/Crossdock/Downstream.php new file mode 100644 index 0000000..d8d03f5 --- /dev/null +++ b/src/Jaeger/Thrift/Crossdock/Downstream.php @@ -0,0 +1,219 @@ + array( + 'var' => 'serviceName', + 'isRequired' => true, + 'type' => TType::STRING, + ), + 2 => array( + 'var' => 'serverRole', + 'isRequired' => true, + 'type' => TType::STRING, + ), + 3 => array( + 'var' => 'host', + 'isRequired' => true, + 'type' => TType::STRING, + ), + 4 => array( + 'var' => 'port', + 'isRequired' => true, + 'type' => TType::STRING, + ), + 5 => array( + 'var' => 'transport', + 'isRequired' => true, + 'type' => TType::I32, + ), + 6 => array( + 'var' => 'downstream', + 'isRequired' => false, + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Crossdock\Downstream', + ), + ); + + /** + * @var string + */ + public $serviceName = null; + /** + * @var string + */ + public $serverRole = null; + /** + * @var string + */ + public $host = null; + /** + * @var string + */ + public $port = null; + /** + * @var int + */ + public $transport = null; + /** + * @var \Jaeger\Thrift\Crossdock\Downstream + */ + public $downstream = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['serviceName'])) { + $this->serviceName = $vals['serviceName']; + } + if (isset($vals['serverRole'])) { + $this->serverRole = $vals['serverRole']; + } + if (isset($vals['host'])) { + $this->host = $vals['host']; + } + if (isset($vals['port'])) { + $this->port = $vals['port']; + } + if (isset($vals['transport'])) { + $this->transport = $vals['transport']; + } + if (isset($vals['downstream'])) { + $this->downstream = $vals['downstream']; + } + } + } + + public function getName() + { + return 'Downstream'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->serviceName); + } else { + $xfer += $input->skip($ftype); + } + break; + case 2: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->serverRole); + } else { + $xfer += $input->skip($ftype); + } + break; + case 3: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->host); + } else { + $xfer += $input->skip($ftype); + } + break; + case 4: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->port); + } else { + $xfer += $input->skip($ftype); + } + break; + case 5: + if ($ftype == TType::I32) { + $xfer += $input->readI32($this->transport); + } else { + $xfer += $input->skip($ftype); + } + break; + case 6: + if ($ftype == TType::STRUCT) { + $this->downstream = new \Jaeger\Thrift\Crossdock\Downstream(); + $xfer += $this->downstream->read($input); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('Downstream'); + if ($this->serviceName !== null) { + $xfer += $output->writeFieldBegin('serviceName', TType::STRING, 1); + $xfer += $output->writeString($this->serviceName); + $xfer += $output->writeFieldEnd(); + } + if ($this->serverRole !== null) { + $xfer += $output->writeFieldBegin('serverRole', TType::STRING, 2); + $xfer += $output->writeString($this->serverRole); + $xfer += $output->writeFieldEnd(); + } + if ($this->host !== null) { + $xfer += $output->writeFieldBegin('host', TType::STRING, 3); + $xfer += $output->writeString($this->host); + $xfer += $output->writeFieldEnd(); + } + if ($this->port !== null) { + $xfer += $output->writeFieldBegin('port', TType::STRING, 4); + $xfer += $output->writeString($this->port); + $xfer += $output->writeFieldEnd(); + } + if ($this->transport !== null) { + $xfer += $output->writeFieldBegin('transport', TType::I32, 5); + $xfer += $output->writeI32($this->transport); + $xfer += $output->writeFieldEnd(); + } + if ($this->downstream !== null) { + if (!is_object($this->downstream)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('downstream', TType::STRUCT, 6); + $xfer += $this->downstream->write($output); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Crossdock/JoinTraceRequest.php b/src/Jaeger/Thrift/Crossdock/JoinTraceRequest.php new file mode 100644 index 0000000..c80fec8 --- /dev/null +++ b/src/Jaeger/Thrift/Crossdock/JoinTraceRequest.php @@ -0,0 +1,123 @@ + array( + 'var' => 'serverRole', + 'isRequired' => true, + 'type' => TType::STRING, + ), + 2 => array( + 'var' => 'downstream', + 'isRequired' => false, + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Crossdock\Downstream', + ), + ); + + /** + * @var string + */ + public $serverRole = null; + /** + * @var \Jaeger\Thrift\Crossdock\Downstream + */ + public $downstream = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['serverRole'])) { + $this->serverRole = $vals['serverRole']; + } + if (isset($vals['downstream'])) { + $this->downstream = $vals['downstream']; + } + } + } + + public function getName() + { + return 'JoinTraceRequest'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->serverRole); + } else { + $xfer += $input->skip($ftype); + } + break; + case 2: + if ($ftype == TType::STRUCT) { + $this->downstream = new \Jaeger\Thrift\Crossdock\Downstream(); + $xfer += $this->downstream->read($input); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('JoinTraceRequest'); + if ($this->serverRole !== null) { + $xfer += $output->writeFieldBegin('serverRole', TType::STRING, 1); + $xfer += $output->writeString($this->serverRole); + $xfer += $output->writeFieldEnd(); + } + if ($this->downstream !== null) { + if (!is_object($this->downstream)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('downstream', TType::STRUCT, 2); + $xfer += $this->downstream->write($output); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Crossdock/ObservedSpan.php b/src/Jaeger/Thrift/Crossdock/ObservedSpan.php new file mode 100644 index 0000000..20fcfd2 --- /dev/null +++ b/src/Jaeger/Thrift/Crossdock/ObservedSpan.php @@ -0,0 +1,142 @@ + array( + 'var' => 'traceId', + 'isRequired' => true, + 'type' => TType::STRING, + ), + 2 => array( + 'var' => 'sampled', + 'isRequired' => true, + 'type' => TType::BOOL, + ), + 3 => array( + 'var' => 'baggage', + 'isRequired' => true, + 'type' => TType::STRING, + ), + ); + + /** + * @var string + */ + public $traceId = null; + /** + * @var bool + */ + public $sampled = null; + /** + * @var string + */ + public $baggage = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['traceId'])) { + $this->traceId = $vals['traceId']; + } + if (isset($vals['sampled'])) { + $this->sampled = $vals['sampled']; + } + if (isset($vals['baggage'])) { + $this->baggage = $vals['baggage']; + } + } + } + + public function getName() + { + return 'ObservedSpan'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->traceId); + } else { + $xfer += $input->skip($ftype); + } + break; + case 2: + if ($ftype == TType::BOOL) { + $xfer += $input->readBool($this->sampled); + } else { + $xfer += $input->skip($ftype); + } + break; + case 3: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->baggage); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('ObservedSpan'); + if ($this->traceId !== null) { + $xfer += $output->writeFieldBegin('traceId', TType::STRING, 1); + $xfer += $output->writeString($this->traceId); + $xfer += $output->writeFieldEnd(); + } + if ($this->sampled !== null) { + $xfer += $output->writeFieldBegin('sampled', TType::BOOL, 2); + $xfer += $output->writeBool($this->sampled); + $xfer += $output->writeFieldEnd(); + } + if ($this->baggage !== null) { + $xfer += $output->writeFieldBegin('baggage', TType::STRING, 3); + $xfer += $output->writeString($this->baggage); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Crossdock/StartTraceRequest.php b/src/Jaeger/Thrift/Crossdock/StartTraceRequest.php new file mode 100644 index 0000000..2170171 --- /dev/null +++ b/src/Jaeger/Thrift/Crossdock/StartTraceRequest.php @@ -0,0 +1,171 @@ + array( + 'var' => 'serverRole', + 'isRequired' => true, + 'type' => TType::STRING, + ), + 2 => array( + 'var' => 'sampled', + 'isRequired' => true, + 'type' => TType::BOOL, + ), + 3 => array( + 'var' => 'baggage', + 'isRequired' => true, + 'type' => TType::STRING, + ), + 4 => array( + 'var' => 'downstream', + 'isRequired' => true, + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Crossdock\Downstream', + ), + ); + + /** + * @var string + */ + public $serverRole = null; + /** + * @var bool + */ + public $sampled = null; + /** + * @var string + */ + public $baggage = null; + /** + * @var \Jaeger\Thrift\Crossdock\Downstream + */ + public $downstream = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['serverRole'])) { + $this->serverRole = $vals['serverRole']; + } + if (isset($vals['sampled'])) { + $this->sampled = $vals['sampled']; + } + if (isset($vals['baggage'])) { + $this->baggage = $vals['baggage']; + } + if (isset($vals['downstream'])) { + $this->downstream = $vals['downstream']; + } + } + } + + public function getName() + { + return 'StartTraceRequest'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->serverRole); + } else { + $xfer += $input->skip($ftype); + } + break; + case 2: + if ($ftype == TType::BOOL) { + $xfer += $input->readBool($this->sampled); + } else { + $xfer += $input->skip($ftype); + } + break; + case 3: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->baggage); + } else { + $xfer += $input->skip($ftype); + } + break; + case 4: + if ($ftype == TType::STRUCT) { + $this->downstream = new \Jaeger\Thrift\Crossdock\Downstream(); + $xfer += $this->downstream->read($input); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('StartTraceRequest'); + if ($this->serverRole !== null) { + $xfer += $output->writeFieldBegin('serverRole', TType::STRING, 1); + $xfer += $output->writeString($this->serverRole); + $xfer += $output->writeFieldEnd(); + } + if ($this->sampled !== null) { + $xfer += $output->writeFieldBegin('sampled', TType::BOOL, 2); + $xfer += $output->writeBool($this->sampled); + $xfer += $output->writeFieldEnd(); + } + if ($this->baggage !== null) { + $xfer += $output->writeFieldBegin('baggage', TType::STRING, 3); + $xfer += $output->writeString($this->baggage); + $xfer += $output->writeFieldEnd(); + } + if ($this->downstream !== null) { + if (!is_object($this->downstream)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('downstream', TType::STRUCT, 4); + $xfer += $this->downstream->write($output); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Crossdock/TraceResponse.php b/src/Jaeger/Thrift/Crossdock/TraceResponse.php new file mode 100644 index 0000000..898ddd6 --- /dev/null +++ b/src/Jaeger/Thrift/Crossdock/TraceResponse.php @@ -0,0 +1,158 @@ + array( + 'var' => 'span', + 'isRequired' => false, + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Crossdock\ObservedSpan', + ), + 2 => array( + 'var' => 'downstream', + 'isRequired' => false, + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Crossdock\TraceResponse', + ), + 3 => array( + 'var' => 'notImplementedError', + 'isRequired' => true, + 'type' => TType::STRING, + ), + ); + + /** + * @var \Jaeger\Thrift\Crossdock\ObservedSpan + */ + public $span = null; + /** + * @var \Jaeger\Thrift\Crossdock\TraceResponse + */ + public $downstream = null; + /** + * @var string + */ + public $notImplementedError = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['span'])) { + $this->span = $vals['span']; + } + if (isset($vals['downstream'])) { + $this->downstream = $vals['downstream']; + } + if (isset($vals['notImplementedError'])) { + $this->notImplementedError = $vals['notImplementedError']; + } + } + } + + public function getName() + { + return 'TraceResponse'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::STRUCT) { + $this->span = new \Jaeger\Thrift\Crossdock\ObservedSpan(); + $xfer += $this->span->read($input); + } else { + $xfer += $input->skip($ftype); + } + break; + case 2: + if ($ftype == TType::STRUCT) { + $this->downstream = new \Jaeger\Thrift\Crossdock\TraceResponse(); + $xfer += $this->downstream->read($input); + } else { + $xfer += $input->skip($ftype); + } + break; + case 3: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->notImplementedError); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('TraceResponse'); + if ($this->span !== null) { + if (!is_object($this->span)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('span', TType::STRUCT, 1); + $xfer += $this->span->write($output); + $xfer += $output->writeFieldEnd(); + } + if ($this->downstream !== null) { + if (!is_object($this->downstream)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('downstream', TType::STRUCT, 2); + $xfer += $this->downstream->write($output); + $xfer += $output->writeFieldEnd(); + } + if ($this->notImplementedError !== null) { + $xfer += $output->writeFieldBegin('notImplementedError', TType::STRING, 3); + $xfer += $output->writeString($this->notImplementedError); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Crossdock/TracedServiceClient.php b/src/Jaeger/Thrift/Crossdock/TracedServiceClient.php new file mode 100644 index 0000000..e44c66d --- /dev/null +++ b/src/Jaeger/Thrift/Crossdock/TracedServiceClient.php @@ -0,0 +1,150 @@ +input_ = $input; + $this->output_ = $output ? $output : $input; + } + + + public function startTrace(\Jaeger\Thrift\Crossdock\StartTraceRequest $request) + { + $this->send_startTrace($request); + return $this->recv_startTrace(); + } + + public function send_startTrace(\Jaeger\Thrift\Crossdock\StartTraceRequest $request) + { + $args = new \Jaeger\Thrift\Crossdock\TracedService_startTrace_args(); + $args->request = $request; + $bin_accel = ($this->output_ instanceof TBinaryProtocolAccelerated) && function_exists('thrift_protocol_write_binary'); + if ($bin_accel) { + thrift_protocol_write_binary( + $this->output_, + 'startTrace', + TMessageType::CALL, + $args, + $this->seqid_, + $this->output_->isStrictWrite() + ); + } else { + $this->output_->writeMessageBegin('startTrace', TMessageType::CALL, $this->seqid_); + $args->write($this->output_); + $this->output_->writeMessageEnd(); + $this->output_->getTransport()->flush(); + } + } + + public function recv_startTrace() + { + $bin_accel = ($this->input_ instanceof TBinaryProtocolAccelerated) && function_exists('thrift_protocol_read_binary'); + if ($bin_accel) { + $result = thrift_protocol_read_binary( + $this->input_, + '\Jaeger\Thrift\Crossdock\TracedService_startTrace_result', + $this->input_->isStrictRead() + ); + } else { + $rseqid = 0; + $fname = null; + $mtype = 0; + + $this->input_->readMessageBegin($fname, $mtype, $rseqid); + if ($mtype == TMessageType::EXCEPTION) { + $x = new TApplicationException(); + $x->read($this->input_); + $this->input_->readMessageEnd(); + throw $x; + } + $result = new \Jaeger\Thrift\Crossdock\TracedService_startTrace_result(); + $result->read($this->input_); + $this->input_->readMessageEnd(); + } + if ($result->success !== null) { + return $result->success; + } + throw new \Exception("startTrace failed: unknown result"); + } + + public function joinTrace(\Jaeger\Thrift\Crossdock\JoinTraceRequest $request) + { + $this->send_joinTrace($request); + return $this->recv_joinTrace(); + } + + public function send_joinTrace(\Jaeger\Thrift\Crossdock\JoinTraceRequest $request) + { + $args = new \Jaeger\Thrift\Crossdock\TracedService_joinTrace_args(); + $args->request = $request; + $bin_accel = ($this->output_ instanceof TBinaryProtocolAccelerated) && function_exists('thrift_protocol_write_binary'); + if ($bin_accel) { + thrift_protocol_write_binary( + $this->output_, + 'joinTrace', + TMessageType::CALL, + $args, + $this->seqid_, + $this->output_->isStrictWrite() + ); + } else { + $this->output_->writeMessageBegin('joinTrace', TMessageType::CALL, $this->seqid_); + $args->write($this->output_); + $this->output_->writeMessageEnd(); + $this->output_->getTransport()->flush(); + } + } + + public function recv_joinTrace() + { + $bin_accel = ($this->input_ instanceof TBinaryProtocolAccelerated) && function_exists('thrift_protocol_read_binary'); + if ($bin_accel) { + $result = thrift_protocol_read_binary( + $this->input_, + '\Jaeger\Thrift\Crossdock\TracedService_joinTrace_result', + $this->input_->isStrictRead() + ); + } else { + $rseqid = 0; + $fname = null; + $mtype = 0; + + $this->input_->readMessageBegin($fname, $mtype, $rseqid); + if ($mtype == TMessageType::EXCEPTION) { + $x = new TApplicationException(); + $x->read($this->input_); + $this->input_->readMessageEnd(); + throw $x; + } + $result = new \Jaeger\Thrift\Crossdock\TracedService_joinTrace_result(); + $result->read($this->input_); + $this->input_->readMessageEnd(); + } + if ($result->success !== null) { + return $result->success; + } + throw new \Exception("joinTrace failed: unknown result"); + } +} diff --git a/src/Jaeger/Thrift/Crossdock/TracedServiceIf.php b/src/Jaeger/Thrift/Crossdock/TracedServiceIf.php new file mode 100644 index 0000000..35b942b --- /dev/null +++ b/src/Jaeger/Thrift/Crossdock/TracedServiceIf.php @@ -0,0 +1,39 @@ + array( + 'var' => 'request', + 'isRequired' => false, + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Crossdock\JoinTraceRequest', + ), + ); + + /** + * @var \Jaeger\Thrift\Crossdock\JoinTraceRequest + */ + public $request = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['request'])) { + $this->request = $vals['request']; + } + } + } + + public function getName() + { + return 'TracedService_joinTrace_args'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::STRUCT) { + $this->request = new \Jaeger\Thrift\Crossdock\JoinTraceRequest(); + $xfer += $this->request->read($input); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('TracedService_joinTrace_args'); + if ($this->request !== null) { + if (!is_object($this->request)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('request', TType::STRUCT, 1); + $xfer += $this->request->write($output); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Crossdock/TracedService_joinTrace_result.php b/src/Jaeger/Thrift/Crossdock/TracedService_joinTrace_result.php new file mode 100644 index 0000000..1adb3c9 --- /dev/null +++ b/src/Jaeger/Thrift/Crossdock/TracedService_joinTrace_result.php @@ -0,0 +1,99 @@ + array( + 'var' => 'success', + 'isRequired' => false, + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Crossdock\TraceResponse', + ), + ); + + /** + * @var \Jaeger\Thrift\Crossdock\TraceResponse + */ + public $success = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['success'])) { + $this->success = $vals['success']; + } + } + } + + public function getName() + { + return 'TracedService_joinTrace_result'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 0: + if ($ftype == TType::STRUCT) { + $this->success = new \Jaeger\Thrift\Crossdock\TraceResponse(); + $xfer += $this->success->read($input); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('TracedService_joinTrace_result'); + if ($this->success !== null) { + if (!is_object($this->success)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('success', TType::STRUCT, 0); + $xfer += $this->success->write($output); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Crossdock/TracedService_startTrace_args.php b/src/Jaeger/Thrift/Crossdock/TracedService_startTrace_args.php new file mode 100644 index 0000000..2c812a0 --- /dev/null +++ b/src/Jaeger/Thrift/Crossdock/TracedService_startTrace_args.php @@ -0,0 +1,99 @@ + array( + 'var' => 'request', + 'isRequired' => false, + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Crossdock\StartTraceRequest', + ), + ); + + /** + * @var \Jaeger\Thrift\Crossdock\StartTraceRequest + */ + public $request = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['request'])) { + $this->request = $vals['request']; + } + } + } + + public function getName() + { + return 'TracedService_startTrace_args'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::STRUCT) { + $this->request = new \Jaeger\Thrift\Crossdock\StartTraceRequest(); + $xfer += $this->request->read($input); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('TracedService_startTrace_args'); + if ($this->request !== null) { + if (!is_object($this->request)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('request', TType::STRUCT, 1); + $xfer += $this->request->write($output); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Crossdock/TracedService_startTrace_result.php b/src/Jaeger/Thrift/Crossdock/TracedService_startTrace_result.php new file mode 100644 index 0000000..3e8d307 --- /dev/null +++ b/src/Jaeger/Thrift/Crossdock/TracedService_startTrace_result.php @@ -0,0 +1,99 @@ + array( + 'var' => 'success', + 'isRequired' => false, + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Crossdock\TraceResponse', + ), + ); + + /** + * @var \Jaeger\Thrift\Crossdock\TraceResponse + */ + public $success = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['success'])) { + $this->success = $vals['success']; + } + } + } + + public function getName() + { + return 'TracedService_startTrace_result'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 0: + if ($ftype == TType::STRUCT) { + $this->success = new \Jaeger\Thrift\Crossdock\TraceResponse(); + $xfer += $this->success->read($input); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('TracedService_startTrace_result'); + if ($this->success !== null) { + if (!is_object($this->success)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('success', TType::STRUCT, 0); + $xfer += $this->success->write($output); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Crossdock/Transport.php b/src/Jaeger/Thrift/Crossdock/Transport.php new file mode 100644 index 0000000..5cb5ba5 --- /dev/null +++ b/src/Jaeger/Thrift/Crossdock/Transport.php @@ -0,0 +1,33 @@ + 'HTTP', + 1 => 'TCHANNEL', + 2 => 'DUMMY', + ); +} + diff --git a/src/Jaeger/Thrift/JaegerThriftSpan.php b/src/Jaeger/Thrift/JaegerThriftSpan.php deleted file mode 100644 index a31124e..0000000 --- a/src/Jaeger/Thrift/JaegerThriftSpan.php +++ /dev/null @@ -1,122 +0,0 @@ -tags); - $tagsObj = Tags::getInstance(); - $tagsObj->setTags($tags); - $thriftTags = $tagsObj->buildTags(); - - $processThrift = [ - 'serverName' => $jaeger->serverName, - 'tags' => $thriftTags, - ]; - - return $processThrift; - } - - public function buildJaegerSpanThrift(Span $span) - { - $spContext = $span->spanContext; - assert($spContext instanceof \Jaeger\SpanContext); - - $thriftSpan = [ - 'traceIdLow' => $spContext->traceIdLow, - 'traceIdHigh' => $spContext->traceIdHigh, - 'spanId' => $spContext->spanId, - 'parentSpanId' => $spContext->parentId, - 'operationName' => $span->getOperationName(), - 'flags' => intval($spContext->flags), - 'startTime' => $span->startTime, - 'duration' => $span->duration, - 'tags' => $this->buildTags($span->tags), - 'logs' => $this->buildLogs($span->logs), - 'references' => $this->buildReferences($span->references), - ]; - - return $thriftSpan; - } - - private function buildTags($tags) - { - $tagsObj = Tags::getInstance(); - $tagsObj->setTags($tags); - - return $tagsObj->buildTags(); - } - - private function buildLogs($logs) - { - $resultLogs = []; - $tagsObj = Tags::getInstance(); - foreach ($logs as $log) { - $tagsObj->setTags($log['fields']); - $fields = $tagsObj->buildTags(); - $resultLogs[] = [ - 'timestamp' => $log['timestamp'], - 'fields' => $fields, - ]; - } - - return $resultLogs; - } - - /** - * @param array $references - * - * @return array - */ - private function buildReferences($references) - { - $spanRef = []; - foreach ($references as $ref) { - if ($ref->isType(Reference::CHILD_OF)) { - $type = SpanRefType::CHILD_OF; - } elseif ($ref->isType(Reference::FOLLOWS_FROM)) { - $type = SpanRefType::FOLLOWS_FROM; - } else { - throw new \LogicException('Unsupported reference type'); - } - - $ctx = $ref->getSpanContext(); - assert($ctx instanceof \Jaeger\SpanContext); - - $spanRef[] = [ - 'refType' => $type, - 'traceIdLow' => $ctx->traceIdLow, - 'traceIdHigh' => $ctx->traceIdHigh, - 'spanId' => $ctx->spanId, - ]; - } - - return $spanRef; - } -} diff --git a/src/Jaeger/Thrift/Log.php b/src/Jaeger/Thrift/Log.php new file mode 100644 index 0000000..416bbb7 --- /dev/null +++ b/src/Jaeger/Thrift/Log.php @@ -0,0 +1,140 @@ + array( + 'var' => 'timestamp', + 'isRequired' => true, + 'type' => TType::I64, + ), + 2 => array( + 'var' => 'fields', + 'isRequired' => true, + 'type' => TType::LST, + 'etype' => TType::STRUCT, + 'elem' => array( + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Tag', + ), + ), + ); + + /** + * @var int + */ + public $timestamp = null; + /** + * @var \Jaeger\Thrift\Tag[] + */ + public $fields = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['timestamp'])) { + $this->timestamp = $vals['timestamp']; + } + if (isset($vals['fields'])) { + $this->fields = $vals['fields']; + } + } + } + + public function getName() + { + return 'Log'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::I64) { + $xfer += $input->readI64($this->timestamp); + } else { + $xfer += $input->skip($ftype); + } + break; + case 2: + if ($ftype == TType::LST) { + $this->fields = array(); + $_size0 = 0; + $_etype3 = 0; + $xfer += $input->readListBegin($_etype3, $_size0); + for ($_i4 = 0; $_i4 < $_size0; ++$_i4) { + $elem5 = null; + $elem5 = new \Jaeger\Thrift\Tag(); + $xfer += $elem5->read($input); + $this->fields []= $elem5; + } + $xfer += $input->readListEnd(); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('Log'); + if ($this->timestamp !== null) { + $xfer += $output->writeFieldBegin('timestamp', TType::I64, 1); + $xfer += $output->writeI64($this->timestamp); + $xfer += $output->writeFieldEnd(); + } + if ($this->fields !== null) { + if (!is_array($this->fields)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('fields', TType::LST, 2); + $output->writeListBegin(TType::STRUCT, count($this->fields)); + foreach ($this->fields as $iter6) { + $xfer += $iter6->write($output); + } + $output->writeListEnd(); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/Process.php b/src/Jaeger/Thrift/Process.php index 7ce98b0..8c6befb 100644 --- a/src/Jaeger/Thrift/Process.php +++ b/src/Jaeger/Thrift/Process.php @@ -1,95 +1,140 @@ array( + 'var' => 'serviceName', + 'isRequired' => true, + 'type' => TType::STRING, + ), + 2 => array( + 'var' => 'tags', + 'isRequired' => false, + 'type' => TType::LST, + 'etype' => TType::STRUCT, + 'elem' => array( + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Tag', + ), + ), + ); /** * @var string */ - public static $serverName = ''; - + public $serviceName = null; /** - * @var array + * @var \Jaeger\Thrift\Tag[] */ - public static $thriftTags = []; + public $tags = null; - /** - * @var string - */ - public static $wrote = ''; - - public function __construct($processThrift) - { - self::$serverName = $processThrift['serverName'] ?? ''; - self::$thriftTags = $processThrift['tags'] ?? ''; - self::$wrote = $processThrift['wrote'] ?? ''; - } - - public function write(TProtocol $t) + public function __construct($vals = null) { - self::$tptl = $t; - - if (self::$wrote) { - $tran = self::$tptl->getTransport(); - $tran->write(self::$wrote); - } else { - self::$tptl->writeStructBegin('Process'); - - $this->handleProcessSName(); - $this->handleProcessTags(); - - self::$tptl->writeFieldStop(); - self::$tptl->writeStructEnd(); + if (is_array($vals)) { + if (isset($vals['serviceName'])) { + $this->serviceName = $vals['serviceName']; + } + if (isset($vals['tags'])) { + $this->tags = $vals['tags']; + } } } - private function handleProcessSName() + public function getName() { - self::$tptl->writeFieldBegin('serverName', TType::STRING, 1); - - self::$tptl->writeString(self::$serverName); - - self::$tptl->writeFieldEnd(); + return 'Process'; } - private function handleProcessTags() - { - if (count(self::$thriftTags) > 0) { - self::$tptl->writeFieldBegin('tags', TType::LST, 2); - self::$tptl->writeListBegin(TType::STRUCT, count(self::$thriftTags)); - - $tagsObj = Tags::getInstance(); - $tagsObj->setThriftTags(self::$thriftTags); - $tagsObj->write(self::$tptl); - self::$tptl->writeListEnd(); - self::$tptl->writeFieldEnd(); + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->serviceName); + } else { + $xfer += $input->skip($ftype); + } + break; + case 2: + if ($ftype == TType::LST) { + $this->tags = array(); + $_size28 = 0; + $_etype31 = 0; + $xfer += $input->readListBegin($_etype31, $_size28); + for ($_i32 = 0; $_i32 < $_size28; ++$_i32) { + $elem33 = null; + $elem33 = new \Jaeger\Thrift\Tag(); + $xfer += $elem33->read($input); + $this->tags []= $elem33; + } + $xfer += $input->readListEnd(); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); } + $xfer += $input->readStructEnd(); + return $xfer; } - public function read(TProtocol $t) + public function write($output) { + $xfer = 0; + $xfer += $output->writeStructBegin('Process'); + if ($this->serviceName !== null) { + $xfer += $output->writeFieldBegin('serviceName', TType::STRING, 1); + $xfer += $output->writeString($this->serviceName); + $xfer += $output->writeFieldEnd(); + } + if ($this->tags !== null) { + if (!is_array($this->tags)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('tags', TType::LST, 2); + $output->writeListBegin(TType::STRUCT, count($this->tags)); + foreach ($this->tags as $iter34) { + $xfer += $iter34->write($output); + } + $output->writeListEnd(); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; } } diff --git a/src/Jaeger/Thrift/Span.php b/src/Jaeger/Thrift/Span.php index 4695ced..f04e952 100644 --- a/src/Jaeger/Thrift/Span.php +++ b/src/Jaeger/Thrift/Span.php @@ -1,213 +1,400 @@ array( + 'var' => 'traceIdLow', + 'isRequired' => true, + 'type' => TType::I64, + ), + 2 => array( + 'var' => 'traceIdHigh', + 'isRequired' => true, + 'type' => TType::I64, + ), + 3 => array( + 'var' => 'spanId', + 'isRequired' => true, + 'type' => TType::I64, + ), + 4 => array( + 'var' => 'parentSpanId', + 'isRequired' => true, + 'type' => TType::I64, + ), + 5 => array( + 'var' => 'operationName', + 'isRequired' => true, + 'type' => TType::STRING, + ), + 6 => array( + 'var' => 'references', + 'isRequired' => false, + 'type' => TType::LST, + 'etype' => TType::STRUCT, + 'elem' => array( + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\SpanRef', + ), + ), + 7 => array( + 'var' => 'flags', + 'isRequired' => true, + 'type' => TType::I32, + ), + 8 => array( + 'var' => 'startTime', + 'isRequired' => true, + 'type' => TType::I64, + ), + 9 => array( + 'var' => 'duration', + 'isRequired' => true, + 'type' => TType::I64, + ), + 10 => array( + 'var' => 'tags', + 'isRequired' => false, + 'type' => TType::LST, + 'etype' => TType::STRUCT, + 'elem' => array( + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Tag', + ), + ), + 11 => array( + 'var' => 'logs', + 'isRequired' => false, + 'type' => TType::LST, + 'etype' => TType::STRUCT, + 'elem' => array( + 'type' => TType::STRUCT, + 'class' => '\Jaeger\Thrift\Log', + ), + ), + ); + + /** + * @var int + */ + public $traceIdLow = null; + /** + * @var int + */ + public $traceIdHigh = null; + /** + * @var int + */ + public $spanId = null; + /** + * @var int + */ + public $parentSpanId = null; + /** + * @var string + */ + public $operationName = null; + /** + * @var \Jaeger\Thrift\SpanRef[] + */ + public $references = null; + /** + * @var int + */ + public $flags = null; + /** + * @var int + */ + public $startTime = null; + /** + * @var int + */ + public $duration = null; + /** + * @var \Jaeger\Thrift\Tag[] + */ + public $tags = null; + /** + * @var \Jaeger\Thrift\Log[] + */ + public $logs = null; + + public function __construct($vals = null) { - } - - public static function getInstance() - { - if (!(self::$instance instanceof self)) { - self::$instance = new self(); + if (is_array($vals)) { + if (isset($vals['traceIdLow'])) { + $this->traceIdLow = $vals['traceIdLow']; + } + if (isset($vals['traceIdHigh'])) { + $this->traceIdHigh = $vals['traceIdHigh']; + } + if (isset($vals['spanId'])) { + $this->spanId = $vals['spanId']; + } + if (isset($vals['parentSpanId'])) { + $this->parentSpanId = $vals['parentSpanId']; + } + if (isset($vals['operationName'])) { + $this->operationName = $vals['operationName']; + } + if (isset($vals['references'])) { + $this->references = $vals['references']; + } + if (isset($vals['flags'])) { + $this->flags = $vals['flags']; + } + if (isset($vals['startTime'])) { + $this->startTime = $vals['startTime']; + } + if (isset($vals['duration'])) { + $this->duration = $vals['duration']; + } + if (isset($vals['tags'])) { + $this->tags = $vals['tags']; + } + if (isset($vals['logs'])) { + $this->logs = $vals['logs']; + } } - - return self::$instance; } - public function setThriftSpan($thriftSpan = []) + public function getName() { - self::$thriftSpan = $thriftSpan; + return 'Span'; } - public function getThriftSpan() - { - return self::$thriftSpan; - } - public function write(TProtocol $t) + public function read($input) { - self::$tptl = $t; - if (isset(self::$thriftSpan['wrote']) && self::$thriftSpan['wrote']) { - $tran = self::$tptl->getTransport(); - $tran->write(self::$thriftSpan['wrote']); - } else { - $this->handleSpan(self::$thriftSpan); + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::I64) { + $xfer += $input->readI64($this->traceIdLow); + } else { + $xfer += $input->skip($ftype); + } + break; + case 2: + if ($ftype == TType::I64) { + $xfer += $input->readI64($this->traceIdHigh); + } else { + $xfer += $input->skip($ftype); + } + break; + case 3: + if ($ftype == TType::I64) { + $xfer += $input->readI64($this->spanId); + } else { + $xfer += $input->skip($ftype); + } + break; + case 4: + if ($ftype == TType::I64) { + $xfer += $input->readI64($this->parentSpanId); + } else { + $xfer += $input->skip($ftype); + } + break; + case 5: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->operationName); + } else { + $xfer += $input->skip($ftype); + } + break; + case 6: + if ($ftype == TType::LST) { + $this->references = array(); + $_size7 = 0; + $_etype10 = 0; + $xfer += $input->readListBegin($_etype10, $_size7); + for ($_i11 = 0; $_i11 < $_size7; ++$_i11) { + $elem12 = null; + $elem12 = new \Jaeger\Thrift\SpanRef(); + $xfer += $elem12->read($input); + $this->references []= $elem12; + } + $xfer += $input->readListEnd(); + } else { + $xfer += $input->skip($ftype); + } + break; + case 7: + if ($ftype == TType::I32) { + $xfer += $input->readI32($this->flags); + } else { + $xfer += $input->skip($ftype); + } + break; + case 8: + if ($ftype == TType::I64) { + $xfer += $input->readI64($this->startTime); + } else { + $xfer += $input->skip($ftype); + } + break; + case 9: + if ($ftype == TType::I64) { + $xfer += $input->readI64($this->duration); + } else { + $xfer += $input->skip($ftype); + } + break; + case 10: + if ($ftype == TType::LST) { + $this->tags = array(); + $_size13 = 0; + $_etype16 = 0; + $xfer += $input->readListBegin($_etype16, $_size13); + for ($_i17 = 0; $_i17 < $_size13; ++$_i17) { + $elem18 = null; + $elem18 = new \Jaeger\Thrift\Tag(); + $xfer += $elem18->read($input); + $this->tags []= $elem18; + } + $xfer += $input->readListEnd(); + } else { + $xfer += $input->skip($ftype); + } + break; + case 11: + if ($ftype == TType::LST) { + $this->logs = array(); + $_size19 = 0; + $_etype22 = 0; + $xfer += $input->readListBegin($_etype22, $_size19); + for ($_i23 = 0; $_i23 < $_size19; ++$_i23) { + $elem24 = null; + $elem24 = new \Jaeger\Thrift\Log(); + $xfer += $elem24->read($input); + $this->logs []= $elem24; + } + $xfer += $input->readListEnd(); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); } + $xfer += $input->readStructEnd(); + return $xfer; } - public function read(TProtocol $t) - { - // TODO: Implement read() method. - } - - private function handleSpan($span) + public function write($output) { - self::$tptl->writeStructBegin('Span'); - - self::$tptl->writeFieldBegin('traceIdLow', TType::I64, 1); - self::$tptl->writeI64($span['traceIdLow']); - self::$tptl->writeFieldEnd(); - - self::$tptl->writeFieldBegin('traceIdHigh', TType::I64, 2); - self::$tptl->writeI64($span['traceIdHigh']); - self::$tptl->writeFieldEnd(); - - self::$tptl->writeFieldBegin('spanId', TType::I64, 3); - self::$tptl->writeI64($span['spanId']); - self::$tptl->writeFieldEnd(); - - self::$tptl->writeFieldBegin('parentSpanId', TType::I64, 4); - self::$tptl->writeI64($span['parentSpanId']); - self::$tptl->writeFieldEnd(); - - self::$tptl->writeFieldBegin('operationName', TType::STRING, 5); - self::$tptl->writeString($span['operationName']); - self::$tptl->writeFieldEnd(); - - if (isset($span['references'])) { - $this->handleSpanRefes($span['references']); + $xfer = 0; + $xfer += $output->writeStructBegin('Span'); + if ($this->traceIdLow !== null) { + $xfer += $output->writeFieldBegin('traceIdLow', TType::I64, 1); + $xfer += $output->writeI64($this->traceIdLow); + $xfer += $output->writeFieldEnd(); } - - self::$tptl->writeFieldBegin('flags', TType::I32, 7); - self::$tptl->writeI32($span['flags']); - self::$tptl->writeFieldEnd(); - - self::$tptl->writeFieldBegin('startTime', TType::I64, 8); - self::$tptl->writeI64($span['startTime']); - self::$tptl->writeFieldEnd(); - - self::$tptl->writeFieldBegin('duration', TType::I64, 9); - self::$tptl->writeI64($span['duration']); - self::$tptl->writeFieldEnd(); - - if (isset($span['tags'])) { - $this->handleSpanTags($span['tags']); + if ($this->traceIdHigh !== null) { + $xfer += $output->writeFieldBegin('traceIdHigh', TType::I64, 2); + $xfer += $output->writeI64($this->traceIdHigh); + $xfer += $output->writeFieldEnd(); } - - if (isset($span['logs'])) { - $this->handleSpanLogs($span['logs']); + if ($this->spanId !== null) { + $xfer += $output->writeFieldBegin('spanId', TType::I64, 3); + $xfer += $output->writeI64($this->spanId); + $xfer += $output->writeFieldEnd(); } - - self::$tptl->writeFieldStop(); - self::$tptl->writeStructEnd(); - } - - private function handleSpanLogs($logs) - { - self::$tptl->writeFieldBegin('logs', TType::LST, 11); - self::$tptl->writeListBegin(TType::STRUCT, count($logs)); - - foreach ($logs as $log) { - $this->handleLog($log); + if ($this->parentSpanId !== null) { + $xfer += $output->writeFieldBegin('parentSpanId', TType::I64, 4); + $xfer += $output->writeI64($this->parentSpanId); + $xfer += $output->writeFieldEnd(); } - - self::$tptl->writeListEnd(); - self::$tptl->writeFieldEnd(); - } - - private function handleLog($log) - { - self::$tptl->writeStructBegin('Log'); - - self::$tptl->writeFieldBegin('timestamp', TType::I64, 1); - self::$tptl->writeI64($log['timestamp']); - self::$tptl->writeFieldEnd(); - - $this->handleLogFields($log['fields']); - - self::$tptl->writeFieldStop(); - self::$tptl->writeStructEnd(); - } - - private function handleLogFields($fields) - { - self::$tptl->writeFieldBegin('fields', TType::LST, 2); - self::$tptl->writeListBegin(TType::STRUCT, count($fields)); - - $tagsObj = Tags::getInstance(); - $tagsObj->setThriftTags($fields); - $tagsObj->write(self::$tptl); - - self::$tptl->writeListEnd(); - self::$tptl->writeFieldEnd(); - } - - private function handleSpanTags($tags) - { - self::$tptl->writeFieldBegin('tags', TType::LST, 10); - self::$tptl->writeListBegin(TType::STRUCT, count($tags)); - - $tagsObj = Tags::getInstance(); - $tagsObj->setThriftTags($tags); - $tagsObj->write(self::$tptl); - - self::$tptl->writeListEnd(); - self::$tptl->writeFieldEnd(); - } - - private function handleSpanRefes($references) - { - self::$tptl->writeFieldBegin('references', TType::LST, 6); - self::$tptl->writeListBegin(TType::STRUCT, count($references)); - - foreach ($references as $refe) { - $this->handleSpanRefe($refe); + if ($this->operationName !== null) { + $xfer += $output->writeFieldBegin('operationName', TType::STRING, 5); + $xfer += $output->writeString($this->operationName); + $xfer += $output->writeFieldEnd(); } - - self::$tptl->writeListEnd(); - self::$tptl->writeFieldEnd(); - } - - private function handleSpanRefe($refe) - { - self::$tptl->writeStructBegin('SpanRef'); - - self::$tptl->writeFieldBegin('refType', TType::I32, 1); - self::$tptl->writeI32($refe['refType']); - self::$tptl->writeFieldEnd(); - - self::$tptl->writeFieldBegin('traceIdLow', TType::I64, 2); - self::$tptl->writeI64($refe['traceIdLow']); - self::$tptl->writeFieldEnd(); - - self::$tptl->writeFieldBegin('traceIdHigh', TType::I64, 3); - self::$tptl->writeI64($refe['traceIdHigh']); - self::$tptl->writeFieldEnd(); - - self::$tptl->writeFieldBegin('spanId', TType::I64, 4); - self::$tptl->writeI64($refe['spanId']); - self::$tptl->writeFieldEnd(); - - self::$tptl->writeFieldStop(); - self::$tptl->writeStructEnd(); + if ($this->references !== null) { + if (!is_array($this->references)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('references', TType::LST, 6); + $output->writeListBegin(TType::STRUCT, count($this->references)); + foreach ($this->references as $iter25) { + $xfer += $iter25->write($output); + } + $output->writeListEnd(); + $xfer += $output->writeFieldEnd(); + } + if ($this->flags !== null) { + $xfer += $output->writeFieldBegin('flags', TType::I32, 7); + $xfer += $output->writeI32($this->flags); + $xfer += $output->writeFieldEnd(); + } + if ($this->startTime !== null) { + $xfer += $output->writeFieldBegin('startTime', TType::I64, 8); + $xfer += $output->writeI64($this->startTime); + $xfer += $output->writeFieldEnd(); + } + if ($this->duration !== null) { + $xfer += $output->writeFieldBegin('duration', TType::I64, 9); + $xfer += $output->writeI64($this->duration); + $xfer += $output->writeFieldEnd(); + } + if ($this->tags !== null) { + if (!is_array($this->tags)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('tags', TType::LST, 10); + $output->writeListBegin(TType::STRUCT, count($this->tags)); + foreach ($this->tags as $iter26) { + $xfer += $iter26->write($output); + } + $output->writeListEnd(); + $xfer += $output->writeFieldEnd(); + } + if ($this->logs !== null) { + if (!is_array($this->logs)) { + throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA); + } + $xfer += $output->writeFieldBegin('logs', TType::LST, 11); + $output->writeListBegin(TType::STRUCT, count($this->logs)); + foreach ($this->logs as $iter27) { + $xfer += $iter27->write($output); + } + $output->writeListEnd(); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; } } diff --git a/src/Jaeger/Thrift/SpanRef.php b/src/Jaeger/Thrift/SpanRef.php new file mode 100644 index 0000000..407bf4d --- /dev/null +++ b/src/Jaeger/Thrift/SpanRef.php @@ -0,0 +1,166 @@ + array( + 'var' => 'refType', + 'isRequired' => true, + 'type' => TType::I32, + ), + 2 => array( + 'var' => 'traceIdLow', + 'isRequired' => true, + 'type' => TType::I64, + ), + 3 => array( + 'var' => 'traceIdHigh', + 'isRequired' => true, + 'type' => TType::I64, + ), + 4 => array( + 'var' => 'spanId', + 'isRequired' => true, + 'type' => TType::I64, + ), + ); + + /** + * @var int + */ + public $refType = null; + /** + * @var int + */ + public $traceIdLow = null; + /** + * @var int + */ + public $traceIdHigh = null; + /** + * @var int + */ + public $spanId = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['refType'])) { + $this->refType = $vals['refType']; + } + if (isset($vals['traceIdLow'])) { + $this->traceIdLow = $vals['traceIdLow']; + } + if (isset($vals['traceIdHigh'])) { + $this->traceIdHigh = $vals['traceIdHigh']; + } + if (isset($vals['spanId'])) { + $this->spanId = $vals['spanId']; + } + } + } + + public function getName() + { + return 'SpanRef'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::I32) { + $xfer += $input->readI32($this->refType); + } else { + $xfer += $input->skip($ftype); + } + break; + case 2: + if ($ftype == TType::I64) { + $xfer += $input->readI64($this->traceIdLow); + } else { + $xfer += $input->skip($ftype); + } + break; + case 3: + if ($ftype == TType::I64) { + $xfer += $input->readI64($this->traceIdHigh); + } else { + $xfer += $input->skip($ftype); + } + break; + case 4: + if ($ftype == TType::I64) { + $xfer += $input->readI64($this->spanId); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('SpanRef'); + if ($this->refType !== null) { + $xfer += $output->writeFieldBegin('refType', TType::I32, 1); + $xfer += $output->writeI32($this->refType); + $xfer += $output->writeFieldEnd(); + } + if ($this->traceIdLow !== null) { + $xfer += $output->writeFieldBegin('traceIdLow', TType::I64, 2); + $xfer += $output->writeI64($this->traceIdLow); + $xfer += $output->writeFieldEnd(); + } + if ($this->traceIdHigh !== null) { + $xfer += $output->writeFieldBegin('traceIdHigh', TType::I64, 3); + $xfer += $output->writeI64($this->traceIdHigh); + $xfer += $output->writeFieldEnd(); + } + if ($this->spanId !== null) { + $xfer += $output->writeFieldBegin('spanId', TType::I64, 4); + $xfer += $output->writeI64($this->spanId); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/SpanRefType.php b/src/Jaeger/Thrift/SpanRefType.php index accf2ce..d657a92 100644 --- a/src/Jaeger/Thrift/SpanRefType.php +++ b/src/Jaeger/Thrift/SpanRefType.php @@ -1,23 +1,30 @@ 'CHILD_OF', + 1 => 'FOLLOWS_FROM', + ); } + diff --git a/src/Jaeger/Thrift/TStruct.php b/src/Jaeger/Thrift/TStruct.php deleted file mode 100644 index 7a4361c..0000000 --- a/src/Jaeger/Thrift/TStruct.php +++ /dev/null @@ -1,25 +0,0 @@ - array( + 'var' => 'key', + 'isRequired' => true, + 'type' => TType::STRING, + ), + 2 => array( + 'var' => 'vType', + 'isRequired' => true, + 'type' => TType::I32, + ), + 3 => array( + 'var' => 'vStr', + 'isRequired' => false, + 'type' => TType::STRING, + ), + 4 => array( + 'var' => 'vDouble', + 'isRequired' => false, + 'type' => TType::DOUBLE, + ), + 5 => array( + 'var' => 'vBool', + 'isRequired' => false, + 'type' => TType::BOOL, + ), + 6 => array( + 'var' => 'vLong', + 'isRequired' => false, + 'type' => TType::I64, + ), + 7 => array( + 'var' => 'vBinary', + 'isRequired' => false, + 'type' => TType::STRING, + ), + ); + + /** + * @var string + */ + public $key = null; + /** + * @var int + */ + public $vType = null; + /** + * @var string + */ + public $vStr = null; + /** + * @var double + */ + public $vDouble = null; + /** + * @var bool + */ + public $vBool = null; + /** + * @var int + */ + public $vLong = null; + /** + * @var string + */ + public $vBinary = null; + + public function __construct($vals = null) + { + if (is_array($vals)) { + if (isset($vals['key'])) { + $this->key = $vals['key']; + } + if (isset($vals['vType'])) { + $this->vType = $vals['vType']; + } + if (isset($vals['vStr'])) { + $this->vStr = $vals['vStr']; + } + if (isset($vals['vDouble'])) { + $this->vDouble = $vals['vDouble']; + } + if (isset($vals['vBool'])) { + $this->vBool = $vals['vBool']; + } + if (isset($vals['vLong'])) { + $this->vLong = $vals['vLong']; + } + if (isset($vals['vBinary'])) { + $this->vBinary = $vals['vBinary']; + } + } + } + + public function getName() + { + return 'Tag'; + } + + + public function read($input) + { + $xfer = 0; + $fname = null; + $ftype = 0; + $fid = 0; + $xfer += $input->readStructBegin($fname); + while (true) { + $xfer += $input->readFieldBegin($fname, $ftype, $fid); + if ($ftype == TType::STOP) { + break; + } + switch ($fid) { + case 1: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->key); + } else { + $xfer += $input->skip($ftype); + } + break; + case 2: + if ($ftype == TType::I32) { + $xfer += $input->readI32($this->vType); + } else { + $xfer += $input->skip($ftype); + } + break; + case 3: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->vStr); + } else { + $xfer += $input->skip($ftype); + } + break; + case 4: + if ($ftype == TType::DOUBLE) { + $xfer += $input->readDouble($this->vDouble); + } else { + $xfer += $input->skip($ftype); + } + break; + case 5: + if ($ftype == TType::BOOL) { + $xfer += $input->readBool($this->vBool); + } else { + $xfer += $input->skip($ftype); + } + break; + case 6: + if ($ftype == TType::I64) { + $xfer += $input->readI64($this->vLong); + } else { + $xfer += $input->skip($ftype); + } + break; + case 7: + if ($ftype == TType::STRING) { + $xfer += $input->readString($this->vBinary); + } else { + $xfer += $input->skip($ftype); + } + break; + default: + $xfer += $input->skip($ftype); + break; + } + $xfer += $input->readFieldEnd(); + } + $xfer += $input->readStructEnd(); + return $xfer; + } + + public function write($output) + { + $xfer = 0; + $xfer += $output->writeStructBegin('Tag'); + if ($this->key !== null) { + $xfer += $output->writeFieldBegin('key', TType::STRING, 1); + $xfer += $output->writeString($this->key); + $xfer += $output->writeFieldEnd(); + } + if ($this->vType !== null) { + $xfer += $output->writeFieldBegin('vType', TType::I32, 2); + $xfer += $output->writeI32($this->vType); + $xfer += $output->writeFieldEnd(); + } + if ($this->vStr !== null) { + $xfer += $output->writeFieldBegin('vStr', TType::STRING, 3); + $xfer += $output->writeString($this->vStr); + $xfer += $output->writeFieldEnd(); + } + if ($this->vDouble !== null) { + $xfer += $output->writeFieldBegin('vDouble', TType::DOUBLE, 4); + $xfer += $output->writeDouble($this->vDouble); + $xfer += $output->writeFieldEnd(); + } + if ($this->vBool !== null) { + $xfer += $output->writeFieldBegin('vBool', TType::BOOL, 5); + $xfer += $output->writeBool($this->vBool); + $xfer += $output->writeFieldEnd(); + } + if ($this->vLong !== null) { + $xfer += $output->writeFieldBegin('vLong', TType::I64, 6); + $xfer += $output->writeI64($this->vLong); + $xfer += $output->writeFieldEnd(); + } + if ($this->vBinary !== null) { + $xfer += $output->writeFieldBegin('vBinary', TType::STRING, 7); + $xfer += $output->writeString($this->vBinary); + $xfer += $output->writeFieldEnd(); + } + $xfer += $output->writeFieldStop(); + $xfer += $output->writeStructEnd(); + return $xfer; + } +} diff --git a/src/Jaeger/Thrift/TagType.php b/src/Jaeger/Thrift/TagType.php new file mode 100644 index 0000000..c526ada --- /dev/null +++ b/src/Jaeger/Thrift/TagType.php @@ -0,0 +1,39 @@ + 'STRING', + 1 => 'DOUBLE', + 2 => 'BOOL', + 3 => 'LONG', + 4 => 'BINARY', + ); +} + diff --git a/src/Jaeger/Thrift/Tags.php b/src/Jaeger/Thrift/Tags.php deleted file mode 100644 index 34b0b64..0000000 --- a/src/Jaeger/Thrift/Tags.php +++ /dev/null @@ -1,171 +0,0 @@ -thriftTags)) { - return false; - } - - foreach ($this->thriftTags as $tag) { - self::$tptl->writeStructBegin('Tag'); - - if (isset($tag['key'])) { - self::$tptl->writeFieldBegin('key', TType::STRING, 1); - self::$tptl->writeString(strval($tag['key'])); - self::$tptl->writeFieldEnd(); - } - - if (isset($tag['vType'])) { - self::$tptl->writeFieldBegin('vType', TType::I32, 2); - self::$tptl->writeI32(Types::stringToTagType($tag['vType'])); - self::$tptl->writeFieldEnd(); - } - - if (isset($tag['vStr'])) { - self::$tptl->writeFieldBegin('vStr', TType::STRING, 3); - self::$tptl->writeString($tag['vStr']); - self::$tptl->writeFieldEnd(); - } - - if (isset($tag['vDouble'])) { - self::$tptl->writeFieldBegin('vDouble', TType::DOUBLE, 4); - self::$tptl->writeDouble($tag['vDouble']); - self::$tptl->writeFieldEnd(); - } - - if (isset($tag['vBool'])) { - self::$tptl->writeFieldBegin('vBool', TType::BOOL, 5); - self::$tptl->writeBool($tag['vBool']); - self::$tptl->writeFieldEnd(); - } - - if (isset($tag['vLong'])) { - self::$tptl->writeFieldBegin('vLong', TType::I64, 6); - self::$tptl->writeI64($tag['vLong']); - self::$tptl->writeFieldEnd(); - } - - if (isset($tag['vBinary'])) { - self::$tptl->writeFieldBegin('vBinary', TType::STRING, 7); - self::$tptl->writeByte($tag['vBinary']); - self::$tptl->writeFieldEnd(); - } - - self::$tptl->writeFieldStop(); - self::$tptl->writeStructEnd(); - } - - return true; - } - - public function read(TProtocol $t) - { - } - - public function setThriftTags($thriftTags) - { - $this->thriftTags = $thriftTags; - } - - public function setTags($tags) - { - $this->tags = $tags; - } - - public function buildTags() - { - $thriftTags = []; - if (empty($this->tags)) { - return $thriftTags; - } - - foreach ($this->tags as $k => $v) { - switch (gettype($v)) { - case 'string': - $thriftTags[] = [ - 'key' => $k, - 'vType' => 'STRING', - 'vStr' => $v, - ]; - break; - case 'boolean': - $thriftTags[] = [ - 'key' => $k, - 'vType' => 'BOOL', - 'vBool' => $v, - ]; - break; - case 'integer': - case 'double': - $thriftTags[] = [ - 'key' => $k, - 'vType' => 'DOUBLE', - 'vDouble' => $v, - ]; - break; - case 'array': - $thriftTags[] = [ - 'key' => $k, - 'vType' => 'STRING', - 'vStr' => json_encode($v, JSON_UNESCAPED_UNICODE), - ]; - break; - default: - $thriftTags[] = [ - 'key' => $k, - 'vType' => 'STRING', - 'vStr' => is_string($v), - ]; - } - } - - return $thriftTags; - } -} diff --git a/src/Jaeger/Thrift/Types.php b/src/Jaeger/Thrift/Types.php deleted file mode 100644 index a465efe..0000000 --- a/src/Jaeger/Thrift/Types.php +++ /dev/null @@ -1,61 +0,0 @@ -tran = new TMemoryBuffer(); - $this->thriftProtocol = new TCompactProtocol($this->tran); - } - public function buildAndCalcSizeOfProcessThrift(Jaeger $jaeger) - { - $jaeger->processThrift = (new JaegerThriftSpan())->buildJaegerProcessThrift($jaeger); - $jaeger->process = (new Process($jaeger->processThrift)); - $this->procesSize = $this->getAndCalcSizeOfSerializedThrift($jaeger->process, $jaeger->processThrift); - $this->bufferSize += $this->procesSize; + $this->protocol = new TCompactProtocol($this->tran); + + $this->agentClient = new AgentClient($this->protocol, null); + + $this->udpClient = new UdpClient(self::$hostPort, $this->agentClient, $this->tran); + + $this->jaegerThrift = new JaegerThrift(); } /** @@ -79,69 +107,68 @@ public function buildAndCalcSizeOfProcessThrift(Jaeger $jaeger) */ public function append(Jaeger $jaeger) { - if (null == $jaeger->process) { + if (null == $this->process) { $this->buildAndCalcSizeOfProcessThrift($jaeger); } $thriftSpansBuffer = []; // Uncommitted span used to temporarily store shards foreach ($jaeger->spans as $span) { - $spanThrift = (new JaegerThriftSpan())->buildJaegerSpanThrift($span); - - $agentSpan = Span::getInstance(); - $agentSpan->setThriftSpan($spanThrift); - $spanSize = $this->getAndCalcSizeOfSerializedThrift($agentSpan, $spanThrift); - + $spanThrift = $this->jaegerThrift->buildSpanThrift($span); + $spanSize = $this->getAndCalcSizeOfSerializedThrift($spanThrift); if ($spanSize > self::$maxSpanBytes) { //throw new \Exception("Span is too large"); continue; } - if ($this->bufferSize + $spanSize >= self::$maxSpanBytes) { - self::$batchs[] = [ - 'thriftProcess' => $jaeger->processThrift, - 'thriftSpans' => $thriftSpansBuffer, - ]; + $thriftSpansBuffer[] = $spanThrift; + $this->bufferSize += $spanSize; + + if ($this->bufferSize >= self::$maxSpanBytes) { + self::$batch = new Batch([ + 'process' => $this->process, + 'spans' => $thriftSpansBuffer, + ]); $this->flush(); $thriftSpansBuffer = []; // Empty the temp buffer } - - $thriftSpansBuffer[] = $spanThrift; - $this->bufferSize += $spanSize; } - if ($thriftSpansBuffer) { - self::$batchs[] = [ - 'thriftProcess' => $jaeger->processThrift, - 'thriftSpans' => $thriftSpansBuffer, - ]; + if (count($thriftSpansBuffer) > 0) { + self::$batch = new Batch([ + 'process' => $this->process, + 'spans' => $thriftSpansBuffer, + ]); $this->flush(); } + $this->process = null; + return true; } - public function resetBuffer() + public function buildAndCalcSizeOfProcessThrift(Jaeger $jaeger) { - $this->bufferSize = $this->procesSize; - self::$batchs = []; + $this->process = $this->jaegerThrift->buildProcessThrift($jaeger); + $this->procesSize = $this->getAndCalcSizeOfSerializedThrift($this->process); + $this->bufferSize += $this->procesSize; } /** - * 获取序列化后的thrift和计算序列化后的thrift字符长度. + * 计算序列化后的thrift字符长度. * * @param mixed $serializedThrift * * @return mixed */ - private function getAndCalcSizeOfSerializedThrift(TStruct $ts, &$serializedThrift) + private function getAndCalcSizeOfSerializedThrift($thrift) { - $ts->write($this->thriftProtocol); - $serThriftStrlen = $this->tran->available(); - //获取后buf清空 - $serializedThrift['wrote'] = $this->tran->read(Constants\UDP_PACKET_MAX_LENGTH); + $thrift->write($this->protocol); + $len = $this->tran->available(); + // 清空buf, 避免重复序列化 + $this->tran->read($len); - return $serThriftStrlen; + return $len; } /** @@ -149,27 +176,21 @@ private function getAndCalcSizeOfSerializedThrift(TStruct $ts, &$serializedThrif */ public function flush() { - $batchNum = count(self::$batchs); - if ($batchNum <= 0) { + if (self::$batch == null) { return 0; } - $spanNum = 0; - $udp = new UdpClient(self::$hostPort, new AgentClient()); - - foreach (self::$batchs as $batch) { - $spanNum += count($batch['thriftSpans']); - $udp->emitBatch($batch); - } + $spanNum = count(self::$batch->spans); + $this->udpClient->emitBatch(self::$batch); - $udp->close(); $this->resetBuffer(); return $spanNum; } - public function getBatchs() + public function resetBuffer() { - return self::$batchs; + $this->bufferSize = $this->procesSize; + self::$batch = null; } } diff --git a/src/Jaeger/UdpClient.php b/src/Jaeger/UdpClient.php index f37b76b..c877edd 100644 --- a/src/Jaeger/UdpClient.php +++ b/src/Jaeger/UdpClient.php @@ -15,7 +15,8 @@ namespace Jaeger; -use Jaeger\Thrift\AgentClient; +use Jaeger\Thrift\Agent\AgentClient; +use Thrift\Transport\TMemoryBuffer; /** * send thrift to jaeger-agent @@ -27,15 +28,27 @@ class UdpClient private $post = ''; + /** + * @var resource|string + */ private $socket = ''; + /** + * @var AgentClient|null + */ private $agentClient = null; - public function __construct($hostPost, AgentClient $agentClient) + /** + * @var null|TMemoryBuffer + */ + private $tran = null; + + public function __construct(string $hostPost, AgentClient $agentClient, TMemoryBuffer $tran) { [$this->host, $this->post] = explode(':', $hostPost); $this->agentClient = $agentClient; - $this->socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); + $this->socket = fsockopen("udp://$this->host", $this->post); + $this->tran = $tran; } /** @@ -55,13 +68,12 @@ public function isOpen() * * @throws \Exception */ - public function emitBatch($batch) + public function emitBatch(\Jaeger\Thrift\Batch $batch) { - $buildThrift = $this->agentClient->buildThrift($batch); - if (isset($buildThrift['len']) && $buildThrift['len'] && $this->isOpen()) { - $len = $buildThrift['len']; - $enitThrift = $buildThrift['thriftStr']; - $res = socket_sendto($this->socket, $enitThrift, $len, 0, $this->host, $this->post); + $this->agentClient->emitBatch($batch); + $len = $this->tran->available(); + if ($len > 0 && $this->isOpen()) { + $res = fwrite($this->socket, $this->tran->getBuffer()); if (false === $res) { throw new \Exception('emit failse'); } @@ -74,7 +86,7 @@ public function emitBatch($batch) public function close() { - socket_close($this->socket); + fclose($this->socket); $this->socket = null; } } diff --git a/tests/Transport/TransportUdpTest.php b/tests/Transport/TransportUdpTest.php index 66e97fa..893e91b 100644 --- a/tests/Transport/TransportUdpTest.php +++ b/tests/Transport/TransportUdpTest.php @@ -17,23 +17,68 @@ use Jaeger\Transport\TransportUdp; use PHPUnit\Framework\TestCase; +use Jaeger\Jaeger; +use Jaeger\Reporter\RemoteReporter; +use Jaeger\Sampler\ConstSampler; +use Jaeger\ScopeManager; class TransportUdpTest extends TestCase { public $tran = null; + public $tracer = null; + public function setUp() { $this->tran = new TransportUdp('localhost:6831'); - } -// public function testFlush(){ -// $this->tran->append(); -// } + $reporter = new RemoteReporter($this->tran); + $sampler = new ConstSampler(); + $scopeManager = new ScopeManager(); + + $this->tracer = new Jaeger('jaeger', $reporter, $sampler, $scopeManager); + } public function testResetBuffer() { $this->tran->resetBuffer(); $this->assertCount(0, $this->tran->getBatchs()); } + + public function testBuildAndCalcSizeOfProcessThrift() + { + $span = $this->tracer->startSpan('BuildAndCalcSizeOfProcessThrift'); + $span->finish(); + $this->tran->buildAndCalcSizeOfProcessThrift($this->tracer); + $this->assertEquals(95, $this->tran->procesSize); + } + + public function testSpanIsTooLarge() + { + $this->tran::$maxSpanBytes = 50; + $span = $this->tracer->startSpan('SpanIsTooLarge'); + $span->finish(); + $this->tran->append($this->tracer); + $this->assertEquals(0, $this->tran->flushSpanNum); + } + + public function testSplitEmit() + { + $i = 0; + $this->tran::$maxSpanBytes = 150; + $span = $this->tracer->startSpan('SplitEmit1'); + $i++; + $span->finish(); + + $span = $this->tracer->startSpan('SplitEmit2'); + $i++; + $span->finish(); + + $span = $this->tracer->startSpan('SplitEmit3'); + $i++; + $span->finish(); + + $this->tran->append($this->tracer); + $this->assertEquals(3, $this->tran->flushSpanNum); + } } diff --git a/tests/UdpClientTest.php b/tests/UdpClientTest.php index 8be2a13..57e820e 100644 --- a/tests/UdpClientTest.php +++ b/tests/UdpClientTest.php @@ -15,20 +15,40 @@ namespace tests; -use Jaeger\Thrift\AgentClient; +use Jaeger\Thrift\Agent\AgentClient; use Jaeger\UdpClient; use PHPUnit\Framework\TestCase; +use Thrift\Protocol\TCompactProtocol; +use Thrift\Transport\TMemoryBuffer; class UdpClientTest extends TestCase { + /** + * @var UdpClient|null + */ public $udpClient = null; + /** + * @var AgentClient|null + */ public $agentClient = null; + /** + * @var TMemoryBuffer|null + */ + public $tran = null; + + /** + * @var TCompactProtocol|null + */ + public $protocol = null; + public function setUp() { - $this->agentClient = $this->createMock(AgentClient::class); - $this->udpClient = new UdpClient('localhost:6831', $this->agentClient); + $this->tran = new TMemoryBuffer(); + $this->protocol = new TCompactProtocol($this->tran); + $this->agentClient = (new AgentClient($this->protocol, null)); + $this->udpClient = new UdpClient('localhost:6831', $this->agentClient, $this->tran); } public function testIsOpen() @@ -36,34 +56,61 @@ public function testIsOpen() $this->assertTrue($this->udpClient->isOpen()); } - public function testEmitBatch() - { - $this->agentClient->expects($this->once())->method('buildThrift') - ->willReturn(['len' => 3, 'thriftStr' => 123]); - $batch = ['thriftProcess' => '', 'thriftSpans' => '']; - - $this->assertTrue($this->udpClient->emitBatch($batch)); - } - public function testEmitBatchFalse() + public function testClose() { - $batch = ['thriftProcess' => '', 'thriftSpans' => '']; - - $this->agentClient->expects($this->any())->method('buildThrift') - ->willReturn(['thriftStr' => 123]); - - $this->assertFalse($this->udpClient->emitBatch($batch)); - $this->udpClient->close(); - $this->agentClient->expects($this->any())->method('buildThrift') - ->willReturn(['len' => 3, 'thriftStr' => 123]); - - $this->assertFalse($this->udpClient->emitBatch($batch)); + $this->assertFalse($this->udpClient->isOpen()); } - public function testClose() + + public function testEmitBatch() { - $this->udpClient->close(); - $this->assertFalse($this->udpClient->isOpen()); + $span = new \Jaeger\Thrift\Span( + [ + 'traceIdLow' => 1609214197859399756, + 'traceIdHigh' => 1609214197860113544, + 'spanId' => 1609214197859399756, + 'parentSpanId' => 0, + 'operationName' => 'test', + 'flags' => 1, + 'startTime' => 1609214197860775, + 'duration' => 3216877, + 'tags' => [], + 'logs' => [], + ] + ); + + $batch = new \Jaeger\Thrift\Batch( + [ + 'process' => new \Jaeger\Thrift\Process([ + 'serviceName' => 'EmitBatch', + 'tags' => [ + (new \Jaeger\Thrift\Tag([ + 'key' => 'peer.ipv4', + 'vType' => 0, + 'vStr' => '0.0.0.0', + ])), + (new \Jaeger\Thrift\Tag([ + 'key' => 'peer.port', + 'vType' => 0, + 'vStr' => '80', + ])), + (new \Jaeger\Thrift\Tag([ + 'key' => 'sampler.type', + 'vType' => 0, + 'vStr' => 'const', + ])), + (new \Jaeger\Thrift\Tag([ + 'key' => 'sampler.param', + 'vType' => 2, + 'vBool' => true, + ])), + ], + ]), + 'spans' => [$span], + ] + ); + $this->assertTrue($this->udpClient->emitBatch($batch)); } } From 28cec1a310f79d73b0aee4a1da43785449a8a84b Mon Sep 17 00:00:00 2001 From: "742161455@qq.com" <742161455@qq.com> Date: Tue, 29 Dec 2020 19:12:39 +0800 Subject: [PATCH 19/32] unit test of JaegerThrift --- example/HTTP.php | 2 +- example/Hprose.php | 2 +- example/Istio1.php | 2 +- example/Istio2.php | 2 +- example/Istio3.php | 2 +- src/Jaeger/Config.php | 18 +++++ src/Jaeger/Jaeger.php | 10 ++- src/Jaeger/JaegerThrift.php | 11 +-- src/Jaeger/Propagator/ZipkinPropagator.php | 2 +- tests/JaegerThriftTest.php | 86 ++++++++++++++++++++++ tests/Transport/TransportUdpTest.php | 12 +-- 11 files changed, 127 insertions(+), 22 deletions(-) create mode 100644 tests/JaegerThriftTest.php diff --git a/example/HTTP.php b/example/HTTP.php index 1cecd19..a54120d 100644 --- a/example/HTTP.php +++ b/example/HTTP.php @@ -27,7 +27,7 @@ $config->gen128bit(); -$config::$propagator = Jaeger\Constants\PROPAGATOR_JAEGER; +$config::$propagator = Jaeger\Constants\PROPAGATOR_ZIPKIN; $tracer = $config->initTracer('example', 'localhost:6831'); diff --git a/example/Hprose.php b/example/Hprose.php index 392b483..e10c516 100644 --- a/example/Hprose.php +++ b/example/Hprose.php @@ -13,7 +13,7 @@ * the License. */ -require_once dirname(__FILE__, 4).'/autoload.php'; +require_once dirname(__FILE__, 2).'/vendor/autoload.php'; use Hprose\Client; use Jaeger\Config; diff --git a/example/Istio1.php b/example/Istio1.php index 14c07e6..8b24de8 100644 --- a/example/Istio1.php +++ b/example/Istio1.php @@ -13,7 +13,7 @@ * the License. */ -require_once dirname(__FILE__, 4).'/autoload.php'; +require_once dirname(__FILE__, 2).'/vendor/autoload.php'; use GuzzleHttp\Client; use Jaeger\Config; diff --git a/example/Istio2.php b/example/Istio2.php index b18e4a1..299c076 100644 --- a/example/Istio2.php +++ b/example/Istio2.php @@ -13,7 +13,7 @@ * the License. */ -require_once dirname(__FILE__, 4).'/autoload.php'; +require_once dirname(__FILE__, 2).'/vendor/autoload.php'; use Jaeger\Config; use OpenTracing\Formats; diff --git a/example/Istio3.php b/example/Istio3.php index 40efbaf..6637e2d 100644 --- a/example/Istio3.php +++ b/example/Istio3.php @@ -13,7 +13,7 @@ * the License. */ -require_once dirname(__FILE__, 4).'/autoload.php'; +require_once dirname(__FILE__, 2).'/vendor/autoload.php'; use Jaeger\Config; use OpenTracing\Formats; diff --git a/src/Jaeger/Config.php b/src/Jaeger/Config.php index 583ce26..56c7f2b 100644 --- a/src/Jaeger/Config.php +++ b/src/Jaeger/Config.php @@ -27,18 +27,36 @@ class Config { + /** + * @var \Jaeger\Transport\Transport|null + */ private $transport = null; + /** + * @var \Jaeger\Reporter\Reporter|null + */ private $reporter = null; + /** + * @var \Jaeger\Sampler\Sampler|null + */ private $sampler = null; + /** + * @var \OpenTracing\ScopeManager|null + */ private $scopeManager = null; private $gen128bit = false; + /** + * @var Tracer|null + */ public static $tracer = null; + /** + * @var \OpenTracing\Span|null + */ public static $span = null; public static $instance = null; diff --git a/src/Jaeger/Jaeger.php b/src/Jaeger/Jaeger.php index 1335b26..6caa44d 100644 --- a/src/Jaeger/Jaeger.php +++ b/src/Jaeger/Jaeger.php @@ -21,8 +21,14 @@ class Jaeger implements \OpenTracing\Tracer { + /** + * @var Reporter|null + */ private $reporter = null; + /** + * @var Sampler|null + */ private $sampler = null; private $gen128bit = false; @@ -41,8 +47,8 @@ class Jaeger implements \OpenTracing\Tracer /** @var Propagator|null */ public $propagator = null; - public function __construct(string $serviceName, Reporter $reporter, Sampler $sampler, - ScopeManager $scopeManager) + public function __construct(string $serviceName, Reporter $reporter = null, Sampler $sampler = null, + ScopeManager $scopeManager = null) { $this->reporter = $reporter; diff --git a/src/Jaeger/JaegerThrift.php b/src/Jaeger/JaegerThrift.php index 47895f8..fb7dd97 100644 --- a/src/Jaeger/JaegerThrift.php +++ b/src/Jaeger/JaegerThrift.php @@ -51,7 +51,7 @@ public function buildSpanThrift(jspan $span): Span $spContext = $span->spanContext; assert($spContext instanceof \Jaeger\SpanContext); - $spanVals = [ + $thriftSpan = new Span([ 'traceIdLow' => intval($spContext->traceIdLow), 'traceIdHigh' => intval($spContext->traceIdHigh), 'spanId' => intval($spContext->spanId), @@ -62,13 +62,8 @@ public function buildSpanThrift(jspan $span): Span 'duration' => $span->duration, 'tags' => $this->buildTags($span->tags), 'logs' => $this->buildLogs($span->logs), - ]; - - if($spContext->parentId != 0) { - $spanVals['references'] = $this->buildReferences($span->references); - } - - $thriftSpan = new Span($spanVals); + 'references' => $this->buildReferences($span->references), + ]); return $thriftSpan; } diff --git a/src/Jaeger/Propagator/ZipkinPropagator.php b/src/Jaeger/Propagator/ZipkinPropagator.php index 882fefc..54558be 100644 --- a/src/Jaeger/Propagator/ZipkinPropagator.php +++ b/src/Jaeger/Propagator/ZipkinPropagator.php @@ -23,7 +23,7 @@ class ZipkinPropagator implements Propagator /** * {@inheritDoc} */ - public function inject(SpanContext $spanContext, $format, &$carrier) + public function inject(\OpenTracing\SpanContext $spanContext, $format, &$carrier) { $carrier[Constants\X_B3_TRACEID] = $spanContext->traceIdLowToString(); $carrier[Constants\X_B3_PARENT_SPANID] = $spanContext->parentIdToString(); diff --git a/tests/JaegerThriftTest.php b/tests/JaegerThriftTest.php new file mode 100644 index 0000000..e0e3fbc --- /dev/null +++ b/tests/JaegerThriftTest.php @@ -0,0 +1,86 @@ +tracer = new Jaeger('jaeger', $reporter, $sampler, $scopeManager); + + $this->jaegerThrift = new JaegerThrift(); + } + + public function testBuildProcessThrift(){ + $process = $this->jaegerThrift->buildProcessThrift($this->tracer); + $this->assertEquals('jaeger', $process->serviceName); + } + + public function testBuildTags(){ + $tags = ['event' => 'test']; + $jtags = $this->jaegerThrift->buildTags($tags); + $this->assertEquals('event', $jtags[0]->key); + $this->assertEquals(TagType::STRING, $jtags[0]->vType); + $this->assertEquals('test', $jtags[0]->vStr); + + $tags = ['success' => true]; + $jtags = $this->jaegerThrift->buildTags($tags); + $this->assertEquals('success', $jtags[0]->key); + $this->assertEquals(TagType::BOOL, $jtags[0]->vType); + $this->assertTrue($jtags[0]->vBool); + + $tags = ['data' => [1,2]]; + $jtags = $this->jaegerThrift->buildTags($tags); + $this->assertEquals('data', $jtags[0]->key); + $this->assertEquals(TagType::STRING, $jtags[0]->vType); + + $tags = ['num' => 1]; + $jtags = $this->jaegerThrift->buildTags($tags); + $this->assertEquals('num', $jtags[0]->key); + $this->assertEquals(TagType::LONG, $jtags[0]->vType); + $this->assertEquals(1, $jtags[0]->vDouble); + } + + public function testBuildSpanThrift() { + $span = $this->tracer->startSpan('BuildSpanThrift'); + $jspan = $this->jaegerThrift->buildSpanThrift($span); + $this->assertEquals('BuildSpanThrift', $jspan->operationName); + } +} diff --git a/tests/Transport/TransportUdpTest.php b/tests/Transport/TransportUdpTest.php index 893e91b..6a06f74 100644 --- a/tests/Transport/TransportUdpTest.php +++ b/tests/Transport/TransportUdpTest.php @@ -24,8 +24,14 @@ class TransportUdpTest extends TestCase { + /** + * @var TransportUdp|null + */ public $tran = null; + /** + * @var Jaeger|null + */ public $tracer = null; public function setUp() @@ -39,12 +45,6 @@ public function setUp() $this->tracer = new Jaeger('jaeger', $reporter, $sampler, $scopeManager); } - public function testResetBuffer() - { - $this->tran->resetBuffer(); - $this->assertCount(0, $this->tran->getBatchs()); - } - public function testBuildAndCalcSizeOfProcessThrift() { $span = $this->tracer->startSpan('BuildAndCalcSizeOfProcessThrift'); From 65e36117d6e3fe5c2f54562ff2377fd3f57b35e0 Mon Sep 17 00:00:00 2001 From: "742161455@qq.com" <742161455@qq.com> Date: Wed, 30 Dec 2020 17:30:58 +0800 Subject: [PATCH 20/32] unit test of transportUdp --- src/Jaeger/Constants.php | 2 +- src/Jaeger/Reporter/NullReporter.php | 31 +++++ src/Jaeger/Reporter/RemoteReporter.php | 2 +- src/Jaeger/Sender/NullSender.php | 31 +++++ src/Jaeger/Sender/Sender.php | 24 ++++ .../{UdpClient.php => Sender/UdpSender.php} | 8 +- src/Jaeger/Transport/Transport.php | 2 + src/Jaeger/Transport/TransportUdp.php | 22 ++-- tests/JaegerTest.php | 118 ++++++++---------- .../UdpSenderTest.php} | 18 +-- tests/SpanContextTest.php | 66 +++++----- tests/Transport/TransportUdpTest.php | 23 ++-- 12 files changed, 215 insertions(+), 132 deletions(-) create mode 100644 src/Jaeger/Reporter/NullReporter.php create mode 100644 src/Jaeger/Sender/NullSender.php create mode 100644 src/Jaeger/Sender/Sender.php rename src/Jaeger/{UdpClient.php => Sender/UdpSender.php} (93%) rename tests/{UdpClientTest.php => Sender/UdpSenderTest.php} (88%) diff --git a/src/Jaeger/Constants.php b/src/Jaeger/Constants.php index 4960b4e..de7a8ba 100644 --- a/src/Jaeger/Constants.php +++ b/src/Jaeger/Constants.php @@ -23,7 +23,7 @@ const Jaeger_Debug_Header = 'jaeger-debug-id'; -const EMIT_BATCH_OVER_HEAD = 30; +const EMIT_BATCH_OVER_HEAD = 70; const UDP_PACKET_MAX_LENGTH = 65000; diff --git a/src/Jaeger/Reporter/NullReporter.php b/src/Jaeger/Reporter/NullReporter.php new file mode 100644 index 0000000..ee50900 --- /dev/null +++ b/src/Jaeger/Reporter/NullReporter.php @@ -0,0 +1,31 @@ +tran->flush(); + $this->tran->close(); } } diff --git a/src/Jaeger/Sender/NullSender.php b/src/Jaeger/Sender/NullSender.php new file mode 100644 index 0000000..9e7ff57 --- /dev/null +++ b/src/Jaeger/Sender/NullSender.php @@ -0,0 +1,31 @@ +agentClient->emitBatch($batch); $len = $this->tran->available(); if ($len > 0 && $this->isOpen()) { - $res = fwrite($this->socket, $this->tran->getBuffer()); + $res = fwrite($this->socket, $this->tran->read($len)); if (false === $res) { throw new \Exception('emit failse'); } diff --git a/src/Jaeger/Transport/Transport.php b/src/Jaeger/Transport/Transport.php index b11e47c..48b0b33 100644 --- a/src/Jaeger/Transport/Transport.php +++ b/src/Jaeger/Transport/Transport.php @@ -22,4 +22,6 @@ interface Transport public function append(Jaeger $jaeger); public function flush(); + + public function close(); } diff --git a/src/Jaeger/Transport/TransportUdp.php b/src/Jaeger/Transport/TransportUdp.php index 6ec5c7c..8f291fc 100644 --- a/src/Jaeger/Transport/TransportUdp.php +++ b/src/Jaeger/Transport/TransportUdp.php @@ -18,13 +18,13 @@ use Jaeger\Constants; use Jaeger\Jaeger; use Jaeger\JaegerThrift; +use Jaeger\Sender\UdpSender; use Jaeger\Thrift\Batch; use Jaeger\Thrift\Process; -use Jaeger\UdpClient; +use Jaeger\Sender\Sender; use Thrift\Protocol\TCompactProtocol; use Thrift\Transport\TMemoryBuffer; use Jaeger\Thrift\Agent\AgentClient; -use Jaeger\Thrift\ClientStats; class TransportUdp implements Transport { @@ -34,9 +34,9 @@ class TransportUdp implements Transport private $tran = null; /** - * @var UdpClient|null + * @var Sender|null */ - private $udpClient = null; + private $sender = null; /** * @var AgentClient|null @@ -76,7 +76,7 @@ class TransportUdp implements Transport const MAC_UDP_MAX_SIZE = 9216; - public function __construct($hostport = '', $maxPacketSize = '') + public function __construct($hostport = '', $maxPacketSize = '', Sender $udpSender = null) { if ('' == $hostport) { $hostport = $this->agentServerHostPort; @@ -95,7 +95,10 @@ public function __construct($hostport = '', $maxPacketSize = '') $this->agentClient = new AgentClient($this->protocol, null); - $this->udpClient = new UdpClient(self::$hostPort, $this->agentClient, $this->tran); + $this->sender = $udpSender; + if($this->sender == null) { + $this->sender = new UdpSender(self::$hostPort, $this->agentClient, $this->tran); + } $this->jaegerThrift = new JaegerThrift(); } @@ -181,7 +184,7 @@ public function flush() } $spanNum = count(self::$batch->spans); - $this->udpClient->emitBatch(self::$batch); + $this->sender->emitBatch(self::$batch); $this->resetBuffer(); @@ -193,4 +196,9 @@ public function resetBuffer() $this->bufferSize = $this->procesSize; self::$batch = null; } + + public function close() + { + $this->sender->close(); + } } diff --git a/tests/JaegerTest.php b/tests/JaegerTest.php index 301cdbd..a0906d6 100644 --- a/tests/JaegerTest.php +++ b/tests/JaegerTest.php @@ -18,118 +18,115 @@ use Jaeger\Constants; use Jaeger\Jaeger; use Jaeger\Propagator\JaegerPropagator; -use Jaeger\Reporter\RemoteReporter; +use Jaeger\Reporter\NullReporter; use Jaeger\Sampler\ConstSampler; use Jaeger\ScopeManager; use Jaeger\Span; use Jaeger\SpanContext; -use Jaeger\Transport\TransportUdp; use OpenTracing\Formats; use OpenTracing\Reference; use PHPUnit\Framework\TestCase; class JaegerTest extends TestCase { - public function getJaeger() + /** + * @var Jaeger|null + */ + public $tracer = null; + + public function setUp() { - $tranSport = new TransportUdp(); - $reporter = new RemoteReporter($tranSport); + $reporter = new NullReporter(); $sampler = new ConstSampler(); $scopeManager = new ScopeManager(); - - return new Jaeger('jaeger', $reporter, $sampler, $scopeManager); + $this->tracer = new Jaeger('jaeger', $reporter, $sampler, $scopeManager); } public function testNew() { - $Jaeger = $this->getJaeger(); - $this->assertInstanceOf(Jaeger::class, $Jaeger); + $this->assertInstanceOf(Jaeger::class, $this->tracer); } public function testGetEnvTags() { $_SERVER['JAEGER_TAGS'] = 'a=b,c=d'; - $Jaeger = $this->getJaeger(); - $tags = $Jaeger->getEnvTags(); - + $tags = $this->tracer->getEnvTags(); $this->assertTrue(count($tags) > 0); } public function testSetTags() { - $Jaeger = $this->getJaeger(); - - $Jaeger->setTags(['version' => '2.0.0']); - $this->assertTrue('2.0.0' == $Jaeger->tags['version']); + $this->tracer->setTags(['version' => '2.0.0']); + $this->assertTrue('2.0.0' == $this->tracer->tags['version']); } public function testInject() { - $Jaeger = $this->getJaeger(); - $Jaeger->setPropagator(new JaegerPropagator()); + $this->tracer->setPropagator(new JaegerPropagator()); $context = new SpanContext(1, 1, 1, null, 1); - $Jaeger->inject($context, Formats\TEXT_MAP, $_SERVER); + $this->tracer->inject($context, Formats\TEXT_MAP, $_SERVER); $this->assertTrue('0:1:1:1' == $_SERVER[strtoupper(Constants\Tracer_State_Header_Name)]); } + /** + * @expectedException \OpenTracing\UnsupportedFormatException + */ public function testInjectUnSupportFormat() { - $Jaeger = $this->getJaeger(); - $Jaeger->setPropagator(new JaegerPropagator()); + $this->tracer->setPropagator(new JaegerPropagator()); $context = new SpanContext(1, 1, 1, null, 1); $this->expectExceptionMessage('The format "http_headers" is not supported.'); - $Jaeger->inject($context, Formats\HTTP_HEADERS, $_SERVER); + $this->tracer->inject($context, Formats\HTTP_HEADERS, $_SERVER); } public function testExtract() { - $Jaeger = $this->getJaeger(); - $Jaeger->setPropagator(new JaegerPropagator()); + $this->tracer->setPropagator(new JaegerPropagator()); $carrier[strtoupper(Constants\Tracer_State_Header_Name)] = '1:1:1:1'; - $spanContext = $Jaeger->extract(Formats\TEXT_MAP, $carrier); + $spanContext = $this->tracer->extract(Formats\TEXT_MAP, $carrier); $this->assertTrue(1 == $spanContext->parentId); $this->assertTrue(1 == $spanContext->traceIdLow); $this->assertTrue(1 == $spanContext->flags); $this->assertTrue(1 == $spanContext->spanId); } + /** + * @expectedException \OpenTracing\UnsupportedFormatException + */ public function testExtractUnSupportFormat() { - $Jaeger = $this->getJaeger(); - $Jaeger->setPropagator(new JaegerPropagator()); + $this->tracer->setPropagator(new JaegerPropagator()); $_SERVER[strtoupper(Constants\Tracer_State_Header_Name)] = '1:1:1:1'; $this->expectExceptionMessage('The format "http_headers" is not supported.'); - $Jaeger->extract(Formats\HTTP_HEADERS, $_SERVER); + $this->tracer->extract(Formats\HTTP_HEADERS, $_SERVER); } public function testStartSpan() { - $Jaeger = $this->getJaeger(); - $span = $Jaeger->startSpan('test'); + $span = $this->tracer->startSpan('test'); $this->assertNotNull($span->startTime); - $this->assertNotEmpty($Jaeger->getSpans()); + $this->assertNotEmpty($this->tracer->getSpans()); } public function testStartSpanWithFollowsFromTypeRef() { - $jaeger = $this->getJaeger(); - $rootSpan = $jaeger->startSpan('root-a'); - $childSpan = $jaeger->startSpan('span-a', [ + $rootSpan = $this->tracer->startSpan('root-a'); + $childSpan = $this->tracer->startSpan('span-a', [ 'references' => Reference::createForSpan(Reference::FOLLOWS_FROM, $rootSpan), ]); $this->assertSame($childSpan->spanContext->traceIdLow, $rootSpan->spanContext->traceIdLow); $this->assertSame(current($childSpan->references)->getSpanContext(), $rootSpan->spanContext); - $otherRootSpan = $jaeger->startSpan('root-a'); - $childSpan = $jaeger->startSpan('span-b', [ + $otherRootSpan = $this->tracer->startSpan('root-a'); + $childSpan = $this->tracer->startSpan('span-b', [ 'references' => [ Reference::createForSpan(Reference::FOLLOWS_FROM, $rootSpan), Reference::createForSpan(Reference::FOLLOWS_FROM, $otherRootSpan), @@ -141,10 +138,9 @@ public function testStartSpanWithFollowsFromTypeRef() public function testStartSpanWithChildOfTypeRef() { - $jaeger = $this->getJaeger(); - $rootSpan = $jaeger->startSpan('root-a'); - $otherRootSpan = $jaeger->startSpan('root-b'); - $childSpan = $jaeger->startSpan('span-a', [ + $rootSpan = $this->tracer->startSpan('root-a'); + $otherRootSpan = $this->tracer->startSpan('root-b'); + $childSpan = $this->tracer->startSpan('span-a', [ 'references' => [ Reference::createForSpan(Reference::CHILD_OF, $rootSpan), Reference::createForSpan(Reference::CHILD_OF, $otherRootSpan), @@ -156,18 +152,16 @@ public function testStartSpanWithChildOfTypeRef() public function testStartSpanWithCustomStartTime() { - $jaeger = $this->getJaeger(); - $span = $jaeger->startSpan('test', ['start_time' => 1499355363.123456]); + $span = $this->tracer->startSpan('test', ['start_time' => 1499355363.123456]); $this->assertSame(1499355363123456, $span->startTime); } public function testStartSpanWithAllRefType() { - $jaeger = $this->getJaeger(); - $rootSpan = $jaeger->startSpan('root-a'); - $otherRootSpan = $jaeger->startSpan('root-b'); - $childSpan = $jaeger->startSpan('span-a', [ + $rootSpan = $this->tracer->startSpan('root-a'); + $otherRootSpan = $this->tracer->startSpan('root-b'); + $childSpan = $this->tracer->startSpan('span-a', [ 'references' => [ Reference::createForSpan(Reference::FOLLOWS_FROM, $rootSpan), Reference::createForSpan(Reference::CHILD_OF, $otherRootSpan), @@ -179,46 +173,38 @@ public function testStartSpanWithAllRefType() public function testReportSpan() { - $Jaeger = $this->getJaeger(); - $Jaeger->startSpan('test'); - $Jaeger->reportSpan(); - $this->assertEmpty($Jaeger->getSpans()); + $this->tracer->startSpan('test'); + $this->tracer->reportSpan(); + $this->assertEmpty($this->tracer->getSpans()); } public function testStartActiveSpan() { - $Jaeger = $this->getJaeger(); - $Jaeger->startActiveSpan('test'); - - $this->assertNotEmpty($Jaeger->getSpans()); + $this->tracer->startActiveSpan('test'); + $this->assertNotEmpty($this->tracer->getSpans()); } public function testGetActiveSpan() { - $Jaeger = $this->getJaeger(); - $Jaeger->startActiveSpan('test'); - - $span = $Jaeger->getActiveSpan(); + $this->tracer->startActiveSpan('test'); + $span = $this->tracer->getActiveSpan(); $this->assertInstanceOf(Span::class, $span); } public function testFlush() { - $Jaeger = $this->getJaeger(); - $Jaeger->startSpan('test'); - $Jaeger->flush(); - $this->assertEmpty($Jaeger->getSpans()); + $this->tracer->startSpan('test'); + $this->tracer->flush(); + $this->assertEmpty($this->tracer->getSpans()); } public function testNestedSpanBaggage() { - $tracer = $this->getJaeger(); - - $parent = $tracer->startSpan('parent'); + $parent = $this->tracer->startSpan('parent'); $parent->addBaggageItem('key', 'value'); - $child = $tracer->startSpan('child', [Reference::CHILD_OF => $parent]); + $child = $this->tracer->startSpan('child', [Reference::CHILD_OF => $parent]); $this->assertEquals($parent->getBaggageItem('key'), $child->getBaggageItem('key')); } diff --git a/tests/UdpClientTest.php b/tests/Sender/UdpSenderTest.php similarity index 88% rename from tests/UdpClientTest.php rename to tests/Sender/UdpSenderTest.php index 57e820e..bcd85e2 100644 --- a/tests/UdpClientTest.php +++ b/tests/Sender/UdpSenderTest.php @@ -16,17 +16,17 @@ namespace tests; use Jaeger\Thrift\Agent\AgentClient; -use Jaeger\UdpClient; +use Jaeger\Sender\UdpSender; use PHPUnit\Framework\TestCase; use Thrift\Protocol\TCompactProtocol; use Thrift\Transport\TMemoryBuffer; -class UdpClientTest extends TestCase +class UdpSenderTest extends TestCase { /** - * @var UdpClient|null + * @var UdpSender|null */ - public $udpClient = null; + public $udpSender = null; /** * @var AgentClient|null @@ -48,19 +48,19 @@ public function setUp() $this->tran = new TMemoryBuffer(); $this->protocol = new TCompactProtocol($this->tran); $this->agentClient = (new AgentClient($this->protocol, null)); - $this->udpClient = new UdpClient('localhost:6831', $this->agentClient, $this->tran); + $this->udpSender = new UdpSender('localhost:6831', $this->agentClient, $this->tran); } public function testIsOpen() { - $this->assertTrue($this->udpClient->isOpen()); + $this->assertTrue($this->udpSender->isOpen()); } public function testClose() { - $this->udpClient->close(); - $this->assertFalse($this->udpClient->isOpen()); + $this->udpSender->close(); + $this->assertFalse($this->udpSender->isOpen()); } @@ -111,6 +111,6 @@ public function testEmitBatch() 'spans' => [$span], ] ); - $this->assertTrue($this->udpClient->emitBatch($batch)); + $this->assertTrue($this->udpSender->emitBatch($batch)); } } diff --git a/tests/SpanContextTest.php b/tests/SpanContextTest.php index 2dff2fe..632efe0 100644 --- a/tests/SpanContextTest.php +++ b/tests/SpanContextTest.php @@ -20,78 +20,76 @@ class SpanContextTest extends TestCase { - public function getSpanContext() + /** + * @var SpanContext|null + */ + public $spanContext = null; + + public function setUp() { - return new SpanContext(1, 1, 1, null, 1); + $this->spanContext = new SpanContext(1, 1, 1, null, 1); } public function testNew() { - $spanContext = $this->getSpanContext(); - $this->assertInstanceOf(SpanContext::class, $spanContext); + $this->assertInstanceOf(SpanContext::class, $this->spanContext); } public function testWithBaggageItem() { - $spanContext = $this->getSpanContext(); - $res = $spanContext->withBaggageItem('version', '2.0.0'); + $res = $this->spanContext->withBaggageItem('version', '2.0.0'); // TODO // $this->assertTrue($res); } public function testGetBaggageItem() { - $spanContext = $this->getSpanContext(); - $spanContext->withBaggageItem('version', '2.0.0'); + $this->spanContext->withBaggageItem('version', '2.0.0'); - $version = $spanContext->getBaggageItem('version'); + $version = $this->spanContext->getBaggageItem('version'); $this->assertTrue('2.0.0' === $version); - $service = $spanContext->getBaggageItem('service'); + $service = $this->spanContext->getBaggageItem('service'); $this->assertNull($service); } public function testBuildString() { - $spanContext = $this->getSpanContext(); - $spanContext->traceIdLow = 1; - $this->assertTrue('1:1:1:1' == $spanContext->buildString()); + $this->spanContext->traceIdLow = 1; + $this->assertTrue('1:1:1:1' == $this->spanContext->buildString()); - $spanContext->traceIdHigh = 1; - $this->assertTrue('10000000000000001:1:1:1' == $spanContext->buildString()); + $this->spanContext->traceIdHigh = 1; + $this->assertTrue('10000000000000001:1:1:1' == $this->spanContext->buildString()); } public function testSpanIdToString() { - $spanContext = $this->getSpanContext(); - $this->assertTrue('1' == $spanContext->spanIdToString()); + $this->assertTrue('1' == $this->spanContext->spanIdToString()); - $spanContext->spanId = '111111'; - $this->assertTrue('1b207' == $spanContext->spanIdToString()); + $this->spanContext->spanId = '111111'; + $this->assertTrue('1b207' == $this->spanContext->spanIdToString()); } public function testTraceIdLowToString() { - $spanContext = $this->getSpanContext(); - $spanContext->traceIdLow = '111111'; - $this->assertTrue('1b207' == $spanContext->traceIdLowToString()); + $this->spanContext->traceIdLow = '111111'; + $this->assertTrue('1b207' == $this->spanContext->traceIdLowToString()); - $spanContext->traceIdHigh = '111111'; - $this->assertTrue('1b207000000000001b207' == $spanContext->traceIdLowToString()); + $this->spanContext->traceIdHigh = '111111'; + $this->assertTrue('1b207000000000001b207' == $this->spanContext->traceIdLowToString()); } public function testTraceIdToString() { - $spanContext = $this->getSpanContext(); - $spanContext->traceIdToString('1b207000000000001b207'); - $this->assertTrue('111111' == $spanContext->traceIdLow); - $this->assertTrue('1954685383581106176' == $spanContext->traceIdHigh); - - $spanContext->traceIdLow = null; - $spanContext->traceIdHigh = null; - $spanContext->traceIdToString('1b207'); - $this->assertTrue('111111' == $spanContext->traceIdLow); - $this->assertTrue(null === $spanContext->traceIdHigh); + $this->spanContext->traceIdToString('1b207000000000001b207'); + $this->assertTrue('111111' == $this->spanContext->traceIdLow); + $this->assertTrue('1954685383581106176' == $this->spanContext->traceIdHigh); + + $this->spanContext->traceIdLow = null; + $this->spanContext->traceIdHigh = null; + $this->spanContext->traceIdToString('1b207'); + $this->assertTrue('111111' == $this->spanContext->traceIdLow); + $this->assertTrue(null === $this->spanContext->traceIdHigh); } } diff --git a/tests/Transport/TransportUdpTest.php b/tests/Transport/TransportUdpTest.php index 6a06f74..3d53070 100644 --- a/tests/Transport/TransportUdpTest.php +++ b/tests/Transport/TransportUdpTest.php @@ -15,12 +15,14 @@ namespace tests; +use Jaeger\Sender\Sender; use Jaeger\Transport\TransportUdp; use PHPUnit\Framework\TestCase; use Jaeger\Jaeger; use Jaeger\Reporter\RemoteReporter; use Jaeger\Sampler\ConstSampler; use Jaeger\ScopeManager; +use Jaeger\Sender\NullSender; class TransportUdpTest extends TestCase { @@ -36,7 +38,10 @@ class TransportUdpTest extends TestCase public function setUp() { - $this->tran = new TransportUdp('localhost:6831'); + $senderMock = $this->createMock(Sender::class); + $senderMock->method('emitBatch')->willReturn(true); + + $this->tran = new TransportUdp('localhost:6831', 0, $senderMock); $reporter = new RemoteReporter($this->tran); $sampler = new ConstSampler(); @@ -53,14 +58,13 @@ public function testBuildAndCalcSizeOfProcessThrift() $this->assertEquals(95, $this->tran->procesSize); } - public function testSpanIsTooLarge() - { - $this->tran::$maxSpanBytes = 50; - $span = $this->tracer->startSpan('SpanIsTooLarge'); - $span->finish(); - $this->tran->append($this->tracer); - $this->assertEquals(0, $this->tran->flushSpanNum); - } +// public function testSpanIsTooLarge() +// { +// $this->tran::$maxSpanBytes = 50; +// $span = $this->tracer->startSpan('SpanIsTooLarge'); +// $span->finish(); +// $this->tran->append($this->tracer); +// } public function testSplitEmit() { @@ -79,6 +83,5 @@ public function testSplitEmit() $span->finish(); $this->tran->append($this->tracer); - $this->assertEquals(3, $this->tran->flushSpanNum); } } From 3c3a767fb2fac7a951a88783365a024429157ae5 Mon Sep 17 00:00:00 2001 From: "742161455@qq.com" <742161455@qq.com> Date: Wed, 30 Dec 2020 17:39:49 +0800 Subject: [PATCH 21/32] unit test of propagator --- tests/Propagator/JaegerPropagatorTest.php | 25 ++++++++++--------- tests/Propagator/ZipkinPropagatorTest.php | 29 +++++++++++++---------- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/tests/Propagator/JaegerPropagatorTest.php b/tests/Propagator/JaegerPropagatorTest.php index 4738bc3..b2d2a69 100644 --- a/tests/Propagator/JaegerPropagatorTest.php +++ b/tests/Propagator/JaegerPropagatorTest.php @@ -23,33 +23,36 @@ class JaegerPropagatorTest extends TestCase { - public function getSpanContext() + /** + * @var SpanContext|null + */ + public $spanContext = null; + + public function setUp() { - return new SpanContext(1562237095801441413, 0, 1, null, 1); + $this->spanContext = new SpanContext(1, 1, 1, null, 1); } public function testInject() { - $context = $this->getSpanContext(); - $context->traceIdLow = 1562237095801441413; + $this->spanContext->traceIdLow = 1562237095801441413; $jaeger = new JaegerPropagator(); $carrier = []; - $jaeger->inject($context, Formats\TEXT_MAP, $carrier); - $this->assertTrue('15ae2e5c8e2ecc85:15ae2e5c8e2ecc85:0:1' == $carrier[strtoupper(Constants\Tracer_State_Header_Name)]); + $jaeger->inject( $this->spanContext, Formats\TEXT_MAP, $carrier); + $this->assertTrue('15ae2e5c8e2ecc85:1:1:1' == $carrier[strtoupper(Constants\Tracer_State_Header_Name)]); } public function testInject128Bit() { - $context = $this->getSpanContext(); - $context->traceIdLow = 1562289663898779811; - $context->traceIdHigh = 1562289663898881723; + $this->spanContext->traceIdLow = 1562289663898779811; + $this->spanContext->traceIdHigh = 1562289663898881723; $jaeger = new JaegerPropagator(); $carrier = []; - $jaeger->inject($context, Formats\TEXT_MAP, $carrier); - $this->assertTrue('15ae5e2c04f50ebb15ae5e2c04f380a3:15ae2e5c8e2ecc85:0:1' + $jaeger->inject( $this->spanContext, Formats\TEXT_MAP, $carrier); + $this->assertTrue('15ae5e2c04f50ebb15ae5e2c04f380a3:1:1:1' == $carrier[strtoupper(Constants\Tracer_State_Header_Name)]); } diff --git a/tests/Propagator/ZipkinPropagatorTest.php b/tests/Propagator/ZipkinPropagatorTest.php index 60396b6..bafe717 100644 --- a/tests/Propagator/ZipkinPropagatorTest.php +++ b/tests/Propagator/ZipkinPropagatorTest.php @@ -23,40 +23,43 @@ class ZipkinPropagatorTest extends TestCase { - public function getSpanContext() + /** + * @var SpanContext|null + */ + public $spanContext = null; + + public function setUp() { - return new SpanContext(1562237095801441413, 0, 1, null, 1); + $this->spanContext = new SpanContext(1, 1, 1, null, 1); } public function testInject() { - $context = $this->getSpanContext(); - $context->traceIdLow = 1562237095801441413; + $this->spanContext->traceIdLow = 1562237095801441413; $zipkin = new ZipkinPropagator(); $carrier = []; - $zipkin->inject($context, Formats\TEXT_MAP, $carrier); + $zipkin->inject($this->spanContext, Formats\TEXT_MAP, $carrier); $this->assertTrue('15ae2e5c8e2ecc85' == $carrier[Constants\X_B3_TRACEID]); - $this->assertTrue(0 == $carrier[Constants\X_B3_PARENT_SPANID]); - $this->assertTrue('15ae2e5c8e2ecc85' == $carrier[Constants\X_B3_SPANID]); + $this->assertTrue(1 == $carrier[Constants\X_B3_PARENT_SPANID]); + $this->assertTrue(1 == $carrier[Constants\X_B3_SPANID]); $this->assertTrue(1 == $carrier[Constants\X_B3_SAMPLED]); } public function testInject128Bit() { - $context = $this->getSpanContext(); - $context->traceIdLow = 1562289663898779811; - $context->traceIdHigh = 1562289663898881723; + $this->spanContext->traceIdLow = 1562289663898779811; + $this->spanContext->traceIdHigh = 1562289663898881723; $zipkin = new ZipkinPropagator(); $carrier = []; - $zipkin->inject($context, Formats\TEXT_MAP, $carrier); + $zipkin->inject($this->spanContext, Formats\TEXT_MAP, $carrier); $this->assertTrue('15ae5e2c04f50ebb15ae5e2c04f380a3' == $carrier[Constants\X_B3_TRACEID]); - $this->assertTrue(0 == $carrier[Constants\X_B3_PARENT_SPANID]); - $this->assertTrue('15ae2e5c8e2ecc85' == $carrier[Constants\X_B3_SPANID]); + $this->assertTrue(1 == $carrier[Constants\X_B3_PARENT_SPANID]); + $this->assertTrue(1 == $carrier[Constants\X_B3_SPANID]); $this->assertTrue(1 == $carrier[Constants\X_B3_SAMPLED]); } From 6f56e262b26998a80f3fe958da8a3397c63d0de9 Mon Sep 17 00:00:00 2001 From: "742161455@qq.com" <742161455@qq.com> Date: Wed, 30 Dec 2020 17:53:30 +0800 Subject: [PATCH 22/32] unit test of config --- src/Jaeger/Config.php | 7 +++++-- tests/ConfigTest.php | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/Jaeger/Config.php b/src/Jaeger/Config.php index 56c7f2b..32ca8df 100644 --- a/src/Jaeger/Config.php +++ b/src/Jaeger/Config.php @@ -50,7 +50,7 @@ class Config private $gen128bit = false; /** - * @var Tracer|null + * @var array|null */ public static $tracer = null; @@ -59,6 +59,9 @@ class Config */ public static $span = null; + /** + * @var null + */ public static $instance = null; public static $disabled = false; @@ -83,7 +86,7 @@ public static function getInstance() } /** - * init jaeger, return can use flush buffers. + * init tracer. * * @param string $agentHostPort * diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 9051b4f..cdeaa53 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -16,7 +16,9 @@ namespace tests; use Jaeger\Config; +use Jaeger\Reporter\NullReporter; use OpenTracing\NoopTracer; +use OpenTracing\Tracer; use PHPUnit\Framework\TestCase; class ConfigTest extends TestCase @@ -37,4 +39,29 @@ public function testNoopTracer() $this->assertTrue($trace instanceof NoopTracer); } + + public function testflushMulTracer() + { + $report = new NullReporter(); + $config = Config::getInstance(); + $config->setDisabled(false); + $config->setReporter($report); + $tracer1 = $config->initTracer('tracer1'); + $this->assertTrue($tracer1 instanceof Tracer); + $tracer2 = $config->initTracer('tracer2'); + $this->assertTrue($tracer2 instanceof Tracer); + $this->assertTrue($config->flush()); + } + + /** + * @expectedException \RuntimeException + */ + public function testEmptyServiceName() + { + $report = new NullReporter(); + $config = Config::getInstance(); + $config->setDisabled(false); + $config->setReporter($report); + $config->initTracer(''); + } } From 7843d6285db31383e81e8ab4f2567e9b92700b1e Mon Sep 17 00:00:00 2001 From: Xiangxuan Liu Date: Thu, 31 Dec 2020 11:24:42 +0800 Subject: [PATCH 23/32] PHPStan: ignore thrift functions not found errors --- phpstan.neon | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/phpstan.neon b/phpstan.neon index 284c978..921b7c2 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,6 +4,7 @@ parameters: - src - tests ignoreErrors: - - + - message: '#Constant#' path: src/Jaeger/Constants.php + - '#Function thrift_protocol_.* not found#' From 349ea9d881424ba2c44ad4091a290b348f190cd3 Mon Sep 17 00:00:00 2001 From: Xiangxuan Liu Date: Thu, 31 Dec 2020 11:35:34 +0800 Subject: [PATCH 24/32] Remove unmatch ignored error pattern --- phpstan.neon | 3 --- 1 file changed, 3 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 921b7c2..6bfb433 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,7 +4,4 @@ parameters: - src - tests ignoreErrors: - - - message: '#Constant#' - path: src/Jaeger/Constants.php - '#Function thrift_protocol_.* not found#' From ef00bc9d0eb9f896d5809a6f5d9a62f57550e77c Mon Sep 17 00:00:00 2001 From: Xiangxuan Liu Date: Wed, 6 Jan 2021 14:33:10 +0800 Subject: [PATCH 25/32] Improve TransportUdp type hint --- src/Jaeger/Transport/TransportUdp.php | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/Jaeger/Transport/TransportUdp.php b/src/Jaeger/Transport/TransportUdp.php index 8f291fc..5f36a12 100644 --- a/src/Jaeger/Transport/TransportUdp.php +++ b/src/Jaeger/Transport/TransportUdp.php @@ -28,6 +28,8 @@ class TransportUdp implements Transport { + const DEFAULT_AGENT_HOST_PORT = 'localhost:6831'; + /** * @var null|TMemoryBuffer */ @@ -53,7 +55,11 @@ class TransportUdp implements Transport */ private $process = null; - public static $hostPort = ''; + + /** + * @var string + */ + public $agentHostPort = ''; // sizeof(Span) * numSpans + processByteSize + emitBatchOverhead <= maxPacketSize public static $maxSpanBytes = 0; @@ -76,14 +82,11 @@ class TransportUdp implements Transport const MAC_UDP_MAX_SIZE = 9216; - public function __construct($hostport = '', $maxPacketSize = '', Sender $udpSender = null) + public function __construct(string $hostport = self::DEFAULT_AGENT_HOST_PORT, int $maxPacketSize = 0, Sender $udpSender = null) { - if ('' == $hostport) { - $hostport = $this->agentServerHostPort; - } - self::$hostPort = $hostport; + $this->agentHostPort = $hostport; - if (0 == $maxPacketSize) { + if (0 === $maxPacketSize) { $maxPacketSize = stristr(PHP_OS, 'DAR') ? self::MAC_UDP_MAX_SIZE : Constants\UDP_PACKET_MAX_LENGTH; } @@ -96,8 +99,8 @@ public function __construct($hostport = '', $maxPacketSize = '', Sender $udpSend $this->agentClient = new AgentClient($this->protocol, null); $this->sender = $udpSender; - if($this->sender == null) { - $this->sender = new UdpSender(self::$hostPort, $this->agentClient, $this->tran); + if ($this->sender == null) { + $this->sender = new UdpSender($this->agentServerHostPort, $this->agentClient, $this->tran); } $this->jaegerThrift = new JaegerThrift(); From 5c7e72bf9d64836124ee910d332e9d50e4a6c38e Mon Sep 17 00:00:00 2001 From: Xiangxuan Liu Date: Thu, 7 Jan 2021 22:20:16 +0800 Subject: [PATCH 26/32] Code format --- .php_cs | 1 + example/HTTP.php | 2 +- src/Jaeger/JaegerThrift.php | 11 ++-- src/Jaeger/Sender/Sender.php | 3 +- src/Jaeger/Sender/UdpSender.php | 2 +- src/Jaeger/Transport/TransportUdp.php | 13 +++-- tests/JaegerThriftTest.php | 64 ++++++++++++----------- tests/Propagator/JaegerPropagatorTest.php | 4 +- tests/Sender/UdpSenderTest.php | 4 +- tests/Transport/TransportUdpTest.php | 13 +++-- 10 files changed, 57 insertions(+), 60 deletions(-) diff --git a/.php_cs b/.php_cs index 82a2b41..f1767d3 100644 --- a/.php_cs +++ b/.php_cs @@ -11,6 +11,7 @@ // Exclude some directories that are excluded by Git anyways to speed up the sniffing $finder = PhpCsFixer\Finder::create() ->exclude('vendor') + ->exclude('src/Jaeger/Thrift') ->in(__DIR__); return PhpCsFixer\Config::create() diff --git a/example/HTTP.php b/example/HTTP.php index a54120d..05bdd03 100644 --- a/example/HTTP.php +++ b/example/HTTP.php @@ -35,7 +35,7 @@ $spanContext = $tracer->extract(Formats\TEXT_MAP, $_SERVER); $options = []; -if($spanContext != null) { +if (null != $spanContext) { $options = ['child_of' => $spanContext]; } diff --git a/src/Jaeger/JaegerThrift.php b/src/Jaeger/JaegerThrift.php index fb7dd97..9cbacea 100644 --- a/src/Jaeger/JaegerThrift.php +++ b/src/Jaeger/JaegerThrift.php @@ -16,14 +16,14 @@ namespace Jaeger; use Jaeger\Span as jspan; -use OpenTracing\Reference; +use Jaeger\Thrift\Log; use Jaeger\Thrift\Process; use Jaeger\Thrift\Span; -use Jaeger\Thrift\TagType; -use Jaeger\Thrift\Log; -use Jaeger\Thrift\Tag; use Jaeger\Thrift\SpanRef; use Jaeger\Thrift\SpanRefType; +use Jaeger\Thrift\Tag; +use Jaeger\Thrift\TagType; +use OpenTracing\Reference; class JaegerThrift { @@ -83,8 +83,6 @@ private function buildLogs(array $logs): array /** * @param array $references - * - * @return array */ private function buildReferences(array $references): array { @@ -112,7 +110,6 @@ private function buildReferences(array $references): array return $spanRef; } - public function buildTags(array $tags): array { $jTags = []; diff --git a/src/Jaeger/Sender/Sender.php b/src/Jaeger/Sender/Sender.php index 632f706..bc7aac3 100644 --- a/src/Jaeger/Sender/Sender.php +++ b/src/Jaeger/Sender/Sender.php @@ -12,6 +12,7 @@ * or implied. See the License for the specific language governing permissions and limitations under * the License. */ + namespace Jaeger\Sender; interface Sender @@ -21,4 +22,4 @@ public function isOpen(); public function emitBatch(\Jaeger\Thrift\Batch $batch); public function close(); -} \ No newline at end of file +} diff --git a/src/Jaeger/Sender/UdpSender.php b/src/Jaeger/Sender/UdpSender.php index d83b619..7f08fd9 100644 --- a/src/Jaeger/Sender/UdpSender.php +++ b/src/Jaeger/Sender/UdpSender.php @@ -39,7 +39,7 @@ class UdpSender implements Sender private $agentClient = null; /** - * @var null|TMemoryBuffer + * @var TMemoryBuffer|null */ private $tran = null; diff --git a/src/Jaeger/Transport/TransportUdp.php b/src/Jaeger/Transport/TransportUdp.php index 5f36a12..e44ab76 100644 --- a/src/Jaeger/Transport/TransportUdp.php +++ b/src/Jaeger/Transport/TransportUdp.php @@ -18,20 +18,20 @@ use Jaeger\Constants; use Jaeger\Jaeger; use Jaeger\JaegerThrift; +use Jaeger\Sender\Sender; use Jaeger\Sender\UdpSender; +use Jaeger\Thrift\Agent\AgentClient; use Jaeger\Thrift\Batch; use Jaeger\Thrift\Process; -use Jaeger\Sender\Sender; use Thrift\Protocol\TCompactProtocol; use Thrift\Transport\TMemoryBuffer; -use Jaeger\Thrift\Agent\AgentClient; class TransportUdp implements Transport { const DEFAULT_AGENT_HOST_PORT = 'localhost:6831'; /** - * @var null|TMemoryBuffer + * @var TMemoryBuffer|null */ private $tran = null; @@ -55,7 +55,6 @@ class TransportUdp implements Transport */ private $process = null; - /** * @var string */ @@ -72,7 +71,7 @@ class TransportUdp implements Transport public $agentServerHostPort = 'localhost:6831'; /** - * @var null|TCompactProtocol + * @var TCompactProtocol|null */ public $protocol = null; @@ -99,7 +98,7 @@ public function __construct(string $hostport = self::DEFAULT_AGENT_HOST_PORT, in $this->agentClient = new AgentClient($this->protocol, null); $this->sender = $udpSender; - if ($this->sender == null) { + if (null == $this->sender) { $this->sender = new UdpSender($this->agentServerHostPort, $this->agentClient, $this->tran); } @@ -182,7 +181,7 @@ private function getAndCalcSizeOfSerializedThrift($thrift) */ public function flush() { - if (self::$batch == null) { + if (null == self::$batch) { return 0; } diff --git a/tests/JaegerThriftTest.php b/tests/JaegerThriftTest.php index e0e3fbc..8a1bc1e 100644 --- a/tests/JaegerThriftTest.php +++ b/tests/JaegerThriftTest.php @@ -15,18 +15,17 @@ namespace tests; -use Jaeger\JaegerThrift; -use PHPUnit\Framework\TestCase; use Jaeger\Jaeger; +use Jaeger\JaegerThrift; use Jaeger\Reporter\RemoteReporter; use Jaeger\Sampler\ConstSampler; use Jaeger\ScopeManager; -use Jaeger\Transport\TransportUdp; use Jaeger\Thrift\TagType; +use Jaeger\Transport\TransportUdp; +use PHPUnit\Framework\TestCase; class JaegerThriftTest extends TestCase { - /** * @var JaegerThrift|null */ @@ -48,39 +47,42 @@ public function setUp() $this->jaegerThrift = new JaegerThrift(); } - public function testBuildProcessThrift(){ + public function testBuildProcessThrift() + { $process = $this->jaegerThrift->buildProcessThrift($this->tracer); $this->assertEquals('jaeger', $process->serviceName); } - public function testBuildTags(){ - $tags = ['event' => 'test']; - $jtags = $this->jaegerThrift->buildTags($tags); - $this->assertEquals('event', $jtags[0]->key); - $this->assertEquals(TagType::STRING, $jtags[0]->vType); - $this->assertEquals('test', $jtags[0]->vStr); + public function testBuildTags() + { + $tags = ['event' => 'test']; + $jtags = $this->jaegerThrift->buildTags($tags); + $this->assertEquals('event', $jtags[0]->key); + $this->assertEquals(TagType::STRING, $jtags[0]->vType); + $this->assertEquals('test', $jtags[0]->vStr); - $tags = ['success' => true]; - $jtags = $this->jaegerThrift->buildTags($tags); - $this->assertEquals('success', $jtags[0]->key); - $this->assertEquals(TagType::BOOL, $jtags[0]->vType); - $this->assertTrue($jtags[0]->vBool); + $tags = ['success' => true]; + $jtags = $this->jaegerThrift->buildTags($tags); + $this->assertEquals('success', $jtags[0]->key); + $this->assertEquals(TagType::BOOL, $jtags[0]->vType); + $this->assertTrue($jtags[0]->vBool); - $tags = ['data' => [1,2]]; - $jtags = $this->jaegerThrift->buildTags($tags); - $this->assertEquals('data', $jtags[0]->key); - $this->assertEquals(TagType::STRING, $jtags[0]->vType); + $tags = ['data' => [1, 2]]; + $jtags = $this->jaegerThrift->buildTags($tags); + $this->assertEquals('data', $jtags[0]->key); + $this->assertEquals(TagType::STRING, $jtags[0]->vType); - $tags = ['num' => 1]; - $jtags = $this->jaegerThrift->buildTags($tags); - $this->assertEquals('num', $jtags[0]->key); - $this->assertEquals(TagType::LONG, $jtags[0]->vType); - $this->assertEquals(1, $jtags[0]->vDouble); - } + $tags = ['num' => 1]; + $jtags = $this->jaegerThrift->buildTags($tags); + $this->assertEquals('num', $jtags[0]->key); + $this->assertEquals(TagType::LONG, $jtags[0]->vType); + $this->assertEquals(1, $jtags[0]->vDouble); + } - public function testBuildSpanThrift() { - $span = $this->tracer->startSpan('BuildSpanThrift'); - $jspan = $this->jaegerThrift->buildSpanThrift($span); - $this->assertEquals('BuildSpanThrift', $jspan->operationName); - } + public function testBuildSpanThrift() + { + $span = $this->tracer->startSpan('BuildSpanThrift'); + $jspan = $this->jaegerThrift->buildSpanThrift($span); + $this->assertEquals('BuildSpanThrift', $jspan->operationName); + } } diff --git a/tests/Propagator/JaegerPropagatorTest.php b/tests/Propagator/JaegerPropagatorTest.php index b2d2a69..b1475b3 100644 --- a/tests/Propagator/JaegerPropagatorTest.php +++ b/tests/Propagator/JaegerPropagatorTest.php @@ -39,7 +39,7 @@ public function testInject() $jaeger = new JaegerPropagator(); $carrier = []; - $jaeger->inject( $this->spanContext, Formats\TEXT_MAP, $carrier); + $jaeger->inject($this->spanContext, Formats\TEXT_MAP, $carrier); $this->assertTrue('15ae2e5c8e2ecc85:1:1:1' == $carrier[strtoupper(Constants\Tracer_State_Header_Name)]); } @@ -51,7 +51,7 @@ public function testInject128Bit() $jaeger = new JaegerPropagator(); $carrier = []; - $jaeger->inject( $this->spanContext, Formats\TEXT_MAP, $carrier); + $jaeger->inject($this->spanContext, Formats\TEXT_MAP, $carrier); $this->assertTrue('15ae5e2c04f50ebb15ae5e2c04f380a3:1:1:1' == $carrier[strtoupper(Constants\Tracer_State_Header_Name)]); } diff --git a/tests/Sender/UdpSenderTest.php b/tests/Sender/UdpSenderTest.php index bcd85e2..ce172e9 100644 --- a/tests/Sender/UdpSenderTest.php +++ b/tests/Sender/UdpSenderTest.php @@ -15,8 +15,8 @@ namespace tests; -use Jaeger\Thrift\Agent\AgentClient; use Jaeger\Sender\UdpSender; +use Jaeger\Thrift\Agent\AgentClient; use PHPUnit\Framework\TestCase; use Thrift\Protocol\TCompactProtocol; use Thrift\Transport\TMemoryBuffer; @@ -56,14 +56,12 @@ public function testIsOpen() $this->assertTrue($this->udpSender->isOpen()); } - public function testClose() { $this->udpSender->close(); $this->assertFalse($this->udpSender->isOpen()); } - public function testEmitBatch() { $span = new \Jaeger\Thrift\Span( diff --git a/tests/Transport/TransportUdpTest.php b/tests/Transport/TransportUdpTest.php index 3d53070..e216e0f 100644 --- a/tests/Transport/TransportUdpTest.php +++ b/tests/Transport/TransportUdpTest.php @@ -15,14 +15,13 @@ namespace tests; -use Jaeger\Sender\Sender; -use Jaeger\Transport\TransportUdp; -use PHPUnit\Framework\TestCase; use Jaeger\Jaeger; use Jaeger\Reporter\RemoteReporter; use Jaeger\Sampler\ConstSampler; use Jaeger\ScopeManager; -use Jaeger\Sender\NullSender; +use Jaeger\Sender\Sender; +use Jaeger\Transport\TransportUdp; +use PHPUnit\Framework\TestCase; class TransportUdpTest extends TestCase { @@ -71,15 +70,15 @@ public function testSplitEmit() $i = 0; $this->tran::$maxSpanBytes = 150; $span = $this->tracer->startSpan('SplitEmit1'); - $i++; + ++$i; $span->finish(); $span = $this->tracer->startSpan('SplitEmit2'); - $i++; + ++$i; $span->finish(); $span = $this->tracer->startSpan('SplitEmit3'); - $i++; + ++$i; $span->finish(); $this->tran->append($this->tracer); From 3a6cda74d1bead517c3605a40a90c26948a2cdd4 Mon Sep 17 00:00:00 2001 From: Xiangxuan Liu Date: Thu, 7 Jan 2021 22:34:52 +0800 Subject: [PATCH 27/32] Fix PHPStan errors --- src/Jaeger/Config.php | 2 +- src/Jaeger/Jaeger.php | 4 ++++ src/Jaeger/Propagator/JaegerPropagator.php | 2 ++ src/Jaeger/Propagator/ZipkinPropagator.php | 2 ++ src/Jaeger/Sender/UdpSender.php | 6 ++---- src/Jaeger/Transport/TransportUdp.php | 2 +- 6 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Jaeger/Config.php b/src/Jaeger/Config.php index 32ca8df..150b59a 100644 --- a/src/Jaeger/Config.php +++ b/src/Jaeger/Config.php @@ -60,7 +60,7 @@ class Config public static $span = null; /** - * @var null + * @var self|null */ public static $instance = null; diff --git a/src/Jaeger/Jaeger.php b/src/Jaeger/Jaeger.php index 6caa44d..1b43240 100644 --- a/src/Jaeger/Jaeger.php +++ b/src/Jaeger/Jaeger.php @@ -78,6 +78,8 @@ public function setTags(array $tags = []) /** * {@inheritDoc} + * + * @return Span */ public function startSpan(string $operationName, $options = []): \OpenTracing\Span { @@ -137,6 +139,8 @@ public function inject(\OpenTracing\SpanContext $spanContext, string $format, &$ /** * {@inheritDoc} + * + * @return SpanContext */ public function extract(string $format, $carrier): ?\OpenTracing\SpanContext { diff --git a/src/Jaeger/Propagator/JaegerPropagator.php b/src/Jaeger/Propagator/JaegerPropagator.php index ea19ea8..e8abe34 100644 --- a/src/Jaeger/Propagator/JaegerPropagator.php +++ b/src/Jaeger/Propagator/JaegerPropagator.php @@ -22,6 +22,8 @@ class JaegerPropagator implements Propagator { /** * {@inheritDoc} + * + * @param SpanContext $spanContext */ public function inject(\OpenTracing\SpanContext $spanContext, $format, &$carrier) { diff --git a/src/Jaeger/Propagator/ZipkinPropagator.php b/src/Jaeger/Propagator/ZipkinPropagator.php index 54558be..5900cee 100644 --- a/src/Jaeger/Propagator/ZipkinPropagator.php +++ b/src/Jaeger/Propagator/ZipkinPropagator.php @@ -22,6 +22,8 @@ class ZipkinPropagator implements Propagator { /** * {@inheritDoc} + * + * @param SpanContext $spanContext */ public function inject(\OpenTracing\SpanContext $spanContext, $format, &$carrier) { diff --git a/src/Jaeger/Sender/UdpSender.php b/src/Jaeger/Sender/UdpSender.php index 7f08fd9..86e4d01 100644 --- a/src/Jaeger/Sender/UdpSender.php +++ b/src/Jaeger/Sender/UdpSender.php @@ -29,9 +29,9 @@ class UdpSender implements Sender private $post = ''; /** - * @var resource|string + * @var resource|null */ - private $socket = ''; + private $socket = null; /** * @var AgentClient|null @@ -62,8 +62,6 @@ public function isOpen() /** * send thrift. * - * @param mixed $batch - * * @return bool * * @throws \Exception diff --git a/src/Jaeger/Transport/TransportUdp.php b/src/Jaeger/Transport/TransportUdp.php index e44ab76..571834e 100644 --- a/src/Jaeger/Transport/TransportUdp.php +++ b/src/Jaeger/Transport/TransportUdp.php @@ -162,7 +162,7 @@ public function buildAndCalcSizeOfProcessThrift(Jaeger $jaeger) /** * 计算序列化后的thrift字符长度. * - * @param mixed $serializedThrift + * @param mixed $thrift * * @return mixed */ From 641abfc43f6f031a6a1063266d4efb7426fc93bf Mon Sep 17 00:00:00 2001 From: Xiangxuan Liu Date: Thu, 14 Jan 2021 14:54:32 +0800 Subject: [PATCH 28/32] Fix opentracing/opentracing 1.0.1 return type --- composer.json | 2 +- src/Jaeger/SpanContext.php | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 2d9c9db..c4d43a9 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ "php": ">=7.1", "ext-json": "*", "packaged/thrift" : "~0.13.0", - "opentracing/opentracing" : "^1.0.0" + "opentracing/opentracing" : "^1.0.1" }, "authors": [ { diff --git a/src/Jaeger/SpanContext.php b/src/Jaeger/SpanContext.php index 0126adc..155afa9 100644 --- a/src/Jaeger/SpanContext.php +++ b/src/Jaeger/SpanContext.php @@ -62,12 +62,11 @@ public function getBaggageItem(string $key): ?string /** * {@inheritDoc} */ - public function withBaggageItem(string $key, string $value): ?\OpenTracing\SpanContext + public function withBaggageItem(string $key, string $value): \OpenTracing\SpanContext { $this->baggage[$key] = $value; - // TODO - return null; + return $this; } /** From 41b0e3adb7bb991b8d0f825f5a805ed01cde6104 Mon Sep 17 00:00:00 2001 From: Xiangxuan Liu Date: Tue, 2 Feb 2021 12:14:47 +0800 Subject: [PATCH 29/32] Fix always send data to localhost:6831 --- src/Jaeger/Transport/TransportUdp.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Jaeger/Transport/TransportUdp.php b/src/Jaeger/Transport/TransportUdp.php index 571834e..8c4c3ce 100644 --- a/src/Jaeger/Transport/TransportUdp.php +++ b/src/Jaeger/Transport/TransportUdp.php @@ -68,8 +68,6 @@ class TransportUdp implements Transport */ public static $batch = null; - public $agentServerHostPort = 'localhost:6831'; - /** * @var TCompactProtocol|null */ @@ -99,7 +97,7 @@ public function __construct(string $hostport = self::DEFAULT_AGENT_HOST_PORT, in $this->sender = $udpSender; if (null == $this->sender) { - $this->sender = new UdpSender($this->agentServerHostPort, $this->agentClient, $this->tran); + $this->sender = new UdpSender($this->agentHostPort, $this->agentClient, $this->tran); } $this->jaegerThrift = new JaegerThrift(); From f34d6586a1f6f0e0ca51dcd195be8238c2102ad2 Mon Sep 17 00:00:00 2001 From: Xiangxuan Liu Date: Tue, 2 Feb 2021 13:59:24 +0800 Subject: [PATCH 30/32] Fix int tag values always be 0 --- src/Jaeger/JaegerThrift.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Jaeger/JaegerThrift.php b/src/Jaeger/JaegerThrift.php index 9cbacea..2133071 100644 --- a/src/Jaeger/JaegerThrift.php +++ b/src/Jaeger/JaegerThrift.php @@ -130,7 +130,7 @@ public function buildTags(array $tags): array $jTags[] = new Tag([ 'key' => $k, 'vType' => TagType::LONG, - 'vDouble' => $v, + 'vLong' => $v, ]); break; case 'double': @@ -142,6 +142,7 @@ public function buildTags(array $tags): array break; case 'array': $v = json_encode($v, JSON_UNESCAPED_UNICODE); + // no break default: $jTags[] = new Tag([ 'key' => $k, From 5e599ca95d1d0750028cc8341c8d63d9b8c5281b Mon Sep 17 00:00:00 2001 From: 742161455 <742161455@qq.com> Date: Fri, 16 Apr 2021 16:36:33 +0800 Subject: [PATCH 31/32] Update README.md --- README.md | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/README.md b/README.md index 910458b..c110804 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,105 @@ [![License](https://img.shields.io/github/license/jukylin/jaeger-php.svg)](https://github.com/jukylin/jaeger-php/blob/master/LICENSE) [![Coverage Status](https://coveralls.io/repos/github/jukylin/jaeger-php/badge.svg?branch=master)](https://coveralls.io/github/jukylin/jaeger-php?branch=master) +# jaeger-php + +## Install + +Install via composer. + +``` +composer require jukylin/jaeger-php +``` + +## Init Jaeger-php + +```php +$config = Config::getInstance(); +$tracer = $config->initTracer('example', '0.0.0.0:6831'); +``` + +## 128bit + +```php +$config->gen128bit(); +``` + +## Extract from Superglobals + +```php +$spanContext = $tracer->extract(Formats\TEXT_MAP, $_SERVER); +``` + +## Start Span + +```php +$serverSpan = $tracer->startSpan('example HTTP', ['child_of' => $spanContext]); +``` + +## Distributed context propagation +```php +$serverSpan->addBaggageItem("version", "2.0.0"); +``` + +## Inject into Superglobals + +```php +$clientTrace->inject($clientSpan1->spanContext, Formats\TEXT_MAP, $_SERVER); +``` + +## Tags and Log + +```php +// tags are searchable in Jaeger UI +$span->setTag('http.status', '200'); + +// log record +$span->log(['error' => 'HTTP request timeout']); +``` + +## Close Tracer + +```php +$config->setDisabled(true); +``` + +## Zipkin B3 Propagation + +*no support for* `Distributed context propagation` + +```php +$config::$propagator = \Jaeger\Constants\PROPAGATOR_ZIPKIN; +``` + +## Finish span and flush Tracer + +```php +$span->finish(); +$config->flush(); +``` + +## More example + +- [HTTP](https://github.com/jukylin/jaeger-php/blob/master/example/HTTP.php) +- [Hprose](https://github.com/jukylin/blog/blob/master/Uber%E5%88%86%E5%B8%83%E5%BC%8F%E8%BF%BD%E8%B8%AA%E7%B3%BB%E7%BB%9FJaeger%E4%BD%BF%E7%94%A8%E4%BB%8B%E7%BB%8D%E5%92%8C%E6%A1%88%E4%BE%8B%E3%80%90PHP%20%20%20Hprose%20%20%20Go%E3%80%91.md#跨语言调用案例) +- [Istio](https://github.com/jukylin/jaeger-php/blob/master/example/README.md) + +## Features + +- Transports + - via Thrift over UDP + +- Sampling + - ConstSampler + - ProbabilisticSampler + +## Reference + +[OpenTracing](https://opentracing.io/) + +[Jaeger](https://uber.github.io/jaeger/) + + ## TODO - [x] Implement the latest opentracing/opentracing - [ ] Improve documentation From 1341e8476f6a181ef6dedcea7a6fbc80bf4f7a18 Mon Sep 17 00:00:00 2001 From: Revenkroz Date: Thu, 12 May 2022 20:37:51 +0300 Subject: [PATCH 32/32] Fix type for php 8.1 --- src/Jaeger/SpanContext.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Jaeger/SpanContext.php b/src/Jaeger/SpanContext.php index 155afa9..33850f9 100644 --- a/src/Jaeger/SpanContext.php +++ b/src/Jaeger/SpanContext.php @@ -72,10 +72,9 @@ public function withBaggageItem(string $key, string $value): \OpenTracing\SpanCo /** * {@inheritDoc} */ - public function getIterator() + public function getIterator(): \Traversable { - // @phpstan-ignore-next-line - // TODO: Implement getIterator() method. + return new \ArrayIterator($this->baggage); } public function buildString()