-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Gelf logger handler implementation
- Loading branch information
Showing
14 changed files
with
332 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/BundledPlugin/Logger/Internal/GelfTransport/GelfHttpTransport.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Luzrain\PHPStreamServer\BundledPlugin\Logger\Internal\GelfTransport; | ||
|
||
use Amp\Http\Client\HttpClient; | ||
use Amp\Http\Client\HttpClientBuilder; | ||
use Amp\Http\Client\Request; | ||
use Amp\Http\Client\SocketException; | ||
|
||
final class GelfHttpTransport implements GelfTransport | ||
{ | ||
private HttpClient $httpClient; | ||
private bool $inErrorState = false; | ||
|
||
public function __construct(private readonly string $url) | ||
{ | ||
if (!\class_exists(HttpClient::class)) { | ||
throw new \RuntimeException(\sprintf('You cannot use "%s" as the "http-client" package is not installed. Try running "composer require amphp/http-client".', __CLASS__)); | ||
} | ||
} | ||
|
||
public function start(): void | ||
{ | ||
$this->httpClient = (new HttpClientBuilder())->followRedirects(0)->build(); | ||
} | ||
|
||
public function write(string $buffer): void | ||
{ | ||
$request = new Request($this->url, 'POST', $buffer); | ||
$request->setHeader('Content-Type', 'application/json'); | ||
$request->setTransferTimeout(5); | ||
|
||
try { | ||
$this->httpClient->request($request); | ||
$this->inErrorState = false; | ||
} catch (SocketException $e) { | ||
if($this->inErrorState === false) { | ||
\trigger_error($e->getMessage(), E_USER_WARNING); | ||
$this->inErrorState = true; | ||
} | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/BundledPlugin/Logger/Internal/GelfTransport/GelfTcpTransport.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Luzrain\PHPStreamServer\BundledPlugin\Logger\Internal\GelfTransport; | ||
|
||
use Amp\ByteStream\StreamException; | ||
use Amp\ByteStream\WritableStream; | ||
use Amp\Socket\ConnectContext; | ||
use Amp\Socket\ConnectException; | ||
use Amp\Socket\DnsSocketConnector; | ||
use Luzrain\PHPStreamServer\BundledPlugin\Logger\Internal\NullWritableStream; | ||
use Revolt\EventLoop; | ||
|
||
final class GelfTcpTransport implements GelfTransport | ||
{ | ||
private const CONNECT_TIMEOUT = 4; | ||
private const RECONNECT_TIMEOUT = 10; | ||
|
||
private WritableStream $socket; | ||
private bool $inErrorState = false; | ||
|
||
public function __construct(private readonly string $host, private readonly int $port) | ||
{ | ||
} | ||
|
||
public function start(): void | ||
{ | ||
$connector = new DnsSocketConnector(); | ||
$context = (new ConnectContext())->withConnectTimeout(self::CONNECT_TIMEOUT); | ||
|
||
try { | ||
$this->socket = $connector->connect(\sprintf('tcp://%s:%d', $this->host, $this->port), $context); | ||
$this->inErrorState = false; | ||
} catch (ConnectException $e) { | ||
$this->socket = new NullWritableStream(); | ||
|
||
if ($this->inErrorState === false) { | ||
\trigger_error($e->getMessage(), E_USER_WARNING); | ||
$this->inErrorState = true; | ||
} | ||
|
||
EventLoop::delay(self::RECONNECT_TIMEOUT, function () { | ||
$this->start(); | ||
}); | ||
} | ||
} | ||
|
||
public function write(string $buffer): void | ||
{ | ||
try { | ||
$this->socket->write($buffer . "\0"); | ||
} catch (StreamException) { | ||
$this->start(); | ||
// try to send second time after connect | ||
try { | ||
$this->socket->write($buffer. "\0"); | ||
} catch (StreamException) { | ||
// do nothing | ||
} | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/BundledPlugin/Logger/Internal/GelfTransport/GelfTransport.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Luzrain\PHPStreamServer\BundledPlugin\Logger\Internal\GelfTransport; | ||
|
||
interface GelfTransport | ||
{ | ||
public function start(): void; | ||
|
||
public function write(string $buffer): void; | ||
} |
Oops, something went wrong.