OpenTelemetry/Instrumentation/LWP/UserAgent.pm

filter: Statementstmt 98.9% 98 / 99 Branchbran 86.3% 19 / 22 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 23 / 23 Podpod 66.6% 2 / 3 totaltotal 96.7% 151 / 156 CC 15 SCAR 10.8
CC
15
Cov
97%
__ANON__
7.0
install
5.0
__ANON__
3.0
CRAP 3.0
« OpenTelemetry/Propagator/TraceContext/TraceState.pm ↑ Summary OpenTelemetry/Common.pm »
1 package OpenTelemetry::Instrumentation::LWP::UserAgent;
2 # ABSTRACT: OpenTelemetry instrumentation for LWP::UserAgent
3  
4 2 our $VERSION = '0.040';
5  
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Class::Inspector;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Class::Method::Modifiers 'install_modifier';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Feature::Compat::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Syntax::Keyword::Dynamically;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use List::Util 'none';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use OpenTelemetry::Constants qw( SPAN_KIND_CLIENT SPAN_STATUS_ERROR );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use OpenTelemetry::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use OpenTelemetry::Trace;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use OpenTelemetry;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 ▶use parent 'OpenTelemetry::Instrumentation';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶sub dependencies { 'LWP::UserAgent' }
Subroutine dependencies: called CC=1 SCAR=0.0
Pod documented
23  
24 11 ▶my sub get_headers ( $have, $want, $prefix ) {
Subroutine get_headers: called CC=2 SCAR=6.9
25 11 ▶ return unless @$want;
Branch: 1 branch
BranchTrueFalseTotal
unless @$want 8 3 11
26  
27 3 my %attributes;
28 13 ▶ $have->scan( sub ( $key, $value ) {
Subroutine __ANON__: called CC=3 SCAR=11.0
29 13 $key =~ tr/-/_/;
30 13 ▶ return if none { $key =~ $_ } @$want;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &none(sub { $key =~ /$_/; } , @$want) 6 7 13
31 7 ▶ push @{ $attributes{ $prefix . '.' . lc $key } //= [] }, $value;
Condition100%2/2
$attributes{$prefix . '.' . lc($key)} //= []
l!l
25
MC/DC100%1/1
$attributes{$prefix . '.' . lc($key)} //= []
$attributes{$prefix . '.' . lc($key)}
Truth table100%2/2
A
A= $attributes{$prefix . '.' . lc($key)}
Aresult
11
00
32 3 });
33  
34 3 %attributes;
35 }
36  
37 2 my ( $original, $loaded );
38  
39 4 ▶sub uninstall ( $class ) {
Subroutine uninstall: called CC=2 SCAR=6.9
Pod undocumented
40 4 ▶ return unless $loaded;
Branch: 1 branch
BranchTrueFalseTotal
unless $loaded 1 3 4
41 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
43 3 delete $Class::Method::Modifiers::MODIFIER_CACHE{'LWP::UserAgent'}{simple_request};
44 3 *{'LWP::UserAgent::simple_request'} = $original;
45 3 undef $loaded;
46 3 return;
47 }
48  
49 5 ▶sub install ( $class, %config ) {
Subroutine install: called CC=5 SCAR=16.1
Pod documented
50 5 ▶ return if $loaded;
Branch: 1 branch
BranchTrueFalseTotal
if $loaded 0 5 5
51 5 ▶ return unless Class::Inspector->loaded('LWP::UserAgent');
Branch: 1 branch
BranchTrueFalseTotal
unless "Class::Inspector"->loaded("LWP::UserAgent") 1 4 5
52  
53 my @wanted_request_headers = map qr/^\Q$_\E$/i, map tr/-/_/r,
54 4 ▶ @{ delete $config{request_headers} // [] };
Condition100%2/2
delete $config{"request_headers"} // []
l!l
13
MC/DC100%1/1
delete $config{"request_headers"} // []
delete $config{"request_headers"}
Truth table100%2/2
A
A= delete $config{"request_headers"}
Aresult
11
00
55  
56 my @wanted_response_headers = map qr/^\Q$_\E$/i, map tr/-/_/r,
57 4 ▶ @{ delete $config{response_headers} // [] };
Condition100%2/2
delete $config{"response_headers"} // []
l!l
13
MC/DC100%1/1
delete $config{"response_headers"} // []
delete $config{"response_headers"}
Truth table100%2/2
A
A= delete $config{"response_headers"}
Aresult
11
00
58  
59 4 $original = \&LWP::UserAgent::simple_request;
60 4 ▶ install_modifier 'LWP::UserAgent' => around => simple_request => sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
61 4 my ( $code, $self, $request, @rest ) = @_;
62  
63 4 my $uri = $request->uri->clone;
64 4 my $method = $request->method;
65 4 my $length = length $request->content;
66  
67 4 ▶ $uri->userinfo('REDACTED:REDACTED') if $uri->userinfo;
Branch: 1 branch
BranchTrueFalseTotal
if $uri->userinfo 1 3 4
68  
69 4 my $agent = $self->agent;
70  
71 4 ▶ my $span = OpenTelemetry->tracer_provider->tracer(
Branch: 2 branches
BranchTrueFalseTotal
$agent ? : 4 0 4
$length ? : 1 3 4
72 name => __PACKAGE__,
73 version => $VERSION,
74 )->create_span(
75 name => $method,
76 kind => SPAN_KIND_CLIENT,
77 attributes => {
78 # As per https://github.com/open-telemetry/semantic-conventions/blob/main/docs/http/http-spans.md
79 'http.request.method' => $method,
80 'network.protocol.name' => 'http',
81 'network.protocol.version' => '1.1',
82 'network.transport' => 'tcp',
83 'server.address' => $uri->host,
84 'server.port' => $uri->port,
85 'url.full' => "$uri", # redacted
86  
87 get_headers(
88 $self->default_headers,
89 \@wanted_request_headers,
90 'http.request.header'
91 ),
92  
93 get_headers(
94 $request->headers,
95 \@wanted_request_headers,
96 'http.request.header'
97 ),
98  
99 $agent ? ( 'user_agent.original' => $agent ) : (),
100 $length ? ( 'http.request.body.size' => $length ) : (),
101 },
102 );
103  
104 4 dynamically OpenTelemetry::Context->current
105 = OpenTelemetry::Trace->context_with_span($span);
106  
107 OpenTelemetry->propagator->inject(
108 $request,
109 undef,
110 1 ▶ sub { shift->header(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
111 4 );
112  
113 4 try {
114 4 my $response = $self->$code( $request, @rest );
115 3 $span->set_attribute( 'http.response.status_code' => $response->code );
116  
117 # TODO: this should include retries
118 3 ▶ if ( my $count = $response->redirects ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $count = $response->redirects) 0 3 3
119 0 $span->set_attribute( 'http.resend_count' => $count )
120 }
121  
122 3 my $length = $response->header('content-length');
123 3 ▶ $span->set_attribute( 'http.response.body.size' => $length )
Branch: 1 branch
BranchTrueFalseTotal
if defined $length 1 2 3
124 if defined $length;
125  
126 3 ▶ $span->set_status( SPAN_STATUS_ERROR, $response->code )
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 1 2 3
127 unless $response->is_success;
128  
129 3 $span->set_attribute(
130 get_headers(
131 $response->headers,
132 \@wanted_response_headers,
133 'http.response.header'
134 )
135 );
136  
137 3 return $response;
138 }
139 catch ($error) {
140 1 my ($description) = split /\n/, $error =~ s/^\s+|\s+$//gr, 2;
141 1 $description =~ s/ at \S+ line \d+\.$//a;
142  
143 1 $span->record_exception($error);
144 1 $span->set_status( SPAN_STATUS_ERROR, $description );
145  
146 1 die $error;
147 }
148 finally {
149 4 $span->end;
150 }
151 4 };
152  
153 4 return $loaded = 1;
154 }
155  
156 2 1;
« OpenTelemetry/Propagator/TraceContext/TraceState.pm ↑ Summary OpenTelemetry/Common.pm »