File Coverage

blib/lib/Sentry/Integration/MojoTemplate.pm
Criterion Covered Total %
statement 44 44 100.0
branch 8 12 66.6
condition 10 15 66.6
subroutine 6 6 100.0
pod 0 1 0.0
total 68 78 87.1


line stmt bran cond sub pod time code
1             package Sentry::Integration::MojoTemplate;
2 5     5   511 use Mojo::Base 'Sentry::Integration::Base', -signatures;
  5         10  
  5         37  
3              
4 5     5   1439 use Try::Tiny;
  5         11  
  5         369  
5 5     5   89 use Sentry::Util 'around';
  5         9  
  5         7335  
6              
7             has tracing => 1;
8             has fix_stacktrace => 1;
9              
10 6     6 0 463958 sub setup_once ($self, $add_global_event_processor, $get_current_hub) {
  6         14  
  6         27  
  6         20  
  6         14  
11 5         10 around(
12             'Mojo::Template',
13 5     5   11 render => sub ($orig, $mojo_template, @args) {
  5         8  
  5         14  
  5         9  
14 5         17 my $hub = $get_current_hub->();
15 5   33     26 my $parent_span = $self->tracing && $hub->get_current_scope->get_span;
16 5         65 my $output;
17             $hub->with_scope(sub ($scope) {
18 5         22 my $namespace = $mojo_template->namespace;
19              
20 5         43 my $span;
21 5 50       16 if ($parent_span) {
22 5 50       17 $span = $parent_span->start_child({
23             op => 'mojo.template',
24             description => $mojo_template->name,
25             data => { compiled => $mojo_template->compiled ? 'yes' : 'no', },
26             });
27 5         26 $scope->set_span($span);
28             }
29              
30             try {
31 5         279 $output = $orig->($mojo_template, @args);
32 5 100 100     105972 if ( $self->fix_stacktrace
      66        
33             && ref $output
34             && $output->isa('Mojo::Exception')) {
35 1         23 _fix_template_stack_frames($namespace, $output);
36             }
37             } finally {
38 5 50       190 $span->finish() if $span;
39 5         50 };
40 5         66 });
41 5         150 return $output;
42             }
43 6         48 );
44             }
45              
46 1     1   4 sub _fix_template_stack_frames($namespace, $exc) {
  1         2  
  1         3  
  1         2  
47 1         4 for my $frame ($exc->frames->@*) {
48             # Frames coming from Mojo templates will have their module set to
49             # $namespace which is not very useful since it will be the same for all
50             # templates. Additionally, if using Mojolicious::Plugin::EPRenderer, the
51             # namespace will contain a hash value, which means it will mess up issue
52             # grouping.
53             # Remove module and subroutine from the frame so that Sentry falls back
54             # to using the filename in the UI and for grouping.
55 27 100 66     124 if ($frame->[0] && $frame->[0] eq $namespace && $frame->[1]) {
    50 66        
56 1         56 $frame->[0] = ''; # module
57 1         4 $frame->[3] = ''; # subroutine
58             } elsif ($frame->[3]) {
59             # Remove namespace from subroutine name
60 26         116 $frame->[3] =~ s/^${namespace}:://;
61             }
62             }
63             }
64              
65             1;