spatie/laravel-prometheus
Export Laravel app metrics to Prometheus via a /prometheus endpoint. Register custom gauges/counters with simple callbacks, use built-in queue and Horizon metrics, and optionally secure the endpoint. Ideal for scraping by Prometheus and charting in Grafana.
Overview
A Counter is a metric that only increases in value. It is typically used to count events, such as the number of requests received or tasks completed.
Creating a Counter
You can create a Counter metric using the addCounter method:
$counter = Prometheus::addCounter('my_counter');
Setting an Initial Value
You can also define an initial value for your counter when creating it. This is useful if you want to start counting from a specific number:
$counter = Prometheus::addCounter('my counter')->setInitialValue(100);
Incrementing the Counter
To increment the counter, you can use the inc method:
$counter->inc();
This will increase the counter by one.
If you need to increment the counter by a value greater than one, you can pass the value as an argument to the inc() method:
$counter->inc(2);
Best Practices
user_registration_total or api_request_count.By default, the cache setting is set to null, which means that the metric will be stored in memory without using Laravel's caching system. This is suitable for simple setups or testing environments.
You can configure the cache in the config/prometheus.php file:
/**
* Select a cache to store gauges, counters, summaries and histograms between requests.
* In a multi node setup you should ensure that each node writes to its own
* cache instance or uses a node specific prefix.
* Configure the cache store in config/cache.php.
*
* to use an in memory adapter for testing use array or null as your store
* or remove the cache entry all together:
* 'cache' => null // InMemory implementation without laravel cache
* 'cache' => 'array' // InMemory implementation using laravel cache
*/
'cache' => null,
config/cache.php file:
'cache' => 'redis', // Using Redis for caching metrics
By properly configuring the cache, you can ensure that your metrics are persisted and shared across requests, making them more reliable in production environments.
How can I help you explore Laravel packages today?