Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[agroal#219] Provide Prometheus metrics cache
This commit refactors the way the Prometheus metrics are served in order to provide the capability to cache the responses for a specified amount of time. Two new configuration settings have been introduced: - `metrics_cache_max_age`, expressed in seconds, defines for how long a cached response can be served without having to compute a new one set of metrics. This parameter is required for the caching machinery to be enabled. - `metrics_cache_max_size` allows the user to avoid caching of responses greater than the expressed value in bytes. In any case, a cache response is tied to 1MegaByte, by a value set in the source code. If the cache overflows the 1MB memory limit, it is immediatly marked as invalid, that is the system is not going to serve the cached result. However, as soon as a new Prometheus request arrives, the cache will be re-filled from scratch, so if the current answer is lower than 1MB and lower than the optional `metrics_cache_max_size`, the response will be cached. Whenever an overflow of the cache happens, the user is warned. Due to the nature the cache is populated, the user could see more than one single advice in logs (at level DEBUG). The `struct prometheus` has been expanded with an inner struct, named `struct prometheus_cache` that holds the cache payload, a lock (for concurrency), the size of payload, and the timestamp the cache will be valid until. New utility functions have been added: - `metrics_cache_append(char* data)` that appends the data to the cache in the case it is safe to do it, that is no overflow in the cache size. In case the data to append makes the cache to overflow, the cache is invalidated; - `metrics_cache_finalize(void)` set the timestamp for the cache vlidation. - `metrics_cache_invalidate(void)` set the cache as invalid by zero-filling the data. Used for example when an overflow is detected. - `metrics_cache_size_to_alloc(void)` computes the size, in bytes, required for the cache payload allocation. The size is set to the default, or `metrics_cache_max_size` if configured and in any case nothing more than the max size of 1MB. - `metrics_cache_alloc(struct prometheus* prometheus)` allocates the memory for the cache and sets the initial values. This has to be called when the `struct prometheus` is allocated and initialized. The `metrics_page()` method has been refactored to check if the cache is valid, in such case the response is served directly out of cache. If the cache is not valid, the function gets the cache lock and invokes several times the append function (in inner method calls). Last, it finalizes the cache and release the lock. If the optional configuration parameter `metrics_cache_max_size` changes, the system does not reallocate memory, rather it issue a warning about the need for a restart. The documentation has been updated to reflect changes. Close agroal#219 Modified the prometheus_cache structure to handle dynamic data. Use dynamic cache content. Not yet protected by the lock. Sahred memory for the cache. Cache locking Uncrustify
- Loading branch information