Agent/CGI.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« CGI/DBIx/Tracer.pm ↑ Summary
1 package SlapbirdAPM::Agent::CGI;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use CGI;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use LWP::UserAgent;
9 use POSIX ();
10 use SlapbirdAPM::CGI::DBIx::Tracer;
11 use IO::Tee;
12 use IO::Pipe;
13 use Time::HiRes;
14 use HTTP::Request;
15 use HTTP::Response;
16 use System::Info;
17 use JSON;
18  
19 $Carp::Internal{__PACKAGE__} = 1;
20  
21 our $VERSION = '0.05';
22  
23 my %request_headers;
24 our $cgi = CGI->new();
25 our $handler;
26 our $start_time;
27 our @error;
28 our $queries = [];
29 our $key;
30 pipe( our $reader, our $writer );
31  
32 SlapbirdAPM::CGI::DBIx::Tracer->new(
33 sub {
34 my %args = @_;
35 push @$queries, { sql => $args{sql}, total_time => $args{time} };
36 }
37 );
38  
39 sub import {
40 $key = $ENV{SLAPBIRDAPM_API_KEY};
41  
42 if ( !$key ) {
43 warn(
44 "Your SlapbirdAPM key is not set, set the SLAPBIRDAPM_API_KEY environment variable to use SlapbirdAPM."
45 );
46 return;
47 }
48  
49 $start_time = time * 1_000;
50 ($handler) = caller;
51 %request_headers = map { $_ => $cgi->http($_) } $cgi->http();
52 local *tee = IO::Tee->new( $writer, *STDOUT{IO} );
53  
54 *{OLD_STDOUT} = *STDOUT{IO};
55 *{STDOUT} = *tee;
56  
57 $SIG{__DIE__} = sub {
58 @error = @_;
59 };
60  
61 return;
62 }
63  
64 END {
65 if ( !$key ) {
66 close($writer);
67 close($reader);
68 return;
69 }
70  
71 my $end_time = time * 1_000;
72  
73 close($writer);
74 my $raw_response = do {
75 local $/ = undef;
76 <$reader>;
77 };
78 close($reader);
79  
80 if ( $raw_response !~ /^HTTP\/\d+\s\d+\s[A-Za-z].*/mxi ) {
81 if ( !@error ) {
82 $raw_response = "HTTP/1.1 200 OK\r\n$raw_response";
83 }
84 else {
85 $raw_response =
86 "HTTP/1.1 500 Internal Server Error\r\n$raw_response";
87 }
88 }
89  
90 local $SIG{CHLD} = 'IGNORE';
91  
92 if ( fork() ) {
93 return;
94 }
95  
96 my $res = HTTP::Response->parse($raw_response);
97  
98 my $slapbird_hash = {
99 type => 'cgi',
100 method => $ENV{REQUEST_METHOD},
101 end_point => $cgi->url( -path_info => 1, -query => 1, -absolute => 1 ),
102 start_time => $start_time,
103 end_time => $end_time,
104 response_code => +$res->code,
105 response_size => $res->header('content-length')
106 // length( $res->content ) // 0,
107 response_headers => +{ $res->headers->flatten() },
108 request_headers => \%request_headers,
109 request_size => $request_headers{HTTP_CONTENT_LENGTH} // 0,
110 error => join( "\n", @error ),
111 requestor => $request_headers{HTTP_X_SLAPBIRD_NAME} // 'UNKNOWN',
112 handler => $handler eq 'main' ? 'CGI' : $handler,
113 stack => [], # We don't trace stacks in CGI, because the overhead
114 os => System::Info->new->os,
115 queries => $queries,
116 num_queries => scalar @$queries
117 };
118  
119 my $ua = LWP::UserAgent->new();
120  
121 my $uri =
122 $ENV{SLAPBIRD_APM_URI}
123 ? $ENV{SLAPBIRD_APM_URI} . '/apm'
124 : 'https://slapbirdapm.com/apm';
125  
126 my $sb_request = HTTP::Request->new( POST => $uri );
127 $sb_request->content_type('application/json');
128 $sb_request->content( encode_json($slapbird_hash) );
129 $sb_request->header( 'x-slapbird-apm' => $key );
130 my $result = $ua->request($sb_request);
131  
132 if ( !$result->is_success ) {
133 warn( "Unable to communicate with Slapbird, got status code: "
134 . $result->code );
135 }
136  
137 POSIX::_exit(0);
138 }
139  
140 1;
141  
142 =pod
143  
144 =encoding utf8
145  
146 =head1 NAME
147  
148 SlapbirdAPM::Agent::CGI
149  
150 The L<SlapbirdAPM|https://www.slapbirdapm.com> user-agent for L<CGI> applications.
151  
152 =head1 SYNOPSIS
153  
154 =over 2
155  
156 =item *
157  
158 Create an application on L<SlapbirdAPM|https://www.slapbirdapm.com>
159  
160 =item *
161  
162 Install this ie C<cpanm SlapbirdAPM::Agent::CGI>, C<cpan -I SlapbirdAPM::Agent::CGI>
163  
164 =item *
165  
166 Add C<use SlapbirdAPM::Agent::CGI> near the top of your L<CGI> script
167  
168 =item *
169  
170 Add your API key to your environment, in Apache that looks like: C<SetEnv SLAPBIRDAPM_API_KEY ...>
171  
172 =item *
173  
174 Restart your web-server
175  
176 =back
177  
178 =head1 EXAMPLE
179  
180 #!/usr/bin/env perl
181
182 use strict;
183 use warnings;
184
185 use DBI;
186 use SlapbirdAPM::Agent::CGI;
187 use CGI;
188
189 my $dbh = DBI->connect('dbi:SQLite:dbname=file.db', '', '');
190
191 my $sth = $dbh->prepare(q[select time('now');]);
192 $sth->execute();
193 my $time = $sth->fetch->[0];
194 my $response = 'Hello World! It is ' . $time . " o'clock";
195
196 my $cgi = CGI->new();
197
198 print $cgi->header();
199 print <<"END"
200 <!DOCTYPE html>
201 <html>
202 <body>
203 <p>$response</p>
204 </body>
205 </html>
206 END
207  
208 =head1 SEE ALSO
209  
210 L<SlapbirdAPM::Agent::Plack>
211  
212 L<SlapbirdAPM::Agent::Mojo>
213  
214 L<SlapbirdAPM::Agent::Dancer2>
215  
216 =head1 AUTHOR
217  
218 Mollusc Software Solutions (formerly Mollusc Labs), C<https://github.com/mollusc-labs>
219  
220 =head1 LICENSE
221  
222 SlapbirdAPM::Agent::CGI like all SlapbirdAPM user-agents is licensed under the MIT license.
223  
224 SlapbirdAPM (the website) however, is licensed under the GNU AGPL version 3.0.
225  
226 =cut
« CGI/DBIx/Tracer.pm ↑ Summary