-
-
Notifications
You must be signed in to change notification settings - Fork 341
/
Copy pathSentryHub.m
653 lines (567 loc) · 22.3 KB
/
SentryHub.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
#import "SentryHub.h"
#import "SentryClient+Private.h"
#import "SentryCrashWrapper.h"
#import "SentryCurrentDateProvider.h"
#import "SentryDefaultCurrentDateProvider.h"
#import "SentryDependencyContainer.h"
#import "SentryEnvelope.h"
#import "SentryEnvelopeItemType.h"
#import "SentryEvent+Private.h"
#import "SentryFileManager.h"
#import "SentryId.h"
#import "SentryLog.h"
#import "SentryProfilesSampler.h"
#import "SentrySDK+Private.h"
#import "SentrySamplingContext.h"
#import "SentryScope.h"
#import "SentrySerialization.h"
#import "SentryTracer.h"
#import "SentryTracesSampler.h"
#import "SentryTransaction.h"
#import "SentryTransactionContext+Private.h"
NS_ASSUME_NONNULL_BEGIN
@interface
SentryHub ()
@property (nullable, nonatomic, strong) SentryClient *client;
@property (nullable, nonatomic, strong) SentryScope *scope;
@property (nonatomic, strong) SentryCrashWrapper *crashWrapper;
@property (nonatomic, strong) SentryTracesSampler *tracesSampler;
@property (nonatomic, strong) SentryProfilesSampler *profilesSampler;
@property (nonatomic, strong) id<SentryCurrentDateProvider> currentDateProvider;
@property (nonatomic, strong) NSMutableArray<id<SentryIntegrationProtocol>> *installedIntegrations;
@property (nonatomic, strong) NSMutableSet<NSString *> *installedIntegrationNames;
@end
@implementation SentryHub {
NSObject *_sessionLock;
NSObject *_integrationsLock;
}
- (instancetype)initWithClient:(nullable SentryClient *)client
andScope:(nullable SentryScope *)scope
{
if (self = [super init]) {
_client = client;
_scope = scope;
_sessionLock = [[NSObject alloc] init];
_integrationsLock = [[NSObject alloc] init];
_installedIntegrations = [[NSMutableArray alloc] init];
_installedIntegrationNames = [[NSMutableSet alloc] init];
_crashWrapper = [SentryCrashWrapper sharedInstance];
_tracesSampler = [[SentryTracesSampler alloc] initWithOptions:client.options];
#if SENTRY_TARGET_PROFILING_SUPPORTED
if (client.options.isProfilingEnabled) {
_profilesSampler = [[SentryProfilesSampler alloc] initWithOptions:client.options];
}
#endif
_currentDateProvider = [SentryDefaultCurrentDateProvider sharedInstance];
}
return self;
}
/** Internal constructor for testing */
- (instancetype)initWithClient:(nullable SentryClient *)client
andScope:(nullable SentryScope *)scope
andCrashWrapper:(SentryCrashWrapper *)crashWrapper
andCurrentDateProvider:(id<SentryCurrentDateProvider>)currentDateProvider
{
self = [self initWithClient:client andScope:scope];
_crashWrapper = crashWrapper;
_currentDateProvider = currentDateProvider;
return self;
}
- (void)startSession
{
SentrySession *lastSession = nil;
SentryScope *scope = self.scope;
SentryOptions *options = [_client options];
if (nil == options || nil == options.releaseName) {
[SentryLog
logWithMessage:[NSString stringWithFormat:@"No option or release to start a session."]
andLevel:kSentryLevelError];
return;
}
@synchronized(_sessionLock) {
if (nil != _session) {
lastSession = _session;
}
_session = [[SentrySession alloc] initWithReleaseName:options.releaseName];
NSString *environment = options.environment;
if (nil != environment) {
_session.environment = environment;
}
[scope applyToSession:_session];
[self storeCurrentSession:_session];
// TODO: Capture outside the lock. Not the reference in the scope.
[self captureSession:_session];
}
[lastSession endSessionExitedWithTimestamp:[self.currentDateProvider date]];
[self captureSession:lastSession];
}
- (void)endSession
{
[self endSessionWithTimestamp:[self.currentDateProvider date]];
}
- (void)endSessionWithTimestamp:(NSDate *)timestamp
{
SentrySession *currentSession = nil;
@synchronized(_sessionLock) {
currentSession = _session;
_session = nil;
[self deleteCurrentSession];
}
if (nil == currentSession) {
SENTRY_LOG_DEBUG(@"No session to end with timestamp.");
return;
}
[currentSession endSessionExitedWithTimestamp:timestamp];
[self captureSession:currentSession];
}
- (void)storeCurrentSession:(SentrySession *)session
{
[[_client fileManager] storeCurrentSession:session];
}
- (void)deleteCurrentSession
{
[[_client fileManager] deleteCurrentSession];
}
- (void)closeCachedSessionWithTimestamp:(nullable NSDate *)timestamp
{
SentryFileManager *fileManager = [_client fileManager];
SentrySession *session = [fileManager readCurrentSession];
if (nil == session) {
SENTRY_LOG_DEBUG(@"No cached session to close.");
return;
}
SENTRY_LOG_DEBUG(@"A cached session was found.");
// Make sure there's a client bound.
SentryClient *client = _client;
if (nil == client) {
SENTRY_LOG_DEBUG(@"No client bound.");
return;
}
// The crashed session is handled in SentryCrashIntegration. Checkout the comments there to find
// out more.
if (!self.crashWrapper.crashedLastLaunch) {
if (nil == timestamp) {
SENTRY_LOG_DEBUG(@"No timestamp to close session was provided. Closing as abnormal. "
@"Using session's start time %@",
session.started);
timestamp = session.started;
[session endSessionAbnormalWithTimestamp:timestamp];
} else {
SENTRY_LOG_DEBUG(@"Closing cached session as exited.");
[session endSessionExitedWithTimestamp:timestamp];
}
[self deleteCurrentSession];
[client captureSession:session];
}
}
- (void)captureSession:(nullable SentrySession *)session
{
if (nil != session) {
SentryClient *client = _client;
if (client.options.diagnosticLevel == kSentryLevelDebug) {
NSData *sessionData = [NSJSONSerialization dataWithJSONObject:[session serialize]
options:0
error:nil];
NSString *sessionString = [[NSString alloc] initWithData:sessionData
encoding:NSUTF8StringEncoding];
[SentryLog
logWithMessage:[NSString stringWithFormat:@"Capturing session with status: %@",
sessionString]
andLevel:kSentryLevelDebug];
}
[client captureSession:session];
}
}
- (nullable SentrySession *)incrementSessionErrors
{
SentrySession *sessionCopy = nil;
@synchronized(_sessionLock) {
if (nil != _session) {
[_session incrementErrors];
[self storeCurrentSession:_session];
sessionCopy = [_session copy];
}
}
return sessionCopy;
}
- (void)captureCrashEvent:(SentryEvent *)event
{
[self captureCrashEvent:event withScope:self.scope];
}
/**
* If autoSessionTracking is enabled we want to send the crash and the event together to get proper
* numbers for release health statistics. If there are multiple crash events to be sent on the start
* of the SDK there is currently no way to know which one belongs to the crashed session so we just
* send the session with the first crashed event we receive.
*/
- (void)captureCrashEvent:(SentryEvent *)event withScope:(SentryScope *)scope
{
event.isCrashEvent = YES;
SentryClient *client = _client;
if (nil == client) {
return;
}
// Check this condition first to avoid unnecessary I/O
if (client.options.enableAutoSessionTracking) {
SentryFileManager *fileManager = [client fileManager];
SentrySession *crashedSession = [fileManager readCrashedSession];
// It can be that there is no session yet, because autoSessionTracking was just enabled and
// there is a previous crash on disk. In this case we just send the crash event.
if (nil != crashedSession) {
[client captureCrashEvent:event withSession:crashedSession withScope:scope];
[fileManager deleteCrashedSession];
return;
}
}
[client captureCrashEvent:event withScope:scope];
}
- (SentryId *)captureTransaction:(SentryTransaction *)transaction withScope:(SentryScope *)scope
{
return [self captureTransaction:transaction withScope:scope additionalEnvelopeItems:@[]];
}
- (SentryId *)captureTransaction:(SentryTransaction *)transaction
withScope:(SentryScope *)scope
additionalEnvelopeItems:(NSArray<SentryEnvelopeItem *> *)additionalEnvelopeItems
{
SentrySampleDecision decision = transaction.trace.context.sampled;
if (decision != kSentrySampleDecisionYes) {
[self.client recordLostEvent:kSentryDataCategoryTransaction
reason:kSentryDiscardReasonSampleRate];
return SentryId.empty;
}
return [self captureEvent:transaction
withScope:scope
additionalEnvelopeItems:additionalEnvelopeItems];
}
- (SentryId *)captureEvent:(SentryEvent *)event
{
return [self captureEvent:event withScope:self.scope];
}
- (SentryId *)captureEvent:(SentryEvent *)event withScope:(SentryScope *)scope
{
return [self captureEvent:event withScope:scope additionalEnvelopeItems:@[]];
}
- (SentryId *)captureEvent:(SentryEvent *)event
withScope:(SentryScope *)scope
additionalEnvelopeItems:(NSArray<SentryEnvelopeItem *> *)additionalEnvelopeItems
{
SentryClient *client = _client;
if (nil != client) {
return [client captureEvent:event
withScope:scope
additionalEnvelopeItems:additionalEnvelopeItems];
}
return SentryId.empty;
}
- (id<SentrySpan>)startTransactionWithName:(NSString *)name operation:(NSString *)operation
{
return [self startTransactionWithContext:[[SentryTransactionContext alloc]
initWithName:name
nameSource:kSentryTransactionNameSourceCustom
operation:operation]];
}
- (id<SentrySpan>)startTransactionWithName:(NSString *)name
nameSource:(SentryTransactionNameSource)source
operation:(NSString *)operation
{
return [self
startTransactionWithContext:[[SentryTransactionContext alloc] initWithName:name
nameSource:source
operation:operation]];
}
- (id<SentrySpan>)startTransactionWithName:(NSString *)name
operation:(NSString *)operation
bindToScope:(BOOL)bindToScope
{
return [self startTransactionWithContext:[[SentryTransactionContext alloc]
initWithName:name
nameSource:kSentryTransactionNameSourceCustom
operation:operation]
bindToScope:bindToScope];
}
- (id<SentrySpan>)startTransactionWithName:(NSString *)name
nameSource:(SentryTransactionNameSource)source
operation:(NSString *)operation
bindToScope:(BOOL)bindToScope
{
return
[self startTransactionWithContext:[[SentryTransactionContext alloc] initWithName:name
nameSource:source
operation:operation]
bindToScope:bindToScope];
}
- (id<SentrySpan>)startTransactionWithContext:(SentryTransactionContext *)transactionContext
{
return [self startTransactionWithContext:transactionContext customSamplingContext:@{}];
}
- (id<SentrySpan>)startTransactionWithContext:(SentryTransactionContext *)transactionContext
bindToScope:(BOOL)bindToScope
{
return [self startTransactionWithContext:transactionContext
bindToScope:bindToScope
customSamplingContext:@{}];
}
- (id<SentrySpan>)startTransactionWithContext:(SentryTransactionContext *)transactionContext
customSamplingContext:(NSDictionary<NSString *, id> *)customSamplingContext
{
return [self startTransactionWithContext:transactionContext
bindToScope:false
customSamplingContext:customSamplingContext];
}
- (id<SentrySpan>)startTransactionWithContext:(SentryTransactionContext *)transactionContext
bindToScope:(BOOL)bindToScope
customSamplingContext:(NSDictionary<NSString *, id> *)customSamplingContext
{
return [self startTransactionWithContext:transactionContext
bindToScope:bindToScope
waitForChildren:NO
customSamplingContext:customSamplingContext];
}
- (id<SentrySpan>)startTransactionWithContext:(SentryTransactionContext *)transactionContext
bindToScope:(BOOL)bindToScope
waitForChildren:(BOOL)waitForChildren
customSamplingContext:(NSDictionary<NSString *, id> *)customSamplingContext
{
SentrySamplingContext *samplingContext =
[[SentrySamplingContext alloc] initWithTransactionContext:transactionContext
customSamplingContext:customSamplingContext];
SentryTracesSamplerDecision *samplerDecision = [_tracesSampler sample:samplingContext];
transactionContext.sampled = samplerDecision.decision;
transactionContext.sampleRate = samplerDecision.sampleRate;
SentryProfilesSamplerDecision *profilesSamplerDecision =
[_profilesSampler sample:samplingContext tracesSamplerDecision:samplerDecision];
id<SentrySpan> tracer = [[SentryTracer alloc] initWithTransactionContext:transactionContext
hub:self
profilesSamplerDecision:profilesSamplerDecision
waitForChildren:waitForChildren];
if (bindToScope)
self.scope.span = tracer;
return tracer;
}
- (SentryTracer *)startTransactionWithContext:(SentryTransactionContext *)transactionContext
bindToScope:(BOOL)bindToScope
customSamplingContext:(NSDictionary<NSString *, id> *)customSamplingContext
idleTimeout:(NSTimeInterval)idleTimeout
dispatchQueueWrapper:(SentryDispatchQueueWrapper *)dispatchQueueWrapper
{
SentrySamplingContext *samplingContext =
[[SentrySamplingContext alloc] initWithTransactionContext:transactionContext
customSamplingContext:customSamplingContext];
SentryTracesSamplerDecision *samplerDecision = [_tracesSampler sample:samplingContext];
transactionContext.sampled = samplerDecision.decision;
transactionContext.sampleRate = samplerDecision.sampleRate;
SentryProfilesSamplerDecision *profilesSamplerDecision =
[_profilesSampler sample:samplingContext tracesSamplerDecision:samplerDecision];
SentryTracer *tracer = [[SentryTracer alloc] initWithTransactionContext:transactionContext
hub:self
profilesSamplerDecision:profilesSamplerDecision
idleTimeout:idleTimeout
dispatchQueueWrapper:dispatchQueueWrapper];
if (bindToScope)
self.scope.span = tracer;
return tracer;
}
- (SentryId *)captureMessage:(NSString *)message
{
return [self captureMessage:message withScope:self.scope];
}
- (SentryId *)captureMessage:(NSString *)message withScope:(SentryScope *)scope
{
SentryClient *client = _client;
if (nil != client) {
return [client captureMessage:message withScope:scope];
}
return SentryId.empty;
}
- (SentryId *)captureError:(NSError *)error
{
return [self captureError:error withScope:self.scope];
}
- (SentryId *)captureError:(NSError *)error withScope:(SentryScope *)scope
{
SentrySession *currentSession = _session;
SentryClient *client = _client;
if (nil != client) {
if (nil != currentSession) {
return [client captureError:error
withScope:scope
incrementSessionErrors:^(void) { return [self incrementSessionErrors]; }];
} else {
return [client captureError:error withScope:scope];
}
}
return SentryId.empty;
}
- (SentryId *)captureException:(NSException *)exception
{
return [self captureException:exception withScope:self.scope];
}
- (SentryId *)captureException:(NSException *)exception withScope:(SentryScope *)scope
{
SentrySession *currentSession = _session;
SentryClient *client = _client;
if (nil != client) {
if (nil != currentSession) {
return [client captureException:exception
withScope:scope
incrementSessionErrors:^(void) { return [self incrementSessionErrors]; }];
} else {
return [client captureException:exception withScope:scope];
}
}
return SentryId.empty;
}
- (void)captureUserFeedback:(SentryUserFeedback *)userFeedback
{
SentryClient *client = _client;
if (nil != client) {
[client captureUserFeedback:userFeedback];
}
}
- (void)addBreadcrumb:(SentryBreadcrumb *)crumb
{
SentryOptions *options = [[self client] options];
if (options.maxBreadcrumbs < 1) {
return;
}
SentryBeforeBreadcrumbCallback callback = [options beforeBreadcrumb];
if (nil != callback) {
crumb = callback(crumb);
}
if (nil == crumb) {
SENTRY_LOG_DEBUG(@"Discarded Breadcrumb in `beforeBreadcrumb`");
return;
}
[self.scope addBreadcrumb:crumb];
}
- (nullable SentryClient *)getClient
{
return _client;
}
- (void)bindClient:(nullable SentryClient *)client
{
self.client = client;
}
- (SentryScope *)scope
{
@synchronized(self) {
if (_scope == nil) {
SentryClient *client = _client;
if (nil != client) {
_scope = [[SentryScope alloc] initWithMaxBreadcrumbs:client.options.maxBreadcrumbs];
} else {
_scope = [[SentryScope alloc] init];
}
}
return _scope;
}
}
- (void)configureScope:(void (^)(SentryScope *scope))callback
{
SentryScope *scope = self.scope;
SentryClient *client = _client;
if (nil != client && nil != scope) {
callback(scope);
}
}
/**
* Checks if a specific Integration (`integrationClass`) has been installed.
* @return BOOL If instance of `integrationClass` exists within
* `SentryHub.installedIntegrations`.
*/
- (BOOL)isIntegrationInstalled:(Class)integrationClass
{
@synchronized(_integrationsLock) {
for (id<SentryIntegrationProtocol> item in _installedIntegrations) {
if ([item isKindOfClass:integrationClass]) {
return YES;
}
}
return NO;
}
}
- (BOOL)hasIntegration:(NSString *)integrationName
{
// installedIntegrations and installedIntegrationNames share the same lock.
// Instead of creating an extra lock object, we use _installedIntegrations.
@synchronized(_integrationsLock) {
return [_installedIntegrationNames containsObject:integrationName];
}
}
- (void)addInstalledIntegration:(id<SentryIntegrationProtocol>)integration name:(NSString *)name
{
@synchronized(_integrationsLock) {
[_installedIntegrations addObject:integration];
[_installedIntegrationNames addObject:name];
}
}
- (void)removeAllIntegrations
{
@synchronized(_integrationsLock) {
[_installedIntegrations removeAllObjects];
[_installedIntegrationNames removeAllObjects];
}
}
- (NSArray<id<SentryIntegrationProtocol>> *)installedIntegrations
{
@synchronized(_integrationsLock) {
return _installedIntegrations.copy;
}
}
- (NSSet<NSString *> *)installedIntegrationNames
{
@synchronized(_integrationsLock) {
return _installedIntegrationNames.copy;
}
}
- (void)setUser:(nullable SentryUser *)user
{
SentryScope *scope = self.scope;
if (nil != scope) {
[scope setUser:user];
}
}
- (void)captureEnvelope:(SentryEnvelope *)envelope
{
SentryClient *client = _client;
if (nil == client) {
return;
}
[client captureEnvelope:[self updateSessionState:envelope]];
}
- (SentryEnvelope *)updateSessionState:(SentryEnvelope *)envelope
{
if ([self envelopeContainsEventWithErrorOrHigher:envelope.items]) {
SentrySession *currentSession = [self incrementSessionErrors];
if (nil != currentSession) {
// Create a new envelope with the session update
NSMutableArray<SentryEnvelopeItem *> *itemsToSend =
[[NSMutableArray alloc] initWithArray:envelope.items];
[itemsToSend addObject:[[SentryEnvelopeItem alloc] initWithSession:currentSession]];
return [[SentryEnvelope alloc] initWithHeader:envelope.header items:itemsToSend];
}
}
return envelope;
}
- (BOOL)envelopeContainsEventWithErrorOrHigher:(NSArray<SentryEnvelopeItem *> *)items
{
for (SentryEnvelopeItem *item in items) {
if ([item.header.type isEqualToString:SentryEnvelopeItemTypeEvent]) {
// If there is no level the default is error
SentryLevel level = [SentrySerialization levelFromData:item.data];
if (level >= kSentryLevelError) {
return YES;
}
}
}
return NO;
}
- (void)flush:(NSTimeInterval)timeout
{
SentryClient *client = _client;
if (nil != client) {
[client flush:timeout];
}
}
@end
NS_ASSUME_NONNULL_END