itsemon245/lamet
High-performance Laravel metrics recorder and aggregator for Grafana dashboards. Install via Artisan, configure DB/cache, track counters, gauges, and timings, flush/clean via commands, and run scheduled tasks to persist aggregates.
A simple, high-performance package to record and aggregate metrics in Laravel applications, ready for Grafana dashboards.
Install via Composer:
composer require itsemon245/lamet
Publish the configuration and migration:
php artisan lamet:install
This will publish config/lamet.php and the migration to database/migrations/lamet/.
Alternatively, you can publish them separately:
php artisan vendor:publish --tag=lamet-config
php artisan vendor:publish --tag=lamet-migrations
Configure your database and cache:
config/database.php (e.g., sqlite, mysql, pgsql). Postgres is recommended 'lamet' => [
'driver' => env('LAMET_DB_CONNECTION', 'pgsql'),
'url' => env('LAMET_DATABASE_URL'),
'host' => env('LAMET_DB_HOST', '127.0.0.1'),
'port' => env('LAMET_DB_PORT', '5432'),
'database' => env('LAMET_DB_DATABASE', 'lamet_metrics'),
'username' => env('LAMET_DB_USERNAME', 'root'),
'password' => env('LAMET_DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'search_path' => env('LAMET_DB_CONNECTION', 'pgsql') === 'pgsql' ? 'public' : null,
'sslmode' => env('LAMET_DB_CONNECTION', 'pgsql') === 'pgsql' ? 'prefer' : null,
],
config/cache.php (e.g., redis).#Database Connection
LAMET_DB_CONNECTION=pgsql
LAMET_DB_HOST=127.0.0.1
LAMET_DB_PORT=5432
LAMET_DB_DATABASE=lamet_metrics
LAMET_DB_USERNAME=root
LAMET_DB_PASSWORD=password
#Other Config options (these are the defaults)
LAMET_CACHE_STORE=redis
LAMET_CACHE_PREFIX=metrics:
LAMET_CACHE_TTL=3600
LAMET_CACHE_BATCH_SIZE=1000
LAMET_LOG=false
[!NOTE] > Quick Setup: Use the provided
misc/postgres-docker-compose.ymlexample to quickly spin up a PostgreSQL container for development and testing.
Run the migration:
php artisan migrate
[!IMPORTANT] The 5th step is very important. If you skip this your metrics won't be saved in the database. Also keep in mind that you have to set the frequency lower than the ttl value in
config/lamet.php
Schedule periodic flushing:
In app/Console/Kernel.php:
protected function schedule(Schedule $schedule): void
{
$schedule->command('lamet:flush')->everyFiveMinutes();
}
[!TIP] Higher frequency means less granularity, not suitable for realtime metrics but lower memory usage Lower frequency means more granularity, suitable for realtime metrics but higher memory usage Keep it between 5-20 minutes in general.
Record a simple metric using the facade:
use Itsemon245\Lamet\Facades\Metrics;
// Record an API request
Metrics::increment('api.requests', 1, [
'endpoint' => '/users',
'method' => 'GET',
'status_code' => 200
]);
Each method supports three usage patterns:
Metrics::methodName()metricsMethodName()MetricsManagerFor advanced usage and query examples, see:
tags column is flexible and can store any key-value pairs.recorded_at column is used for time-series queries in Grafana.type column defaults to counter for all metrics.unit column is available for any unitconfig/lamet.php for all options.lamet:installInstalls the package by publishing configuration and migration files.
php artisan lamet:install
Options:
--force: Overwrite existing fileslamet:flushFlushes cached metrics to the database.
php artisan lamet:flush
Options:
--dry-run: Run the command without actually doing anything-P|--print: Print the flush keys--force: Force flush even if cache is disabledlamet:cleanCleans old metrics from the database.
php artisan lamet:clean
Options:
--dry-run: Run the command without actually doing anything--days=30: Number of days to keep (default: 30)--force: Force the operation without confirmationThe package configuration is located in config/lamet.php. You can customize the following options:
enabled: Enable/disable metrics recordinglog_metrics: Log metrics to Laravel's log systemdefault_tags: Default tags to add to all metrics (environment, app_name, user fields)db_query.enabled: Enable/disable auto database query monitoringdb_query.metric_name: Name for database query metricsdb_query.tags: Tags to include with database queriesdb_query.store_only_slow_query: Store only slow queries (default: true)db_query.slow_query_name_suffix: Suffix for slow query metric names (default: '.slow')db_query.slow_query_threshold: Threshold in milliseconds for slow queries (default: 1500ms)exception.enabled: Enable/disable auto exception monitoringexception.metric_name: Name for exception metricsexception.tags: Tags to include with exception metricsignore.paths: Array of paths to ignore when recording metricsignore.exceptions: Array of exception classes to ignoreignore.db_query.tables: Array of database tables to ignore when monitoring queriesignore.db_query.sql_patterns: Array of regex patterns to ignore specific SQL queriescache.store: Cache store to use (default: redis)cache.prefix: Prefix for cache keys (default: metrics:)cache.ttl: Time to live for cached metrics (default: 3600 seconds)cache.batch_size: Number of metrics to insert in one batch (default: 1000)table: Database table name for storing metricsconnection: Database connection to use (null to disable database storage)You can configure the package using these environment variables:
LAMET_ENABLED=true
LAMET_LOG=false
LAMET_TABLE=lamet
# Cache Configuration
LAMET_CACHE_STORE=redis
LAMET_CACHE_PREFIX=lamet:
LAMET_CACHE_TTL=3600
LAMET_CACHE_BATCH_SIZE=1000
The package includes a caching system that stores metrics in cache first, then periodically flushes them to the database. This provides:
LAMET_CACHE_STORE: Cache store to use (default: redis)LAMET_CACHE_PREFIX: Prefix for cache keys (default: metrics:)LAMET_CACHE_TTL: Time to live for cached metrics (default: 3600 seconds)LAMET_CACHE_BATCH_SIZE: Number of metrics to insert in one batch (default: 1000)Add these to your app/Console/Kernel.php to automatically flush and clean metrics:
protected function schedule(Schedule $schedule): void
{
// Flush metrics every 5 minutes
$schedule->command('lamet:flush')->everyFiveMinutes();
// Clean old metrics daily at 2 AM (keep last 90 days)
// $schedule->command('lamet:clean --days=90 --force')->dailyAt('02:00');
}
The package is designed to work seamlessly with Grafana for creating beautiful dashboards and visualizations.
Connect your metrics database to Grafana:
Add Data Source in Grafana:
Connection Settings:
Host: your-database-host
Database: your-metrics-database
User: your-database-user
Password: your-database-password
Set up Grafana alerts based on your metrics:
-- High response time alert
SELECT avg(value) as avg_response_time
FROM lamet
WHERE name = 'api.response_time'
AND recorded_at >= now() - interval '5 minutes'
HAVING avg(value) > 1000; -- Alert if > 1 second
For more Grafana query examples, see Grafana Queries.
Run the test suite:
composer test
The MIT License (MIT). Please see License File for more information.
How can I help you explore Laravel packages today?