Laravelアプリの検索フォームのリニューアル時に、ユーザーへのアンケートではなく、chromeの開発チームのように利用統計から改善したいと思いました。
最も利用される検索フォームは電話番号か氏名のどちらか、と調べられたり、レスポンス改善の優先度を見える化するためにミリ秒単位でロード時間を測るまでをゴールとします。
ミドルウェアを使って、簡単に実装できました。ファイルもartisanコマンドで作成できます。
$ php artisan make:model HttpRequest --migration
Model created successfully.
Created Migration: 2020_06_20_010616_create_http_requests_table
$ php artisan make:middleware CollectHttpRequestStat
Middleware created successfully.
作成された3ファイルにhttps://github.com/umihico/laravel-demo/commit/4a5e2a3のように修正し、マイグレーションを実行します。
$ php artisan migrate --path=database/migrations/2020_06_20_010616_create_http_requests_table.php
Migrating: 2020_06_20_010616_create_http_requests_table
Migrated: 2020_06_20_010616_create_http_requests_table (0.05 seconds)
デプロイすると、この画像のような形でDBにデータが取れるようになります。
複数アプリがあることを想定し、APP_NAMEを挿入しています。ログインしていればユーザーIDが入ります。検索クエリーはdataカラムに、レスポンス時間はmillisecondsカラムに入ります。
以上です。