blib/lib/Mojolicious/Plugin/OpenTelemetry.pm

filter: Statementstmt 98.1% 54 / 55 Branchbran 66.6% 8 / 12 Conditioncond 52.6% 10 / 19 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 90.0% 9 / 10 Podpod 100% 1 / 1 totaltotal 77.5% 83 / 107 CC 14 SCAR 18.9
CC
14
Cov
84%
__ANON__
11.9
register
3.1
__ANON__
2.0
CRAP 6.6
↑ Summary
1 package Mojolicious::Plugin::OpenTelemetry;
2 # ABSTRACT: An OpenTelemetry integration for Mojolicious
3  
4 1 our $VERSION = '0.006';
5  
6 1 ▶use Mojo::Base 'Mojolicious::Plugin', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Feature::Compat::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use OpenTelemetry -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use OpenTelemetry::Constants -span;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Syntax::Keyword::Dynamically;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 12 ▶sub register ( $, $app, $config, @ ) {
Subroutine register: called CC=3 SCAR=11.2
Pod documented
14 12 ▶ $config->{tracer}{name} //= otel_config('SERVICE_NAME') // $app->moniker;
Condition33%2/6
$config->{"tracer"}{"name"} //= otel_config("SERVICE_NAME") // $app->moniker
l!l&&r!l&&!r
0120
otel_config("SERVICE_NAME") // $app->moniker
l!l&&r!l&&!r
0120
MC/DC0%0/3
$config->{"tracer"}{"name"} //= otel_config("SERVICE_NAME") // $app->moniker
$config->{"tracer"}{"name"} otel_config("SERVICE_NAME") $app->moniker
Truth table0%0/4
A //= B // C
A= $config->{"tracer"}{"name"}B= otel_config("SERVICE_NAME")C= $app->moniker
ABCresult
1XX1
01X1
0011
0000
15  
16 # First, around_dispatch sets up the span and populates what it can
17 19 ▶ $app->hook( around_dispatch => sub ( $next, $c ) {
Subroutine __ANON__: called CC=11 SCAR=24.7
18 19 my $tracer = otel_tracer_provider->tracer( %{ $config->{tracer} } );
19  
20 19 my $tx = $c->tx;
21 19 my $req = $tx->req;
22 19 my $url = $req->url->to_abs;
23 19 my $query = $url->query->to_string;
24 19 my $method = $req->method;
25 19 my $headers = $req->headers;
26 19 my $agent = $headers->user_agent;
27  
28 # https://opentelemetry.io/docs/specs/semconv/http/http-spans/#setting-serveraddress-and-serverport-attributes
29 19 my $hostport;
30 19 ▶ if ( my $fwd = $headers->header('forwarded') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fwd = $headers->header("forwarded")) 4 15 19
31 4 my ($first) = split ',', $fwd, 2;
32 4 ▶ $hostport = $1 // $2 if $first =~ /host=(?:"([^"]+)"|([^;]+))/;
Branch: 1 branch
BranchTrueFalseTotal
if $first =~ /host=(?:"([^"]+)"|([^;]+))/u 4 0 4
Condition33%1/3
$1 // $2
l!l&&r!l&&!r
040
MC/DC0%0/2
$1 // $2
$1 $2
Truth table33%1/3
A // B
A= $1B= $2
ABresult
1X1
011
000
33 }
34  
35 19 ▶ $hostport //= $headers->header('x-forwarded-proto')
Condition66%4/6
$hostport //= $headers->header("x-forwarded-proto") // $headers->header("host")
l!l&&r!l&&!r
4150
$headers->header("x-forwarded-proto") // $headers->header("host")
l!l&&r!l&&!r
2130
MC/DC0%0/3
$hostport //= $headers->header("x-forwarded-proto") // $headers->header("host")
$hostport $headers->header("x-forwarded-proto") $headers->header("host")
Truth table0%0/4
A //= B // C
A= $hostportB= $headers->header("x-forwarded-proto")C= $headers->header("host")
ABCresult
1XX1
01X1
0011
0000
36 // $headers->header('host');
37  
38 19 my ( $host, $port ) = $hostport =~ /(.*?)(?::([0-9]+))?$/g;
39  
40 my $context = otel_propagator->extract(
41 $headers,
42 undef,
43 0 ▶ sub ( $carrier, $key ) { $carrier->header($key) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 19 );
45  
46 19 ▶ my $span = $tracer->create_span(
Branch: 4 branches
BranchTrueFalseTotal
$host ? : 19 0 19
$port ? : 15 4 19
$agent ? : 19 0 19
$query ? : 0 19 19
47 name => $method,
48 kind => SPAN_KIND_SERVER,
49 parent => $context,
50 attributes => {
51 'http.request.method' => $method,
52 'network.protocol.version' => $req->version,
53 'url.path' => $url->path->to_string,
54 'url.scheme' => $url->scheme,
55 'client.address' => $tx->remote_address,
56 'client.port' => $tx->remote_port,
57 $host ? ( 'server.address' => $host ) : (),
58 $port ? ( 'server.port' => $port ) : (),
59 $agent ? ( 'user_agent.original' => $agent ) : (),
60 $query ? ( 'url.query' => $query ) : (),
61 },
62 );
63  
64 # dynamically works across Future::AsyncAwait boundaries, so we don't need to
65 # store the span inside the current Mojolicious controller.
66 19 dynamically otel_current_context
67 = otel_context_with_span( $span, $context );
68  
69 # Now that we have a new span/context, we can update Mojolicious's data
70 # for convenience.
71 # This sets the ID that gets logged by the $c->log helper
72 19 $req->request_id( $span->context->hex_span_id );
73  
74 # When the transaction is finished, get the response data
75 # XXX: For websockets, this will be when the websocket closes, which may not
76 # be ideal: It would miss the HTTP handshake 101 response.
77 19 ▶ $c->tx->on(finish => sub ($tx) {
Subroutine __ANON__: called CC=1 SCAR=0.0
78 19 $span->set_attribute('http.response.status_code', $tx->res->code);
79 19 $span->end;
80 19 });
81  
82 # around_dispatch handles exceptions
83 19 try {
84 19 $next->();
85 }
86 catch ($error) {
87 1 my ($message) = split /\n/, "$error", 2;
88 1 $message =~ s/ at \S+ line \d+\.$//a;
89  
90 1 ▶ $span
Condition50%1/2
ref $error || 'string'
l!l
10
MC/DC0%0/1
ref $error || 'string'
ref $error
Truth table50%1/2
A
A= ref $error
Aresult
11
00
91 ->record_exception($error)
92 ->set_status( SPAN_STATUS_ERROR, $message )
93 ->set_attribute(
94 'error.type' => ref $error || 'string',
95 );
96 1 die $error;
97 }
98 12 });
99  
100 # around_action fills in more attributes, since it now knows what action is being taken
101 19 ▶ $app->hook( around_action => sub( $next, $c, $action, $last, @ ) {
Subroutine __ANON__: called CC=2 SCAR=6.9
102 # We don't check $last because this may still be the last action we do.
103 # If, for example, an intermediate action throws an exception or otherwise
104 # interrupts the dispatch cycle.
105 19 my $tx = $c->tx;
106  
107 # Mojolicious normalizes routes to remove the trailing slash, which is fine
108 # until it's the only thing in the route.
109 19 ▶ my $route = $c->match->endpoint->to_string || '/';
Condition100%2/2
$c->match->endpoint->to_string || '/'
l!l
181
MC/DC100%1/1
$c->match->endpoint->to_string || '/'
$c->match->endpoint->to_string
Truth table100%2/2
A
A= $c->match->endpoint->to_string
Aresult
11
00
110  
111 19 my $span = otel_span_from_context;
112 19 $span->set_name( $tx->req->method . ' ' . $route );
113 19 $span->set_attribute( 'http.route' => $route );
114  
115 19 $next->();
116 12 });
117 }
118  
119 1 1;
↑ Summary