blib/lib/Sentry/Client.pm untested

filter: Statementstmt 0.0% 0 / 104 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 37 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 32 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 206 CC 8 SCAR 13.3
CC
8
Cov
0%
_prepare_event
12.0
_die
6.0
event_from_exception
6.0
CRAP 3.8
1 package Sentry::Client;
2 use Mojo::Base -base, -signatures;
3  
4 use Mojo::Home;
5 use Mojo::Util 'dumper';
6 use Sentry::DSN;
7 use Sentry::Hub::Scope;
8 use Sentry::Integration;
9 use Sentry::Logger 'logger';
10 use Sentry::SourceFileRegistry;
11 use Sentry::Stacktrace;
12 use Sentry::Transport::Http;
13 use Sentry::Util qw(uuid4 truncate);
14 use Time::HiRes;
15 use Try::Tiny;
16  
17 has _dsn => sub ($self) { Sentry::DSN->parse($self->_options->{dsn}) };
18 has _options => sub { {} };
19 has _transport =>
20 sub ($self) { Sentry::Transport::Http->new(dsn => $self->_dsn) };
21 has scope => sub { Sentry::Hub::Scope->new };
22 has integrations => sub ($self) { $self->_options->{integrations} // [] };
23  
24 sub setup_integrations ($self) {
25 Sentry::Integration->setup($self->integrations);
26 }
27  
28 # (alternatively normal constructor) This takes typically an object with options + dsn.
29 sub from_config ($package, $config) { }
30  
31 sub event_from_message ($self, $message, $level = Sentry::Severity->Info,
32 $hint = undef) {
33 my %event = (
34 event_id => $hint && $hint->{event_id},
35 level => $level,
36 message => $message,
37 );
38  
39 return \%event;
40 }
41  
42 sub capture_message ($self, $message, $level = undef, $hint = undef,
43 $scope = undef) {
44 my $event = $self->event_from_message($message, $level, $hint);
45  
46 return $self->_capture_event($event, $hint, $scope);
47 }
48  
49 sub capture_event ($self, $event, $hint = undef, $scope = undef) {
50 my $event_id = ($hint // {})->{event_id};
51  
52 return $self->_capture_event($event, $hint, $scope);
53 }
54  
55 sub _map_file_to_context ($self, $file, $line) {
56 return $self->_source_file_registry->get_context_lines($file, $line);
57 }
58  
59 sub is_file_of_app ($frame) {
60 return scalar $frame->filename !~ m{\A /}xms;
61 }
62  
63 sub _die { CORE::die ref $_[0] ? $_[0] : Mojo::Exception->new(shift)->trace }
64  
65 sub event_from_exception ($self, $exception, $hint = undef, $scope = undef) {
66 if (!ref($exception)) {
67 $exception = Mojo::Exception->new($exception)->trace;
68 }
69  
70 my $stacktrace = Sentry::Stacktrace->new({
71 exception => $exception,
72 frame_filter => sub ($frame) {
73 index($frame->package, 'Sentry') == -1
74 && $frame->package !~ m{(Class::MOP|CGI::Carp|Try::Tiny)}xms;
75 },
76 });
77  
78 return {
79 exception => {
80 values => [{
81 type => ref($exception),
82 value => $exception->to_string,
83 module => ref($exception),
84 stacktrace => $stacktrace,
85 }]
86 }
87 };
88 }
89  
90 sub capture_exception ($self, $exception, $hint = undef, $scope = undef) {
91 my $event = $self->event_from_exception($exception, $hint);
92  
93 return $self->_capture_event($event, $hint, $scope);
94 }
95  
96 sub _capture_event ($self, $event, $hint = undef, $scope = undef) {
97 my $event_id;
98  
99 try {
100 $event_id = $self->_process_event($event, $hint, $scope)->{event_id};
101 } catch {
102 logger->error($_);
103 };
104  
105 return $event_id;
106 }
107  
108 # Captures the event by merging it with other data with defaults from the
109 # client. In addition, if a scope is passed to this system, the data from the
110 # scope passes it to the internal transport.
111 # sub capture_event ($self, $event, $scope) { }
112  
113 # Flushes out the queue for up to timeout seconds. If the client can guarantee
114 # delivery of events only up to the current point in time this is preferred.
115 # This might block for timeout seconds. The client should be disabled or
116 # disposed after close is called
117 sub close ($self, $timeout) { }
118  
119 # Same as close difference is that the client is NOT disposed after calling flush
120 sub flush ($self, $timeout) { }
121  
122 # Applies `normalize` function on necessary `Event` attributes to make them safe for serialization.
123 # Normalized keys:
124 # - `breadcrumbs.data`
125 # - `user`
126 # - `contexts`
127 # - `extra`
128 sub _normalize_event ($self, $event) {
129 my %normalized = ($event->%*,);
130 return \%normalized;
131 }
132  
133 sub _apply_client_options ($self, $event) {
134 my $options = $self->_options;
135 my $max_value_length = $options->{max_value_length} // 250;
136  
137 $event->{environment} //= $options->{environment} // 'production';
138 $event->{dist} //= $options->{dist};
139  
140 $event->{message} = truncate($event->{message}, $max_value_length)
141 if $event->{message};
142  
143 return;
144 }
145  
146 sub _apply_integrations_metadata ($self, $event) {
147 $event->{sdk} //= {};
148  
149 my @integrations = $self->integrations->@*;
150 $event->{sdk}->{integrations} = [map { ref($_) } @integrations]
151 if @integrations;
152 }
153  
154 # Adds common information to events.
155 #
156 # The information includes release and environment from `options`,
157 # breadcrumbs and context (extra, tags and user) from the scope.
158 #
159 # Information that is already present in the event is never overwritten. For
160 # nested objects, such as the context, keys are merged.
161 #
162 # @param event The original event.
163 # @param hint May contain additional information about the original exception.
164 # @param scope A scope containing event metadata.
165 # @returns A new event with more information.
166 sub _prepare_event ($self, $event, $scope, $hint = undef) {
167 my %prepared = (
168 $event->%*,
169 sdk => $self->_options->{_metadata}{sdk},
170 platform => 'perl',
171 event_id => $event->{event_id} // ($hint // {})->{event_id} // uuid4(),
172 timestamp => $event->{timestamp} // time,
173 );
174  
175 $self->_apply_client_options(\%prepared);
176 $self->_apply_integrations_metadata(\%prepared);
177  
178 # If we have scope given to us, use it as the base for further modifications.
179 # This allows us to prevent unnecessary copying of data if `capture_context`
180 # is not provided.
181 my $final_scope = $scope;
182 if (exists(($hint // {})->{capture_context})) {
183 $final_scope = $scope->clone()->update($hint->{captureconsole});
184 }
185  
186 # We prepare the result here with a resolved Event.
187 my $result = \%prepared;
188  
189 # This should be the last thing called, since we want that
190 # {@link Hub.addEventProcessor} gets the finished prepared event.
191 if ($final_scope) {
192  
193 # In case we have a hub we reassign it.
194 $result = $final_scope->apply_to_event(\%prepared, $hint);
195 }
196  
197 return $self->_normalize_event($result);
198 }
199  
200 sub _process_event ($self, $event, $hint, $scope) {
201 my $prepared = $self->_prepare_event($event, $scope, $hint);
202  
203 my $is_transaction = ($event->{type} // '') eq 'transaction';
204  
205 die 'An event processor returned undef, will not send event.'
206 unless $prepared;
207  
208 $self->_send_event($prepared);
209  
210 return $prepared;
211 }
212  
213 sub _send_event ($self, $event) {
214 $self->_transport->send($event);
215 return;
216 }
217  
218 1;
219