Merge pull request #680 from grahamc/prometheus

export a /prometheus endpoint
This commit is contained in:
Eelco Dolstra 2019-09-25 19:46:42 +02:00 committed by GitHub
commit 037f6488f6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 1 deletions

View file

@ -1,5 +1,5 @@
{ hydraSrc ? builtins.fetchGit ./. { hydraSrc ? builtins.fetchGit ./.
, nixpkgs ? builtins.fetchGit { url = https://github.com/NixOS/nixpkgs-channels.git; ref = "nixos-19.03-small"; } , nixpkgs ? builtins.fetchGit { url = https://github.com/NixOS/nixpkgs-channels.git; ref = "nixos-19.09-small"; }
, officialRelease ? false , officialRelease ? false
, shell ? false , shell ? false
}: }:
@ -99,6 +99,7 @@ rec {
LWP LWP
LWPProtocolHttps LWPProtocolHttps
NetAmazonS3 NetAmazonS3
NetPrometheus
NetStatsd NetStatsd
PadWalker PadWalker
Readonly Readonly

View file

@ -6,6 +6,7 @@ use warnings;
use base 'Hydra::Base::Controller::ListBuilds'; use base 'Hydra::Base::Controller::ListBuilds';
use Hydra::Helper::Nix; use Hydra::Helper::Nix;
use Hydra::Helper::CatalystUtils; use Hydra::Helper::CatalystUtils;
use Hydra::View::TT;
use Digest::SHA1 qw(sha1_hex); use Digest::SHA1 qw(sha1_hex);
use Nix::Store; use Nix::Store;
use Nix::Config; use Nix::Config;
@ -13,6 +14,7 @@ use Encode;
use File::Basename; use File::Basename;
use JSON; use JSON;
use List::MoreUtils qw{any}; use List::MoreUtils qw{any};
use Net::Prometheus;
# Put this controller at top-level. # Put this controller at top-level.
__PACKAGE__->config->{namespace} = ''; __PACKAGE__->config->{namespace} = '';
@ -200,6 +202,49 @@ sub machines :Local Args(0) {
$self->status_ok($c, entity => $c->stash->{machines}); $self->status_ok($c, entity => $c->stash->{machines});
} }
sub prometheus :Local Args(0) {
my ($self, $c) = @_;
my $machines = getMachines;
my $client = Net::Prometheus->new;
my $duration = $client->new_histogram(
name => "hydra_machine_build_duration",
help => "How long builds are taking per server. Note: counts are gauges, NOT counters.",
labels => [ "machine" ],
buckets => [
60,
600,
1800,
3600,
7200,
21600,
43200,
86400,
172800,
259200,
345600,
518400,
604800,
691200
]
);
my $steps = dbh($c)->selectall_arrayref(
"select machine, s.starttime as starttime " .
"from BuildSteps s join Builds b on s.build = b.id " .
"where busy != 0 order by machine, stepnr",
{ Slice => {} });
foreach my $step (@$steps) {
my $name = $step->{machine} ? Hydra::View::TT->stripSSHUser(undef, $step->{machine}) : "";
$name = "localhost" unless $name;
$duration->labels($name)->observe(time - $step->{starttime});
}
$c->stash->{'plain'} = { data => $client->render };
$c->forward('Hydra::View::Plain');
}
# Hydra::Base::Controller::ListBuilds needs this. # Hydra::Base::Controller::ListBuilds needs this.
sub get_builds : Chained('/') PathPart('') CaptureArgs(0) { sub get_builds : Chained('/') PathPart('') CaptureArgs(0) {