Plack::Middleware::StatsPerRequest - Measure HTTP stats on each request
version 0.902
use Plack::Builder; use Measure::Everything::Adapter; Measure::Everything::Adapter->set('InfluxDB::File', { file => '/tmp/yourapp.stats', }); builder { enable "Plack::Middleware::StatsPerRequest", app_name => 'YourApp', ; $app; }; # curl http://localhost:3000/some/path # cat /tmp/yourapp.stats http_request,app=YourApp,method=GET,path=/some/path,status=400 hit=1i,request_time=0.02476 1519658691411352000
Plack::Middleware::StatsPerRequest lets you collect stats about all your HTTP requests via Measure::Everything. Plack::Middleware::StatsPerRequest calculates the duration of a requests and collects some additonal data like request path, HTTP method and response status.
Plack::Middleware::StatsPerRequest
You can then use this data to plot some nice graps, find bottlenecks or set up alerts; or do anything else your stats toolkit supports.
enable "Plack::Middleware::StatsPerRequest", metric_name => 'http', app_name => 'YourApp', path_cleanups => [ \&your_custom_cleanup, \&another_cleanup ], add_headers => [ qw( Accept-Language X-Requested-With )], long_request => 3 ;
The name of the metric generated. Defaults to http_request.
http_request
The name of your application. Defaults to unknown.
unknown
app_name will be added to each metric as a tag.
app_name
A list of functions to be called to transform / cleanup the request path. Defaults to [ 'replace_idish' ].
[ 'replace_idish' ]
Set to an empty list to do no path cleanups. This is not recommended, unless your statistic tool can normalize paths which might include a lot of distinct ids etc; or your app does not include ids in its URLs (maybe they are all passed via query params?)
See replace_idish for more info on the default path cleanup handler.
A list of HTTP header fields. Default to [ ] (empty list).
[ ]
If you use add_headers, all HTTP headers matching the ones provided will be added as a tag, with the respective header values as the tag values.
add_headers
enable "Plack::Middleware::StatsPerRequest", add_headers => [ 'Accept-Language' ]; # header_accept-language=Accept-Language
If a header is not sent by a client, a value of not_set will be reported.
not_set
Checks if a HTTP header is set, and adds a tag containing 1 or 0. This makes sense if you just what to count if a header was sent, but don't care about it's content (eg a bearer token):
enable "Plack::Middleware::StatsPerRequest", has_headers => [ 'Authorization' ]; # has_header_authorization=1
Requests taking longer than long_request seconds will be logged as a warning. Defaults to 5 seconds.
long_request
warning
5
Set to 0 to turn off.
0
enable "Plack::Middleware::StatsPerRequest", long_request => 10; # curl http://localhost/very/slow/endpoint # cat /log/warnings Long request, took 23.042: GET /very/slow/endpoint
my $clean = Plack::Middleware::StatsPerRequest::replace_idish( $dirty );
Takes a URI path and replaces things that look like ids with fixed strings, so you can calc proper stats on the generic paths.
This is the default path_cleanups action, so unless you specify your own, or explicitly set path_cleanups to an empty array, the following transformations will be done on the path:
All path fragments looking like a SHA1 checksum are replaced by :sha1.
:sha1
All path fragments looking like a UUID are replaced by :uuid.
:uuid
Any part of the path consisting of 6 or more digits is replaced by :int.
:int
A llpath fragments consisting solely of digits are also replaced by :int.
All path fragments looking like hex are replaced by :hex.
:hex
All path fragments longer than 55 characters are replaced by :long.
:long
A chain of path fragments looking like hex-code is replaced by :hexpath.
:hexpath
All path fragments looking like an email message id (as generated by one of our tools) are replaced by :msgid.
:msgid
All path fragments looking like 300x200 are replaced by :imgdim. (Of course this happens for all formats, not just 300 and 200).
300x200
:imgdim
For details, please inspect the source code and t/20_replace_idish.t.
These transformations proved useful in the two years we used Plack::Middleware::StatsPerRequest in house. If you have any additions or change requests, just tell us!
Measure::Everything is used to actually report the stats
Log::Any is used for logging.
Thanks to
validad.com for supporting Open Source.
Thomas Klausner <domm@plix.at>
This software is copyright (c) 2018 - 2021 by Thomas Klausner.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Plack::Middleware::StatsPerRequest, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Plack::Middleware::StatsPerRequest
CPAN shell
perl -MCPAN -e shell install Plack::Middleware::StatsPerRequest
For more information on module installation, please visit the detailed CPAN module installation guide.