Skip to content

Commit

Permalink
Add test for exception handling in DBI integration
Browse files Browse the repository at this point in the history
  • Loading branch information
jjatria committed Oct 10, 2023
1 parent 772495c commit c1b1051
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions t/OpenTelemetry/Integration/DBI.t
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@ my $otel = mock OpenTelemetry => override => [
};
},
set_status => sub ( $self, $status, $desc = '' ) {
return if defined $self->{otel}{status};

$self->{otel}{status} = {
code => $status,
$desc ? ( description => $desc ) : (),
};
},
record_exception => sub ( $self, $e, %attributes ) {
push @{ $self->{otel}{exceptions} //= [] }, {
exception => $e,
attributes => \%attributes,
};
},
end => sub ( $self ) {
$self->{otel}{ended} = 1;
},
Expand Down Expand Up @@ -106,6 +114,37 @@ subtest Mem => sub {
'server.port' => 1234,
},
}, 'Captured select data';

my $sth = $db->prepare('SELECT * FROM foo WHERE id = ?');
my $mock = mock $sth, override => [ finish => sub { die 'boom' } ];

like dies { $sth->execute('secret') },
match qr/boom/,
'Exception propagates';

is $span->{otel}, {
status => {
code => SPAN_STATUS_ERROR,
description => match qr/boom at /,
},
ended => T,
kind => SPAN_KIND_CLIENT,
name => 'SELECT * FROM foo WHERE id = ?',
attributes => {
'db.connection_string' => '(RaiseError=1):port=1234;',
'db.statement' => 'SELECT * FROM foo WHERE id = ?',
'db.system' => 'mem',
'db.user' => U,
'server.address' => U,
'server.port' => 1234,
},
exceptions => [
{
exception => match qr/boom at /,
attributes => {},
},
],
}, 'Captured exception';
};

done_testing;

0 comments on commit c1b1051

Please sign in to comment.