Skip to content

Commit

Permalink
Batch Metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
turbo124 committed Apr 23, 2020
1 parent 084b2cb commit 230e780
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 1 deletion.
12 changes: 11 additions & 1 deletion config/collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@

return [

/**
* The API endpoint for logs
*/
'endpoint' => 'http://collector.test:8000',

'api_key' => '123456',
/**
* Your API key
*/
'api_key' => '123456',

/**
* Should batch requests
*/
'batch' => true,
];
6 changes: 6 additions & 0 deletions src/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Turbo124\Collector;

use Illuminate\Support\Facades\Cache;
use Turbo124\Collector\Collector\Generator;
use Turbo124\Collector\Jobs\CreateMetric;

Expand Down Expand Up @@ -54,4 +55,9 @@ public function queue()
{
CreateMetric::dispatch($this->metric);
}

public function batch()
{
Cache::tags(['collector'])->put(config('collector.api_key'), $this->metric);
}
}
13 changes: 13 additions & 0 deletions src/Collector/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ private function endPoint()
return config('collector.endpoint') . '/collect';
}

private function batchEndPoint()
{
return config('collector.endpoint') . '/collect/batch';
}

private function apiKey()
{
return config('collector.api_key');
Expand All @@ -28,6 +33,14 @@ public function fire($metric)
return $this->handleResponse($response);
}

public function batchFire($metric_array)
{
$client = $this->httpClient();
$response = $client->request('POST',$this->batchEndPoint(), ['form_params' => $metric_array]);
return $this->handleResponse($response);
}


private function handleResponse($response)
{
switch ($response->getStatusCode()) {
Expand Down
9 changes: 9 additions & 0 deletions src/CollectorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Turbo124\Collector;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\ServiceProvider;
use Turbo124\Collector\Collector;
use Turbo124\Collector\Jobs\BatchMetrics;

class CollectorServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -38,5 +40,12 @@ public function register()
$this->app->singleton('collector', function () {
return new Collector;
});

/* Register the scheduler */
$this->app->booted(function () {
$schedule = app(Schedule::class);
$schedule->job(new BatchMetrics())->everyFiveMinutes();
});

}
}
40 changes: 40 additions & 0 deletions src/Jobs/BatchMetrics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Turbo124\Collector\Jobs;

use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Turbo124\Collector\Collector\Generator;

class BatchMetrics
{
use Dispatchable;

/**
* Create a new job instance.
*
* @return void
*/

public function __construct()
{
}

/**
* Execute the job.
*
* @return void
*/
public function handle()
{

$metrics = Cache::tags(['collector'])->get(config('collector.api_key'));

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

Cache::tags(['collector'])->flush();

}
}

0 comments on commit 230e780

Please sign in to comment.