Skip to content

Commit

Permalink
Refactor how we store metrics in cache
Browse files Browse the repository at this point in the history
  • Loading branch information
turbo124 committed Mar 2, 2023
1 parent 72b2b45 commit 1887780
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 30 deletions.
5 changes: 5 additions & 0 deletions config/beacon.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
*/
'cache_key' => 'beacon',

/**
* Configure you cache connection here
*/
'cache_connection' => '',

/**
* Determines whether to log the
* host system variables using
Expand Down
18 changes: 9 additions & 9 deletions src/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ public function batch()
if(!config('beacon.enabled') || empty(config('beacon.api_key')))
return;

$data = Cache::get(config('beacon.cache_key') . '_' . $this->metric->type);
// $data = Cache::get(config('beacon.cache_key') . '_' . $this->metric->type);

if(is_array($data)){
$data[] = $this->metric;
}
else {
$data = [];
$data[] = $this->metric;
}
// if(is_array($data)){
// $data[] = $this->metric;
// }
// else {
// $data = [];
// $data[] = $this->metric;
// }

Cache::put(config('beacon.cache_key') . '_' . $this->metric->type, $data);
Cache::put(config('beacon.cache_key') . $this->metric->type.microtime(true), $this->metric);

}
}
16 changes: 11 additions & 5 deletions src/Commands/ForceSend.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

namespace Turbo124\Beacon\Commands;

use App;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Turbo124\Beacon\Jobs\BatchMetrics;

use Illuminate\Support\Facades;

class ForceSend extends Command
{
Expand All @@ -30,7 +28,11 @@ public function handle()

foreach($metric_types as $type)
{
$metrics = Cache::get(config('beacon.cache_key') . '_' . $type);
$redis = Facades\Redis::connection(config('beacon.cache_connection',''));

$prefix = config('cache.prefix').':'.config('beacon.cache_key').$type.'*';

$metrics = $redis->keys($prefix);

if(is_array($metrics))
$this->logMessage("I have " . count($metrics) . "pending to be sent");
Expand All @@ -43,7 +45,11 @@ public function handle()

foreach($metric_types as $type)
{
$metrics = Cache::get(config('beacon.cache_key') . '_' . $type);
$redis = Facades\Redis::connection(config('beacon.cache_connection',''));

$prefix = config('cache.prefix').':'.config('beacon.cache_key').$type.'*';

$metrics = $redis->keys($prefix);

if(is_array($metrics))
$this->logMessage("I have " . count($metrics) . "pending to be sent");
Expand Down
48 changes: 32 additions & 16 deletions src/Jobs/BatchMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

namespace Turbo124\Beacon\Jobs;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Turbo124\Beacon\Generator;
use Turbo124\Beacon\Jobs\SystemMetric;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades;

class BatchMetrics implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;

/**
* Create a new job instance.
Expand All @@ -33,27 +35,41 @@ public function __construct()
*/
public function handle()
{

if(!config('beacon.enabled') || empty(config('beacon.api_key')))
if (!config('beacon.enabled') || empty(config('beacon.api_key'))) {
return;

}

SystemMetric::dispatch();

$metric_types = ['counter', 'gauge', 'multi_metric', 'mixed_metric'];

foreach($metric_types as $type)
{
$metrics = Cache::get(config('beacon.cache_key') . '_' . $type);

if(!is_array($metrics))
foreach ($metric_types as $type) {

$redis = Facades\Redis::connection(config('beacon.cache_connection',''));

$prefix = config('cache.prefix').':'.config('beacon.cache_key').$type.'*';

$keys = $redis->keys($prefix);

$metrics = false;

if (count($keys) > 0) {
$metrics = $redis->mget($keys);

$redis->pipeline(function ($pipe) use ($keys) {
foreach ($keys as $key) {
$pipe->del($key);
}
});
}

if (!is_array($metrics)) {
continue;
}

Cache::put(config('beacon.cache_key') . '_' . $type, []);

$generator = new Generator();
$generator->batchFire($metrics);

$generator->batchFire($metrics);
}

}
}

0 comments on commit 1887780

Please sign in to comment.