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   448381  
  4         27  
  4         21  
3             use Mojo::Util 'dumper';
4 4     4   779 use Sentry::Hub::Scope;
  4         7  
  4         151  
5 4     4   781 use Sentry::Logger 'logger';
  4         8  
  4         22  
6 4     4   1714 use Sentry::Severity;
  4         9  
  4         178  
7 4     4   23 use Sentry::Tracing::SamplingMethod;
  4         17  
  4         18  
8 4     4   1539 use Sentry::Tracing::Transaction;
  4         10  
  4         36  
9 4     4   113 use Sentry::Util qw(uuid4);
  4         6  
  4         26  
10 4     4   101 use Time::HiRes qw(time);
  4         6  
  4         156  
11 4     4   19 use Try::Tiny;
  4         7  
  4         23  
12 4     4   2065  
  4         4396  
  4         5393  
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 51  
  16         23  
  16         21  
26 16         66 $self->client($client);
27             $client->setup_integrations();
28             }
29 17     17 0 24  
  17         22  
  17         22  
  17         19  
30 17         49 return @{ $package->get_current_hub()->scopes }[-1];
31 17         306 }
32              
33             $Instance //= Sentry::Hub->new();
34 70     70 0 92 return $Instance;
  70         74  
  70         73  
35 70         77 }
  70         124  
36              
37             $cb->($self->get_current_scope);
38             }
39 186   66 186 0 16027  
40 186         394 my $scope = $self->get_current_scope->clone;
41             push $self->scopes->@*, $scope;
42             return $scope;
43 34     34 0 45 }
  34         37  
  34         46  
  34         37  
44 34         62  
45             my $scope = $self->push_scope;
46              
47 8     8 0 10 try {
  8         12  
  8         10  
48 8         19 $cb->($scope);
49 8         23 } finally {
50 8         36 $self->pop_scope;
51             };
52 8     8 0 12 }
  8         12  
  8         10  
  8         13  
  8         19  
53              
54 3     3 0 3 return $self->get_current_scope;
  3         4  
  3         3  
  3         4  
55 3         8 }
56              
57             my $client = $self->client;
58 3     3   155 my $scope = $self->get_current_scope;
59              
60 3     3   85 if ($client->can($method)) {
61 3         16 $client->$method(@args, $scope);
62             } else {
63             warn "Unknown method: $method";
64 2     2 0 14 }
  2         3  
  2         3  
65 2         4 }
66              
67             $self->_last_event_id(uuid4());
68 19     19   35 return $self->_last_event_id;
  19         25  
  19         23  
  19         27  
  19         19  
69 19         51 }
70 19         69  
71             $self, $message,
72 19 50       160 $level = Sentry::Severity->Info,
73 19         73 $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   26  
  19         23  
  19         20  
80 19         59 return $event_id;
81 19         120 }
82              
83             my $event_id = $self->_new_event_id();
84              
85 8         11 $self->_invoke_client('capture_exception', $exception,
  8         14  
86 8         21 { event_id => $event_id });
87              
88 8     8 0 16 return $event_id;
  8         12  
  8         16  
89 8         21 }
90              
91 8         55 my $event_id = $self->_new_event_id();
92              
93             $self->_invoke_client('capture_event', $event,
94 8         52 { $hint->%*, event_id => $event_id });
95              
96             return $event_id;
97 2     2 0 2 }
  2         4  
  2         3  
  2         4  
  2         3  
98 2         5  
99             $self->get_current_scope->add_breadcrumb($crumb);
100 2         13 }
101              
102             $cb->($self);
103 2         32 }
104              
105             my $client = $self->client;
106 9     9 0 44 my $options = ($client && $client->get_options) // {};
  9         17  
  9         12  
  9         15  
  9         11  
107 9         18  
108             # nothing to do if there's no client or if tracing is disabled
109 9         80 if (!$client || !$options->{traces_sample_rate}) {
110             $transaction->sampled(0);
111             return $transaction;
112 9         43 }
113              
114             # if the user has forced a sampling decision by passing a `sampled` value in
115 5     5 0 383 # their transaction context, go with that
  5         9  
  5         17  
  5         8  
  5         6  
116 5         12 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 19 }
  9         14  
  9         12  
  9         12  
  9         12  
124 9         67  
125 9   33     99 my $sample_rate;
      50        
126              
127             if (defined $sampling_context->{parent_sampled}) {
128 9 50 33     103 $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       35 $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         58 '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         20 # will cause it to be automatically cast to 1 if it's true and 0 if it's
154 9         55 # 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       68 '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         40 return $transaction;
175             }
176              
177 9 50       61 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     71 );
187             }
188              
189             1;