Skip to content

Leave metrics out of exports

To keep metrics out of every export, register a filter with Metrics::exclude(). The filter receives each metric and returns true to leave it out. It applies to the Prometheus scrape and to metrics:push.

Register it in the boot() method of a service provider:

app/Providers/AppServiceProvider.php
use Nwidart\Metrics\Facades\Metrics;
use OpenTelemetry\SDK\Metrics\Data\Metric;
public function boot(): void
{
Metrics::exclude(fn (Metric $metric): bool => str_starts_with($metric->name, 'legacy.'));
}

Each call adds a filter. A metric that any filter matches is left out.

The filter receives an OpenTelemetry\SDK\Metrics\Data\Metric. Besides name, it has unit, description and instrumentationScope.

To leave out everything one meter recorded:

Metrics::exclude(fn (Metric $metric): bool => $metric->instrumentationScope->getName() === 'billing');

The built-in metrics use the meter nwidart/laravel-metrics. Metrics::meter() uses app unless you give another name.

To stop recording a whole built-in instrumentation, turn it off instead: see Turn off instrumentation. Its series stay in the store, so also exclude them until the store is cleared:

Metrics::exclude(fn (Metric $metric): bool => $metric->name === 'laravel.cache.operations');

A filter hides series but keeps them in the store. To remove every series, clear the store:

Terminal window
php artisan tinker --execute="app(Nwidart\MetricsStore\MetricStore::class)->clear()"