Skip to content

Commit 299db53

Browse files
committed
monitoring
1 parent 8db5ad0 commit 299db53

File tree

3 files changed

+82
-9
lines changed

3 files changed

+82
-9
lines changed

InfluxDbStorage.php

+73-9
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,70 @@ class InfluxDbStorage implements StatsStorage
2121
/**
2222
* @var string
2323
*/
24-
private $measurMessages;
24+
private $measurementMessages;
2525

2626
/**
2727
* @var string
2828
*/
29-
private $measurConsumers;
29+
private $measurementConsumers;
3030

3131
/**
3232
* @var Database
3333
*/
3434
private $database;
3535

3636
/**
37-
* @param Client $client
38-
* @param string $dbName
37+
* The config could be an array, string DSN or null. In case of null it will attempt to connect to localhost.
38+
*
39+
* $config = [
40+
* 'dsn' => 'wamp://127.0.0.1:9090',
41+
* 'host' => '127.0.0.1',
42+
* 'port' => '9090',
43+
* 'topic' => 'stats',
44+
* 'max_retries' => 15,
45+
* 'initial_retry_delay' => 1.5,
46+
* 'max_retry_delay' => 300,
47+
* 'retry_delay_growth' => 1.5,
48+
* ]
49+
*
50+
* or
51+
*
52+
* wamp://127.0.0.1:9090?max_retries=10
53+
*
54+
* @param array|string|null $config
3955
*/
40-
public function __construct(Client $client, string $dbName)
56+
public function __construct($config = 'influxdb:')
4157
{
4258
$this->client = $client;
4359
$this->dbName = $dbName;
44-
$this->measurMessages = 'messages';
45-
$this->measurConsumers = 'consumers';
60+
$this->measurementMessages = 'messages';
61+
$this->measurementConsumers = 'consumers';
62+
63+
if (false == class_exists(Client::class)) {
64+
throw new \LogicException('Seems client library is not installed. Please install "influxdb/influxdb-php"');
65+
}
66+
67+
if (empty($config)) {
68+
$config = $this->parseDsn('influxdb:');
69+
} elseif (is_string($config)) {
70+
$config = $this->parseDsn($config);
71+
} elseif (is_array($config)) {
72+
$config = empty($config['dsn']) ? $config : $this->parseDsn($config['dsn']);
73+
} else {
74+
throw new \LogicException('The config must be either an array of options, a DSN string or null');
75+
}
76+
77+
$config = array_replace([
78+
'host' => '127.0.0.1',
79+
'port' => '9090',
80+
'topic' => 'stats',
81+
'max_retries' => 15,
82+
'initial_retry_delay' => 1.5,
83+
'max_retry_delay' => 300,
84+
'retry_delay_growth' => 1.5,
85+
], $config);
86+
87+
$this->config = $config;
4688
}
4789

4890
public function pushConsumerStats(ConsumerStats $event): void
@@ -72,7 +114,7 @@ public function pushConsumerStats(ConsumerStats $event): void
72114
$values['finishedAtMs'] = $event->getFinishedAtMs();
73115
}
74116

75-
$points[] = new Point($this->measurConsumers, null, $tags, $values, $event->getTimestampMs());
117+
$points[] = new Point($this->measurementConsumers, null, $tags, $values, $event->getTimestampMs());
76118
}
77119

78120
$this->getDb()->writePoints($points, Database::PRECISION_MILLISECONDS);
@@ -97,7 +139,7 @@ public function pushMessageStats(MessageStats $event): void
97139
$runtime = $event->getTimestampMs() - $event->getReceivedAtMs();
98140

99141
$points = [
100-
new Point($this->measurMessages, $runtime, $tags, $values, $event->getTimestampMs()),
142+
new Point($this->measurementMessages, $runtime, $tags, $values, $event->getTimestampMs()),
101143
];
102144

103145
$this->getDb()->writePoints($points, Database::PRECISION_MILLISECONDS);
@@ -112,4 +154,26 @@ private function getDb(): Database
112154

113155
return $this->database;
114156
}
157+
158+
private function parseDsn(string $dsn): array
159+
{
160+
$dsn = new Dsn($dsn);
161+
162+
if (false === in_array($dsn->getSchemeProtocol(), ['wamp', 'ws'], true)) {
163+
throw new \LogicException(sprintf(
164+
'The given scheme protocol "%s" is not supported. It must be "wamp"',
165+
$dsn->getSchemeProtocol()
166+
));
167+
}
168+
169+
return array_filter(array_replace($dsn->getQuery(), [
170+
'host' => $dsn->getHost(),
171+
'port' => $dsn->getPort(),
172+
'topic' => $dsn->getQueryParameter('topic'),
173+
'max_retries' => $dsn->getInt('max_retries'),
174+
'initial_retry_delay' => $dsn->getFloat('initial_retry_delay'),
175+
'max_retry_delay' => $dsn->getInt('max_retry_delay'),
176+
'retry_delay_growth' => $dsn->getFloat('retry_delay_growth'),
177+
]), function ($value) { return null !== $value; });
178+
}
115179
}

WampStorage.php

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ class WampStorage implements StatsStorage
5858
*/
5959
public function __construct($config = 'wamp:')
6060
{
61+
if (false == class_exists(Client::class) || false == class_exists(PawlTransportProvider::class)) {
62+
throw new \LogicException('Seems client libraries are not installed. Please install "thruway/client" and "thruway/pawl-transport"');
63+
}
64+
6165
if (empty($config)) {
6266
$config = $this->parseDsn('wamp:');
6367
} elseif (is_string($config)) {

composer.json

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
"phpunit/phpunit": "~5.4.0",
1616
"enqueue/test": "0.9.x-dev"
1717
},
18+
"suggest": {
19+
"thruway/client": "Client for Thruway and the WAMP (Web Application Messaging Protocol).",
20+
"thruway/pawl-transport": "Pawl WebSocket Transport for Thruway Client",
21+
"influxdb/influxdb-php": "A PHP Client for InfluxDB, a time series database"
22+
},
1823
"support": {
1924
"email": "[email protected]",
2025
"issues": "https://github.com/php-enqueue/enqueue-dev/issues",

0 commit comments

Comments
 (0)