Skip to content

Test your metrics

To check what a test records, call Metrics::fake() before the code under test. The fake keeps the metrics in memory, with the rules of the real stores. The test needs no Redis and sees only its own metrics.

Tests that do not call the fake still record into the configured store. To keep them in memory, use the cache store with the array cache driver:

phpunit.xml
<php>
<env name="CACHE_STORE" value="array"/>
<env name="METRICS_STORE" value="cache"/>
</php>

Laravel’s phpunit.xml already sets CACHE_STORE=array.

Do not set METRICS_ENABLED=false in tests that call the fake. With metrics off, nothing is recorded, and every assertion that expects a metric fails.

tests/Feature/PlaceOrderTest.php
use Nwidart\Metrics\Facades\Metrics;
it('counts the order', function () {
$metrics = Metrics::fake();
$this->post('/orders', ['sku' => 'TSHIRT'])->assertCreated();
$metrics->assertValue('app.orders.placed', 1, ['app.order.channel' => 'web']);
});

The attributes you pass match every series that has at least those attributes. assertValue() adds up the matching series.

The built-in instrumentation records in tests too:

it('records the request and its queries', function () {
$metrics = Metrics::fake();
$this->get('/orders')->assertOk();
$metrics->assertRecorded('http.server.request.duration', [
'http.route' => '/orders',
'http.response.status_code' => 200,
]);
$metrics->assertRecorded('db.client.operation.duration', ['db.operation.name' => 'SELECT']);
$metrics->assertNotRecorded('laravel.exceptions');
});

For a histogram, the value is the number of recorded values, not their sum. Two requests to /orders give a value of 2:

$metrics = Metrics::fake();
$this->get('/orders');
$this->get('/orders');
$metrics->assertValue('http.server.request.duration', 2, ['http.route' => '/orders']);

For a counter or an up-down counter, the value is the total. For a gauge, it is the last value.

With QUEUE_CONNECTION=sync, a dispatched job runs at once and records messaging.process.duration:

use App\Jobs\ShipOrder;
$metrics = Metrics::fake();
ShipOrder::dispatch($order);
$metrics->assertRecorded('messaging.process.duration', ['laravel.job.name' => ShipOrder::class]);

A job that throws has the exception class as error.type.

use App\Exceptions\PaymentFailed;
$metrics = Metrics::fake();
report(new PaymentFailed('Card declined'));
$metrics->assertValue('laravel.exceptions', 1, ['exception.type' => PaymentFailed::class]);

laravel.exceptions counts through the application’s exception handler. Exceptions::fake() replaces that handler, so do not combine it with this assertion.

To check something the assertions do not cover, read the values:

$metrics->value('http.server.request.duration', ['http.route' => '/orders']); // int|float|null
$metrics->metrics(); // list<OpenTelemetry\SDK\Metrics\Data\Metric>

value() returns null when no series matches.

$metrics = Metrics::fake();
$this->artisan('inspire');
$metrics->assertNothingRecorded();

The fake asserts what the application records. Metrics from Metrics::onExport() are read only on export. To test them, export and look for the metric:

use OpenTelemetry\SDK\Resource\ResourceInfoFactory;
Metrics::fake();
$names = array_map(
fn ($metric) => $metric->name,
Metrics::export(ResourceInfoFactory::emptyResource()),
);
expect($names)->toContain('app.queue.size');