diff --git a/lib/CF.pm b/lib/CF.pm index fbcce86d70f2e7de7cb00f41c7a47f16003677b1..85a9eb81efb417d39dbd5c764326351157820f58 100644 --- a/lib/CF.pm +++ b/lib/CF.pm @@ -20,6 +20,11 @@ sub startup { } } + $self->plugin('SentrySDK', { + dsn => $cfg->{sentry_dsn}, + traces_sample_rate => 1, + }) if $cfg->{sentry_dsn}; + # migrace schematu my $pg = Mojo::Pg->new ->dsn($cfg->{db_dsn}) diff --git a/lib/Mojolicious/Plugin/SentrySDK.pm b/lib/Mojolicious/Plugin/SentrySDK.pm new file mode 100644 index 0000000000000000000000000000000000000000..83d30b051876d423f1c55d2e04bced6d58a0c35e --- /dev/null +++ b/lib/Mojolicious/Plugin/SentrySDK.pm @@ -0,0 +1,82 @@ +package Mojolicious::Plugin::SentrySDK; +use Mojo::Base 'Mojolicious::Plugin', -signatures; + +use Sentry::SDK; +use Try::Tiny; + +sub register ($self, $app, $conf) { + $app->hook( + before_server_start => sub ($server, $app) { + Sentry::SDK->init($conf); + } + ); + + $app->hook( + around_action => sub ($next, $c, $action, $last) { + return $next->() unless $last; + + my $req = $c->req; + + Sentry::Hub->get_current_hub()->with_scope(sub ($scope) { + my %cookies = map { ($_->name, $_->value) } ($req->cookies // [])->@*; + my $transaction = Sentry::SDK->start_transaction( + { + name => $c->match->endpoint->pattern->unparsed || '/', + op => 'http.server', + request => { + url => $req->url->to_abs->to_string, + cookies => \%cookies, + method => $req->method, + query_string => $req->url->query->to_hash, + headers => $req->headers->to_hash, + env => \%ENV, + }, + }, + ); + + Sentry::SDK->configure_scope(sub ($scope) { + $scope->set_span($transaction); + }); + + try { + $next->(); + } catch { + Sentry::SDK->capture_exception($_); + $c->reply->exception($_) + } finally { + my $status = $c->res->code; + $transaction->set_http_status($status) if $status; + $transaction->finish(); + } + + }); + } + ); +} + +1; + +=encoding utf8 + +=head1 NAME + +Mojolicious::Plugin::SentrySDK - Sentry plugin for Mojolicious + +=head1 SYNOPSIS + +=head1 DESCRIPTION + +=head1 OPTIONS + +=head2 register + + my $config = $plugin->register(Mojolicious->new); + my $config = $plugin->register(Mojolicious->new, \%options); + +Register Sentry in L<Mojolicious> application. + +=head1 SEE ALSO + +L<Sentry::SDK>. + +=cut diff --git a/openapi.yaml b/openapi.yaml index 7492694e49b0a48b58fe6c396688085aa12e4eb4..7b928f44250a43c2608bc500df9520ef88b2b0f9 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -1,7 +1,7 @@ openapi: 3.0.3 info: - version: "2.7.2" + version: "2.8.0" title: CF Online description: CF Online license: