| 1 | package Sentry::Integration::DBI; | ||
| 2 | use Mojo::Base 'Sentry::Integration::Base', -signatures; | ||
| 3 | |||
| 4 | use Mojo::Util qw(dumper monkey_patch); | ||
| 5 | |||
| 6 | has breadcrumbs => 1; | ||
| 7 | has tracing => 1; | ||
| 8 | |||
| 9 | # DBI is special. Classes are generated on-the-fly. | ||
| 10 | sub around ($package, $method, $cb) { | ||
| 11 | no strict 'refs'; | ||
| 12 | no warnings 'redefine'; | ||
| 13 | |||
| 14 | my $symbol = join('::', $package, $method); | ||
| 15 | |||
| 16 | my $orig = \&{$symbol}; | ||
| 17 | *{$symbol} = sub { $cb->($orig, @_) }; | ||
| 18 | |||
| 19 | return; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub setup_once ($self, $add_global_event_processor, $get_current_hub) { | ||
| 23 | |||
| 24 | around( | ||
| 25 | 'DBI::db', | ||
| 26 | do => sub ($orig, $dbh, $statement, @args) { | ||
| 27 | |||
| 28 | my $hub = $get_current_hub->(); | ||
| 29 | |||
| 30 | my $span; | ||
| 31 | |||
| 32 | if ($self->tracing && (my $parent_span = $hub->get_scope()->get_span)) { | ||
| 33 | $span = $parent_span->start_child({ | ||
| 34 | op => 'sql.query', description => $statement, }); | ||
| 35 | } | ||
| 36 | |||
| 37 | my $value = $orig->($dbh, $statement, @args); | ||
| 38 | |||
| 39 | $hub->add_breadcrumb({ | ||
| 40 | type => 'query', category => 'do', data => { sql => $statement }, }) | ||
| 41 | if $self->breadcrumbs; | ||
| 42 | |||
| 43 | if ($self->tracing) { | ||
| 44 | $span->finish(); | ||
| 45 | } | ||
| 46 | |||
| 47 | return $value; | ||
| 48 | } | ||
| 49 | ); | ||
| 50 | |||
| 51 | return if (!$self->breadcrumbs && !$self->tracing); | ||
| 52 | |||
| 53 | around( | ||
| 54 | 'DBI::st', | ||
| 55 | execute => sub ($orig, $sth, @args) { | ||
| 56 | my $statement = $sth->{Statement}; | ||
| 57 | |||
| 58 | my $hub = $get_current_hub->(); | ||
| 59 | |||
| 60 | my $span; | ||
| 61 | |||
| 62 | if ($self->tracing && (my $parent_span = $hub->get_scope()->get_span)) { | ||
| 63 | $span = $parent_span->start_child({ | ||
| 64 | op => 'sql.query', | ||
| 65 | description => $statement, | ||
| 66 | data => { args => [@args], }, | ||
| 67 | }); | ||
| 68 | } | ||
| 69 | |||
| 70 | my $value = $orig->($sth, @args); | ||
| 71 | |||
| 72 | $hub->add_breadcrumb({ | ||
| 73 | type => 'query', | ||
| 74 | category => 'execute', | ||
| 75 | data => { sql => $statement, args => [@args], }, | ||
| 76 | }) | ||
| 77 | if $self->breadcrumbs; | ||
| 78 | |||
| 79 | if ($self->tracing) { | ||
| 80 | $span->finish(); | ||
| 81 | } | ||
| 82 | |||
| 83 | return $value; | ||
| 84 | } | ||
| 85 | ); | ||
| 86 | } | ||
| 87 | |||
| 88 | 1; |