| 1 | package CGI::Application::Plugin::Sentry; | ||
| 2 | use Mojo::Base -base, -signatures; | ||
| 3 | |||
| 4 | use CGI::Application; | ||
| 5 | use Mojo::Util 'dumper'; | ||
| 6 | use Sentry; | ||
| 7 | |||
| 8 | CGI::Application->add_callback( | ||
| 9 | init => sub ($c, %) { | ||
| 10 | Sentry->init({ dsn => 'fixme', release => '1.0.0', dist => '12345', }); | ||
| 11 | } | ||
| 12 | ); | ||
| 13 | |||
| 14 | CGI::Application->add_callback( | ||
| 15 | error => sub ($c, $error) { | ||
| 16 | Sentry->capture_exception($error); | ||
| 17 | } | ||
| 18 | ); | ||
| 19 | |||
| 20 | CGI::Application->add_callback( | ||
| 21 | prerun => sub ($c, $rm) { | ||
| 22 | Sentry::Hub->get_current_hub()->push_scope(); | ||
| 23 | |||
| 24 | my $transaction = Sentry->start_transaction( | ||
| 25 | { name => $rm, op => 'http.server', }, | ||
| 26 | { | ||
| 27 | request => { | ||
| 28 | url => $c->query->url(-full => 1), | ||
| 29 | method => $c->query->request_method, | ||
| 30 | query => { $c->query->Vars }, | ||
| 31 | headers => { map { $_ => $c->query->http($_) } $c->query->http }, | ||
| 32 | env => \%ENV, | ||
| 33 | } | ||
| 34 | } | ||
| 35 | ); | ||
| 36 | |||
| 37 | $c->param('__sentry__transaction', $transaction); | ||
| 38 | |||
| 39 | Sentry->configure_scope(sub ($scope) { | ||
| 40 | $scope->set_span($transaction); | ||
| 41 | }); | ||
| 42 | } | ||
| 43 | ); | ||
| 44 | |||
| 45 | CGI::Application->add_callback( | ||
| 46 | postrun => sub ($c, $body_ref) { | ||
| 47 | my $transaction = $c->param('__sentry__transaction'); | ||
| 48 | $transaction->set_http_status(200); # FIXME | ||
| 49 | $transaction->finish(); | ||
| 50 | } | ||
| 51 | ); | ||
| 52 | |||
| 53 | CGI::Application->add_callback( | ||
| 54 | teardown => sub ($c) { | ||
| 55 | Sentry::Hub->get_current_hub()->pop_scope(); | ||
| 56 | } | ||
| 57 | ); | ||
| 58 | |||
| 59 | 1; |