feat(PHP): add aurweb Prometheus metrics

Along with this initial requests metric implementation,
we also now serve the `/metrics` route, which grabs request
metrics out of cache and renders them properly for Prometheus.

**NOTE** Metrics are only enabled when the aurweb system admin
has enabled caching by configuring `options.cache` correctly
in `$AUR_CONFIG`. Otherwise, an error is logged about no cache
being configured.

New dependencies have been added which require the use of
`composer`. See `INSTALL` for the dependency section in regards
to composer dependencies and how to install them properly for
aurweb.

Metrics are in the following forms:

    aurweb_http_requests_count(method="GET",route="/some_route")
    aurweb_api_requests_count(method="GET",route="/rpc",type="search")

This should allow us to search through the requests for specific routes
and queries.

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-09-26 20:26:24 -07:00
parent 4e5b67f0a6
commit 986fa9ee30
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
5 changed files with 183 additions and 2 deletions

View file

@ -3,10 +3,39 @@ set_include_path(get_include_path() . PATH_SEPARATOR . '../lib');
include_once("aur.inc.php");
include_once("pkgfuncs.inc.php");
include_once("cachefuncs.inc.php");
include_once("metricfuncs.inc.php");
$path = $_SERVER['PATH_INFO'];
$tokens = explode('/', $path);
$query_string = $_SERVER['QUERY_STRING'];
// If no options.cache is configured, we no-op metric storage operations.
$is_cached = defined('EXTENSION_LOADED_APC') || defined('EXTENSION_LOADED_MEMCACHE');
if ($is_cached) {
$method = $_SERVER['REQUEST_METHOD'];
// We'll always add +1 to our total request count to this $path,
// unless this path == /metrics.
if ($path !== "/metrics")
add_metric("http_requests_count", $method, $path);
// Extract $type out of $query_string, if we can.
$type = null;
$query = array();
if ($query_string)
parse_str($query_string, $query);
$type = $query['type'];
// Only store RPC metrics for valid types.
$good_types = [
"info", "multiinfo", "search", "msearch",
"suggest", "suggest-pkgbase", "get-comment-form"
];
if ($path === "/rpc" && in_array($type, $good_types))
add_metric("api_requests_count", $method, $path, $type);
}
if (config_get_bool('options', 'enable-maintenance') && (empty($tokens[1]) || ($tokens[1] != "css" && $tokens[1] != "images"))) {
if (!in_array($_SERVER['REMOTE_ADDR'], explode(" ", config_get('options', 'maintenance-exceptions')))) {
header("HTTP/1.0 503 Service Unavailable");