Masatoshi Kawazoe
and 1 contributors


Plack::Middleware::QueryCounter::DBI - DBI query counter per request middleware


Enable this middleware using Plack::Builder.

    use MyApp;
    use Plack::Builder;

    my $app = MyApp->psgi_app;

    builder {
        enable 'QueryCounter::DBI';

You can specify HTTP header using prefix option.

    builder {
        enable 'QueryCounter::DBI', prefix => 'X-MyQueryCounter';


Plack::Middleware::QueryCounter::DBI is a middleware to count SQL query per each HTTP request. Count result outputs on HTTP header.

The counted quieries classify read, write or other query.

You'll get following HTTP headers.

X-QueryCounter-DBI-Total: 20 X-QueryCounter-DBI-Read: 16 X-QueryCounter-DBI-Write: 4 X-QueryCounter-DBI-Other: 0

Then, you can write to access log using nginx.

    log_format ltsv   'host:$remote_addr\t'

LTSV is Labeled Tab-separated Values, see

Additionally, I recommend to remove these header for end-user response.

    location / {
        proxy_hide_header 'X-QueryCounter-DBI-Total';
        proxy_hide_header 'X-QueryCounter-DBI-Read';
        proxy_hide_header 'X-QueryCounter-DBI-Write';
        proxy_hide_header 'X-QueryCounter-DBI-Other';

        proxy_pass http://backend;


Plack Plack::Builder


Copyright (C) Masatoshi Kawazoe (acidlemon).

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


Masatoshi Kawazoe (acidlemon) <>