| 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 critic (TestingAndDebugging::ProhibitNoStrict, TestingAndDebugging::ProhibitNoWarnings, TestingAndDebugging::ProhibitProlongedStrictureOverride) | ||
| 12 | no strict 'refs'; | ||
| 13 | no warnings 'redefine'; | ||
| 14 | |||
| 15 | my $symbol = join('::', $package, $method); | ||
| 16 | |||
| 17 | my $orig = \&{$symbol}; | ||
| 18 | *{$symbol} = sub { $cb->($orig, @_) }; | ||
| 19 | |||
| 20 | return; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub setup_once ($self, $add_global_event_processor, $get_current_hub) { | ||
| 24 | |||
| 25 | around( | ||
| 26 | 'DBI::db', | ||
| 27 | do => sub ($orig, $dbh, $statement, @args) { | ||
| 28 | |||
| 29 | my $hub = $get_current_hub->(); | ||
| 30 | |||
| 31 | my $span; | ||
| 32 | |||
| 33 | if ($self->tracing && (my $parent_span = $hub->get_scope()->get_span)) { | ||
| 34 | $span = $parent_span->start_child({ | ||
| 35 | op => 'sql.query', description => $statement, }); | ||
| 36 | } | ||
| 37 | |||
| 38 | my $value = $orig->($dbh, $statement, @args); | ||
| 39 | |||
| 40 | $hub->add_breadcrumb({ | ||
| 41 | type => 'query', category => 'do', data => { sql => $statement }, }) | ||
| 42 | if $self->breadcrumbs; | ||
| 43 | |||
| 44 | if ($self->tracing) { | ||
| 45 | $span->finish(); | ||
| 46 | } | ||
| 47 | |||
| 48 | return $value; | ||
| 49 | } | ||
| 50 | ); | ||
| 51 | |||
| 52 | return if (!$self->breadcrumbs && !$self->tracing); | ||
| 53 | |||
| 54 | around( | ||
| 55 | 'DBI::st', | ||
| 56 | execute => sub ($orig, $sth, @args) { | ||
| 57 | my $statement = $sth->{Statement}; | ||
| 58 | |||
| 59 | my $hub = $get_current_hub->(); | ||
| 60 | |||
| 61 | my $span; | ||
| 62 | |||
| 63 | if ($self->tracing && (my $parent_span = $hub->get_scope()->get_span)) { | ||
| 64 | $span = $parent_span->start_child({ | ||
| 65 | op => 'sql.query', | ||
| 66 | description => $statement, | ||
| 67 | data => { args => [@args], }, | ||
| 68 | }); | ||
| 69 | } | ||
| 70 | |||
| 71 | my $value = $orig->($sth, @args); | ||
| 72 | |||
| 73 | $hub->add_breadcrumb({ | ||
| 74 | type => 'query', | ||
| 75 | category => 'execute', | ||
| 76 | data => { sql => $statement, args => [@args], }, | ||
| 77 | }) | ||
| 78 | if $self->breadcrumbs; | ||
| 79 | |||
| 80 | if ($self->tracing) { | ||
| 81 | $span->finish(); | ||
| 82 | } | ||
| 83 | |||
| 84 | return $value; | ||
| 85 | } | ||
| 86 | ); | ||
| 87 | } | ||
| 88 | |||
| 89 | 1; |