Testing
Metrics::fake() returns a Nwidart\Metrics\Testing\MetricsFake.
use Nwidart\Metrics\Facades\Metrics;
$metrics = Metrics::fake();Installation
Section titled “Installation”Metrics::fake() calls MetricsFake::install($app), which:
- forgets the meter provider and the listeners of the built-in instrumentation, so that they are built again on the fake store;
- binds a new
Nwidart\MetricsStore\InMemoryStoreasNwidart\MetricsStore\MetricStore.
The in-memory store follows the merge rules of the real stores. Its cardinality limit is 2000 series per metric, whatever cardinality_limit is. It needs no Redis and holds only what the test records.
Instruments created before the call keep the previous meter provider. Eloquent models counted before the call but not yet flushed go to the previous provider too, so the fake starts without them.
Matching rules
Section titled “Matching rules”These rules apply to every method that takes $name and $attributes:
$nameis the OpenTelemetry metric name, such ashttp.server.request.duration, not the Prometheus name.$attributesmatches every series that has at least those attributes, with equal values (==).[]matches every series of the metric.- Only sums, gauges and histograms match.
- Every method flushes the meter provider first, so it sees everything recorded so far.
A value is the sum of the matching series:
| Instrument | Value of one series |
|---|---|
| Counter, up-down counter | The sum |
| Gauge | The last value |
| Histogram | The count of values |
Methods
Section titled “Methods”metrics()
Section titled “metrics()”public function metrics(): arrayReturns the stored metrics as a list<OpenTelemetry\SDK\Metrics\Data\Metric>, with an empty resource. Metrics read on export are not included.
value()
Section titled “value()”public function value(string $name, array $attributes = []): int|float|nullReturns the value of the matching series, added up. Returns null when no series matches.
assertRecorded()
Section titled “assertRecorded()”public function assertRecorded(string $name, array $attributes = []): voidPasses when at least one series matches. Failure message:
The metric [<name>] was not recorded with {"<attribute>":"<value>"}.assertValue()
Section titled “assertValue()”public function assertValue(string $name, int|float $expected, array $attributes = []): voidPasses when at least one series matches and the value equals $expected (assertEquals). Failure messages: the one of assertRecorded(), or:
The metric [<name>] with {"<attribute>":"<value>"} has another value.assertNotRecorded()
Section titled “assertNotRecorded()”public function assertNotRecorded(string $name, array $attributes = []): voidPasses when no series matches. Failure message:
The metric [<name>] was recorded with {"<attribute>":"<value>"}.assertNothingRecorded()
Section titled “assertNothingRecorded()”public function assertNothingRecorded(): voidPasses when the store holds no metric. Failure message, with the names of the stored metrics:
Metrics were recorded: <name>, <name>.The with {...} part of a message is left out when $attributes is empty.
Example
Section titled “Example”use Nwidart\Metrics\Facades\Metrics;
$metrics = Metrics::fake();
$this->post('/orders', $order);
$metrics->assertValue('orders.placed', 1, ['channel' => 'web']);$metrics->assertRecorded('http.server.request.duration', ['http.route' => '/orders']);$metrics->assertNotRecorded('laravel.exceptions');See Test your metrics.