Core\Metrics::observe
Core\Metrics::observe records one measurement, such as how long a request took or how big an upload was. The measurements go into a histogram.
A histogram does not keep each value. It counts how many values were in each range, and it keeps their number and their sum. With these, your monitoring system can show the average and how slow the slowest requests were. Give the value in the unit the name ends with: seconds for _seconds, bytes for _bytes.
After the first call, this name is a histogram. Calling increment or setGauge with the same name later throws a LogicError. Labels split one histogram into several, for example one per database table.
Good to know: a program cannot read the histogram back. The call returns nothing.
The examples below show recording durations, then recording sizes in bytes, then timing queries per table with a label.
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. Ataintedname does not compile either.$valuefloatThe measurement, in the unit of the series. Use seconds for a name that ends in_seconds, and bytes for a name that ends in_bytes.
Options
labelsarray<string>optionalThe label values of this series, with the label name as the key. Ataintedorsecretvalue is not allowed. Use an enum case, a value converted withas, 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 themax_serieslimit is not an error. Novis does not record that series, but counts how many it did not record. The call returns normally.
Examples
Request durations
<?nvs// Shows how to record how long each request took.
array<float> $durations = [0.042, 0.310, 0.008];
foreach ($durations as float $seconds) { // Each call records one duration in the histogram `checkout_duration_seconds`. Core\Metrics::observe('checkout_duration_seconds', $seconds); echo 'checkout took ', $seconds, " s\n";}checkout took 0.042 scheckout took 0.31 scheckout took 0.008 sUpload sizes in bytes
<?nvs// Shows how to record sizes in bytes, from a whole number.
array<uint> $uploads = [2048, 512000, 73];uint $total = 0;
foreach ($uploads as uint $bytes) { // `observe` needs a float, so `as float` converts the size first. Core\Metrics::observe('upload_size_bytes', $bytes as float); $total = $total + $bytes;}
// This prints 514121.echo 'bytes uploaded: ', $total, "\n";bytes uploaded: 514121Query time per table
<?nvs// Shows how to record the time of each database query, with one histogram per table.
class Query{ public function constructor(public string $table, public float $seconds) {}}
array<Query> $queries = [ new Query('orders', 0.012), new Query('customers', 0.004), new Query('orders', 0.230),];
foreach ($queries as Query $query) { // The label `table` gives each table its own histogram. // The table names are written in the program, so they are allowed as label values. Core\Metrics::observe('db_query_duration_seconds', $query->seconds, { labels: ['table' => $query->table] }); echo $query->table, ': ', $query->seconds, " s\n";}orders: 0.012 scustomers: 0.004 sorders: 0.23 s