| 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
otel_config("SERVICE_NAME") // $app->moniker
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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 31 | 4 | my ($first) = split ',', $fwd, 2; | |||||||||||||||||||||||||||||||||
| 32 | 4 | ▶ | $hostport = $1 // $2 if $first =~ /host=(?:"([^"]+)"|([^;]+))/; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$1 // $2
MC/DC0%0/2
$1 // $2
$1
$2
Truth table33%1/3
A // B
A= $1B= $2
| |||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||
| 35 | 19 | ▶ | $hostport //= $headers->header('x-forwarded-proto') | ||||||||||||||||||||||||||||||||
|
Condition66%4/6
$hostport //= $headers->header("x-forwarded-proto") // $headers->header("host")
$headers->header("x-forwarded-proto") // $headers->header("host")
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")
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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'
MC/DC0%0/1
ref $error || 'string'
ref $error
Truth table50%1/2
A
A= ref $error
| |||||||||||||||||||||||||||||||||||
| 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 || '/'
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
| |||||||||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||||||||