Skip to content

Core\Metrics::increment

Core\Metrics::increment(
    string $name,
    {by?: uint, labels?: array<string>},
): void

Core\Metrics::increment adds to a counter: a number that only goes up, such as how many orders were placed or how many logins failed.

Each call adds one. The option by adds a different amount, and it can never be negative. When your monitoring system collects the metrics, it sees the total. The name is written in lower case letters, digits and _, and by custom a counter's name ends in _total. After the first call, this name is a counter. Calling observe or setGauge with the same name later throws a LogicError.

Labels split one counter into several, for example one per payment status. 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 total back. The call returns nothing.

The examples below show counting one event, then adding more than one with by, then counting payments by their result.

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.

Options

  • byuintoptionalHow much to add. The default is 1. It cannot be negative, because a counter that goes down looks like a restart of the process.
  • 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. Only the monitoring system that collects the metrics can read them.

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.

Examples

Count each signup

<?nvs
// Shows how to count an event each time it happens.
array<string> $emails = ['ana@example.com', 'ben@example.com', 'chen@example.com'];
foreach ($emails as string $email) {
echo 'welcome, ', $email, "\n";
// Each call adds one to the counter `signups_total`. After the loop it is 3.
Core\Metrics::increment('signups_total');
}
Output
welcome, ana@example.com
welcome, ben@example.com
welcome, chen@example.com

Add more than one

<?nvs
// Shows how to add more than one to a counter with the option `by`.
array<uint> $batches = [20, 35, 5];
uint $sent = 0;
foreach ($batches as uint $size) {
// `by` adds the size of the batch. After the loop the counter is 60.
Core\Metrics::increment('emails_sent_total', { by: $size });
$sent = $sent + $size;
}
// This prints 60.
echo 'emails sent: ', $sent, "\n";
Output
emails sent: 60

Payments by result

<?nvs
// Shows how to count payments by their result, with one counter and a label.
class Payments
{
// The result is one of two values written in the program, so it is allowed as a label value.
public static function charge(uint $cents): string
{
if ($cents > 50000) {
return 'declined';
}
return 'paid';
}
}
array<uint> $orders = [1999, 72000, 450];
foreach ($orders as uint $cents) {
string $result = Payments::charge($cents);
// The label `result` is `paid` or `declined`, so each result has its own total.
Core\Metrics::increment('payments_total', { labels: ['result' => $result] });
echo $cents, ' cents: ', $result, "\n";
}
Output
1999 cents: paid
72000 cents: declined
450 cents: paid