Record your first metrics
In this tutorial, we build a small shop application that records metrics. We install the package, read the metrics that it records by itself, add a counter of our own and write a test for it.
We need no Redis. We keep the metrics in a file, on our own computer.
You need PHP 8.2 or later, Composer and curl.
Create the application
Section titled “Create the application”-
Create a new Laravel application named
shop, and go into its folder:Terminal window composer create-project laravel/laravel shopcd shopComposer installs Laravel and runs the first migrations. The last lines show:
INFO Running migrations.0001_01_01_000000_create_users_table ........ 5.52ms DONE0001_01_01_000001_create_cache_table ........ 3.57ms DONE0001_01_01_000002_create_jobs_table ......... 6.91ms DONE
Install the package
Section titled “Install the package”-
Require the package:
Terminal window composer require nwidart/laravel-metrics -
Composer asks if you trust a plugin:
Do you trust "tbachert/spi" to execute code and wish to enable it now? (writes "allow-plugins" to composer.json) [y,n,d,?]Answer
n. The package does not need this plugin.At the end, Composer shows the version it installed:
nwidart/laravel-metrics .................................... DONE...Using version ^0.3.0 for nwidart/laravel-metricsThe package registers itself. It already records every request.
Read the metrics
Section titled “Read the metrics”By default, the package keeps its metrics in Redis. We have no Redis, so we tell it to use the file cache store instead. We also turn on the route that shows the metrics.
-
Add these lines at the end of the
.envfile:.env METRICS_STORE=cacheMETRICS_CACHE_STORE=fileMETRICS_PROMETHEUS=true -
Start the development server:
Terminal window php artisan serveINFO Server running on [http://127.0.0.1:8000].Keep it running. Open a second terminal in the
shopfolder for the next commands. -
Open the home page once, so that the application has some traffic to record:
Terminal window curl -s -o /dev/null http://127.0.0.1:8000/ -
Read the metrics, and keep only two lines of them:
Terminal window curl -s http://127.0.0.1:8000/metrics | grep -E '^(laravel_info|http_server_request_duration_seconds_count)'http_server_request_duration_seconds_count{http_request_method="GET",http_response_status_code="200",http_route="/",network_protocol_version="1.1",otel_scope_name="nwidart/laravel-metrics",url_scheme="http"} 1laravel_info{laravel_version="13.43.0",otel_scope_name="nwidart/laravel-metrics",php_sapi="cli-server",process_runtime_name="php",process_runtime_version="8.4.27"} 1The first line says that the route
/answered oneGETrequest with the status200. The second line shows your Laravel and PHP versions, which can be other than these.Run
curl -s http://127.0.0.1:8000/metricswithoutgrepto see everything: the request durations in buckets, the database queries, and OPcache and disk usage.
Record a metric of our own
Section titled “Record a metric of our own”The shop places orders. We count each order with a counter named orders.placed.
-
Replace the contents of
routes/web.phpwith this:routes/web.php <?phpuse Illuminate\Support\Facades\Route;use Nwidart\Metrics\Facades\Metrics;Route::get('/', function () {return view('welcome');});Route::get('/orders/place', function () {Metrics::meter()->createCounter('orders.placed', '{order}')->add(1, ['channel' => 'web']);return 'Order placed.';});Each call to
/orders/placeadds 1 to the counter, with the attributechannelset toweb. -
Place three orders:
Terminal window curl -s http://127.0.0.1:8000/orders/placecurl -s http://127.0.0.1:8000/orders/placecurl -s http://127.0.0.1:8000/orders/placeEach call answers:
Order placed. -
Read the counter:
Terminal window curl -s http://127.0.0.1:8000/metrics | grep '^orders_placed_total'orders_placed_total{channel="web",otel_scope_name="app"} 3The counter is 3. In the Prometheus format,
orders.placedbecomesorders_placed_total. -
Place one more order, and read the counter again:
Terminal window curl -s http://127.0.0.1:8000/orders/placecurl -s http://127.0.0.1:8000/metrics | grep '^orders_placed_total'orders_placed_total{channel="web",otel_scope_name="app"} 4Each request adds to the same total. The total stays in the store between requests.
-
Stop the development server with
Ctrl+Cin the first terminal.
Test the counter
Section titled “Test the counter”A test must not write into our store. Metrics::fake() keeps the metrics of the test in memory.
-
Create a feature test:
Terminal window php artisan make:test OrderMetricsTestINFO Test [tests/Feature/OrderMetricsTest.php] created successfully. -
Replace the contents of
tests/Feature/OrderMetricsTest.phpwith this:tests/Feature/OrderMetricsTest.php <?phpnamespace Tests\Feature;use Nwidart\Metrics\Facades\Metrics;use Tests\TestCase;class OrderMetricsTest extends TestCase{public function test_placing_an_order_counts_it(): void{$metrics = Metrics::fake();$this->get('/orders/place')->assertOk();$this->get('/orders/place')->assertOk();$metrics->assertValue('orders.placed', 2, ['channel' => 'web']);$metrics->assertRecorded('http.server.request.duration', ['http.route' => '/orders/place']);}}The test places two orders. Then it checks that the counter is 2, and that the package recorded the duration of the requests to
/orders/place. -
Run the test:
Terminal window php artisan test --filter=OrderMetricsTestPASS Tests\Feature\OrderMetricsTest✓ placing an order counts it 0.08sTests: 1 passed (5 assertions)
What we did
Section titled “What we did”We installed Laravel Metrics in a new application. It recorded the HTTP requests with no code. We read the metrics in the Prometheus format, counted orders with a counter of our own, and tested that counter without touching the store.
Next steps
Section titled “Next steps”- Set up for production, with Redis.
- Scrape with Prometheus, and protect the route.
- Push over OTLP to an OpenTelemetry Collector or a backend.
- Record your own metrics: counters, histograms and their buckets.
- Read how it works to learn why each request merges into a store.