-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add async metrics implementation (#13)
- Loading branch information
Showing
9 changed files
with
350 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Spiral\RoadRunner\Metrics; | ||
|
||
use Spiral\Goridge\RPC\RPCInterface; | ||
|
||
abstract class AbstractMetrics implements MetricsInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected const SERVICE_NAME = 'metrics'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace Spiral\RoadRunner\Metrics; | ||
|
||
use Spiral\Goridge\RPC\AsyncRPCInterface; | ||
use Spiral\Goridge\RPC\Exception\ServiceException; | ||
use Spiral\RoadRunner\Metrics\Exception\MetricsException; | ||
|
||
class MetricsIgnoreResponse extends AbstractMetrics | ||
{ | ||
public function __construct( | ||
protected readonly AsyncRPCInterface $rpc | ||
) { | ||
} | ||
|
||
public function add(string $name, float $value, array $labels = []): void | ||
{ | ||
try { | ||
$this->rpc->callIgnoreResponse('metrics.Add', compact('name', 'value', 'labels')); | ||
} catch (ServiceException $e) { | ||
throw new MetricsException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
|
||
public function sub(string $name, float $value, array $labels = []): void | ||
{ | ||
try { | ||
$this->rpc->callIgnoreResponse('metrics.Sub', compact('name', 'value', 'labels')); | ||
} catch (ServiceException $e) { | ||
throw new MetricsException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
|
||
public function observe(string $name, float $value, array $labels = []): void | ||
{ | ||
try { | ||
$this->rpc->callIgnoreResponse('metrics.Observe', compact('name', 'value', 'labels')); | ||
} catch (ServiceException $e) { | ||
throw new MetricsException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
|
||
public function set(string $name, float $value, array $labels = []): void | ||
{ | ||
try { | ||
$this->rpc->callIgnoreResponse('metrics.Set', compact('name', 'value', 'labels')); | ||
} catch (ServiceException $e) { | ||
throw new MetricsException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
|
||
public function declare(string $name, CollectorInterface $collector): void | ||
{ | ||
try { | ||
$this->rpc->call('metrics.Declare', [ | ||
'name' => $name, | ||
'collector' => $collector->toArray(), | ||
]); | ||
} catch (ServiceException $e) { | ||
if (str_contains($e->getMessage(), 'tried to register existing collector')) { | ||
// suppress duplicate metric error | ||
return; | ||
} | ||
|
||
throw new MetricsException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
|
||
public function unregister(string $name): void | ||
{ | ||
try { | ||
$this->rpc->call('metrics.Unregister', $name); | ||
} catch (ServiceException $e) { | ||
throw new MetricsException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.