From a470d1369095eaadba1a7f522e5bfc679471e02f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrej=20Rama=C5=A1euski?= <andrej@x2.cz> Date: Sat, 5 Feb 2022 17:24:16 +0100 Subject: [PATCH] Hack knihovny pro Sentry a napojeni --- lib/CF.pm | 5 ++ lib/Mojolicious/Plugin/SentrySDK.pm | 82 +++++++++++++++++++++++++++++ openapi.yaml | 2 +- 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 lib/Mojolicious/Plugin/SentrySDK.pm diff --git a/lib/CF.pm b/lib/CF.pm index fbcce86..85a9eb8 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 0000000..83d30b0 --- /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 7492694..7b928f4 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: -- GitLab