Skip to content

Commit 3dc6b0d

Browse files
committed
Do not set span status on 4XX errors
1 parent 8c82cd1 commit 3dc6b0d

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

Changes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ Revision history for Mojolicious-Plugin-OpenTelemetry
22

33
{{$NEXT}}
44

5+
* Fixed an issue where spans were set to error in the case
6+
of 4XX responses, which goes agains the specification.
7+
58
0.001 2023-12-03 16:51:34+00:00 Europe/London
69

710
First version.

lib/Mojolicious/Plugin/OpenTelemetry.pm

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,18 @@ sub register ( $, $app, $config, @ ) {
8080

8181
$promise->then( sub {
8282
my $code = $tx->res->code;
83-
my $error = $code >= 400 && $code < 600;
83+
84+
# The status of server spans must be left unset if the
85+
# response is a 4XX error
86+
# See https://github.com/open-telemetry/semantic-conventions/blob/main/docs/http/http-spans.md#status
87+
if ( $code < 400 ) {
88+
$span->set_status(SPAN_STATUS_OK);
89+
}
90+
elsif ( $code >= 500 ) {
91+
$span->set_status(SPAN_STATUS_ERROR);
92+
}
8493

8594
$span
86-
->set_status( $error ? SPAN_STATUS_ERROR : SPAN_STATUS_OK )
8795
->set_attribute( 'http.response.status_code' => $code )
8896
->end;
8997
})->wait;

t/basic.t

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ get '/async' => sub ( $c, @ ) {
6464
});
6565
};
6666

67+
get '/status/:code' => sub ( $c, @ ) {
68+
$c->render( text => 'OK', status => $c->stash('code') );
69+
};
70+
6771
get '/error' => sub ( $c, @ ) {
6872
die 'oops';
6973
};
@@ -206,6 +210,37 @@ subtest Error => sub {
206210
];
207211
};
208212

213+
subtest 'Response codes' => sub {
214+
$tst->get_ok('/status/400')
215+
->status_is(400);
216+
217+
is $span->{otel}, {
218+
attributes => {
219+
'client.address' => '127.0.0.1',
220+
'client.port' => T,
221+
'http.request.method' => 'GET',
222+
'http.route' => '/status/:code',
223+
'network.protocol.version' => '1.1',
224+
'server.address' => '127.0.0.1',
225+
'server.port' => T,
226+
'url.path' => '/status/400',
227+
'url.scheme' => U,
228+
'user_agent.original' => 'Mojolicious (Perl)',
229+
},
230+
kind => SPAN_KIND_SERVER,
231+
name => 'GET /status/:code',
232+
parent => D, # FIXME: cannot use an object check on 5.32?
233+
# parent => object {
234+
# prop isa => 'OpenTelemetry::Context';
235+
# },
236+
};
237+
238+
span_calls [
239+
set_attribute => [ 'http.response.status_code', 400 ],
240+
end => [],
241+
];
242+
};
243+
209244
describe 'Host / port parsing' => sub {
210245
my $port;
211246

0 commit comments

Comments
 (0)