|
| 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 | +``` |
0 commit comments