| 1 | package Catalyst::Plugin::Profile::DBI::Log; | ||||||||||||
| 2 | # ABSTRACT: Capture queries executed during a Catalyst route with DBI::Log | ||||||||||||
| 3 | |||||||||||||
| 4 | 1 | our $VERSION = '0.03'; # VERSION (maintained by DZP::OurPkgVersion) | |||||||||||
| 5 | |||||||||||||
| 6 | 1 | ▶ | use Moose::Role; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | ▶ | use namespace::autoclean; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 1 | ▶ | use CatalystX::InjectComponent; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 1 | ▶ | use Data::UUID; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 1 | ▶ | use DateTime; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 1 | ▶ | use JSON; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 1 | ▶ | use Path::Tiny; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 1 | ▶ | use Time::HiRes; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | # Load DBI::Log, but point it at /dev/null to start with; we'll give it a | ||||||||||||
| 17 | # new filehandle at the beginning of each request. | ||||||||||||
| 18 | 1 | ▶ | use DBI::Log timing => 1, trace => 1, format => 'json', file => '/dev/null'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | |||||||||||||
| 20 | |||||||||||||
| 21 | # Ick - find a better way to pass this around than a global var! | ||||||||||||
| 22 | 1 | my $dbilog_output_dir; | |||||||||||
| 23 | my $dbilog_open_failed; | ||||||||||||
| 24 | |||||||||||||
| 25 | 0 | ▶ | after 'setup_finalize' => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||
| 26 | 0 | my $self = shift; | |||||||||||
| 27 | |||||||||||||
| 28 | 0 | ▶ | my $conf = $self->config->{'Plugin::Profile::DBI::Log'} || {}; | ||||||||||
|
Condition0%0/2
$self->config->{'Plugin::Profile::DBI::Log'} || {}
MC/DC0%0/1
$self->config->{'Plugin::Profile::DBI::Log'} || {}
$self->config->{'Plugin::Profile::DBI::Log'}
Truth table0%0/2
A
A= $self->config->{'Plugin::Profile::DBI::Log'}
| |||||||||||||
| 29 | 0 | ▶ | $dbilog_output_dir = $conf->{dbilog_out_dir} || 'dbilog_output'; | ||||||||||
|
Condition0%0/2
$conf->{'dbilog_out_dir'} || 'dbilog_output'
MC/DC0%0/1
$conf->{'dbilog_out_dir'} || 'dbilog_output'
$conf->{'dbilog_out_dir'}
Truth table0%0/2
A
A= $conf->{'dbilog_out_dir'}
| |||||||||||||
| 30 | |||||||||||||
| 31 | 0 | ▶ | if (!-d $dbilog_output_dir) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 32 | 0 | $self->log->debug("Creating DBI::Log output dir $dbilog_output_dir"); | |||||||||||
| 33 | 0 | Path::Tiny::path($dbilog_output_dir)->mkpath; | |||||||||||
| 34 | } else { | ||||||||||||
| 35 | 0 | $self->log->debug("OK, using DBI::Log output dir $dbilog_output_dir"); | |||||||||||
| 36 | } | ||||||||||||
| 37 | 1 | }; | |||||||||||
| 38 | |||||||||||||
| 39 | 0 | ▶ | after 'setup_components' => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 40 | 0 | my $class = shift; | |||||||||||
| 41 | 0 | $class->log->debug("Inject controller into $class"); | |||||||||||
| 42 | 0 | CatalystX::InjectComponent->inject( | |||||||||||
| 43 | into => $class, | ||||||||||||
| 44 | component => 'Catalyst::Plugin::Profile::DBI::Log::Controller::ControlProfiling', | ||||||||||||
| 45 | as => 'Controller::DBI::Log' | ||||||||||||
| 46 | ); | ||||||||||||
| 47 | 1 | }; | |||||||||||
| 48 | |||||||||||||
| 49 | # Start a profile run when a request begins... | ||||||||||||
| 50 | # FIXME: is this the best hook? Want the Catalyst equivalent of a Dancer | ||||||||||||
| 51 | # `before` hook. `prepare_body` looks like a reasonable "we've read the | ||||||||||||
| 52 | # request from the network, we're about to handle it" point. | ||||||||||||
| 53 | 0 | ▶ | after 'prepare_body' => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||
| 54 | 0 | my $c = shift; | |||||||||||
| 55 | |||||||||||||
| 56 | # We want to name all profile outputs safely and usefully, encoding | ||||||||||||
| 57 | # the request method, path, and timestamp, and a random number for some | ||||||||||||
| 58 | # uniqueness. | ||||||||||||
| 59 | 0 | ▶ | my $path = $c->request->method . '_' . ($c->request->path || '/'); | ||||||||||
|
Condition0%0/2
$c->request->path || '/'
MC/DC0%0/1
$c->request->path || '/'
$c->request->path
Truth table0%0/2
A
A= $c->request->path
| |||||||||||||
| 60 | 0 | $path =~ s{/}{_s_}g; | |||||||||||
| 61 | 0 | $path =~ s{[^a-z0-9]}{_}gi; | |||||||||||
| 62 | 0 | $path .= "_t_" . DateTime->now->strftime('%Y-%m-%d_%H:%M:%S'); | |||||||||||
| 63 | 0 | $path .= substr Data::UUID->new->create_str, 0, 8; | |||||||||||
| 64 | 0 | $path = Path::Tiny::path($dbilog_output_dir, $path); | |||||||||||
| 65 | open my $dbilog_fh, ">", $path | ||||||||||||
| 66 | 0 | ▶ | or do { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 67 | 0 | $c->log->warn("Can't open $path to write - $!"); | |||||||||||
| 68 | 0 | $dbilog_open_failed = 1; | |||||||||||
| 69 | 0 | return; | |||||||||||
| 70 | }; | ||||||||||||
| 71 | |||||||||||||
| 72 | 0 | $dbilog_open_failed = 0; | |||||||||||
| 73 | |||||||||||||
| 74 | # Write our metadata to the log first | ||||||||||||
| 75 | 0 | print {$dbilog_fh} JSON::to_json( | |||||||||||
| 76 | { | ||||||||||||
| 77 | logged_by => __PACKAGE__ . "/$VERSION", | ||||||||||||
| 78 | method => $c->request->method, | ||||||||||||
| 79 | path => $c->request->path, | ||||||||||||
| 80 | path_query => $c->request->uri->path_query, | ||||||||||||
| 81 | ip => $c->request->address, | ||||||||||||
| 82 | user_agent => $c->request->user_agent, | ||||||||||||
| 83 | start_timestamp => Time::HiRes::time(), | ||||||||||||
| 84 | } | ||||||||||||
| 85 | ) . "\n"; | ||||||||||||
| 86 | 0 | $DBI::Log::opts{fh} = $dbilog_fh; | |||||||||||
| 87 | 0 | $DBI::Log::opts{file} = $path; | |||||||||||
| 88 | 1 | }; | |||||||||||
| 89 | |||||||||||||
| 90 | |||||||||||||
| 91 | # And finalise it when the request is finished | ||||||||||||
| 92 | 0 | ▶ | after 'finalize_body' => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=21.1
| |||||||||||||
| 93 | 0 | my $c = shift; | |||||||||||
| 94 | 0 | $c->log->debug("finalize_body fired, stop profiling"); | |||||||||||
| 95 | |||||||||||||
| 96 | 0 | ▶ | return if $dbilog_open_failed; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 97 | |||||||||||||
| 98 | # Make sure the file has been flushed before we do anything | ||||||||||||
| 99 | 0 | $DBI::Log::opts{fh}->flush(); | |||||||||||
| 100 | |||||||||||||
| 101 | # Want to know how many queries were logged; if there were none, then | ||||||||||||
| 102 | # there's no point keeping the log, so we should delete it. | ||||||||||||
| 103 | 0 | seek $DBI::Log::opts{fh}, 0, 0; | |||||||||||
| 104 | 0 | ▶ | my $metadata_json = <$DBI::Log::opts{fh}>; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||
| 105 | 0 | my $first_query = <$DBI::Log::opts{fh}>; | |||||||||||
| 106 | 0 | ▶ | if (!$first_query) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 107 | 0 | $c->log->debug("No queries logged, delete file"); | |||||||||||
| 108 | 0 | unlink $DBI::Log::opts{file}; | |||||||||||
| 109 | } | ||||||||||||
| 110 | |||||||||||||
| 111 | 0 | close $DBI::Log::opts{fh}; | |||||||||||
| 112 | 1 | }; | |||||||||||
| 113 | |||||||||||||
| 114 | |||||||||||||
| 115 | 1 | 1; | |||||||||||
| 116 | |||||||||||||
| 117 | =head1 NAME | ||||||||||||
| 118 | |||||||||||||
| 119 | Catalyst::Plugin::Profile::DBI::Log - per-request DB query logging & profiling | ||||||||||||
| 120 | |||||||||||||
| 121 | =head1 SYNOPSIS | ||||||||||||
| 122 | |||||||||||||
| 123 | Load the plugin like any other Catalyst plugin e.g. | ||||||||||||
| 124 | |||||||||||||
| 125 | use Catalyst qw(Profile::DBI::Log); | ||||||||||||
| 126 | |||||||||||||
| 127 | hit your app with some requests, then point your browser at C</dbi/log/index> | ||||||||||||
| 128 | and you'll see a list of HTTP requests handled, along with info on how many | ||||||||||||
| 129 | queries they ran and how long they spent waiting for the DB, with a clickable | ||||||||||||
| 130 | link to view the actual queries and stack trace of where they came from. | ||||||||||||
| 131 | |||||||||||||
| 132 | =head1 DESCRIPTION | ||||||||||||
| 133 | |||||||||||||
| 134 | I needed a way to quickly and easily see, for each API route invocation (HTTP | ||||||||||||
| 135 | request) my app handled, | ||||||||||||
| 136 | |||||||||||||
| 137 | =over | ||||||||||||
| 138 | |||||||||||||
| 139 | =item How many DB queries were performed | ||||||||||||
| 140 | |||||||||||||
| 141 | =item How long we spent waiting for DB queries | ||||||||||||
| 142 | |||||||||||||
| 143 | =item What the actual queries executed were and how long each took | ||||||||||||
| 144 | |||||||||||||
| 145 | =item Where in our codebase those queries were performed | ||||||||||||
| 146 | |||||||||||||
| 147 | =back | ||||||||||||
| 148 | |||||||||||||
| 149 | This plugin is designed to simplify just that. | ||||||||||||
| 150 | |||||||||||||
| 151 | When loaded, it arranges for L<DBI::Log> to log all queries to log files, | ||||||||||||
| 152 | while adding some metadata of our own to identify the HTTP request being | ||||||||||||
| 153 | processed. It adds a route handler to provide routes to list requests | ||||||||||||
| 154 | profiled along with summary info (how many queries, how long spent waiting on | ||||||||||||
| 155 | queries etc), and clickable links to view all the queries performed, and to | ||||||||||||
| 156 | view a stack trace of where the query was performed from (to see easily what | ||||||||||||
| 157 | part of your codebase triggered it). | ||||||||||||
| 158 | |||||||||||||
| 159 | |||||||||||||
| 160 | =head1 SEE ALSO | ||||||||||||
| 161 | |||||||||||||
| 162 | There are a couple of prior art examples which capture stats from DBIC | ||||||||||||
| 163 | using L<DBIx::Class::Storage::Statistics> such as L<Catalyst::Plugin::DBIC::Profiler> | ||||||||||||
| 164 | but parts of one of our apps, for hairy legacy reasons also go direct to the | ||||||||||||
| 165 | DB with DBI, so we needed to catch those too - and wanted a useful way to | ||||||||||||
| 166 | see the list of profiled requests right in the browser. | ||||||||||||
| 167 | |||||||||||||
| 168 | |||||||||||||
| 169 | =head1 SECURITY | ||||||||||||
| 170 | |||||||||||||
| 171 | This is a development tool. It captures, records and serves up raw SQL queries | ||||||||||||
| 172 | which may well contain sensitive information - e.g. parameters used to search | ||||||||||||
| 173 | the DB, etc. I would not recommend loading it on a production site or exposing | ||||||||||||
| 174 | an app with it loaded to the Internet. | ||||||||||||
| 175 | |||||||||||||
| 176 | =head1 AUTHOR | ||||||||||||
| 177 | |||||||||||||
| 178 | David Precious (BIGPRESH) C<< <davidp@preshweb.co.uk> >> | ||||||||||||
| 179 | |||||||||||||
| 180 | =head1 COPYRIGHT AND LICENCE | ||||||||||||
| 181 | |||||||||||||
| 182 | Copyright (C) 2024 by David Precious | ||||||||||||
| 183 | |||||||||||||
| 184 | This library is free software; you can redistribute it and/or modify it | ||||||||||||
| 185 | under the same terms as Perl itself. | ||||||||||||
| 186 | |||||||||||||
| 187 | =cut | ||||||||||||