« blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Plugin/Log/Dispatch/FileRotate.pm
↑ Summary
blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf.pm »
| 1 | package Dwarf::Plugin::Sentry; | ||
| 2 | use Dwarf::Pragma; | ||
| 3 | use Dwarf::Util qw/add_method/; | ||
| 4 | use Carp; | ||
| 5 | use Devel::StackTrace; | ||
| 6 | use Sentry::Raven; | ||
| 7 | |||
| 8 | # load_plugins(Sentry => { dsn => XXXX }); | ||
| 9 | # $c->call_sentry($error); | ||
| 10 | |||
| 11 | sub init { | ||
| 12 | my ($class, $c, $conf) = @_; | ||
| 13 | $conf ||= {}; | ||
| 14 | $conf->{MAX_HTTP_DATA} //= 2048; | ||
| 15 | |||
| 16 | die "dsn must be specified." unless $conf->{dsn}; | ||
| 17 | |||
| 18 | add_method($c, call_sentry => sub { | ||
| 19 | my ($self, $message, %options) = @_; | ||
| 20 | |||
| 21 | chomp($message); | ||
| 22 | |||
| 23 | my $stacktrace = Devel::StackTrace->new(skip_frames => 1); | ||
| 24 | my $sentry = Sentry::Raven->new(sentry_dsn => $conf->{dsn}); | ||
| 25 | |||
| 26 | my %stacktrace_context = $sentry->stacktrace_context($sentry->_get_frames_from_devel_stacktrace($stacktrace)); | ||
| 27 | |||
| 28 | my $req = $self->request; | ||
| 29 | my $hdr = [ map { my $k = $_; map { { $k => $_ } } $req->headers->header($_) } $req->headers->header_field_names ]; | ||
| 30 | my %env = %{ $req->env }; | ||
| 31 | foreach (keys %env) { | ||
| 32 | delete $env{$_} if (not /^(HTTP|REMOTE)_/); | ||
| 33 | } | ||
| 34 | |||
| 35 | my %rc = $sentry->request_context( | ||
| 36 | $req->request_uri, | ||
| 37 | method => $req->method, | ||
| 38 | data => substr($req->content, 0, $conf->{MAX_HTTP_DATA}), | ||
| 39 | cookies => $env{HTTP_COOKIE}, | ||
| 40 | headers => $hdr, | ||
| 41 | env => \%env | ||
| 42 | ); | ||
| 43 | |||
| 44 | my %context = ( | ||
| 45 | culprit => $0, | ||
| 46 | $sentry->exception_context($message), | ||
| 47 | %stacktrace_context, | ||
| 48 | %rc, | ||
| 49 | ); | ||
| 50 | |||
| 51 | if ($options{user_context}) { | ||
| 52 | %context = (%context, $sentry->user_context(%{ $options{user_context} })); | ||
| 53 | } | ||
| 54 | |||
| 55 | my $event_id = $sentry->capture_message($message, %context); | ||
| 56 | |||
| 57 | if (!defined($event_id)) { | ||
| 58 | die "failed to submit event to sentry service:\n" | ||
| 59 | . $c->dump($sentry->_construct_message_event($message, %context)); | ||
| 60 | } | ||
| 61 | }); | ||
| 62 | } | ||
| 63 | |||
| 64 | 1; |