File Coverage

blib/lib/Sentry/Integration/MojoUserAgent.pm
Criterion Covered Total %
statement 31 36 86.1
branch 4 12 33.3
condition 2 6 33.3
subroutine 5 5 100.0
pod 0 1 0.0
total 42 60 70.0


line stmt bran cond sub pod time code
1             package Sentry::Integration::MojoUserAgent;
2 4     4   28 use Mojo::Base 'Sentry::Integration::Base', -signatures;
  4         8  
  4         32  
3              
4 4     4   1315 use Mojo::Util qw(dumper);
  4         10  
  4         272  
5 4     4   26 use Sentry::Util 'around';
  4         7  
  4         2494  
6              
7             has breadcrumbs => 1;
8             has tracing => 1;
9              
10 3     3 0 7 sub setup_once ($self, $add_global_event_processor, $get_current_hub) {
  3         7  
  3         19  
  3         8  
  3         5  
11 3 0 33     37 return if (!$self->breadcrumbs && !$self->tracing);
12              
13 3         6 around(
14             'Mojo::UserAgent',
15 3     3   7 start => sub ($orig, $ua, $tx, $cb = undef) {
  3         5  
  3         6  
  3         6  
  3         5  
16 3         10 my $url = $tx->req->url;
17              
18             # Exclude Requests to the Sentry server
19 3 50       22 return $orig->($ua, $tx, $cb)
20             if $tx->req->headers->header('x-sentry-auth');
21              
22 3         114 my $hub = $get_current_hub->();
23              
24 3         6 my $span;
25              
26 3 50 33     40 if ($self->tracing && (my $parent_span = $hub->get_scope()->get_span)) {
27 0         0 $span = $parent_span->start_child({
28             op => 'http.client',
29             description => $tx->req->method . ' ' . $tx->req->url->to_string,
30             data =>
31             { url => $tx->req->url->to_string, method => $tx->req->method, },
32             });
33              
34 0         0 $tx->req->headers->add('sentry-trace' => $span->to_trace_parent);
35             }
36              
37 3         29 my $result = $orig->($ua, $tx, $cb);
38              
39 3 50       11587 $hub->add_breadcrumb({
40             type => 'http',
41             category => 'Mojo::UserAgent',
42             data => {
43             url => $tx->req->url->to_string,
44             method => $tx->req->method,
45             status_code => $tx->res->code,
46             },
47             })
48             if $self->breadcrumbs;
49              
50 3 50       12 if ($span) {
51 0 0       0 if (my $http_status = $tx->res->code) {
52 0         0 $span->set_http_status($http_status);
53             }
54 0         0 $span->finish();
55             }
56              
57 3         28 return $result;
58             }
59 3         50 );
60             }
61              
62             1;