summaryrefslogtreecommitdiff
path: root/src/Metrics.php
diff options
context:
space:
mode:
authorAnton Titov <[email protected]>2019-06-27 13:00:34 +0300
committerGitHub <[email protected]>2019-06-27 13:00:34 +0300
commitce7f331a5e6c6129331e06224865abdb1298584d (patch)
tree4fdf5b9158cf481ee77c408ea8d57d83a7e692f3 /src/Metrics.php
parent5b6e0a535fef745594a4966c724509dcda05b422 (diff)
parent1633d128309765536e7cbb176225926efda7a33c (diff)
Merge pull request #168 from spiral/feature/metrics
Feature/metrics
Diffstat (limited to 'src/Metrics.php')
-rw-r--r--src/Metrics.php103
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