diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..bd69cf4 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,17 @@ +# EditorConfig is awesome: https://EditorConfig.org + +# top-most EditorConfig file +root = true + +# Unix-style newlines with a newline ending every file +[*] +end_of_line = lf +insert_final_newline = true + +[*.php] +indent_style = space +indent_size = 4 + +[buddy.yml] +indent_style = space +indent_size = 2 diff --git a/.gitattributes b/.gitattributes index fb351a5..8c5b0ac 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,3 +1,3 @@ /tests export-ignore /example export-ignore -phpunit.xml.dist export-ignore +phpunit.xml export-ignore diff --git a/README.md b/README.md index 2500e82..4666a9e 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ [![Latest Stable Version](https://img.shields.io/packagist/v/whitemerry/phpkin.svg?style=flat-square&label=stable "Latest Stable Version")](https://packagist.org/packages/whitemerry/phpkin) [![OpenTracing Badge](https://img.shields.io/badge/OpenTracing-enabled-blue.svg)](http://opentracing.io) [![Maintainability](https://api.codeclimate.com/v1/badges/a785fa78ec069394b21d/maintainability)](https://codeclimate.com/github/whitemerry/phpkin/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/a785fa78ec069394b21d/test_coverage)](https://codeclimate.com/github/whitemerry/phpkin/test_coverage) +[![buddy pipeline](https://app.buddy.works/whitemerry/phpkin/pipelines/pipeline/204054/badge.svg?token=22731a0e88035e9b44c8ffbe93dfbff5f4dd2a91e4c5d0aada37eb55c43c461b "buddy pipeline")](https://app.buddy.works/whitemerry/phpkin/pipelines/pipeline/204054) First ***production ready***, simple and full Zipkin implementation without dependencies. @@ -161,12 +162,6 @@ For SimpleHttpLogger, short answer, ***yes*** For FileLogger, bit logner answer, you need to upload logs from *zipkin.log* to Zipkin by your own, for example by cron working in background making POST's to the [Zipkin (API)](http://zipkin.io/zipkin-api/#/paths/%252Fspans/post) -## Unit tests -Code Coverage (Generated by PHPUnit): -- Lines: 70.35% (140 / 199) -- Functions and Methods: 52.08% (25 / 48) -- Classes and Traits: 58.33% (7 / 12) - ## TODO - AsyncHttpLogger (Based on CURL) - *Upload to zipkin* cron for FileLogger diff --git a/example/backend/README.md b/example/backend/README.md index d6dd159..91f5cb7 100644 --- a/example/backend/README.md +++ b/example/backend/README.md @@ -3,7 +3,7 @@ Just start application using built-in php server (in this directory) ``` bash php -S 127.0.0.1:1234 ``` -Now your aplication is available at [http://127.0.0.1:1234/](http://127.0.0.1:1234/) +Now your application is available at [http://127.0.0.1:1234/](http://127.0.0.1:1234/) Make sure your Zipkin's application is up on `127.0.0.1:9411`. @@ -11,4 +11,4 @@ If you have other ip/port just modify index.php at line 27. ### Are you getting error? - Fatal error: Uncaught whitemerry\phpkin\Logger\LoggerException: Trace upload failed - * Zipkin's URL is invalid at line 27 in index.php \ No newline at end of file + * Zipkin's URL is invalid at line 27 in index.php diff --git a/example/backend/index.php b/example/backend/index.php index 8e4983f..df12659 100644 --- a/example/backend/index.php +++ b/example/backend/index.php @@ -25,7 +25,7 @@ * Make sure host is available with http:// and port because SimpleHttpLogger does not throw error on failure * For debug purposes you can disable muteErrors */ -$logger = new SimpleHttpLogger(['host' => 'http://127.0.0.1:9411', 'muteErrors' => false]); +$logger = new SimpleHttpLogger(array('host' => 'http://127.0.0.1:9411', 'muteErrors' => false)); /** * Read headers @@ -60,16 +60,16 @@ $spanId = new SpanIdentifier(); // Context for file_get_contents for passing headers (B3 propagation) -$context = stream_context_create([ - 'http' => [ +$context = stream_context_create(array( + 'http' => array( 'method' => 'GET', 'header' => 'X-B3-TraceId: ' . TracerInfo::getTraceId() . "\r\n" . 'X-B3-SpanId: ' . ((string) $spanId) . "\r\n" . 'X-B3-ParentSpanId: ' . TracerInfo::getTraceSpanId() . "\r\n" . 'X-B3-Sampled: ' . ((int) TracerInfo::isSampled()) . "\r\n" - ] -]); + ) +)); $request = file_get_contents($url, false, $context); diff --git a/example/frontend/README.md b/example/frontend/README.md index 3176535..0821d72 100644 --- a/example/frontend/README.md +++ b/example/frontend/README.md @@ -3,7 +3,7 @@ Just start application using built-in php server (in this directory) ``` bash php -S 127.0.0.1:1234 ``` -Now your aplication is available at [http://127.0.0.1:1234/](http://127.0.0.1:1234/) +Now your application is available at [http://127.0.0.1:1234/](http://127.0.0.1:1234/) Make sure your Zipkin's application is up on `127.0.0.1:9411`. @@ -11,4 +11,4 @@ If you have other ip/port just modify index.php at line 27. ### Are you getting error? - Fatal error: Uncaught whitemerry\phpkin\Logger\LoggerException: Trace upload failed - * Zipkin's URL is invalid at line 27 in index.php \ No newline at end of file + * Zipkin's URL is invalid at line 27 in index.php diff --git a/example/frontend/index.php b/example/frontend/index.php index 4f54e81..795846b 100644 --- a/example/frontend/index.php +++ b/example/frontend/index.php @@ -24,7 +24,7 @@ * Make sure host is available with http:// and port because SimpleHttpLogger does not throw error on failure * For debug purposes you can disable muteErrors */ -$logger = new SimpleHttpLogger(['host' => 'http://127.0.0.1:9411', 'muteErrors' => false]); +$logger = new SimpleHttpLogger(array('host' => 'http://127.0.0.1:9411', 'muteErrors' => false)); /** * And create tracer object, if you want to have statically access just initialize TracerProxy @@ -40,16 +40,16 @@ $spanId = new SpanIdentifier(); // Context for file_get_contents for passing headers (B3 propagation) -$context = stream_context_create([ - 'http' => [ +$context = stream_context_create(array( + 'http' => array( 'method' => 'GET', 'header' => 'X-B3-TraceId: ' . TracerInfo::getTraceId() . "\r\n" . 'X-B3-SpanId: ' . ((string) $spanId) . "\r\n" . 'X-B3-ParentSpanId: ' . TracerInfo::getTraceSpanId() . "\r\n" . 'X-B3-Sampled: ' . ((int) TracerInfo::isSampled()) . "\r\n" - ] -]); + ) +)); $request = file_get_contents($url, false, $context); diff --git a/src/AnnotationBlock.php b/src/AnnotationBlock.php index 484f655..411f113 100644 --- a/src/AnnotationBlock.php +++ b/src/AnnotationBlock.php @@ -76,18 +76,18 @@ public function getStartTimestamp() */ public function toArray() { - return [ - [ + return array( + array( 'endpoint' => $this->endpoint->toArray(), 'timestamp' => $this->startTimestamp, 'value' => $this->values[0] - ], - [ + ), + array( 'endpoint' => $this->endpoint->toArray(), 'timestamp' => $this->endTimestamp, 'value' => $this->values[1] - ] - ]; + ) + ); } /** @@ -101,10 +101,10 @@ protected function setValues($type) { switch ($type) { case static::CLIENT: - $this->values = ['cs', 'cr']; + $this->values = array('cs', 'cr'); break; case static::SERVER: - $this->values = ['sr', 'ss']; + $this->values = array('sr', 'ss'); break; default: throw new \InvalidArgumentException('$type must be TYPE_CLIENT or TYPE_SERVER'); diff --git a/src/Endpoint.php b/src/Endpoint.php index 507d717..409bad3 100644 --- a/src/Endpoint.php +++ b/src/Endpoint.php @@ -45,11 +45,11 @@ public function __construct($serviceName, $ip, $port = '80') */ public function toArray() { - return [ + return array( 'serviceName' => $this->serviceName, 'ipv4' => $this->ip, 'port' => $this->port - ]; + ); } /** diff --git a/src/Logger/FileLogger.php b/src/Logger/FileLogger.php index 8f7197d..6f62c55 100644 --- a/src/Logger/FileLogger.php +++ b/src/Logger/FileLogger.php @@ -24,13 +24,14 @@ class FileLogger implements Logger * @param $options array See above * * @throws \BadMethodCallException + * @throws LoggerException */ public function __construct($options = []) { - $defaults = [ + $defaults = array( 'path' => 'tmp' . DIRECTORY_SEPARATOR, 'fileName' => 'zipkin.log' - ]; + ); $this->options = array_merge($defaults, $options); @@ -50,4 +51,4 @@ public function trace($spans) FILE_APPEND | LOCK_EX ); } -} \ No newline at end of file +} diff --git a/src/Logger/Logger.php b/src/Logger/Logger.php index 38b855c..5da1090 100644 --- a/src/Logger/Logger.php +++ b/src/Logger/Logger.php @@ -14,10 +14,10 @@ interface Logger * * @param $options */ - public function __construct($options = []); + public function __construct($options = array()); /** * @param $spans array */ public function trace($spans); -} \ No newline at end of file +} diff --git a/src/Logger/SimpleHttpLogger.php b/src/Logger/SimpleHttpLogger.php index cc4aa9d..bac03fd 100644 --- a/src/Logger/SimpleHttpLogger.php +++ b/src/Logger/SimpleHttpLogger.php @@ -29,29 +29,31 @@ class SimpleHttpLogger implements Logger */ public function __construct($options = []) { - $defaults = [ + $defaults = array( 'host' => 'http://127.0.0.1:9144', 'endpoint' => '/api/v1/spans', 'muteErrors' => true, - 'contextOptions' => [] - ]; + 'contextOptions' => array() + ); $this->options = array_merge($defaults, $options); } /** * @inheritdoc + * + * @throws LoggerException */ public function trace($spans) { - $contextOptions = [ - 'http' => [ + $contextOptions = array( + 'http' => array( 'method' => 'POST', 'header' => 'Content-type: application/json', 'content' => json_encode($spans), 'ignore_errors' => true - ] - ]; + ) + ); $context = stream_context_create(array_merge_recursive($contextOptions, $this->options['contextOptions'])); @file_get_contents($this->options['host'] . $this->options['endpoint'], false, $context); @@ -66,6 +68,8 @@ public function trace($spans) /** * Search for 202 header * + * @param $headers array + * * @return bool */ protected function validResponse($headers) diff --git a/src/Metadata.php b/src/Metadata.php index f8c128d..3bae34a 100644 --- a/src/Metadata.php +++ b/src/Metadata.php @@ -68,7 +68,7 @@ class Metadata /** * @var array[] */ - protected $annotations = []; + protected $annotations = array(); /** * Set meta annotation @@ -88,10 +88,10 @@ public function set($key, $value) throw new \InvalidArgumentException('$value must be string, int or bool'); } - $this->annotations[] = [ + $this->annotations[] = array( 'key' => $key, 'value' => (string) $value - ]; + ); } /** diff --git a/src/Sampler/PercentageSampler.php b/src/Sampler/PercentageSampler.php index adf2f38..01415ff 100644 --- a/src/Sampler/PercentageSampler.php +++ b/src/Sampler/PercentageSampler.php @@ -22,11 +22,11 @@ class PercentageSampler implements Sampler * * @param $options array See above */ - public function __construct($options = []) + public function __construct($options = array()) { - $defaults = [ + $defaults = array( 'percents' => 50 - ]; + ); $this->options = array_merge($defaults, $options); } diff --git a/src/Sampler/Sampler.php b/src/Sampler/Sampler.php index 0fe999a..cc88383 100644 --- a/src/Sampler/Sampler.php +++ b/src/Sampler/Sampler.php @@ -14,7 +14,7 @@ interface Sampler * * @param array $options */ - public function __construct($options = []); + public function __construct($options = array()); /** * Returns calculated flag @@ -22,4 +22,4 @@ public function __construct($options = []); * @return bool */ public function isSampled(); -} \ No newline at end of file +} diff --git a/src/Span.php b/src/Span.php index cb44280..0680290 100644 --- a/src/Span.php +++ b/src/Span.php @@ -51,7 +51,7 @@ class Span * @param $traceId Identifier Trace identifier (default from TraceInfo::getTraceId()) * @param $parentId Identifier Parent identifier (default from TraceInfo::getTraceSpanId()) */ - function __construct( + public function __construct( $id, $name, $annotationBlock, @@ -64,8 +64,10 @@ function __construct( $this->setName($name); $this->setAnnotationBlock($annotationBlock); $this->setMetadata($metadata); - $this->setIdentifier('traceId', $traceId, [TracerInfo::class, 'getTraceId']); - $this->setIdentifier('parentId', $parentId, [TracerInfo::class, 'getTraceSpanId']); + + # FIXME: Class name constant is available in PHP 5.5 only + $this->setIdentifier('traceId', $traceId, array(TracerInfo::class, 'getTraceId')); + $this->setIdentifier('parentId', $parentId, array(TracerInfo::class, 'getTraceSpanId')); } /** @@ -75,14 +77,14 @@ function __construct( */ public function toArray() { - $span = [ + $span = array( 'id' => (string) $this->id, 'traceId' => (string) $this->traceId, 'name' => $this->name, 'timestamp' => $this->annotationBlock->getStartTimestamp(), 'duration' => $this->annotationBlock->getDuration(), 'annotations' => $this->annotationBlock->toArray() - ]; + ); if ($this->parentId !== null) { $span['parentId'] = (string) $this->parentId; diff --git a/src/Tracer.php b/src/Tracer.php index 9f96fdf..585c58a 100644 --- a/src/Tracer.php +++ b/src/Tracer.php @@ -39,7 +39,7 @@ class Tracer /** * @var Span[] */ - protected $spans = []; + protected $spans = array(); /** * @var string diff --git a/src/TracerInfo.php b/src/TracerInfo.php index 786c75a..debc437 100644 --- a/src/TracerInfo.php +++ b/src/TracerInfo.php @@ -39,6 +39,8 @@ class TracerInfo public static function init($sampler, $traceId, $traceSpanId) { static::setSampled($sampler); + + # FIXME: Class name constant is available in PHP 5.5 only static::setIdentifier('traceId', $traceId, TraceIdentifier::class); static::setIdentifier('traceSpanId', $traceSpanId, SpanIdentifier::class); } diff --git a/src/TracerProxy.php b/src/TracerProxy.php index daa4438..3469397 100644 --- a/src/TracerProxy.php +++ b/src/TracerProxy.php @@ -28,6 +28,8 @@ public static function init($instance) /** * @see Tracer::addSpan($span) + * + * @param $span Span */ public static function addSpan($span) { diff --git a/tests/AnnotationBlockTestCase.php b/tests/AnnotationBlockTestCase.php index 7f985bc..ae372e6 100644 --- a/tests/AnnotationBlockTestCase.php +++ b/tests/AnnotationBlockTestCase.php @@ -63,6 +63,7 @@ public function shouldCreateWithEndTime() public function shouldFailOnType() { // given + # FIXME: Class name constant is available in PHP 5.5 only $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessageRegExp('/\$type/'); @@ -83,6 +84,7 @@ public function shouldFailOnType() public function shouldFailOnEndpoint() { // given + # FIXME: Class name constant is available in PHP 5.5 only $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessageRegExp('/\$endpoint/'); @@ -101,6 +103,7 @@ public function shouldFailOnEndpoint() public function shouldFailOnTimestamp() { // given + # FIXME: Class name constant is available in PHP 5.5 only $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessageRegExp('/startTimestamp/'); diff --git a/tests/EndpointTestCase.php b/tests/EndpointTestCase.php index 4ec17e7..7ca1ff6 100644 --- a/tests/EndpointTestCase.php +++ b/tests/EndpointTestCase.php @@ -40,6 +40,7 @@ public function shouldCreate() public function shouldFailOnName() { // given + # FIXME: Class name constant is available in PHP 5.5 only $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessageRegExp('/\$serviceName/'); @@ -55,6 +56,7 @@ public function shouldFailOnName() public function shouldFailOnIp() { // given + # FIXME: Class name constant is available in PHP 5.5 only $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessageRegExp('/\$ip/'); @@ -70,6 +72,7 @@ public function shouldFailOnIp() public function shouldFailOnPort() { // given + # FIXME: Class name constant is available in PHP 5.5 only $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessageRegExp('/\$port/'); diff --git a/tests/MetadataTestCase.php b/tests/MetadataTestCase.php index 5c1d247..f9875f6 100644 --- a/tests/MetadataTestCase.php +++ b/tests/MetadataTestCase.php @@ -17,12 +17,12 @@ class MetadataTestCase extends \PHPUnit_Framework_TestCase public function shouldCreate() { // given - $data = [ - ['part', 'basket'], - ['cabinet', false], - ['perform', 'race'], - ['chain', 1997] - ]; + $data = array( + array('part', 'basket'), + array('cabinet', false), + array('perform', 'race'), + array('chain', 1997) + ); // when $metadata = new Metadata(); @@ -45,6 +45,7 @@ public function shouldCreate() public function shouldFailOnKey() { // given + # FIXME: Class name constant is available in PHP 5.5 only $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessageRegExp('/\$key/'); @@ -63,6 +64,7 @@ public function shouldFailOnKey() public function shouldFailOnValue() { // given + # FIXME: Class name constant is available in PHP 5.5 only $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessageRegExp('/\$value/'); diff --git a/tests/Mocker.php b/tests/Mocker.php index 3b08727..11c484f 100644 --- a/tests/Mocker.php +++ b/tests/Mocker.php @@ -23,7 +23,7 @@ public static function initTracer() new Tracer( 'hut', static::getEndpoint(), - new FileLogger(['path' => './']) + new FileLogger(array('path' => './')) ); } diff --git a/tests/Sampler/PercentageSamplerTestCase.php b/tests/Sampler/PercentageSamplerTestCase.php index 386d84e..bce1ccb 100644 --- a/tests/Sampler/PercentageSamplerTestCase.php +++ b/tests/Sampler/PercentageSamplerTestCase.php @@ -20,7 +20,7 @@ public function shouldCalculate() $percents = 51; // when - $sampler = new PercentageSampler(['percents' => $percents]); + $sampler = new PercentageSampler(array('percents' => $percents)); // then $this->assertTrue(is_bool($sampler->isSampled())); @@ -35,7 +35,7 @@ public function shouldNotBeSampled() $percents = 0; // when - $sampler = new PercentageSampler(['percents' => $percents]); + $sampler = new PercentageSampler(array('percents' => $percents)); // then $this->assertFalse($sampler->isSampled()); @@ -50,9 +50,9 @@ public function shouldBeSampled() $percents = 100; // when - $sampler = new PercentageSampler(['percents' => $percents]); + $sampler = new PercentageSampler(array('percents' => $percents)); // then $this->assertTrue($sampler->isSampled()); } -} \ No newline at end of file +}