| 1 | package PerlGuard::Agent::Frameworks::CGIApplication; | ||
| 2 | use base 'Exporter'; | ||
| 3 | |||
| 4 | use PerlGuard::Agent; | ||
| 5 | |||
| 6 | BEGIN { | ||
| 7 | $PerlGuard::Agent::Frameworks::CGIApplication::VERSION = '1.00'; | ||
| 8 | } | ||
| 9 | |||
| 10 | @EXPORT = qw( | ||
| 11 | perlguard_config | ||
| 12 | perlguard_agent | ||
| 13 | ); | ||
| 14 | |||
| 15 | # register a callback to the standard CGI::Application hooks | ||
| 16 | # one of 'init', 'prerun', 'postrun', 'teardown' or 'load_tmpl' | ||
| 17 | |||
| 18 | sub perlguard_config { | ||
| 19 | my $self = shift; | ||
| 20 | my $config = shift; | ||
| 21 | |||
| 22 | $self->{'PerlGuard::Agent::Frameworks::CGIApplication::perlguard_config'} = $config; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub perlguard_agent { | ||
| 26 | my $self = shift; | ||
| 27 | |||
| 28 | if (not defined($self->{'PerlGuard::Agent::Frameworks::CGIApplication::perlguard_agent'})) { | ||
| 29 | my $agent = PerlGuard::Agent->new( $self->{'PerlGuard::Agent::Frameworks::CGIApplication::perlguard_config'} ); | ||
| 30 | |||
| 31 | $agent->detect_monitors(); | ||
| 32 | $agent->start_monitors(); | ||
| 33 | |||
| 34 | #warn "Creating fresh perlguard agent"; | ||
| 35 | |||
| 36 | $self->{'PerlGuard::Agent::Frameworks::CGIApplication::perlguard_agent'} = $agent; | ||
| 37 | } | ||
| 38 | |||
| 39 | return $self->{'PerlGuard::Agent::Frameworks::CGIApplication::perlguard_agent'}; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub import { | ||
| 43 | my $c = scalar(caller); | ||
| 44 | |||
| 45 | $c->add_callback('init', sub { | ||
| 46 | |||
| 47 | }); | ||
| 48 | |||
| 49 | $c->add_callback('prerun', sub { | ||
| 50 | my $controller_instance = shift; | ||
| 51 | |||
| 52 | #warn "Creating new profile"; | ||
| 53 | $controller_instance->{'PerlGuard::Profile'} = &perlguard_agent($controller_instance)->create_new_profile(); | ||
| 54 | $controller_instance->{'PerlGuard::Profile'}->start_recording(); | ||
| 55 | $PerlGuard::Agent::CURRENT_PROFILE_UUID = $controller_instance->{'PerlGuard::Profile'}->uuid(); | ||
| 56 | }); | ||
| 57 | |||
| 58 | $c->add_callback('postrun', sub { | ||
| 59 | my $controller_instance = shift; | ||
| 60 | my $profile = $controller_instance->{'PerlGuard::Profile'}; | ||
| 61 | |||
| 62 | # my $handle; | ||
| 63 | # open ($handle,'>>','/tmp/cgiapp') or die("Cant open /tmp/cgiapp"); | ||
| 64 | |||
| 65 | # print $handle "\n\n\n==============\n\n\n"; | ||
| 66 | |||
| 67 | |||
| 68 | # use Data::Dumper; | ||
| 69 | # print $handle Dumper $controller_instance->query; | ||
| 70 | # print $handle "\n" . $controller_instance->query->self_url; | ||
| 71 | # print $handle "\n" . $controller_instance->query->request_method; | ||
| 72 | # print $handle "\n" . ref($controller_instance); | ||
| 73 | # print $handle "\n" . $controller_instance->get_current_runmode; | ||
| 74 | |||
| 75 | |||
| 76 | $profile->url( $controller_instance->query->self_url ); | ||
| 77 | $profile->http_method( $controller_instance->query->request_method ); | ||
| 78 | $profile->controller( ref($controller_instance) ); | ||
| 79 | |||
| 80 | #For paul lets update the controller action to include the HTTP method | ||
| 81 | $profile->controller_action( $controller_instance->get_current_runmode . "_" . uc($controller_instance->query->request_method) ); | ||
| 82 | |||
| 83 | |||
| 84 | if( my $cross_application_tracing_id = $controller_instance->query->http("X-PerlGuard-Auto-Track") ) { | ||
| 85 | $profile->cross_application_tracing_id($cross_application_tracing_id); | ||
| 86 | } | ||
| 87 | |||
| 88 | $profile->finish_recording(); | ||
| 89 | $controller_instance->{'PerlGuard::Profile'} = undef; | ||
| 90 | $profile->save; | ||
| 91 | }); | ||
| 92 | |||
| 93 | $c->add_callback('teardown', sub { | ||
| 94 | |||
| 95 | }); | ||
| 96 | |||
| 97 | goto &Exporter::import | ||
| 98 | } | ||
| 99 |