##Using Counter Type Metric
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
- Naming Conventions: Use descriptive names for your counters to make it clear what they are tracking. For example,
user_registration_total or api_request_count.
- Atomic Increments: Ensure that increments are atomic, especially in multi-threaded or multi-process environments, to avoid race conditions.
##Cache Configuration
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.
##Configuring Cache
You can configure the cache in the config/prometheus.php file:
'cache' => null,
##Cache Options
- In-Memory Cache: If you want to use an in-memory cache without relying on Laravel's cache system, keep the cache option as null.
- Laravel Cache: To store metrics in Laravel's cache, set the cache option to a valid cache driver defined in your
config/cache.php file:
'cache' => 'redis',
##Conclusion
By properly configuring the cache, you can ensure that your metrics are persisted and shared across requests, making them more reliable in production environments.