Catalyst-Plugin-Statsd
view release on metacpan or search on metacpan
lib/Catalyst/Plugin/Statsd.pm view on Meta::CPAN
package Catalyst::Plugin::Statsd;
# ABSTRACT: Log Catalyst stats to statsd
use v5.20;
use Moose::Role;
use POSIX qw/ ceil /;
use Ref::Util qw/ is_plain_arrayref /;
use experimental qw/ signatures /;
# RECOMMEND PREREQ: Ref::Util::XS
use namespace::autoclean;
requires qw/ log_stats /;
our $VERSION = 'v0.9.0';
sub statsd_client($c) {
return $c->req->env->{'psgix.monitor.statsd'};
}
sub statsd_metric_name_filter( $c, $stat ) {
return "$stat" unless is_plain_arrayref($stat);
my $metric = "catalyst.stats." . $stat->[1] . ".time";
$metric =~ s/[^\w\-_]+/./g;
return $metric;
}
around log_stats => sub ( $next, $c ) {
state $config = $c->config->{'Plugin::Statsd'} // {};
if ( my $client = $c->statsd_client) {
my $stat = [ -1, "catalyst.response.time", $c->stats->elapsed ];
my $metric = $c->statsd_metric_name_filter($stat) or next;
$client->timing_ms( "catalyst.response.time",
ceil( $stat->[2] * 1000 ) );
foreach my $stat ( $c->stats->report ) {
my $metric = $c->statsd_metric_name_filter($stat) or next;
my $timing = ceil( $stat->[2] * 1000 );
$client->timing_ms( $metric, $timing );
}
}
my $disabled = $config->{disable_stats_report} // !$c->debug;
$c->$next unless $disabled;
};
around finalize => sub ( $next, $c ) {
if (my $client = $c->statsd_client) {
if ($c->can("sessionid") && (my $id = $c->sessionid)) {
$client->set_add("catalyst.sessionid", "$id");
}
# Plack::Middleware::Session
( run in 2.743 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )