Skip to content

Commit

Permalink
Add additional metric types
Browse files Browse the repository at this point in the history
  • Loading branch information
turbo124 committed Apr 27, 2020
1 parent 18847bd commit cd2c72d
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 46 deletions.
11 changes: 8 additions & 3 deletions config/collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@

return [

/**
* Enable or disable the collector
*/
'enabled' => true,

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

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

/**
* Should batch requests
*/
'batch' => true,
'batch' => true,

/**
* The default key used to store
Expand Down
13 changes: 11 additions & 2 deletions src/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,28 @@ public function setCount($count)

public function send()
{
if(!config('collector.enabled'))
return;

$generator = new Generator();
$generator->fire($this->metric);
}

public function queue()
{
if(!config('collector.enabled'))
return;

CreateMetric::dispatch($this->metric);
}

public function batch()
{

$data = Cache::get(config('collector.cache_key'));
if(!config('collector.enabled'))
return;

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

if(is_array($data)){
$data[] = $this->metric;
Expand All @@ -69,6 +78,6 @@ public function batch()
$data[] = $this->metric;
}

Cache::put(config('collector.cache_key'), $data);
Cache::put(config('collector.cache_key') . '_' . $this->metric->type, $data);
}
}
19 changes: 13 additions & 6 deletions src/Collector/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

class Generator
{
private function endPoint()
private function endPoint($uri)
{
return config('collector.endpoint') . '/collect';
return config('collector.endpoint')."/{$uri}/add";
}

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

private function apiKey()
Expand All @@ -28,15 +28,22 @@ private function httpClient()

public function fire($metric)
{
$data['metrics'] = $metric;

$client = $this->httpClient();
$response = $client->request('POST',$this->endPoint(), ['form_params' => $metric]);
$response = $client->request('POST',$this->endPoint($metric->type), ['form_params' => $data]);
return $this->handleResponse($response);
}

public function batchFire($metric_array)
{
if(!is_array($metric_array))
return;

$data['metrics'] = $metric_array;

$client = $this->httpClient();
$response = $client->request('POST',$this->batchEndPoint(), ['form_params' => $metric_array]);
$response = $client->request('POST',$this->batchEndPoint($metric_array[0]->type), ['form_params' => $data]);
return $this->handleResponse($response);
}

Expand Down
41 changes: 7 additions & 34 deletions src/ExampleMetric/GenericCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,22 @@
class GenericCounter
{

/**
* The name of the counter
* @var string
*/
public $name = '';

/**
* The description of what this counter means
*
* @var string
*/
public $description = '';

/**
* The X-Axis label
*
* @var string
*/
public $x_axis = '';

/**
* The Y-Axis label
*
* @var string
*/
public $y_axis = '';

/**
* The type of Sample
*
* Monotonically incrementing counter
*
* - counter
* - gauge
* - histogram
*
* @var string
*/
public $type = '';
public $type = 'counter';

/**
* The api_key for authorization
*
* The name of the counter
* @var string
*/
public $api_key = '';
public $name = '';

/**
* The datetime of the counter measurement
Expand All @@ -64,6 +37,6 @@ class GenericCounter
*
* @var integer
*/
public $counter = 0;
public $metric = 0;

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

namespace Turbo124\Collector\ExampleMetric;

class GenericGauge
{

/**
* The type of Sample
*
* A gauge can be used to measure a given value
* per time interval
*
* - gauge
*
* @var string
*/
public $type = 'gauge';

/**
* The name of the counter
* @var string
*/
public $name = '';

/**
* The datetime of the counter measurement
*
* date("Y-m-d H:i:s")
*
* @var DateTime
*/
public $datetime;

/**
* The increment amount... should always be
* set to 0
*
* @var double
*/
public $metric = 0;

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

namespace Turbo124\Collector\ExampleMetric;

class GenericMultiMetric
{

/**
* The type of Sample
*
* A multric metric allows for multiple
* datapoints per time interval
*
* - multi_metric
*
* @var string
*/
public $type = 'multi_metric';

/**
* The name of the counter
* @var string
*/
public $name = '';

/**
* The datetime of the counter measurement
*
* date("Y-m-d H:i:s")
*
* @var DateTime
*/
public $datetime;

/**
* The metric value
* set to 0
*
* @var double
*/
public $metric1 = 0;

/**
* The metric value
* set to 0
*
* @var double
*/
public $metric2 = 0;

/**
* The metric value
* set to 0
*
* @var double
*/
public $metric3 = 0;

/**
* The metric value
* set to 0
*
* @var double
*/
public $metric4 = 0;

/**
* The metric value
* set to 0
*
* @var double
*/
public $metric5 = 0;


}
2 changes: 1 addition & 1 deletion src/Jobs/BatchMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function handle()
{

$metrics = Cache::get(config('collector.cache_key'));

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

Expand Down

0 comments on commit cd2c72d

Please sign in to comment.