diff options
author | Wolfy-J <[email protected]> | 2019-06-27 12:39:14 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2019-06-27 12:39:14 +0300 |
commit | e2ea9ee27aacd643918fe721735ba57798fb3da2 (patch) | |
tree | 02402acdd5d0521d37a7041e09d4e978ea6a268f /src/Metrics.php | |
parent | c46d80753854eba2b149ae06a2d7b798eb2aa9b8 (diff) |
php adapter for metric collection, more tests
Diffstat (limited to 'src/Metrics.php')
-rw-r--r-- | src/Metrics.php | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/src/Metrics.php b/src/Metrics.php new file mode 100644 index 00000000..c07a1e7a --- /dev/null +++ b/src/Metrics.php @@ -0,0 +1,103 @@ +<?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 +{ + /** @var RPC */ + private $rpc; + + /** + * @param RPC $rpc + */ + public function __construct(RPC $rpc) + { + $this->rpc = $rpc; + } + + /** + * Add collector value. Fallback to appropriate method of related collector. + * + * @param string $collector + * @param float $value + * @param array $labels + * + * @throws MetricException + */ + public function add(string $collector, float $value, array $labels = []) + { + try { + $this->rpc->call('metrics.Add', compact('collector', 'value', 'labels')); + } catch (RPCException $e) { + throw new MetricException($e->getMessage(), $e->getCode(), $e); + } + } + + /** + * Subtract the collector value, only for gauge collector. + * + * @param string $collector + * @param float $value + * @param array $labels + * + * @throws MetricException + */ + public function sub(string $collector, float $value, array $labels = []) + { + try { + $this->rpc->call('metrics.Sub', compact('collector', 'value', 'labels')); + } catch (RPCException $e) { + throw new MetricException($e->getMessage(), $e->getCode(), $e); + } + } + + /** + * Observe collector value, only for histogram and summary collectors. + * + * @param string $collector + * @param float $value + * @param array $labels + * + * @throws MetricException + */ + public function observe(string $collector, float $value, array $labels = []) + { + try { + $this->rpc->call('metrics.Observe', compact('collector', 'value', 'labels')); + } catch (RPCException $e) { + throw new MetricException($e->getMessage(), $e->getCode(), $e); + } + } + + /** + * Set collector value, only for gauge collector. + * + * @param string $collector + * @param float $value + * @param array $labels + * + * @throws MetricException + */ + public function set(string $collector, float $value, array $labels = []) + { + try { + $this->rpc->call('metrics.Set', compact('collector', 'value', 'labels')); + } catch (RPCException $e) { + throw new MetricException($e->getMessage(), $e->getCode(), $e); + } + } +}
\ No newline at end of file |