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 | |
parent | c46d80753854eba2b149ae06a2d7b798eb2aa9b8 (diff) |
php adapter for metric collection, more tests
Diffstat (limited to 'src')
-rw-r--r-- | src/Exception/MetricException.php | 17 | ||||
-rw-r--r-- | src/HttpClient.php | 2 | ||||
-rw-r--r-- | src/Metrics.php | 103 | ||||
-rw-r--r-- | src/Worker.php | 2 |
4 files changed, 122 insertions, 2 deletions
diff --git a/src/Exception/MetricException.php b/src/Exception/MetricException.php new file mode 100644 index 00000000..2d12eefe --- /dev/null +++ b/src/Exception/MetricException.php @@ -0,0 +1,17 @@ +<?php +/** + * Spiral Framework. + * + * @license MIT + * @author Anton Titov (Wolfy-J) + */ +declare(strict_types=1); + +namespace Spiral\RoadRunner\Exception; + +use Spiral\Goridge\Exceptions\RPCException; + +class MetricException extends RPCException +{ + +}
\ No newline at end of file diff --git a/src/HttpClient.php b/src/HttpClient.php index f31a9b50..d2862ad8 100644 --- a/src/HttpClient.php +++ b/src/HttpClient.php @@ -8,7 +8,7 @@ declare(strict_types=1); namespace Spiral\RoadRunner; -class HttpClient +final class HttpClient { /** @var Worker */ private $worker; 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 diff --git a/src/Worker.php b/src/Worker.php index b67ebd3b..00cf072a 100644 --- a/src/Worker.php +++ b/src/Worker.php @@ -22,7 +22,7 @@ use Spiral\RoadRunner\Exception\RoadRunnerException; * $worker->send("DONE", json_encode($context)); * } */ -class Worker +final class Worker { // Send as response context to request worker termination const STOP = '{"stop":true}'; |