File Coverage

blib/lib/Sentry/Integration/LwpUserAgent.pm
Criterion Covered Total %
statement 36 36 100.0
branch 7 12 58.3
condition 3 6 50.0
subroutine 5 5 100.0
pod 0 1 0.0
total 51 60 85.0


line stmt bran cond sub pod time code
1             package Sentry::Integration::LwpUserAgent;
2 5     5   682 use Mojo::Base 'Sentry::Integration::Base', -signatures;
  5         47  
  5         43  
3              
4 5     5   1435 use Mojo::Util qw(dumper);
  5         23  
  5         281  
5 5     5   26 use Sentry::Util 'around';
  5         18  
  5         2716  
6              
7             has _package_name => 'LWP::UserAgent';
8             has breadcrumbs => 1;
9             has tracing => 1;
10              
11 6     6 0 514623 sub setup_once ($self, $add_global_event_processor, $get_current_hub) {
  6         15  
  6         32  
  6         12  
  6         18  
12 6 0 33     36 return if (!$self->breadcrumbs && !$self->tracing);
13              
14 3         7 around(
15             $self->_package_name,
16 3     3   8 request => sub ($orig, $lwp, $request, @args) {
  3         6  
  3         5  
  3         7  
  3         6  
17              
18 3         12 my $url = $request->uri;
19              
20             # Exclude Requests to the Sentry server
21 3 50       47 return $orig->($lwp, $request, @args)
22             if $request->header('x-sentry-auth');
23              
24 3         326 my $hub = $get_current_hub->();
25 3         12 my $span;
26              
27 3 100 66     16 if ($self->tracing && (my $parent_span = $hub->get_scope()->get_span)) {
28 1         48 $span = $parent_span->start_child({
29             op => 'http',
30             description => $request->method . ' ' . $request->uri,
31             data => {
32             url => $request->uri,
33             method => $request->method,
34             headers => $request->headers,
35             },
36             });
37              
38 1         7 $request->header('sentry-trace' => $span->to_trace_parent);
39             }
40              
41 3         166 my $result = $orig->($lwp, $request, @args);
42              
43 3 50       263 $hub->add_breadcrumb({
44             type => 'http',
45             category => 'LWP::UserAgent',
46             data => {
47             url => $request->uri,
48             method => $request->method,
49             status_code => $result->code,
50             }
51             })
52             if $self->breadcrumbs;
53              
54 3 100       12 if ($span) {
55 1 50       4 if ($result->code) {
56 1         16 $span->set_http_status($result->code);
57             }
58 1         14 $span->finish();
59             }
60              
61 3         72 return $result;
62             }
63 6         71 );
64             }
65              
66             1;