NAME

Plack::Middleware::Statsd - send statistics to statsd

VERSION

version v0.4.0

SYNOPSIS

use Plack::Builder;
use Net::Statsd::Tiny;

builder {

  enable "Statsd",
    client      => Net::Statsd::Tiny->new( ... ),
    sample_rate => 1.0;

  ...

  sub {
    my ($env) = @_;

    # Send statistics via other middleware

    if (my $stats = $env->{'psgix.monitor.statsd'}) {

      $stats->increment('myapp.wibble');

    }


  };

};

DESCRIPTION

This middleware gathers metrics from the application send sends them to a statsd server.

ATTRIBUTES

client

This is a statsd client, such as an instance of Net::Statsd::Tiny.

It is required.

psgix.monitor.statsd will be set to the current client if it is not set.

The only restriction on the client is that it has the same API as Net::Statsd::Tiny or similar modules, by supporting the following methods:

This has been tested with Net::Statsd::Lite and Net::Statsd::Client.

Other statsd client modules may be used via a wrapper class.

sample_rate

The default sampling rate to be used, which should be a value between 0 and 1. This will override the default rate of the "client", if there is one.

The default is 1.

histogram

This is a code reference to a wrapper around the "client" timing method. You do not need to set this unless you want to override it.

It takes as arguments the Plack environment and the arguments to pass to the client method, and calls that method. If there are errors then it attempts to log them.

increment

This is a code reference to a wrapper around the "client" increment method. You do not need to set this unless you want to override it.

It takes as arguments the Plack environment and the arguments to pass to the client method, and calls that method. If there are errors then it attempts to log them.

set_add

This is a code reference to a wrapper around the "client" set_add method. You do not need to set this unless you want to override it.

It takes as arguments the Plack environment and the arguments to pass to the client method, and calls that method. If there are errors then it attempts to log them.

METRICS

The following metrics are logged:

If you want to rename these, or modify sampling rates, then you will need to use a wrapper class for the "client".

EXAMPLES

Using from Catalyst

You can access the configured statsd client from Catalyst:

sub finalize {
  my $c = shift;

  if (my $statsd = $c->req->env->{'psgix.monitor.statsd'}) {
    ...


  }

  $c->next::method(@_);
}

Alternatively, you can use Catalyst::Plugin::Statsd.

KNOWN ISSUES

Non-standard HTTP status codes

If your application is returning a status code that is not handled by HTTP::Status, then the metrics may not be logged for that reponse.

SEE ALSO

Net::Statsd::Client

Net::Statsd::Tiny

PSGI

SOURCE

The development version is on github at https://github.com/robrwo/Plack-Middleware-Statsd and may be cloned from git://github.com/robrwo/Plack-Middleware-Statsd.git

BUGS

Please report any bugs or feature requests on the bugtracker website https://github.com/robrwo/Plack-Middleware-Statsd/issues

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

AUTHOR

Robert Rothenberg rrwo@cpan.org

The initial development of this module was sponsored by Science Photo Library https://www.sciencephoto.com.

COPYRIGHT AND LICENSE

This software is Copyright (c) 2018-2020 by Robert Rothenberg.

This is free software, licensed under:

The Artistic License 2.0 (GPL Compatible)