Catalyst-Plugin-PrometheusTiny

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN


    * refresh README, fixup META resources

0.005 - 2021-06-08

    * add META resources
    * add optional controller and action labels to the metrics

0.004 - 2021-06-06

    * allow simple configuration of endpoint path
    * bump deps on Prometheus::Tiny and ::Shared
    * more tests

0.003 - 2021-06-05

    * bump min Perl version to 5.10.1 due to upstream deps

0.002 - 2021-06-05

    * remove a regexp modifier added in Perl 5.22

MANIFEST  view on Meta::CPAN

Changes
lib/Catalyst/Plugin/PrometheusTiny.pm
maint/Makefile.PL.include
Makefile.PL
MANIFEST			This list of files
t/default_config.t
t/endpoint.t
t/include_action_labels.t
t/lib/TestApp.pm
t/lib/TestApp/Controller/Root.pm
t/lib/TestApp/Helper.pm
t/prometheus.t
META.yml                                 Module YAML meta-data (added by MakeMaker)
META.json                                Module JSON meta-data (added by MakeMaker)
README                                   README file (added by Distar)
LICENSE                                  LICENSE file (added by Distar)

README  view on Meta::CPAN

    Once your app has served from requests you can fetch request/response
    metrics:

        curl http://$myappaddress/metrics

DESCRIPTION
    This plugin integrates Prometheus::Tiny::Shared with your Catalyst app,
    providing some default metrics for requests and responses, with the
    ability to easily add further metrics to your app. A default controller
    is included which makes the metrics available via the configured
    "endpoint", though this can be disabled if you prefer to add your own
    controller action.

    See Prometheus::Tiny for more details of the kind of metrics supported.

    The following metrics are included by default:

        http_request_duration_seconds => {
            help => 'Request durations in seconds',
            type => 'histogram',
        },

lib/Catalyst/Plugin/PrometheusTiny.pm  view on Meta::CPAN

            help    => 'Response sizes in bytes',
            type    => 'histogram',
            buckets => [ 1, 50, 100, 1_000, 50_000, 500_000, 1_000_000 ],
        }
    },
};

my ($prometheus,               # instance
    $ignore_path_regexp,       # set from config
    $include_action_labels,    # set from config
    $metrics_endpoint,         # set from config with default
    $no_default_controller,    # set from config
    $request_path              # derived from $metrics_endpoint
);

# for testing
sub _clear_prometheus {
    undef $prometheus;
}

sub prometheus {
    my $c = shift;
    $prometheus //= do {
        my $config = Catalyst::Utils::merge_hashes(
            $defaults,
            $c->config->{'Plugin::PrometheusTiny'} // {}
        );

        $include_action_labels = $config->{include_action_labels};

        $metrics_endpoint = $config->{endpoint};
        if ($metrics_endpoint) {
            if ( $metrics_endpoint !~ m|^/| ) {
                Carp::croak
                  "Plugin::PrometheusTiny endpoint '$metrics_endpoint' does not begin with '/'";
            }
        }
        else {
            $metrics_endpoint = '/metrics';
        }

        $request_path = $metrics_endpoint;
        $request_path =~ s|^/||;

        $ignore_path_regexp = $config->{ignore_path_regexp};
        if ($ignore_path_regexp) {
            $ignore_path_regexp = qr/$ignore_path_regexp/
              unless 'Regexp' eq ref $ignore_path_regexp;
        }

        $no_default_controller = $config->{no_default_controller};

t/endpoint.t  view on Meta::CPAN

use warnings;
use strict;
use lib 'lib', 't/lib';

use Test::More;
use Test::Deep;
use TestApp::Helper;

TestApp::Helper::run(
    { endpoint => '/testme' },
    '/testme',
    superbagof('http_requests_total{code="200",method="GET"} 1')
);

done_testing;

t/lib/TestApp/Helper.pm  view on Meta::CPAN

use strict;

use Test::More;
use Test::Deep;

use HTTP::Request::Common;
use Plack::Test;
use TestApp;

sub get_metrics {
    my ( $test, $endpoint ) = @_;

    my $res = $test->request( GET $endpoint );
    return [ grep { $_ !~ /^#/ } split /\n/, $res->content ];
}

sub run {
    my ( $config, $endpoint, $expect ) = @_;

    TestApp->config( 'Plugin::PrometheusTiny' => $config );
    TestApp->setup;
    my $app  = TestApp->psgi_app;
    my $test = Plack::Test->create($app);

    my $got = get_metrics( $test, $endpoint );
    cmp_deeply $got, [], "We start with no metrics"
      or diag explain $got;

    ok my $res = $test->request( GET "/" ), "GET /";
    is $res->content, "Hello World", "... and content is as expected";

    $got = get_metrics( $test, $endpoint );
    cmp_deeply $got,
      $expect,
      "... and metrics are as expected"
      or diag explain $got;
}

1;

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.338 second using v1.00-cache-2.02-grep-82fe00e-cpan-4673cadbf75 )