From a1770d99c69947383d54a1a2db557ab652384461 Mon Sep 17 00:00:00 2001 From: Piotr Bugaj Date: Sun, 7 May 2017 13:32:06 +0200 Subject: [PATCH 1/2] SimpleHttpLogger --- README.md | 24 +++++++--- src/Logger/FileLogger.php | 2 +- src/Logger/LoggerException.php | 10 ++++ src/Logger/SimpleHttpLogger.php | 82 +++++++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+), 8 deletions(-) create mode 100644 src/Logger/LoggerException.php create mode 100644 src/Logger/SimpleHttpLogger.php diff --git a/README.md b/README.md index 2169930..798fef6 100644 --- a/README.md +++ b/README.md @@ -26,11 +26,13 @@ $endpoint = new Endpoint( '80' // Current application port (default 80) ); ``` -Next, define storage for traces - now implemented is only one, but you can implement own using our interface, FileLogger (Scroll below for more information about loggers in php): +Next, define storage for traces - currently two types are supported - SimpleHttpLogger and FileLogger, +SimpleHttpLogger automatically sends trace data to Zipkin's service, +FileLogger (you can read more about this logger below) sends trace data to file, then you need to write curl for uploading this data to Zipkin's +(of course you can implement own using our interface): ```php -$logger = new FileLogger([ - 'path' => './logs', // Zipkin traces logs location - 'fileName' => 'zipkin.log' // File name +$logger = new SimpleHttpLogger([ + 'host' => 'http://192.168.33.11:9411' // Zipkin's API host with schema (http://) and without trailing slash ]); ``` ***Now you can initialize Tracer!*** @@ -66,7 +68,7 @@ As last step just trigger trace method from $tracer, for example in shutdown eve ```php $tracer->trace(); ``` -Now as you can see, requests to your website are generating new lines in logs/zipkin.log +Now as you can see, you have new entries in the Zipkin's UI! :) #### Adding spans to trace As you already now, in Zipkin, you can store and visualize communication between 2 services (for example databases, microservices). @@ -131,12 +133,20 @@ TracerInfo::isSampled(); // Sampled - X-B3-Sampled ``` Remember to set this headers to request in your client to other services. -#### Why FileLogger? +#### Why do i prefer FileLogger? You can write your own logger. I prefer this type, because optimization. -It's better to send logs to Zipkin in background than increasing page load time by sending next request to API. +It's better to store logs in files and send to Zipkin in background than increasing page load time by sending next request to API. Don't let your users wait :) +Usage example: +```php +$logger = new FileLogger([ + 'path' => './logs', // Zipkin traces logs location + 'fileName' => 'zipkin.log' // File name +]); +``` + #### How can i upload logs to Zipkin? Use Zipkin's rest API and send traces from zipkin.log. diff --git a/src/Logger/FileLogger.php b/src/Logger/FileLogger.php index 5a42795..8f7197d 100644 --- a/src/Logger/FileLogger.php +++ b/src/Logger/FileLogger.php @@ -35,7 +35,7 @@ public function __construct($options = []) $this->options = array_merge($defaults, $options); if (!is_dir($this->options['path'])) { - throw new \BadMethodCallException('Invalid logs directory'); + throw new LoggerException('Invalid logs directory'); } } diff --git a/src/Logger/LoggerException.php b/src/Logger/LoggerException.php new file mode 100644 index 0000000..0d8ba8e --- /dev/null +++ b/src/Logger/LoggerException.php @@ -0,0 +1,10 @@ + + * @package whitemerry\phpkin\Logger + */ +class LoggerException extends \Exception {} diff --git a/src/Logger/SimpleHttpLogger.php b/src/Logger/SimpleHttpLogger.php new file mode 100644 index 0000000..e21c336 --- /dev/null +++ b/src/Logger/SimpleHttpLogger.php @@ -0,0 +1,82 @@ + + * @package whitemerry\phpkin\Logger + */ +class SimpleHttpLogger implements Logger +{ + /** + * @var array + */ + protected $options; + + /** + * @inheritdoc + * + * $options + * ['host'] string Zipkin's host with port, schema and without trailing slash (default http://127.0.0.1:9411) + * ['endpoint'] string Zipkin's endpoint (default /api/v1/spans) + * ['muteErrors'] bool Mute exceptions on upload error (default true) + * ['contextOptions'] array More options for stream_context_create like ssl + * + * @param $options array See above + * + * @throws \BadMethodCallException + */ + public function __construct($options = []) + { + $defaults = [ + 'host' => 'http://127.0.0.1:9144', + 'endpoint' => '/api/v1/spans', + 'muteErrors' => true, + 'contextOptions' => [] + ]; + + $this->options = array_merge($defaults, $options); + } + + /** + * @inheritdoc + */ + public function trace($spans) + { + $contextOptions = [ + 'http' => [ + '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); + + if (!$this->options['muteErrors'] && !$this->validResponse($http_response_header)) { + throw new LoggerException('Trace upload failed'); + } + } + + /** + * Search for 202 header + * + * @return bool + */ + protected function validResponse($headers) + { + if (empty($headers)) { + return false; + } + + foreach ($headers as $header) { + if (preg_match('/202/', $header)) { + return true; + } + } + + return false; + } +} From 6035086939ae5c731e7b0fb17c325ef058d7fde3 Mon Sep 17 00:00:00 2001 From: Piotr Bugaj Date: Sun, 7 May 2017 13:34:15 +0200 Subject: [PATCH 2/2] README.md --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 798fef6..cdf589c 100644 --- a/README.md +++ b/README.md @@ -148,8 +148,11 @@ $logger = new FileLogger([ ``` #### How can i upload logs to Zipkin? -Use Zipkin's rest API and send traces from zipkin.log. +For SimpleHttpLogger: +It does eveything for you. +For FileLogger: +Use Zipkin's rest API and send traces from zipkin.log. How do i do that? Cron every 10 minutes, calling action witch sends POST. You can read more about Zipkin's API endpoint [here](http://zipkin.io/zipkin-api/#/paths/%252Fspans/post)