Skip to content

Commit 13de5c8

Browse files
committed
Add documentation for creating gauges in Prometheus exporter package
1 parent d4d1501 commit 13de5c8

File tree

5 files changed

+93
-17
lines changed

5 files changed

+93
-17
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
title: Creating gauges
3+
weight: 1
4+
---
5+
6+
To export your first metric to Prometheus, you should call `Prometheus::addGauge` method. This can be done anywhere in your code, but typically it's done in the `app/Providers/PrometheusServiceProvider.php` file that was published when installing the package.
7+
8+
```php
9+
Prometheus::addGauge('My gauge')
10+
->value(fn() => 123.45);
11+
```
12+
13+
This will create a gauge metric named `my_gauge` with the value of `123.45`. The metric will be present on the `/prometheus` endpoint.
14+
15+
You can add as many gauges as you want. Here's an example where we export the user count.
16+
17+
```php
18+
Prometheus::addGauge('User count')
19+
->value(fn() => User::count()
20+
```
21+
22+
## Adding a help text
23+
24+
You can add a help text to your metric by chaining the `helpText` method.
25+
26+
```php
27+
Prometheus::addGauge('User count')
28+
->helpText('This is the number of users in our app')
29+
->value(fn() => User::count();
30+
```
31+
32+
## Setting a namespace
33+
34+
When exporting the metrics, a namespace value will be prefixed to the metric name. By default, the namespace is set to `app`. So, when you export a gauge named `User count`, the metric name will be `app_user_count`.
35+
36+
You can change the default namespace in the `namespace` key of the `config/prometheus.php` file.
37+
38+
To change the namespace of a specific gauge, you can chain the `namespace` method.
39+
40+
```php
41+
Prometheus::addGauge('User count')
42+
->namespace('My custom namespace')
43+
->value(fn() => User::count();
44+
```
45+
46+
The above gauge will be exported as `my_custom_namespace_user_count`.
47+
48+
## Using labels
49+
50+
Labels are a powerful feature of Prometheus. They allow you to add additional dimensions to your metrics. For example, you can add a label to the `User count` gauge to distinguish between active and inactive users.
51+
52+
To start using a label, you should call the `label` method on the gauge and pass the label name.
53+
54+
The callable passed to `value` should return an array of tuples. Each tuple should contain the value and an array of labels. The number of labels should match the number of labels defined on the gauge.
55+
56+
```php
57+
Prometheus::addGauge('User count')
58+
->label('status')
59+
->value(function() {
60+
return [
61+
[User::where('status', 'active')->count(), ['active']],
62+
[User::where('status', 'inactive')->count(), ['inactive']],
63+
];
64+
});
65+
```
66+
67+
## Alternative syntax
68+
69+
Instead of using multiple methods, you can also use named arguments to set the gauge properties.
70+
71+
```php
72+
Prometheus::addGauge(
73+
name: 'User count',
74+
helpText: 'This is the number of users in our app',
75+
namespace: 'My custom namespace',
76+
labels: ['status'],
77+
value: function() {
78+
return [
79+
[User::where('status', 'active')->count(), ['active']],
80+
[User::where('status', 'inactive')->count(), ['inactive']],
81+
];
82+
}
83+
);
84+
```

docs/basic-usage/getting-started.md

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
title: Using cached values
3+
weight: 3
4+
---
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
---
22
title: Using Horizon exporters
3-
weight: 1
3+
weight: 2
44
---
55

6-
Coming soon...

resources/stubs/PrometheusServiceProvider.php.stub

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ class PrometheusServiceProvider extends ServiceProvider
2020
* Here you can register all the exporters that you
2121
* want to export to prometheus
2222
*/
23-
Prometheus::addGauge('My gauge', function () {
24-
return 123.45;
25-
});
23+
Prometheus::addGauge('My gauge')
24+
->value(function() {
25+
return 123.45;
26+
});
2627

2728
/*
2829
* Uncomment this line if you want to export

0 commit comments

Comments
 (0)