Skip to content

Commit 5edc355

Browse files
authored
Merge pull request #5 from whitemerry/#4.-Adding-http-option
SimpleHttpLogger
2 parents 064cbd9 + 6035086 commit 5edc355

File tree

4 files changed

+114
-9
lines changed

4 files changed

+114
-9
lines changed

README.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ $endpoint = new Endpoint(
2626
'80' // Current application port (default 80)
2727
);
2828
```
29-
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):
29+
Next, define storage for traces - currently two types are supported - SimpleHttpLogger and FileLogger,
30+
SimpleHttpLogger automatically sends trace data to Zipkin's service,
31+
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
32+
(of course you can implement own using our interface):
3033
```php
31-
$logger = new FileLogger([
32-
'path' => './logs', // Zipkin traces logs location
33-
'fileName' => 'zipkin.log' // File name
34+
$logger = new SimpleHttpLogger([
35+
'host' => 'http://192.168.33.11:9411' // Zipkin's API host with schema (http://) and without trailing slash
3436
]);
3537
```
3638
***Now you can initialize Tracer!***
@@ -66,7 +68,7 @@ As last step just trigger trace method from $tracer, for example in shutdown eve
6668
```php
6769
$tracer->trace();
6870
```
69-
Now as you can see, requests to your website are generating new lines in logs/zipkin.log
71+
Now as you can see, you have new entries in the Zipkin's UI! :)
7072

7173
#### Adding spans to trace
7274
As you already now, in Zipkin, you can store and visualize communication between 2 services (for example databases, microservices).
@@ -131,15 +133,26 @@ TracerInfo::isSampled(); // Sampled - X-B3-Sampled
131133
```
132134
Remember to set this headers to request in your client to other services.
133135

134-
#### Why FileLogger?
136+
#### Why do i prefer FileLogger?
135137
You can write your own logger. I prefer this type, because optimization.
136-
It's better to send logs to Zipkin in background than increasing page load time by sending next request to API.
138+
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.
137139

138140
Don't let your users wait :)
139141

142+
Usage example:
143+
```php
144+
$logger = new FileLogger([
145+
'path' => './logs', // Zipkin traces logs location
146+
'fileName' => 'zipkin.log' // File name
147+
]);
148+
```
149+
140150
#### How can i upload logs to Zipkin?
141-
Use Zipkin's rest API and send traces from zipkin.log.
151+
For SimpleHttpLogger:
152+
It does eveything for you.
142153

154+
For FileLogger:
155+
Use Zipkin's rest API and send traces from zipkin.log.
143156
How do i do that? Cron every 10 minutes, calling action witch sends POST.
144157

145158
You can read more about Zipkin's API endpoint [here](http://zipkin.io/zipkin-api/#/paths/%252Fspans/post)

src/Logger/FileLogger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct($options = [])
3535
$this->options = array_merge($defaults, $options);
3636

3737
if (!is_dir($this->options['path'])) {
38-
throw new \BadMethodCallException('Invalid logs directory');
38+
throw new LoggerException('Invalid logs directory');
3939
}
4040
}
4141

src/Logger/LoggerException.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace whitemerry\phpkin\Logger;
3+
4+
/**
5+
* Class LoggerException for try-catch blocks
6+
*
7+
* @author Piotr Bugaj <[email protected]>
8+
* @package whitemerry\phpkin\Logger
9+
*/
10+
class LoggerException extends \Exception {}

src/Logger/SimpleHttpLogger.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
namespace whitemerry\phpkin\Logger;
3+
4+
/**
5+
* Class SimpleHttpLogger
6+
*
7+
* @author Piotr Bugaj <[email protected]>
8+
* @package whitemerry\phpkin\Logger
9+
*/
10+
class SimpleHttpLogger implements Logger
11+
{
12+
/**
13+
* @var array
14+
*/
15+
protected $options;
16+
17+
/**
18+
* @inheritdoc
19+
*
20+
* $options
21+
* ['host'] string Zipkin's host with port, schema and without trailing slash (default http://127.0.0.1:9411)
22+
* ['endpoint'] string Zipkin's endpoint (default /api/v1/spans)
23+
* ['muteErrors'] bool Mute exceptions on upload error (default true)
24+
* ['contextOptions'] array More options for stream_context_create like ssl
25+
*
26+
* @param $options array See above
27+
*
28+
* @throws \BadMethodCallException
29+
*/
30+
public function __construct($options = [])
31+
{
32+
$defaults = [
33+
'host' => 'http://127.0.0.1:9144',
34+
'endpoint' => '/api/v1/spans',
35+
'muteErrors' => true,
36+
'contextOptions' => []
37+
];
38+
39+
$this->options = array_merge($defaults, $options);
40+
}
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
public function trace($spans)
46+
{
47+
$contextOptions = [
48+
'http' => [
49+
'method' => 'POST',
50+
'header' => 'Content-type: application/json',
51+
'content' => json_encode($spans),
52+
'ignore_errors' => true
53+
]
54+
];
55+
$context = stream_context_create(array_merge_recursive($contextOptions, $this->options['contextOptions']));
56+
@file_get_contents($this->options['host'] . $this->options['endpoint'], false, $context);
57+
58+
if (!$this->options['muteErrors'] && !$this->validResponse($http_response_header)) {
59+
throw new LoggerException('Trace upload failed');
60+
}
61+
}
62+
63+
/**
64+
* Search for 202 header
65+
*
66+
* @return bool
67+
*/
68+
protected function validResponse($headers)
69+
{
70+
if (empty($headers)) {
71+
return false;
72+
}
73+
74+
foreach ($headers as $header) {
75+
if (preg_match('/202/', $header)) {
76+
return true;
77+
}
78+
}
79+
80+
return false;
81+
}
82+
}

0 commit comments

Comments
 (0)