File Coverage

blib/lib/Sentry/Hub.pm
Criterion Covered Total %
statement 145 167 86.8
branch 6 12 50.0
condition 8 15 53.3
subroutine 29 31 93.5
pod 0 17 0.0
total 188 242 77.6


line stmt bran cond sub pod time code
1             use Mojo::Base -base, -signatures;
2 4     4   424610  
  4         21  
  4         55  
3             use Mojo::Util 'dumper';
4 4     4   826 use Sentry::Hub::Scope;
  4         8  
  4         156  
5 4     4   774 use Sentry::Logger 'logger';
  4         8  
  4         27  
6 4     4   1510 use Sentry::Severity;
  4         11  
  4         189  
7 4     4   28 use Sentry::Tracing::SamplingMethod;
  4         15  
  4         23  
8 4     4   1551 use Sentry::Tracing::Transaction;
  4         11  
  4         36  
9 4     4   111 use Sentry::Util qw(uuid4);
  4         6  
  4         24  
10 4     4   102 use Time::HiRes qw(time);
  4         5  
  4         151  
11 4     4   27 use Try::Tiny;
  4         8  
  4         17  
12 4     4   2005  
  4         4374  
  4         5341  
13             my $Instance;
14              
15             has _last_event_id => undef;
16             has _stack => sub { [{}] };
17             has client => undef;
18             has scopes => sub { [Sentry::Hub::Scope->new] };
19              
20             $Instance = Sentry::Hub->new($options);
21 0     0 0 0 }
  0         0  
  0         0  
  0         0  
22 0         0  
23             $self->scopes([Sentry::Hub::Scope->new]);
24             }
25 16     16 0 38  
  16         21  
  16         18  
26 16         76 $self->client($client);
27             $client->setup_integrations();
28             }
29 17     17 0 23  
  17         30  
  17         19  
  17         24  
30 17         44 return @{ $package->get_current_hub()->scopes }[-1];
31 17         266 }
32              
33             $Instance //= Sentry::Hub->new();
34 70     70 0 87 return $Instance;
  70         84  
  70         77  
35 70         76 }
  70         110  
36              
37             $cb->($self->get_current_scope);
38             }
39 186   66 186 0 17960  
40 186         374 my $scope = $self->get_current_scope->clone;
41             push $self->scopes->@*, $scope;
42             return $scope;
43 34     34 0 34 }
  34         37  
  34         36  
  34         35  
44 34         52  
45             my $scope = $self->push_scope;
46              
47 8     8 0 11 try {
  8         9  
  8         8  
48 8         14 $cb->($scope);
49 8         21 } finally {
50 8         34 $self->pop_scope;
51             };
52 8     8 0 13 }
  8         11  
  8         9  
  8         10  
  8         16  
53              
54 3     3 0 6 return $self->get_current_scope;
  3         6  
  3         3  
  3         4  
55 3         6 }
56              
57             my $client = $self->client;
58 3     3   161 my $scope = $self->get_current_scope;
59              
60 3     3   93 if ($client->can($method)) {
61 3         19 $client->$method(@args, $scope);
62             } else {
63             warn "Unknown method: $method";
64 2     2 0 15 }
  2         3  
  2         4  
65 2         5 }
66              
67             $self->_last_event_id(uuid4());
68 19     19   25 return $self->_last_event_id;
  19         26  
  19         21  
  19         27  
  19         28  
69 19         38 }
70 19         70  
71             $self, $message,
72 19 50       160 $level = Sentry::Severity->Info,
73 19         57 $hint = undef
74             ) {
75 0         0 my $event_id = $self->_new_event_id();
76              
77             $self->_invoke_client('capture_message', $message, $level,
78             { event_id => $event_id });
79 19     19   23  
  19         21  
  19         20  
80 19         54 return $event_id;
81 19         122 }
82              
83             my $event_id = $self->_new_event_id();
84              
85 8         10 $self->_invoke_client('capture_exception', $exception,
  8         11  
86 8         17 { event_id => $event_id });
87              
88 8     8 0 11 return $event_id;
  8         11  
  8         9  
89 8         17 }
90              
91 8         56 my $event_id = $self->_new_event_id();
92              
93             $self->_invoke_client('capture_event', $event,
94 8         47 { $hint->%*, event_id => $event_id });
95              
96             return $event_id;
97 2     2 0 5 }
  2         4  
  2         2  
  2         4  
  2         3  
98 2         5  
99             $self->get_current_scope->add_breadcrumb($crumb);
100 2         15 }
101              
102             $cb->($self);
103 2         20 }
104              
105             my $client = $self->client;
106 9     9 0 37 my $options = ($client && $client->get_options) // {};
  9         13  
  9         10  
  9         16  
  9         11  
107 9         18  
108             # nothing to do if there's no client or if tracing is disabled
109 9         66 if (!$client || !$options->{traces_sample_rate}) {
110             $transaction->sampled(0);
111             return $transaction;
112 9         38 }
113              
114             # if the user has forced a sampling decision by passing a `sampled` value in
115 5     5 0 364 # their transaction context, go with that
  5         7  
  5         9  
  5         7  
  5         8  
116 5         10 if (defined $transaction->sampled) {
117             $transaction->tags({
118             $transaction->tags->%*,
119 0     0 0 0 __sentry_samplingMethod => Sentry::Tracing::SamplingMethod->Explicit,
  0         0  
  0         0  
  0         0  
120 0         0 });
121              
122             return $transaction;
123 9     9 0 14 }
  9         12  
  9         14  
  9         11  
  9         12  
124 9         19  
125 9   33     65 my $sample_rate;
      50        
126              
127             if (defined $sampling_context->{parent_sampled}) {
128 9 50 33     88 $sample_rate = $sampling_context->{parent_sampled};
129 0         0 $transaction->tags({
130 0         0 $transaction->tags->%*,
131             __sentry_samplingMethod => Sentry::Tracing::SamplingMethod->Inheritance,
132             });
133             } else {
134             $sample_rate = $options->{traces_sample_rate};
135 9 50       29 $transaction->tags({
136 0         0 $transaction->tags->%*,
137             __sentry_samplingMethod => Sentry::Tracing::SamplingMethod->Rate,
138             __sentry_sampleRate => $sample_rate,
139             });
140             }
141 0         0  
142             if (!$sample_rate) {
143             logger->log(
144 9         55 'Discarding transaction because a negative sampling decision was inherited or tracesSampleRate is set to 0',
145             'Tracing'
146 9 50       25 );
147 0         0 $transaction->sampled(0);
148 0         0 return $transaction;
149             }
150              
151             # Now we roll the dice. Math.random is inclusive of 0, but not of 1, so
152             # strict < is safe here. In case sampleRate is a boolean, the < comparison
153 9         19 # will cause it to be automatically cast to 1 if it's true and 0 if it's
154 9         43 # false.
155             $transaction->sampled(rand() < $sample_rate);
156              
157             # if we're not going to keep it, we're done
158             if (!$transaction->sampled) {
159             logger->log(
160             "Discarding transaction because it's not included in the random sample (sampling rate = $sample_rate)",
161 9 50       55 'Tracing',
162 0         0 );
163             return $transaction;
164             }
165              
166 0         0 logger->log(
167 0         0 sprintf(
168             'Starting %s transaction - %s',
169             $transaction->op // '(unknown op)',
170             $transaction->name
171             ),
172             'Tracing',
173             );
174 9         37 return $transaction;
175             }
176              
177 9 50       54 my $transaction = Sentry::Tracing::Transaction->new(
178 0         0 { $context->%*, _hub => $self, start_timestamp => time });
179              
180             return $self->sample(
181             $transaction,
182 0         0 {
183             parent_sampled => $context->{parent_sampled},
184             ($custom_sampling_context // {})->%*,
185             }
186 9   50     59 );
187             }
188              
189             1;