File Coverage

blib/lib/Sentry/Hub/Scope.pm
Criterion Covered Total %
statement 127 156 81.4
branch 14 22 63.6
condition 7 12 58.3
subroutine 23 28 82.1
pod 0 20 0.0
total 171 238 71.8


line stmt bran cond sub pod time code
1             package Sentry::Hub::Scope;
2 7     7   274362 use Mojo::Base -base, -signatures;
  7         15  
  7         50  
3              
4 7     7   5944 use Clone qw();
  7         4530  
  7         285  
5 7     7   46 use Mojo::Util 'dumper';
  7         15  
  7         380  
6 7     7   5485 use Sentry::Severity;
  7         32  
  7         90  
7 7     7   3960 use Sentry::Tracing::Span;
  7         28  
  7         66  
8 7     7   391 use Sentry::Util 'merge';
  7         10  
  7         367  
9 7     7   48 use Time::HiRes;
  7         12  
  7         35  
10              
11             has breadcrumbs => sub { [] };
12             has contexts => sub { {} };
13             has error_event_processors => sub { [] };
14             has event_processors => sub { [] };
15             has extra => sub { {} };
16             has fingerprint => sub { [] };
17             has level => undef;
18             has span => undef;
19             has tags => sub { {} };
20             has transaction_name => undef;
21             has user => undef;
22              
23             my $DEFAULT_MAX_BREADCRUMBS = 100;
24              
25 19     19 0 127 sub set_span ($self, $span) {
  19         37  
  19         61  
  19         27  
26 19         76 $self->span($span);
27 19         418 return $self;
28             }
29              
30 1     1 0 9 sub set_user ($self, $user) {
  1         2  
  1         3  
  1         2  
31 1         5 $self->user($user);
32             }
33              
34 0     0 0 0 sub set_extra ($self, $name, $value) {
  0         0  
  0         0  
  0         0  
  0         0  
35 0         0 $self->extra->{$name} = $value;
36             }
37              
38 1     1 0 542 sub set_extras ($self, $extras) {
  1         4  
  1         1  
  1         2  
39 1         4 for my $key (%$extras) {
40 2         11 $self->extra->{$key} = $extras->{$key};
41             }
42             }
43              
44 1     1 0 18 sub set_tag ($self, $key, $value) {
  1         2  
  1         3  
  1         3  
  1         2  
45 1         18 $self->tags->{$key} = $value;
46             }
47              
48 19     19 0 194 sub set_tags ($self, $tags) {
  19         31  
  19         24  
  19         28  
49 19         50 $self->tags({ $self->tags->%*, $tags->%* });
50             }
51              
52 0     0 0 0 sub set_context ($self, $key, $context = undef) {
  0         0  
  0         0  
  0         0  
  0         0  
53 0 0       0 if (not defined $context) {
54 0         0 delete $self->contexts->{$key};
55             } else {
56 0         0 $self->contexts->{$key} = $context;
57             }
58              
59             # $self->_notify_scope_listeners();
60              
61 0         0 return $self;
62             }
63              
64 1     1 0 33 sub set_level ($self, $level) {
  1         4  
  1         2  
  1         2  
65 1         5 $self->level($level);
66             }
67              
68 10     10 0 35 sub set_transaction_name ($self, $name) {
  10         17  
  10         15  
  10         12  
69 10         35 $self->transaction_name($name);
70 10         67 return $self;
71             }
72              
73 11     11 0 100 sub get_span ($self) {
  11         22  
  11         17  
74 11         68 return $self->span;
75             }
76              
77 0     0 0 0 sub set_fingerprint ($self, $fingerprint) {
  0         0  
  0         0  
  0         0  
78 0         0 $self->fingerprint($fingerprint);
79             }
80              
81 4     4 0 6 sub add_event_processor ($self, $event_processor) {
  4         5  
  4         5  
  4         4  
82 4         13 push $self->event_processors->@*, $event_processor;
83             }
84              
85 0     0 0 0 sub add_error_processor ($self, $error_event_processor) {
  0         0  
  0         0  
  0         0  
86 0         0 push $self->error_event_processors->@*, $error_event_processor;
87             }
88              
89             sub clear ($self) {
90              
91             # Resets a scope to default values while keeping all registered event
92             # processors. This does not affect either child or parent scopes
93             }
94              
95 24     24 0 588 sub add_breadcrumb ($self, $breadcrumb) {
  24         53  
  24         35  
  24         28  
96 24   66     163 $breadcrumb->{timestamp} //= time;
97              
98 24         69 my $breadcrumbs = $self->breadcrumbs;
99              
100 24   66     151 my $max_crumbs = $ENV{SENTRY_MAX_BREADCRUMBS} || $DEFAULT_MAX_BREADCRUMBS;
101 24 100       72 if (scalar $breadcrumbs->@* >= $max_crumbs) {
102 1         3 shift $breadcrumbs->@*;
103             }
104              
105 24         77 push $breadcrumbs->@*, $breadcrumb;
106             }
107              
108 0     0 0 0 sub clear_breadcrumbs ($self) {
  0         0  
  0         0  
109 0         0 $self->breadcrumbs([]);
110             }
111              
112             # Applies fingerprint from the scope to the event if there's one,
113             # uses message if there's one instead or get rid of empty fingerprint
114 27     27   41 sub _apply_fingerprint ($self, $event) {
  27         42  
  27         38  
  27         50  
115 27   50     137 $event->{fingerprint} //= [];
116              
117             $event->{fingerprint} = [$event->{fingerprint}]
118 27 50       96 if ref($event->{fingerprint} ne 'ARRAY');
119              
120 27         99 $event->{fingerprint} = [$event->{fingerprint}->@*, $self->fingerprint->@*];
121              
122 27 50       158 delete $event->{fingerprint} unless scalar $event->{fingerprint}->@*;
123             }
124              
125             # Applies the scope data to the given event object. This also applies the event
126             # processors stored in the scope internally. Some implementations might want to
127             # set a max breadcrumbs count here.
128 27     27 0 1178 sub apply_to_event ($self, $event, $hint = undef) {
  27         58  
  27         35  
  27         41  
  27         63  
129 27 50       79 merge($event, $self, 'extra') if $self->extra;
130 27 50       128 merge($event, $self, 'tags') if $self->tags;
131 27 50       96 merge($event, $self, 'user') if $self->user;
132 27 50       178 merge($event, $self, 'contexts') if $self->contexts;
133              
134 27 100       72 $event->{level} = $self->level if $self->level;
135 27 100       137 $event->{transaction} = $self->transaction_name if $self->transaction_name;
136              
137 27 100       217 if ($self->span) {
138 24         125 $event->{request} = $self->span->request;
139              
140             $event->{contexts} = {
141             trace => $self->span->get_trace_context(),
142 24   50     186 ($event->{contexts} // {})->%*
143             };
144             }
145              
146 27         412 $self->_apply_fingerprint($event);
147              
148             $event->{breadcrumbs}
149 27   50     180 = [($event->{breadcrumbs} // [])->@*, $self->breadcrumbs->@*];
150              
151             my @event_processors
152 27         207 = (get_global_event_processors()->@*, $self->event_processors->@*);
153              
154 27         108 foreach my $processor (@event_processors) {
155 9         35 $event = $processor->($event, $hint);
156             }
157              
158 27         81 return $event;
159             }
160              
161 16     16 0 120 sub clone ($self) {
  16         30  
  16         22  
162 16         3483 Clone::clone($self);
163             }
164              
165 1     1 0 1 sub update ($self, $fields) {
  1         2  
  1         1  
  1         1  
166 1         3 for (keys $fields->%*) {
167 1         2 my $methodName = "set_$_";
168 1         4 $self->$methodName($fields->{$_});
169             }
170 1         12 return $self;
171             }
172              
173 47     47 0 467136 sub get_global_event_processors () {
  47         60  
174 47         95 state $processors = [];
175 47         142 return $processors;
176             }
177              
178 2     2 0 933 sub add_global_event_processor ($processor) {
  2         5  
  2         4  
179 2         5 push get_global_event_processors()->@*, $processor;
180             }
181              
182             1;