| 1 | package Sentry::Integration::MojoUserAgent; | ||
| 2 | use Mojo::Base 'Sentry::Integration::Base', -signatures; | ||
| 3 | |||
| 4 | use Mojo::Util qw(dumper); | ||
| 5 | use Sentry::Util 'around'; | ||
| 6 | |||
| 7 | has breadcrumbs => 1; | ||
| 8 | has tracing => 1; | ||
| 9 | |||
| 10 | sub setup_once ($self, $add_global_event_processor, $get_current_hub) { | ||
| 11 | return if (!$self->breadcrumbs && !$self->tracing); | ||
| 12 | |||
| 13 | around( | ||
| 14 | 'Mojo::UserAgent', | ||
| 15 | start => sub ($orig, $ua, $tx, $cb) { | ||
| 16 | my $url = $tx->req->url; | ||
| 17 | |||
| 18 | # Exclude Requests to the Sentry server | ||
| 19 | return $orig->($ua, $tx, $cb) | ||
| 20 | if $tx->req->headers->header('x-sentry-auth'); | ||
| 21 | |||
| 22 | my $hub = $get_current_hub->(); | ||
| 23 | |||
| 24 | my $span; | ||
| 25 | |||
| 26 | if ($self->tracing && (my $parent_span = $hub->get_scope()->get_span)) { | ||
| 27 | $span = $parent_span->start_child({ | ||
| 28 | op => 'http', | ||
| 29 | name => 'My Transaction', | ||
| 30 | description => $tx->req->method . ' ' . $tx->req->url->to_string, | ||
| 31 | data => { | ||
| 32 | url => $tx->req->url->to_string, | ||
| 33 | method => $tx->req->method, | ||
| 34 | status_code => $tx->res->code, | ||
| 35 | }, | ||
| 36 | }); | ||
| 37 | |||
| 38 | } | ||
| 39 | |||
| 40 | my $result = $orig->($ua, $tx, $cb); | ||
| 41 | |||
| 42 | $hub->add_breadcrumb({ | ||
| 43 | type => 'http', | ||
| 44 | category => 'Mojo::UserAgent', | ||
| 45 | data => { | ||
| 46 | url => $tx->req->url->to_string, | ||
| 47 | method => $tx->req->method, | ||
| 48 | status_code => $tx->res->code, | ||
| 49 | }, | ||
| 50 | }) | ||
| 51 | if $self->breadcrumbs; | ||
| 52 | |||
| 53 | if ($self->tracing) { | ||
| 54 | $span->set_http_status($tx->res->code); | ||
| 55 | $span->finish(); | ||
| 56 | } | ||
| 57 | |||
| 58 | return $result; | ||
| 59 | } | ||
| 60 | ); | ||
| 61 | |||
| 62 | $add_global_event_processor->( | ||
| 63 | sub ($event, $hint) { | ||
| 64 | |||
| 65 | # warn 'PROCESS' . dumper($event); | ||
| 66 | |||
| 67 | # warn $get_current_hub->(); | ||
| 68 | } | ||
| 69 | ); | ||
| 70 | |||
| 71 | # my $hub = $get_current_hub->(); | ||
| 72 | } | ||
| 73 | |||
| 74 | 1; |