Skip to content

Commit

Permalink
feat: log ES requests to specific channel
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-ht authored and jorge07 committed May 27, 2020
1 parent b19c711 commit 6409c51
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
8 changes: 7 additions & 1 deletion config/packages/dev/monolog.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
monolog:
channels: ['elasticsearch']
handlers:
elasticsearch:
type: stream
path: "%kernel.logs_dir%/elasticsearch_%kernel.environment%.log"
level: debug
channels: ["elasticsearch"]
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
channels: ["!event"]
channels: ["!event", "!elasticsearch"]
# uncomment to get logging in your browser
# you may have to allow bigger header sizes in your Web server configuration
#firephp:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@

final class EventElasticRepository extends ElasticRepository implements EventRepositoryInterface
{
private const INDEX = 'events';

public function __construct(array $elasticConfig)
protected function index(): string
{
parent::__construct($elasticConfig, self::INDEX);
return 'events';
}

public function store(DomainMessage $message): void
Expand Down
35 changes: 21 additions & 14 deletions src/Infrastructure/Share/Query/Repository/ElasticRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,47 @@
use Assert\Assertion;
use Elasticsearch\Client;
use Elasticsearch\ClientBuilder;
use Psr\Log\LoggerInterface;

abstract class ElasticRepository
{
private string $index;

private Client $client;

public function __construct(array $config, string $index)
public function __construct(array $elasticConfig, LoggerInterface $elasticsearchLogger = null)
{
$this->client = ClientBuilder::fromConfig($config, true);
$this->index = $index;
$defaultConfig = [];

if ($elasticsearchLogger) {
$defaultConfig['logger'] = $elasticsearchLogger;
$defaultConfig['tracer'] = $elasticsearchLogger;
}

$this->client = ClientBuilder::fromConfig(\array_replace($defaultConfig, $elasticConfig), true);
}

abstract protected function index(): string;

public function search(array $query): array
{
$finalQuery = [];

$finalQuery['index'] = $this->index;
$finalQuery['index'] = $this->index();
$finalQuery['body'] = $query;

return $this->client->search($finalQuery);
}

public function refresh(): void
{
if ($this->client->indices()->exists(['index' => $this->index])) {
$this->client->indices()->refresh(['index' => $this->index]);
if ($this->client->indices()->exists(['index' => $this->index()])) {
$this->client->indices()->refresh(['index' => $this->index()]);
}
}

public function delete(): void
{
if ($this->client->indices()->exists(['index' => $this->index])) {
$this->client->indices()->delete(['index' => $this->index]);
if ($this->client->indices()->exists(['index' => $this->index()])) {
$this->client->indices()->delete(['index' => $this->index()]);
}
}

Expand All @@ -52,16 +59,16 @@ public function reboot(): void

public function boot(): void
{
if (!$this->client->indices()->exists(['index' => $this->index])) {
$this->client->indices()->create(['index' => $this->index]);
if (!$this->client->indices()->exists(['index' => $this->index()])) {
$this->client->indices()->create(['index' => $this->index()]);
}
}

protected function add(array $document): array
{
$query = [];

$query['index'] = $this->index;
$query['index'] = $this->index();
$query['id'] = $document['id'] ?? null;
$query['body'] = $document;

Expand All @@ -74,7 +81,7 @@ public function page(int $page = 1, int $limit = 50): array

$query = [];

$query['index'] = $this->index;
$query['index'] = $this->index();
$query['from'] = ($page - 1) * $limit;
$query['size'] = $limit;

Expand Down

0 comments on commit 6409c51

Please sign in to comment.