Skip to content

Commit

Permalink
Introduce Logger plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod committed Nov 12, 2020
1 parent 1cb8755 commit 633fe7b
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 43 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ Naming used here is the same as in ClickHouse docs.
- All [ClickHouse Formats](https://clickhouse.yandex/docs/en/interfaces/formats/) support
- Logging ([PSR-3 compliant](https://www.php-fig.org/psr/psr-3/))
- SQL Factory for [parameters "binding"](#parameters-binding)
- Dependency only on PSR interfaces, Guzzle A+ Promises for async requests and Safe

## Contents

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"require": {
"php-64bit": "^7.4",
"guzzlehttp/promises": "^1.4",
"php-http/client-common": "^2.0",
"psr/http-client": "^1.0",
"psr/http-factory": "^1.0",
"psr/http-message": "^1.0",
Expand Down
43 changes: 43 additions & 0 deletions src/Client/Http/LoggerClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace SimPod\ClickHouseClient\Client\Http;

use Http\Client\Common\Plugin;
use Http\Promise\Promise;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use SimPod\ClickHouseClient\Logger\SqlLogger;
use Throwable;

use function uniqid;

final class LoggerClient implements Plugin
{
private SqlLogger $logger;

public function __construct(SqlLogger $logger)
{
$this->logger = $logger;
}

public function handleRequest(RequestInterface $request, callable $next, callable $first) : Promise
{
$id = uniqid('', true);
$this->logger->startQuery($id, (string) $request->getBody());

return $next($request)->then(
function (ResponseInterface $response) use ($id) : ResponseInterface {
$this->logger->stopQuery($id);

return $response;
},
function (Throwable $throwable) use ($id) : void {
$this->logger->stopQuery($id);

throw $throwable;
}
);
}
}
7 changes: 0 additions & 7 deletions src/Client/PsrClickHouseAsyncClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use GuzzleHttp\Promise\PromiseInterface;
use Http\Client\HttpAsyncClient;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use SimPod\ClickHouseClient\Client\Http\RequestFactory;
use SimPod\ClickHouseClient\Client\Http\RequestOptions;
use SimPod\ClickHouseClient\Exception\ServerError;
Expand All @@ -24,8 +23,6 @@ class PsrClickHouseAsyncClient implements ClickHouseAsyncClient

private RequestFactory $requestFactory;

private LoggerInterface $logger;

private string $endpoint;

/** @var array<string, float|int|string> */
Expand All @@ -37,14 +34,12 @@ class PsrClickHouseAsyncClient implements ClickHouseAsyncClient
public function __construct(
HttpAsyncClient $asyncClient,
RequestFactory $requestFactory,
LoggerInterface $logger,
string $endpoint,
array $defaultParameters = [],
?DateTimeZone $clickHouseTimeZone = null
) {
$this->asyncClient = $asyncClient;
$this->requestFactory = $requestFactory;
$this->logger = $logger;
$this->endpoint = $endpoint;
$this->defaultParameters = $defaultParameters;
$this->sqlFactory = new SqlFactory(new ValueFormatter($clickHouseTimeZone));
Expand Down Expand Up @@ -90,8 +85,6 @@ private function executeRequest(
array $requestParameters = [],
?callable $processResponse = null
) : PromiseInterface {
$this->logger->debug($sql, $requestParameters);

$request = $this->requestFactory->prepareRequest(
$this->endpoint,
new RequestOptions(
Expand Down
9 changes: 0 additions & 9 deletions src/Client/PsrClickHouseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use SimPod\ClickHouseClient\Exception\CannotInsert;
use SimPod\ClickHouseClient\Exception\ServerError;
use SimPod\ClickHouseClient\Format\Format;
use SimPod\ClickHouseClient\Logger\SqlLogger;
use SimPod\ClickHouseClient\Output\Output;
use SimPod\ClickHouseClient\Sql\Escaper;
use SimPod\ClickHouseClient\Sql\SqlFactory;
Expand All @@ -31,8 +30,6 @@ class PsrClickHouseClient implements ClickHouseClient

private RequestFactory $requestFactory;

private SqlLogger $logger;

private string $endpoint;

/** @var array<string, float|int|string> */
Expand All @@ -46,14 +43,12 @@ class PsrClickHouseClient implements ClickHouseClient
public function __construct(
ClientInterface $client,
RequestFactory $requestFactory,
SqlLogger $logger,
string $endpoint,
array $defaultParameters = [],
?DateTimeZone $clickHouseTimeZone = null
) {
$this->client = $client;
$this->requestFactory = $requestFactory;
$this->logger = $logger;
$this->endpoint = $endpoint;
$this->defaultParameters = $defaultParameters;
$this->valueFormatter = new ValueFormatter($clickHouseTimeZone);
Expand Down Expand Up @@ -171,12 +166,8 @@ private function executeRequest(string $sql, array $requestParameters = []) : Re
)
);

$this->logger->startQuery($sql);

$response = $this->client->sendRequest($request);

$this->logger->stopQuery();

if ($response->getStatusCode() !== 200) {
throw ServerError::fromResponse($response);
}
Expand Down
9 changes: 4 additions & 5 deletions src/Logger/LoggerChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@ public function __construct(array $loggers = [])
$this->loggers = $loggers;
}

/** @inheritdoc */
public function startQuery(string $sql, array $params = []) : void
public function startQuery(string $id, string $sql) : void
{
foreach ($this->loggers as $logger) {
$logger->startQuery($sql, $params);
$logger->startQuery($id, $sql);
}
}

public function stopQuery() : void
public function stopQuery(string $id) : void
{
foreach ($this->loggers as $logger) {
$logger->stopQuery();
$logger->stopQuery($id);
}
}
}
7 changes: 3 additions & 4 deletions src/Logger/PsrLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ public function __construct(LoggerInterface $logger)
$this->logger = $logger;
}

/** @inheritdoc */
public function startQuery(string $sql, array $params = []) : void
public function startQuery(string $id, string $sql) : void
{
$this->logger->debug($sql, $params);
$this->logger->debug($sql);
}

public function stopQuery() : void
public function stopQuery(string $id) : void
{
}
}
5 changes: 2 additions & 3 deletions src/Logger/SqlLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

interface SqlLogger
{
/** @param array<string, mixed> $params */
public function startQuery(string $sql, array $params = []) : void;
public function startQuery(string $id, string $sql) : void;

public function stopQuery() : void;
public function stopQuery(string $id) : void;
}
17 changes: 7 additions & 10 deletions tests/Logger/LoggerChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,33 @@ final class LoggerChainTest extends TestCase
public function testLog() : void
{
$logger = new class implements SqlLogger {
public ?string $sql = null;
public string $id;

/** @var array<string, mixed>|null $params */
public ?array $params = null;
public ?string $sql = null;

public bool $started = false;

public bool $stopped = false;

/** @inheritDoc */
public function startQuery(string $sql, array $params = []) : void
public function startQuery(string $id, string $sql) : void
{
$this->id = $id;
$this->sql = $sql;
$this->params = $params;
$this->started = true;
}

public function stopQuery() : void
public function stopQuery(string $id) : void
{
$this->stopped = true;
}
};

$chain = new LoggerChain([$logger]);

$chain->startQuery('sql', []);
$chain->stopQuery();
$chain->startQuery('a', 'sql');
$chain->stopQuery('a');

self::assertSame('sql', $logger->sql);
self::assertSame([], $logger->params);
self::assertTrue($logger->started);
self::assertTrue($logger->stopped);
}
Expand Down
4 changes: 0 additions & 4 deletions tests/WithClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use SimPod\ClickHouseClient\Client\Http\RequestFactory;
use SimPod\ClickHouseClient\Client\PsrClickHouseAsyncClient;
use SimPod\ClickHouseClient\Client\PsrClickHouseClient;
use SimPod\ClickHouseClient\Logger\PsrLogger;

use function assert;
use function getenv;
Expand Down Expand Up @@ -64,7 +63,6 @@ public function restartClickHouseClient() : void
new Psr17Factory(),
new Psr17Factory()
),
new PsrLogger(new TestLogger()),
$endpoint,
$defaultParameters
);
Expand All @@ -78,7 +76,6 @@ public function restartClickHouseClient() : void
new Psr17Factory(),
new Psr17Factory()
),
new PsrLogger(new TestLogger()),
$endpoint,
$defaultParameters
);
Expand All @@ -90,7 +87,6 @@ public function restartClickHouseClient() : void
new Psr17Factory(),
new Psr17Factory()
),
new TestLogger(),
$endpoint,
$defaultParameters
);
Expand Down

0 comments on commit 633fe7b

Please sign in to comment.