Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!***
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -131,15 +133,26 @@ 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.
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)
Expand Down
2 changes: 1 addition & 1 deletion src/Logger/FileLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/Logger/LoggerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace whitemerry\phpkin\Logger;

/**
* Class LoggerException for try-catch blocks
*
* @author Piotr Bugaj <whitemerry@outlook.com>
* @package whitemerry\phpkin\Logger
*/
class LoggerException extends \Exception {}
82 changes: 82 additions & 0 deletions src/Logger/SimpleHttpLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
namespace whitemerry\phpkin\Logger;

/**
* Class SimpleHttpLogger
*
* @author Piotr Bugaj <whitemerry@outlook.com>
* @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;
}
}