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.
Installation:
composer require itsemon245/lamet
php artisan lamet:install
This publishes the config file and creates a migration for the metrics table.
Run Migrations:
php artisan migrate
First Metric:
use Lamet\Facades\Lamet;
Lamet::record('api.requests', 1); // Increment a counter
Lamet::record('api.latency', 150); // Record a gauge (ms)
Verify Data:
Check the lamet_metrics table directly or configure Grafana to query it.
Track API request counts and latency:
// In your API middleware or controller
Lamet::record('api.requests', 1);
Lamet::record('api.latency', $executionTimeMs);
Metric Types:
api.requests).memory.usage).Lamet::timer('db.query.time')->start();
// ... DB query ...
Lamet::timer('db.query.time')->stop();
Tagging for Context:
Lamet::record('api.requests', 1, ['endpoint' => 'users.index', 'status' => '200']);
Bulk Recording:
Lamet::bulk([
['api.requests', 1, ['endpoint' => 'users.create']],
['memory.usage', memory_get_usage(true)],
]);
Scheduled Aggregation:
Configure lamet:clean in your app/Console/Kernel.php:
protected function schedule(Schedule $schedule)
{
$schedule->command('lamet:clean')->dailyAt('03:00');
}
Middleware: Record latency for all requests:
public function handle($request, Closure $next)
{
$start = microtime(true);
$response = $next($request);
Lamet::record('http.latency', (microtime(true) - $start) * 1000);
return $response;
}
Service Providers: Boot metrics collection:
public function boot()
{
Lamet::record('app.uptime', time());
}
Grafana: Use the PostgreSQL/MySQL data source with queries like:
SELECT
metric,
tags,
SUM(value) as total,
AVG(value) as avg
FROM lamet_metrics
WHERE created_at > NOW() - INTERVAL '1 hour'
GROUP BY metric, tags
Cache Dependencies:
file). Switching drivers (e.g., to redis) requires:
php artisan config:clear
redis for high-throughput apps to avoid disk I/O bottlenecks.Tagging Quirks:
// ❌ Avoid
Lamet::record('metric', 1, ['tags' => new stdClass()]);
// ✅ Do
Lamet::record('metric', 1, ['tags' => json_encode(['key' => 'value'])]);
Timer Precision:
microtime(true). For sub-millisecond precision, ensure your server’s clock is synchronized (e.g., via NTP).Grafana Time Series:
created_at is already formatted correctly, but custom queries may need:
SELECT
metric,
tags,
value,
EXTRACT(EPOCH FROM created_at) as time
FROM lamet_metrics
Check Raw Data:
php artisan tinker
>>> \DB::table('lamet_metrics')->latest()->take(10)->get();
Flush Cache: If metrics aren’t appearing:
php artisan cache:clear
php artisan lamet:flush
Custom Aggregators:
Override the default aggregation logic in app/Providers/LametServiceProvider.php:
public function boot()
{
Lamet::extend('custom_metric', function ($value, $tags) {
return round($value * 1.5); // Example: Scale values
});
}
Event Listeners:
Hook into metric recording via events (not yet documented; check lamet:install for event hooks):
Lamet::listen('metric.recorded', function ($metric, $value, $tags) {
Log::info("Metric recorded: {$metric} = {$value}");
});
Database Schema:
Extend the lamet_metrics table by publishing and modifying the migration:
php artisan vendor:publish --tag=lamet-migrations
How can I help you explore Laravel packages today?