Actions Status MetaCPAN Release

NAME

Mojolicious::Plugin::Prometheus - Mojolicious Plugin

SYNOPSIS

# Mojolicious, no extra options
$self->plugin('Prometheus');

# Mojolicious::Lite, no extra options
plugin 'Prometheus';

# Mojolicious::Lite, with custom response buckets and metrics pr endpoint
plugin 'Prometheus' => {
  http_requests_total => {
    buckets => [qw/4 5 6/],
    labels  => [qw/worker method endpoint code/],
    cb      => sub {
      my $c = shift;
      my $endpoint = $c->match->endpoint ? $c->match->endpoint->to_string : undef;
      return ($$, $c->req->method, $endpoint || $c->req->url, $c->res->code);
    },
  },
};

# You can add your own route to do access control
my $under = app->routes->under('/secret' =>sub {
  my $c = shift;
  return 1 if $c->req->url->to_abs->userinfo eq 'Bender:rocks';
  $c->res->headers->www_authenticate('Basic');
  $c->render(text => 'Authentication required!', status => 401);
  return undef;
});
plugin Prometheus => {route => $under};

DESCRIPTION

Mojolicious::Plugin::Prometheus is a Mojolicious plugin that exports Prometheus metrics from Mojolicious.

Hooks are also installed to measure requests response time and count requests based on method and HTTP return code.

HELPERS

prometheus.instance

Create further instrumentation into your application by using this helper which gives access to the Net::Prometheus object. Please note that this represents a change from versions up to and including 1.4.x where prometheus was the method that returned the Prometheus instance.

See Net::Prometheus for further usage information.

prometheus.register($collector, $scope)

Register new / custom collectors. This method lets you differentiate those collectors that collect worker-specific metrics and those that collect more generic / global metrics. The difference between these two categories of collectors is that the results of calling collect on worker-specific collectors is cached to shared memory using IPC::ShareLite so that one worker can return collected results for all workers. Results of calling collect on generic / global collectors are not cached. This separation ensures that collectors that are global is not duplicated in the output, ie. does not appear once for each worker.

METHODS

Mojolicious::Plugin::Prometheus inherits all methods from Mojolicious::Plugin and implements the following new ones.

register

$plugin->register($app, \%config);

Register plugin in Mojolicious application. You may pass a hashref containing configuration overrides to register this will be merged with the default configuration values.

%config can have:

METRICS

In addition to exposing the default process metrics that Net::Prometheus already expose this plugin will also expose

Custom configuration of the built in metrics is possible. An example structure can be seen in the synopsis. The four built in metrics from this plugin all have more or less the same structure. Metrics provided by the Perl- and Process-collectors from Net::Prometheus can be used and changed by providing a custom prometheus object instead of using the defaults as detailed previously.

Default configuration for the built in metrics are as follows:

http_request_duration_seconds => {
  buckets => [.005, .01, .025, .05, .075, .1, .25, .5, .75, 1.0, 2.5, 5.0, 7.5, 10],
  labels  => [qw/worker method/],
  cb      =>  sub($c) { $$, $c->req->method, tv_interval($c->stash('prometheus.start_time')) },
}

http_request_size_bytes => {
  buckets => [1, 50, 100, 1_000, 10_000, 50_000, 100_000, 500_000, 1_000_000],
  labels  => [qw/worker method/],
  cb      => sub($c) { $$, $c->req->method, $c->req->content->body_size },
}

http_response_size_bytes => {
  buckets => [5, 50, 100, 1_000, 10_000, 50_000, 100_000, 500_000, 1_000_000],
  labels  => [qw/worker method code/],
  cb      => sub($c) { $$, $c->req->method, $c->res->code, $c->res->content->body_size },
}

http_requests_total => {
  labels  => [qw/worker method code/],
  cb      => sub($c) { $$, $c->req->method, $c->res->code },
}

AUTHOR

(the IPC::ShareLite parts of this code is shamelessly stolen from Mojolicious::Plugin::Status written by Sebastian Riedel and mangled into something that works for me)

COPYRIGHT AND LICENSE

Copyright (C) 2018, Vidar Tyldum

This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.

SEE ALSO