« OpenTelemetry/Propagator/TraceContext/TraceParent.pm
↑ Summary
OpenTelemetry/Propagator/Baggage.pm »
| 1 | package OpenTelemetry::Instrumentation::HTTP::Tiny; | ||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: OpenTelemetry instrumentation for HTTP::Tiny | ||||||||||||||||||||||||||||||||
| 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::Defer; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use List::Util 'any'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use OpenTelemetry::Constants qw( SPAN_KIND_CLIENT SPAN_STATUS_ERROR ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use OpenTelemetry::Context; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use OpenTelemetry::Trace; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use OpenTelemetry; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use Ref::Util qw( is_arrayref is_coderef ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use Syntax::Keyword::Dynamically; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use isa 'URI::http'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | 2 | ▶ | use parent 'OpenTelemetry::Instrumentation'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | 2 | ▶ | sub dependencies { 'HTTP::Tiny' } | ||||||||||||||||||||||||||||||
|
Subroutine
dependencies: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | 24 | ▶ | my sub get_headers ( $have, $want, $prefix ) { | ||||||||||||||||||||||||||||||
|
Subroutine
get_headers: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 27 | 24 | ▶ | return unless @$want; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | map { | ||||||||||||||||||||||||||||||||
| 30 | 5 | my ( $k, $v ) = ( $_->[0], $have->{ $_->[1] } ); | |||||||||||||||||||||||||||||||
| 31 | 5 | ▶ | "$prefix.$k" => is_arrayref $v ? $v : [ $v ] | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||
| 33 | 9 | ▶ | grep { my $k = $_->[0]; any { $k =~ $_ } @$want } | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 34 | 3 | map { [ lc tr/-/_/r, $_ ] } | |||||||||||||||||||||||||||||||
| 35 | keys %$have; | ||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | 2 | my ( $original, $loaded ); | |||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | 8 | ▶ | sub uninstall ( $class ) { | ||||||||||||||||||||||||||||||
|
Subroutine
uninstall: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 41 | 8 | ▶ | return unless $original; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 42 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 43 | 2 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 44 | 7 | delete $Class::Method::Modifiers::MODIFIER_CACHE{'HTTP::Tiny'}{request}; | |||||||||||||||||||||||||||||||
| 45 | 7 | *{'HTTP::Tiny::request'} = $original; | |||||||||||||||||||||||||||||||
| 46 | 7 | undef $loaded; | |||||||||||||||||||||||||||||||
| 47 | 7 | return; | |||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | 8 | ▶ | sub install ( $class, %config ) { | ||||||||||||||||||||||||||||||
|
Subroutine
install: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 51 | 8 | ▶ | return if $loaded; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 52 | 8 | ▶ | return unless Class::Inspector->loaded('HTTP::Tiny'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | 7 | require URI; | |||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | my @wanted_request_headers = map qr/^\Q$_\E$/i, map tr/-/_/r, | ||||||||||||||||||||||||||||||||
| 57 | 7 | ▶ | @{ 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"}
| |||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | my @wanted_response_headers = map qr/^\Q$_\E$/i, map tr/-/_/r, | ||||||||||||||||||||||||||||||||
| 60 | 7 | ▶ | @{ 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"}
| |||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | 7 | $original = \&HTTP::Tiny::request; | |||||||||||||||||||||||||||||||
| 63 | 8 | ▶ | install_modifier 'HTTP::Tiny' => around => request => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=13 SCAR=25.7
| |||||||||||||||||||||||||||||||||
| 64 | 8 | my ( $code, $self, $method, $url, $options ) = @_; | |||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 8 | my $uri = URI->new("$url"); | |||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | 8 | my %url_data; | |||||||||||||||||||||||||||||||
| 69 | 8 | ▶ | if ( isa_URI_http $uri ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 70 | 7 | ▶ | $uri->userinfo('REDACTED:REDACTED') if $uri->userinfo; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 71 | 7 | %url_data = ( | |||||||||||||||||||||||||||||||
| 72 | 'server.address' => $uri->host, | ||||||||||||||||||||||||||||||||
| 73 | 'server.port' => $uri->port, | ||||||||||||||||||||||||||||||||
| 74 | ); | ||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | 8 | my $agent = $self->agent; | |||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||
| 79 | my $span = OpenTelemetry->tracer_provider->tracer( | ||||||||||||||||||||||||||||||||
| 80 | name => __PACKAGE__, | ||||||||||||||||||||||||||||||||
| 81 | version => $VERSION, | ||||||||||||||||||||||||||||||||
| 82 | )->create_span( | ||||||||||||||||||||||||||||||||
| 83 | name => $method, | ||||||||||||||||||||||||||||||||
| 84 | kind => SPAN_KIND_CLIENT, | ||||||||||||||||||||||||||||||||
| 85 | attributes => { | ||||||||||||||||||||||||||||||||
| 86 | %url_data, | ||||||||||||||||||||||||||||||||
| 87 | # As per https://github.com/open-telemetry/semantic-conventions/blob/main/docs/http/http-spans.md | ||||||||||||||||||||||||||||||||
| 88 | 'http.request.method' => $method, | ||||||||||||||||||||||||||||||||
| 89 | 'network.protocol.name' => 'http', | ||||||||||||||||||||||||||||||||
| 90 | 'network.protocol.version' => '1.1', | ||||||||||||||||||||||||||||||||
| 91 | 'network.transport' => 'tcp', | ||||||||||||||||||||||||||||||||
| 92 | 'url.full' => "$uri", # redacted | ||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | # This does not include auto-generated headers | ||||||||||||||||||||||||||||||||
| 95 | # Capturing those would require to hook into the | ||||||||||||||||||||||||||||||||
| 96 | # handle's write_request method | ||||||||||||||||||||||||||||||||
| 97 | get_headers( | ||||||||||||||||||||||||||||||||
| 98 | $self->{default_headers}, # Apologies to the encapsulation gods | ||||||||||||||||||||||||||||||||
| 99 | \@wanted_request_headers, | ||||||||||||||||||||||||||||||||
| 100 | 'http.request.header' | ||||||||||||||||||||||||||||||||
| 101 | ), | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | get_headers( | ||||||||||||||||||||||||||||||||
| 104 | $options->{headers}, | ||||||||||||||||||||||||||||||||
| 105 | \@wanted_request_headers, | ||||||||||||||||||||||||||||||||
| 106 | 'http.request.header' | ||||||||||||||||||||||||||||||||
| 107 | ), | ||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | $agent ? ( 'user_agent.original' => $agent ) : (), | ||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | # Request body can be generated by setting content to a | ||||||||||||||||||||||||||||||||
| 112 | # code reference, in which case we don't set this attribute | ||||||||||||||||||||||||||||||||
| 113 | # Setting it would likely involve us hooking into the | ||||||||||||||||||||||||||||||||
| 114 | # handle's write_body method | ||||||||||||||||||||||||||||||||
| 115 | defined $options->{content} && ! is_coderef $options->{content} | ||||||||||||||||||||||||||||||||
| 116 | ? ( 'http.request.body.size' => length $options->{content} ) | ||||||||||||||||||||||||||||||||
| 117 | 8 | ▶ | : (), | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
defined $options->{"content"} and not is_coderef($options->{"content"})
MC/DC100%2/2
defined $options->{"content"} and not is_coderef($options->{"content"})
defined $options->{"content"}
not is_coderef($options->{"content"})
Truth table100%3/3
A and not(B)
A= defined $options->{"content"}B= not is_coderef($options->{"content"})
| |||||||||||||||||||||||||||||||||
| 118 | }, | ||||||||||||||||||||||||||||||||
| 119 | ); | ||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | 8 | dynamically OpenTelemetry::Context->current | |||||||||||||||||||||||||||||||
| 122 | = OpenTelemetry::Trace->context_with_span($span); | ||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | 8 | OpenTelemetry->propagator->inject( \my %carrier ); | |||||||||||||||||||||||||||||||
| 125 | 8 | ▶ | $options->{headers} = { %{ $options->{headers} // {} }, %carrier } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$options->{"headers"} // {}
MC/DC0%0/1
$options->{"headers"} // {}
$options->{"headers"}
Truth table50%1/2
A
A= $options->{"headers"}
| |||||||||||||||||||||||||||||||||
| 126 | if %carrier; | ||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||
| 128 | 8 | defer { $span->end } | |||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | 8 | my $res = $self->$code( $method, $url, $options ); | |||||||||||||||||||||||||||||||
| 131 | 8 | $span->set_attribute( 'http.response.status_code' => $res->{status} ); | |||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||
| 133 | # TODO: this should include retries | ||||||||||||||||||||||||||||||||
| 134 | 1 | $span->set_attribute( 'http.resend_count' => scalar @{ $res->{redirects} } ) | |||||||||||||||||||||||||||||||
| 135 | 8 | ▶ | if $res->{redirects}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | 8 | my $length = $res->{headers}{'content-length'}; | |||||||||||||||||||||||||||||||
| 138 | 8 | ▶ | $span->set_attribute( 'http.response.body.size' => $length ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 139 | if defined $length; | ||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | 8 | ▶ | unless ( $res->{success} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 142 | 3 | my $description; | |||||||||||||||||||||||||||||||
| 143 | 3 | ▶ | if ( $res->{status} == 599 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 144 | 2 | ▶ | my $error = ( $res->{content} // '' ) =~ s/^\s+|\s+$//r; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$res->{"content"} // ""
MC/DC0%0/1
$res->{"content"} // ""
$res->{"content"}
Truth table50%1/2
A
A= $res->{"content"}
| |||||||||||||||||||||||||||||||||
| 145 | 2 | ($description) = split /\n/, $error, 2; | |||||||||||||||||||||||||||||||
| 146 | 2 | $description =~ s/ at \S+ line \d+\.$//a; | |||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||
| 149 | else { | ||||||||||||||||||||||||||||||||
| 150 | 1 | $description = $res->{status}; | |||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | 3 | $span->set_status( SPAN_STATUS_ERROR, $description ); | |||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||
| 156 | $span->set_attribute( | ||||||||||||||||||||||||||||||||
| 157 | get_headers( | ||||||||||||||||||||||||||||||||
| 158 | $res->{headers}, | ||||||||||||||||||||||||||||||||
| 159 | 8 | \@wanted_response_headers, | |||||||||||||||||||||||||||||||
| 160 | 'http.response.header' | ||||||||||||||||||||||||||||||||
| 161 | ) | ||||||||||||||||||||||||||||||||
| 162 | ); | ||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | 8 | return $res; | |||||||||||||||||||||||||||||||
| 165 | 7 | }; | |||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||
| 167 | 7 | return $loaded = 1; | |||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | 2 | 1; | |||||||||||||||||||||||||||||||