Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for sync query logging #62

Merged
merged 1 commit into from
Sep 22, 2020
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
1 change: 0 additions & 1 deletion src/Client/PsrClickHouseAsyncClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ private function executeRequest(
$promise = promise_for($this->asyncClient->sendAsyncRequest($request));

return $promise->then(
/** @return mixed */
static function (ResponseInterface $response) use ($processResponse) {
if ($response->getStatusCode() !== 200) {
throw ServerError::fromResponse($response);
Expand Down
13 changes: 8 additions & 5 deletions src/Client/PsrClickHouseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
use DateTimeZone;
use Psr\Http\Client\ClientInterface;
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\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,7 +31,7 @@ class PsrClickHouseClient implements ClickHouseClient

private RequestFactory $requestFactory;

private LoggerInterface $logger;
private SqlLogger $logger;

private string $endpoint;

Expand All @@ -46,7 +46,7 @@ class PsrClickHouseClient implements ClickHouseClient
public function __construct(
ClientInterface $client,
RequestFactory $requestFactory,
LoggerInterface $logger,
SqlLogger $logger,
string $endpoint,
array $defaultParameters = [],
?DateTimeZone $clickHouseTimeZone = null
Expand Down Expand Up @@ -162,8 +162,6 @@ public function insertWithFormat(string $table, Format $inputFormat, string $dat
/** @param array<string, float|int|string> $requestParameters */
private function executeRequest(string $sql, array $requestParameters = []) : ResponseInterface
{
$this->logger->debug($sql, $requestParameters);

$request = $this->requestFactory->prepareRequest(
$this->endpoint,
new RequestOptions(
Expand All @@ -173,7 +171,12 @@ 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
32 changes: 32 additions & 0 deletions src/Logger/LoggerChain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace SimPod\ClickHouseClient\Logger;

final class LoggerChain implements SqlLogger
{
/** @var SqlLogger[] */
private array $loggers;

/** @param SqlLogger[] $loggers */
public function __construct(array $loggers = [])
{
$this->loggers = $loggers;
}

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

public function stopQuery() : void
{
foreach ($this->loggers as $logger) {
$logger->stopQuery();
}
}
}
27 changes: 27 additions & 0 deletions src/Logger/PsrLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace SimPod\ClickHouseClient\Logger;

use Psr\Log\LoggerInterface;

final class PsrLogger implements SqlLogger
{
private LoggerInterface $logger;

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

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

public function stopQuery() : void
{
}
}
13 changes: 13 additions & 0 deletions src/Logger/SqlLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace SimPod\ClickHouseClient\Logger;

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

public function stopQuery() : void;
}
49 changes: 49 additions & 0 deletions tests/Logger/LoggerChainTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace SimPod\ClickHouseClient\Tests\Logger;

use PHPUnit\Framework\TestCase;
use SimPod\ClickHouseClient\Logger\LoggerChain;
use SimPod\ClickHouseClient\Logger\SqlLogger;

final class LoggerChainTest extends TestCase
{
public function testLog() : void
{
$logger = new class implements SqlLogger {
public ?string $sql = null;

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

public bool $started = false;

public bool $stopped = false;

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

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

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

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

self::assertSame('sql', $logger->sql);
self::assertSame([], $logger->params);
self::assertTrue($logger->started);
self::assertTrue($logger->stopped);
}
}
2 changes: 1 addition & 1 deletion tests/Snippet/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ final class VersionTest extends TestCaseBase

public function testRun() : void
{
self::assertRegExp('~(\d+\.)+\d+~', Version::run($this->client));
self::assertMatchesRegularExpression('~(\d+\.)+\d+~', Version::run($this->client));
}
}
6 changes: 3 additions & 3 deletions tests/WithClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

use Http\Client\Curl\Client;
use Nyholm\Psr7\Factory\Psr17Factory;
use Psr\Log\NullLogger;
use Psr\Log\Test\TestLogger;
use SimPod\ClickHouseClient\Client\ClickHouseAsyncClient;
use SimPod\ClickHouseClient\Client\ClickHouseClient;
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 +64,7 @@ public function restartClickHouseClient() : void
new Psr17Factory(),
new Psr17Factory()
),
new NullLogger(),
new PsrLogger(new TestLogger()),
$endpoint,
$defaultParameters
);
Expand All @@ -78,7 +78,7 @@ public function restartClickHouseClient() : void
new Psr17Factory(),
new Psr17Factory()
),
new TestLogger(),
new PsrLogger(new TestLogger()),
$endpoint,
$defaultParameters
);
Expand Down