Skip to content

Core\Metrics::setGauge

Core\Metrics::setGauge(
    string $name,
    float $value,
    {labels?: array<string>},
): void

Core\Metrics::setGauge sets a gauge: a number that goes up and down, such as how many jobs are waiting or how many connections are open.

Each call replaces the value. It does not add to it. When your monitoring system collects the metrics, it sees the last value that was set. The name is written in lower case letters, digits and _, and the first use decides that this name is a gauge. Calling increment or observe with the same name later throws a LogicError.

Labels split one gauge into several, for example one per database. A label value from a request is not allowed, because every different value makes a new series.

Good to know: a program cannot read the value back. The call returns nothing.

The examples below show a count of waiting jobs, then the error for a name that is already a counter, then one gauge per connection pool.

Parameters

  • $namestringThe series name, [a-z][a-z0-9_]*. A name written directly in the code that does not match this pattern does not compile. A tainted name does not compile either.
  • $valuefloatThe new value of the gauge. The monitoring system sees the last value set before it collects.

Options

  • labelsarray<string>optionalThe label values of this series, with the label name as the key. A tainted or secret value is not allowed. Use an enum case, a value converted with as, or a route name.

Return value

Returns void. Nothing. A program cannot read a metric back, the same as with increment.

Errors

  • LogicErrorThe name is already a different kind of metric, or it already has other label names. The message shows the first call and this one. A new series above the max_series limit is not an error. Novis does not record that series, but counts how many it did not record. The call returns normally.

Changelog

VersionChange
0.0.6Core\Metrics::setGauge sets the value of a gauge. The old name was gauge.

Examples

Jobs waiting

<?nvs
// Shows how to report how many jobs are still waiting while a list of jobs is worked through.
array<string> $jobs = ['resize photo', 'send invoice', 'update search index'];
float $waiting = 3.0;
// Before the loop, the gauge `jobs_waiting` is 3.
Core\Metrics::setGauge('jobs_waiting', $waiting);
foreach ($jobs as string $job) {
echo 'done: ', $job, "\n";
// Each call replaces the value. After the last job, the gauge is 0.
$waiting = $waiting - 1.0;
Core\Metrics::setGauge('jobs_waiting', $waiting);
}
// This prints 0.
echo 'jobs left: ', $waiting, "\n";
Output
done: resize photo
done: send invoice
done: update search index
jobs left: 0

A name that is already a counter

<?nvs
// Shows the error you get when a name that is already a counter is used as a gauge.
// The first use makes `logins_total` a counter.
Core\Metrics::increment('logins_total');
// A gauge with the same name throws a `LogicError`.
try {
Core\Metrics::setGauge('logins_total', 12.0);
} catch (LogicError $e) {
echo "logins_total is already a counter\n";
}
// A gauge needs its own name. This call works.
Core\Metrics::setGauge('users_online', 12.0);
echo "users_online is 12\n";
Output
logins_total is already a counter
users_online is 12

Open connections per pool

<?nvs
// Shows how to report the open connections of each database pool as one gauge with a label.
class Pools
{
// The label `pool` gives each pool its own value under the same gauge name.
public static function report(string $pool, uint $open): void
{
Core\Metrics::setGauge('db_connections_open', $open as float, { labels: ['pool' => $pool] });
echo $pool, ': ', $open, " open\n";
}
}
// The pool names are written in the program, so they are allowed as label values.
Pools::report('primary', 8);
Pools::report('replica', 3);
// Later the primary pool has fewer connections. The new value replaces 8.
Pools::report('primary', 5);
Output
primary: 8 open
replica: 3 open
primary: 5 open