| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 31 | 7 | ▶ | push @{ $attributes{ $prefix . '.' . lc $key } //= [] }, $value; | ||||||||||||
|
Condition100%2/2
$attributes{$prefix . '.' . lc($key)} //= []
MC/DC100%1/1
$attributes{$prefix . '.' . lc($key)} //= []
$attributes{$prefix . '.' . lc($key)}
Truth table100%2/2
A
A= $attributes{$prefix . '.' . lc($key)}
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 51 | 5 | ▶ | return unless Class::Inspector->loaded('LWP::UserAgent'); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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"} // []
MC/DC100%1/1
delete $config{"request_headers"} // []
delete $config{"request_headers"}
Truth table100%2/2
A
A= delete $config{"request_headers"}
| |||||||||||||||
| 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"} // []
MC/DC100%1/1
delete $config{"response_headers"} // []
delete $config{"response_headers"}
Truth table100%2/2
A
A= delete $config{"response_headers"}
| |||||||||||||||
| 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
| |||||||||||||||
| 68 | |||||||||||||||
| 69 | 4 | my $agent = $self->agent; | |||||||||||||
| 70 | |||||||||||||||
| 71 | 4 | ▶ | my $span = OpenTelemetry->tracer_provider->tracer( | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 124 | if defined $length; | ||||||||||||||
| 125 | |||||||||||||||
| 126 | 3 | ▶ | $span->set_status( SPAN_STATUS_ERROR, $response->code ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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; | |||||||||||||