Moved the statistics logger to the replication driver

This commit is contained in:
Alex Renoki 2020-09-03 06:59:01 +03:00
parent fd46b0cb0b
commit 9938cf6ae2
4 changed files with 9 additions and 55 deletions

View File

@ -189,6 +189,8 @@ return [
'client' => \BeyondCode\LaravelWebSockets\PubSub\Drivers\LocalClient::class, 'client' => \BeyondCode\LaravelWebSockets\PubSub\Drivers\LocalClient::class,
'statistics_logger' => \BeyondCode\LaravelWebSockets\Statistics\Logger\MemoryStatisticsLogger::class,
], ],
/* /*
@ -210,6 +212,8 @@ return [
'client' => \BeyondCode\LaravelWebSockets\PubSub\Drivers\RedisClient::class, 'client' => \BeyondCode\LaravelWebSockets\PubSub\Drivers\RedisClient::class,
'statistics_logger' => \BeyondCode\LaravelWebSockets\Statistics\Logger\RedisStatisticsLogger::class,
], ],
], ],
@ -238,24 +242,6 @@ return [
], ],
/*
|--------------------------------------------------------------------------
| Statistics Logger Handler
|--------------------------------------------------------------------------
|
| The Statistics Logger will, by default, handle the incoming statistics,
| store them into an array and then store them into the database
| on each interval.
|
| You can opt-in to avoid any statistics storage by setting the logger
| to the built-in NullLogger.
|
*/
'logger' => \BeyondCode\LaravelWebSockets\Statistics\Logger\MemoryStatisticsLogger::class,
// 'logger' => \BeyondCode\LaravelWebSockets\Statistics\Logger\NullStatisticsLogger::class,
// 'logger' => \BeyondCode\LaravelWebSockets\Statistics\Logger\RedisStatisticsLogger::class,
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Statistics Interval Period | Statistics Interval Period

View File

@ -71,25 +71,11 @@ protected function schedule(Schedule $schedule)
Each app contains an `enable_statistics` that defines wether that app generates statistics or not. The statistics are being stored for the `interval_in_seconds` seconds and then they are inserted in the database. Each app contains an `enable_statistics` that defines wether that app generates statistics or not. The statistics are being stored for the `interval_in_seconds` seconds and then they are inserted in the database.
However, to disable it entirely and void any incoming statistic, you can uncomment the following line in the config: However, to disable it entirely and void any incoming statistic, you can change the statistics logger to `NullStatisticsLogger` under your current replication driver.
```php ```php
/*
|--------------------------------------------------------------------------
| Statistics Logger Handler
|--------------------------------------------------------------------------
|
| The Statistics Logger will, by default, handle the incoming statistics,
| store them into an array and then store them into the database
| on each interval.
|
| You can opt-in to avoid any statistics storage by setting the logger
| to the built-in NullLogger.
|
*/
// 'logger' => \BeyondCode\LaravelWebSockets\Statistics\Logger\MemoryStatisticsLogger::class, // 'logger' => \BeyondCode\LaravelWebSockets\Statistics\Logger\MemoryStatisticsLogger::class,
'logger' => \BeyondCode\LaravelWebSockets\Statistics\Logger\NullStatisticsLogger::class, // use the `NullStatisticsLogger` instead 'statistics_logger' => \BeyondCode\LaravelWebSockets\Statistics\Logger\NullStatisticsLogger::class, // use the `NullStatisticsLogger` instead
``` ```
## Custom Statistics Drivers ## Custom Statistics Drivers

View File

@ -32,23 +32,3 @@ Now, when your app broadcasts the message, it will make sure the connection reac
The available drivers for replication are: The available drivers for replication are:
- [Redis](redis) - [Redis](redis)
## Configure the Statistics driver
If you work with multi-node environments, beside replication, you shall take a look at the statistics logger. Each time your user connects, disconnects or send a message, you can track the statistics. However, these are centralized in one place before they are dumped in the database.
Unfortunately, you might end up with multiple rows when multiple servers run in parallel.
To fix this, just change the `statistics.logger` class with a logger that is able to centralize the statistics in one place. For example, you might want to store them into a Redis instance:
```php
'statistics' => [
'logger' => \BeyondCode\LaravelWebSockets\Statistics\Logger\RedisStatisticsLogger::class,
...
],
```
Check the `websockets.php` config file for more details.

View File

@ -97,7 +97,9 @@ class StartWebSocketServer extends Command
protected function configureStatisticsLogger() protected function configureStatisticsLogger()
{ {
$this->laravel->singleton(StatisticsLoggerInterface::class, function () { $this->laravel->singleton(StatisticsLoggerInterface::class, function () {
$class = config('websockets.statistics.logger', \BeyondCode\LaravelWebSockets\Statistics\Logger\MemoryStatisticsLogger::class); $replicationDriver = config('websockets.replication.driver', 'local');
$class = config("websockets.replication.{$replicationDriver}.statistics_logger", \BeyondCode\LaravelWebSockets\Statistics\Logger\MemoryStatisticsLogger::class);
return new $class( return new $class(
$this->laravel->make(ChannelManager::class), $this->laravel->make(ChannelManager::class),