| 1 | package DashProfiler::Apache; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Carp; | ||
| 6 | |||
| 7 | use base qw(DashProfiler); | ||
| 8 | |||
| 9 | our $VERSION = sprintf("1.%06d", q$Revision: 43 $ =~ /(\d+)/o); | ||
| 10 | our $trace = 0; | ||
| 11 | |||
| 12 | use constant MP2 => ( | ||
| 13 | ($ENV{MOD_PERL_API_VERSION}||0) >= 2 | ||
| 14 | or eval "require Apache2::ServerUtil; Apache2::ServerUtil::server_root()" ## no critic | ||
| 15 | ); | ||
| 16 | |||
| 17 | BEGIN { | ||
| 18 | if (MP2) { | ||
| 19 | require Apache2::ServerUtil; | ||
| 20 | require Apache2::Const; | ||
| 21 | Apache2::Const->import(qw(OK DECLINED)); | ||
| 22 | |||
| 23 | warn "set_precondition call needs work for mod_perl2"; # see below | ||
| 24 | } | ||
| 25 | else { | ||
| 26 | require Apache; | ||
| 27 | require Apache::Constants; | ||
| 28 | Apache::Constants->import(qw(OK DECLINED)); | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 32 | my $server = eval { | ||
| 33 | (MP2) ? Apache2::ServerUtil->server : Apache->server; | ||
| 34 | }; | ||
| 35 | # warn if we couldn't get a server object, unless were just testing | ||
| 36 | warn $@ if not $server | ||
| 37 | and not ($ENV{HARNESS_VERSION} and $ENV{PERL_DL_NONLAZY}); | ||
| 38 | |||
| 39 | |||
| 40 | =head1 NAME | ||
| 41 | |||
| 42 | DashProfiler::Apache - Hook DashProfiler into Apache mod_perl (v1 or v2) | ||
| 43 | |||
| 44 | =head1 SYNOPSIS | ||
| 45 | |||
| 46 | To hook DashProfiler into Apache you add this to your httpd.conf: | ||
| 47 | |||
| 48 | PerlModule DashProfiler::Apache; | ||
| 49 | PerlInitHandler DashProfiler::Apache::start_sample_period_all_profiles | ||
| 50 | PerlCleanupHandler DashProfiler::Apache::end_sample_period_all_profiles | ||
| 51 | PerlChildExitHandler DashProfiler::Apache::flush_all_profiles | ||
| 52 | |||
| 53 | You'll also need to define at least one profile. An easy way of doing that | ||
| 54 | is to use DashProfiler::Auto to get a predefined profile called 'auto': | ||
| 55 | |||
| 56 | PerlModule DashProfiler::Auto; | ||
| 57 | |||
| 58 | Or you can define your own, like this: | ||
| 59 | |||
| 60 | PerlModule DashProfiler::Apache; | ||
| 61 | <Perl> | ||
| 62 | DashProfile->add_profile( foo => { ... } ); | ||
| 63 | </Perl> | ||
| 64 | |||
| 65 | =head1 DESCRIPTION | ||
| 66 | |||
| 67 | The DashProfiler module itself will work just fine with Apache. | ||
| 68 | The DashProfiler::Apache just fine-tunes the integration in a few ways: | ||
| 69 | |||
| 70 | B<*> Sets a precondition on start_sample_period_all_profiles() so that it only | ||
| 71 | starts a period for 'initial' requests (where $r->is_initial_req is true). | ||
| 72 | This is typically only relevant if your code uses $r->internal_redirect. | ||
| 73 | |||
| 74 | B<*> Adds a simple trace mechanism so you can easily see which | ||
| 75 | DashProfiler::Apache functions are called for which Apache handlers. | ||
| 76 | |||
| 77 | =head2 Example Apache mod_perl Configuration | ||
| 78 | |||
| 79 | PerlModule DashProfiler::Apache; | ||
| 80 | PerlInitHandler DashProfiler::Apache::start_sample_period_all_profiles | ||
| 81 | PerlCleanupHandler DashProfiler::Apache::end_sample_period_all_profiles | ||
| 82 | PerlChildExitHandler DashProfiler::Apache::flush_all_profiles | ||
| 83 | <Perl> | ||
| 84 | # files will be written to $spool_directory/dashprofiler.subsys.ppid.pid | ||
| 85 | DashProfiler->add_profile('subsys', { | ||
| 86 | granularity => 30, | ||
| 87 | flush_interval => 60, | ||
| 88 | add_exclusive_sample => 'other', | ||
| 89 | spool_directory => '/tmp', # needs write permission for apache user | ||
| 90 | }); | ||
| 91 | </Perl> | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | DashProfiler->set_precondition( | ||
| 96 | start_sample_period_all_profiles => sub { | ||
| 97 | # we only want to start a period for 'initial' requests | ||
| 98 | # because we only end them in PerlCleanupHandler and that's only | ||
| 99 | # called for initial requests | ||
| 100 | my $r = (MP2) ? undef : Apache->request; | ||
| 101 | my $is_initial_req = $r->is_initial_req; | ||
| 102 | _trace(sprintf "start precondition = %d (main %d, prev %d)", | ||
| 103 | $is_initial_req, $r->is_main, $r->prev?1:0) | ||
| 104 | if $trace; | ||
| 105 | return $is_initial_req; | ||
| 106 | } | ||
| 107 | ); | ||
| 108 | |||
| 109 | |||
| 110 | sub _trace { | ||
| 111 | return unless $trace; | ||
| 112 | my $r = (MP2) ? undef : Apache->request; | ||
| 113 | my $current_callback = ($r) ? "r$$r ".$r->current_callback." " : ""; | ||
| 114 | my $uri = $r->the_request; | ||
| 115 | print STDERR "${current_callback}@_ $uri\n"; | ||
| 116 | } | ||
| 117 | |||
| 118 | |||
| 119 | sub start_sample_period_all_profiles { | ||
| 120 | _trace("start_sample_period_all_profiles") if $trace; | ||
| 121 | DashProfiler->start_sample_period_all_profiles(); | ||
| 122 | return DECLINED; | ||
| 123 | } | ||
| 124 | |||
| 125 | |||
| 126 | sub end_sample_period_all_profiles { | ||
| 127 | _trace("end_sample_period_all_profiles") if $trace; | ||
| 128 | DashProfiler->end_sample_period_all_profiles(); | ||
| 129 | return DECLINED; | ||
| 130 | } | ||
| 131 | |||
| 132 | |||
| 133 | sub flush_all_profiles { | ||
| 134 | _trace("flush_all_profiles") if $trace; | ||
| 135 | DashProfiler->flush_all_profiles(); | ||
| 136 | return DECLINED; | ||
| 137 | } | ||
| 138 | |||
| 139 | |||
| 140 | sub reset_all_profiles { | ||
| 141 | _trace("reset_all_profiles") if $trace; | ||
| 142 | DashProfiler->reset_all_profiles(); | ||
| 143 | return DECLINED; | ||
| 144 | } | ||
| 145 | |||
| 146 | |||
| 147 | 1; | ||
| 148 | |||
| 149 | =head1 AUTHOR | ||
| 150 | |||
| 151 | DashProfiler by Tim Bunce, L<http://www.tim.bunce.name> and | ||
| 152 | L<http://blog.timbunce.org> | ||
| 153 | |||
| 154 | =head1 COPYRIGHT | ||
| 155 | |||
| 156 | The DashProfiler distribution is Copyright (c) 2007-2008 Tim Bunce. Ireland. | ||
| 157 | All rights reserved. | ||
| 158 | |||
| 159 | You may distribute under the terms of either the GNU General Public | ||
| 160 | License or the Artistic License, as specified in the Perl README file. | ||
| 161 | |||
| 162 | =cut | ||
| 163 |