Modified Prometheus extension to support adding a prefix to metric names (#13272)

* Modified Prometheus extension to allow for a configurable prefix on metric names.

* Added reference in config_definitions.json and updated docs.

* Fixed whitespace in config_definitions.json.

* Added reference to prometheus.prefix in settings.php

* Defined prefix var as private in Prometheus.php
This commit is contained in:
Cathal Mooney
2021-09-29 01:30:28 +01:00
committed by GitHub
parent 66dddbaa66
commit 6bfe1555be
4 changed files with 39 additions and 1 deletions

View File

@@ -38,6 +38,7 @@ class Prometheus extends BaseDatastore
private $base_uri;
private $default_opts;
private $enabled;
private $prefix;
public function __construct(\GuzzleHttp\Client $client)
{
@@ -47,6 +48,10 @@ class Prometheus extends BaseDatastore
$url = Config::get('prometheus.url');
$job = Config::get('prometheus.job', 'librenms');
$this->base_uri = "$url/metrics/job/$job/instance/";
$this->prefix = Config::get('prometheus.prefix', '');
if ($this->prefix) {
$this->prefix = "$this->prefix" . '_';
}
$this->default_opts = [
'headers' => ['Content-Type' => 'text/plain'],
@@ -82,7 +87,7 @@ class Prometheus extends BaseDatastore
foreach ($fields as $k => $v) {
if ($v !== null) {
$vals .= "$k $v\n";
$vals .= $this->prefix . "$k $v\n";
}
}

View File

@@ -34,8 +34,30 @@ continue to function as normal.
$config['prometheus']['enable'] = true;
$config['prometheus']['url'] = 'http://127.0.0.1:9091';
$config['prometheus']['job'] = 'librenms'; # Optional
$config['prometheus']['prefix'] = 'librenms'; # Optional
```
## Prefix
Setting the 'prefix' option will cause all metric names to begin with
the configured value.
For instance without setting this option metric names will be something
like this:
OUTUCASTPKTS
ifOutUcastPkts_rate
INOCTETS
ifInErrors_rate
Configuring a prefix name, for example 'librenms', instead caused those
metrics to be exposed with the following names:
librenms_OUTUCASTPKTS
librenms_ifOutUcastPkts_rate
librenms_INOCTETS
librenms_ifInErrors_rate
## Sample Prometheus Scrape Config (for scraping the Push Gateway)
```yml

View File

@@ -4771,6 +4771,13 @@
"section": "prometheus",
"order": 3
},
"prometheus.prefix": {
"default": "",
"type": "text",
"group": "poller",
"section": "prometheus",
"order": 4
},
"public_status": {
"default": false,
"group": "auth",

View File

@@ -1176,6 +1176,10 @@ return [
'description' => 'Attach Device sysName',
'help' => 'Attach sysName information put to Prometheus.',
],
'prefix' => [
'description' => 'Prefix',
'help' => 'Optional text to prepend to exported metric names',
],
],
'public_status' => [
'description' => 'Show status publicly',