This repository has been archived by the owner on Jul 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
/
ExampleCollector.php
60 lines (53 loc) · 1.57 KB
/
ExampleCollector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
namespace Superbalist\LaravelPrometheusExporter;
use Prometheus\Gauge;
class ExampleCollector implements CollectorInterface
{
/**
* @var Gauge
*/
protected $usersRegisteredGauge;
/**
* Return the name of the collector.
*
* @return string
*/
public function getName()
{
return 'users';
}
/**
* Register all metrics associated with the collector.
*
* The metrics needs to be registered on the exporter object.
* eg:
* ```php
* $exporter->registerCounter('search_requests_total', 'The total number of search requests.');
* ```
*
* @param PrometheusExporter $exporter
*/
public function registerMetrics(PrometheusExporter $exporter)
{
$this->usersRegisteredGauge = $exporter->registerGauge(
'users_registered_total',
'The total number of registered users.',
['group']
);
}
/**
* Collect metrics data, if need be, before exporting.
*
* As an example, this may be used to perform time consuming database queries and set the value of a counter
* or gauge.
*/
public function collect()
{
// retrieve the total number of staff users registered
// eg: $totalUsers = Users::where('group', 'staff')->count();
$this->usersRegisteredGauge->set(36, ['staff']);
// retrieve the total number of regular users registered
// eg: $totalUsers = Users::where('group', 'regular')->count();
$this->usersRegisteredGauge->set(192, ['regular']);
}
}