| 1 | package Sentry::Integration::LwpUserAgent; | ||
| 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 | 'LWP::UserAgent', | ||
| 15 | request => sub ($orig, $lwp, $request, @args) { | ||
| 16 | |||
| 17 | my $url = $request->uri; | ||
| 18 | |||
| 19 | # Exclude Requests to the Sentry server | ||
| 20 | return $orig->($lwp, $request, @args) | ||
| 21 | if $request->header('x-sentry-auth'); | ||
| 22 | |||
| 23 | my $hub = $get_current_hub->(); | ||
| 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 => $request->method . ' ' . $request->uri, | ||
| 31 | data => { | ||
| 32 | url => $request->uri, | ||
| 33 | method => $request->method, | ||
| 34 | headers => $request->headers, | ||
| 35 | }, | ||
| 36 | }); | ||
| 37 | } | ||
| 38 | |||
| 39 | my $result = $orig->($lwp, $request, @args); | ||
| 40 | |||
| 41 | $hub->add_breadcrumb({ | ||
| 42 | type => 'http', | ||
| 43 | category => 'LWP::UserAgent', | ||
| 44 | data => { | ||
| 45 | url => $request->uri, | ||
| 46 | method => $request->method, | ||
| 47 | status_code => $result->code, | ||
| 48 | } | ||
| 49 | }) | ||
| 50 | if $self->breadcrumbs; | ||
| 51 | |||
| 52 | if ($self->tracing) { | ||
| 53 | |||
| 54 | # $span->set_http_status($tx->res->code); | ||
| 55 | $span->finish(); | ||
| 56 | } | ||
| 57 | |||
| 58 | return $result; | ||
| 59 | } | ||
| 60 | ); | ||
| 61 | } | ||
| 62 | |||
| 63 | 1; |