diff options
author | Valery Piashchynski <[email protected]> | 2020-11-18 10:02:58 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2020-11-18 10:02:58 +0300 |
commit | a2ba8de5eb519f73044a9b1c66f087a5b65e3d45 (patch) | |
tree | dbcb1649a5ea8f7b50706ffc1cae775f1ef65b42 /src/Metrics | |
parent | 3cbdd3d3e44b3b4e72565d666391e3b732950774 (diff) |
Add Reset
Diffstat (limited to 'src/Metrics')
-rw-r--r-- | src/Metrics/Metrics.php | 80 | ||||
-rw-r--r-- | src/Metrics/MetricsInterface.php | 64 |
2 files changed, 144 insertions, 0 deletions
diff --git a/src/Metrics/Metrics.php b/src/Metrics/Metrics.php new file mode 100644 index 00000000..d6b6e1da --- /dev/null +++ b/src/Metrics/Metrics.php @@ -0,0 +1,80 @@ +<?php + +/** + * Spiral Framework. + * + * @license MIT + * @author Anton Titov (Wolfy-J) + */ +declare(strict_types=1); + +namespace Spiral\RoadRunner; + +use Spiral\Goridge\Exceptions\RPCException; +use Spiral\Goridge\RPC; +use Spiral\RoadRunner\Exception\MetricException; + +/** + * Application metrics. + */ +final class Metrics implements MetricsInterface +{ + /** @var RPC */ + private $rpc; + + /** + * @param RPC $rpc + */ + public function __construct(RPC $rpc) + { + $this->rpc = $rpc; + } + + /** + * @inheritDoc + */ + public function add(string $name, float $value, array $labels = []): void + { + try { + $this->rpc->call('metrics.Add', compact('name', 'value', 'labels')); + } catch (RPCException $e) { + throw new MetricException($e->getMessage(), $e->getCode(), $e); + } + } + + /** + * @inheritDoc + */ + public function sub(string $name, float $value, array $labels = []): void + { + try { + $this->rpc->call('metrics.Sub', compact('name', 'value', 'labels')); + } catch (RPCException $e) { + throw new MetricException($e->getMessage(), $e->getCode(), $e); + } + } + + /** + * @inheritDoc + */ + public function observe(string $name, float $value, array $labels = []): void + { + try { + $this->rpc->call('metrics.Observe', compact('name', 'value', 'labels')); + } catch (RPCException $e) { + throw new MetricException($e->getMessage(), $e->getCode(), $e); + } + } + + /** + * @inheritDoc + */ + public function set(string $name, float $value, array $labels = []): void + { + try { + $this->rpc->call('metrics.Set', compact('name', 'value', 'labels')); + } catch (RPCException $e) { + throw new MetricException($e->getMessage(), $e->getCode(), $e); + } + } +} diff --git a/src/Metrics/MetricsInterface.php b/src/Metrics/MetricsInterface.php new file mode 100644 index 00000000..ec2009b0 --- /dev/null +++ b/src/Metrics/MetricsInterface.php @@ -0,0 +1,64 @@ +<?php + +/** + * Spiral Framework. + * + * @license MIT + * @author Anton Titov (Wolfy-J) + */ +declare(strict_types=1); + +namespace Spiral\RoadRunner; + +use Spiral\RoadRunner\Exception\MetricException; + +interface MetricsInterface +{ + /** + * Add collector value. Fallback to appropriate method of related collector. + * + * @param string $collector + * @param float $value + * @param mixed[] $labels + * + * @throws MetricException + * @return void + */ + public function add(string $collector, float $value, array $labels = []); + + /** + * Subtract the collector value, only for gauge collector. + * + * @param string $collector + * @param float $value + * @param mixed[] $labels + * + * @throws MetricException + * @return void + */ + public function sub(string $collector, float $value, array $labels = []); + + /** + * Observe collector value, only for histogram and summary collectors. + * + * @param string $collector + * @param float $value + * @param mixed[] $labels + * + * @throws MetricException + * @return void + */ + public function observe(string $collector, float $value, array $labels = []); + + /** + * Set collector value, only for gauge collector. + * + * @param string $collector + * @param float $value + * @param mixed[] $labels + * + * @throws MetricException + * @return void + */ + public function set(string $collector, float $value, array $labels = []); +} |