Expose Prometheus metrics at /metrics

Exposes metrics:

* http_request_duration_seconds_bucket
* http_request_size_bytes_bucket
* http_response_size_bytes_bucket
* http_requests_total

with labels of action and controller to help identify popular
endpoints and their performance characteristics.
This commit is contained in:
Graham Christensen 2021-06-10 17:45:45 -04:00
parent db6bad108b
commit 71453dd341
3 changed files with 51 additions and 1 deletions

View file

@ -70,6 +70,21 @@
}; };
}; };
CatalystPluginPrometheusTiny = final.buildPerlPackage {
pname = "Catalyst-Plugin-PrometheusTiny";
version = "0.005";
src = final.fetchurl {
url = "mirror://cpan/authors/id/S/SY/SYSPETE/Catalyst-Plugin-PrometheusTiny-0.005.tar.gz";
sha256 = "a42ef09efdc3053899ae007c41220d3ed7207582cc86e491b4f534539c992c5a";
};
buildInputs = with final.perlPackages; [ HTTPMessage Plack SubOverride TestDeep ];
propagatedBuildInputs = with final.perlPackages; [ CatalystRuntime Moose PrometheusTiny PrometheusTinyShared ];
meta = {
description = "Prometheus metrics for Catalyst";
license = with final.lib.licenses; [ artistic1 gpl1Plus ];
};
};
CryptArgon2 = final.perlPackages.buildPerlModule { CryptArgon2 = final.perlPackages.buildPerlModule {
pname = "Crypt-Argon2"; pname = "Crypt-Argon2";
version = "0.010"; version = "0.010";
@ -358,6 +373,7 @@
CatalystPluginAccessLog CatalystPluginAccessLog
CatalystPluginAuthorizationRoles CatalystPluginAuthorizationRoles
CatalystPluginCaptcha CatalystPluginCaptcha
CatalystPluginPrometheusTiny
CatalystPluginSessionStateCookie CatalystPluginSessionStateCookie
CatalystPluginSessionStoreFastMmap CatalystPluginSessionStoreFastMmap
CatalystPluginStackTrace CatalystPluginStackTrace

View file

@ -16,7 +16,8 @@ use Catalyst qw/ConfigLoader
Session Session
Session::Store::FastMmap Session::Store::FastMmap
Session::State::Cookie Session::State::Cookie
Captcha/, Captcha
PrometheusTiny/,
'-Log=warn,fatal,error'; '-Log=warn,fatal,error';
use CatalystX::RoleApplicator; use CatalystX::RoleApplicator;
use YAML qw(LoadFile); use YAML qw(LoadFile);
@ -47,6 +48,9 @@ __PACKAGE__->config(
file($ENV{'HYDRA_LDAP_CONFIG'}) file($ENV{'HYDRA_LDAP_CONFIG'})
) : undef ) : undef
}, },
'Plugin::PrometheusTiny' => {
include_action_labels => 1,
},
'Plugin::Static::Simple' => { 'Plugin::Static::Simple' => {
send_etag => 1, send_etag => 1,
expires => 3600 expires => 3600

30
t/Controller/metrics.t Normal file
View file

@ -0,0 +1,30 @@
use feature 'unicode_strings';
use strict;
use Setup;
use JSON qw(decode_json encode_json);
my %ctx = test_init();
require Hydra::Schema;
require Hydra::Model::DB;
require Hydra::Helper::Nix;
use HTTP::Request::Common;
use Test2::V0;
require Catalyst::Test;
Catalyst::Test->import('Hydra');
my $db = Hydra::Model::DB->new;
hydra_setup($db);
request(GET '/');
my $metrics = request(GET '/metrics');
ok($metrics->is_success);
like(
$metrics->content,
qr/http_requests_total\{action="index",code="200",controller="Hydra::Controller::Root",method="GET"\} 1/,
"Metrics are collected"
);
done_testing;