Skip to content

Commit

Permalink
feat: add internal hook for mutating a session payload (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench authored and kattrali committed May 1, 2019
1 parent 7118e54 commit 5aa055d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
21 changes: 21 additions & 0 deletions Source/BugsnagConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ typedef void (^BugsnagNotifyBlock)(BugsnagCrashReport *_Nonnull report);
typedef bool (^BugsnagBeforeSendBlock)(NSDictionary *_Nonnull rawEventData,
BugsnagCrashReport *_Nonnull reports);

/**
* A configuration block for modifying a session. Intended for internal usage only.
*
* @param sessionPayload The session about to be delivered
*/
typedef void(^BeforeSendSession)(NSMutableDictionary *_Nonnull sessionPayload);

/**
* A handler for modifying data before sending it to Bugsnag
*
Expand Down Expand Up @@ -126,6 +133,13 @@ BugsnagBreadcrumbs *breadcrumbs;
*/
@property(readonly, strong, nullable)
NSArray<BugsnagBeforeSendBlock> *beforeSendBlocks;

/**
* Hooks for modifying sessions before they are sent to Bugsnag. Intended for internal use only by React Native/Unity.
*/
@property(readonly, strong, nullable)
NSArray<BeforeSendSession> *beforeSendSessionBlocks;

/**
* Optional handler invoked when a crash or fatal signal occurs
*/
Expand Down Expand Up @@ -205,6 +219,13 @@ BugsnagBreadcrumbs *breadcrumbs;
*/
- (void)addBeforeSendBlock:(BugsnagBeforeSendBlock _Nonnull)block;

/**
* Add a callback to be invoked before a session is sent to Bugsnag. Intended for internal usage only.
*
* @param block A block which can modify the session
*/
- (void)addBeforeSendSession:(BeforeSendSession _Nonnull)block;

/**
* Clear all callbacks
*/
Expand Down
6 changes: 6 additions & 0 deletions Source/BugsnagConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ @interface BugsnagNotifier ()
@interface BugsnagConfiguration ()
@property(nonatomic, readwrite, strong) NSMutableArray *beforeNotifyHooks;
@property(nonatomic, readwrite, strong) NSMutableArray *beforeSendBlocks;
@property(nonatomic, readwrite, strong) NSMutableArray *beforeSendSessionBlocks;
@end

@implementation BugsnagConfiguration
Expand All @@ -62,6 +63,7 @@ - (id)init {
_notifyURL = [NSURL URLWithString:BSGDefaultNotifyUrl];
_beforeNotifyHooks = [NSMutableArray new];
_beforeSendBlocks = [NSMutableArray new];
_beforeSendSessionBlocks = [NSMutableArray new];
_notifyReleaseStages = nil;
_breadcrumbs = [BugsnagBreadcrumbs new];
_automaticallyCollectBreadcrumbs = YES;
Expand Down Expand Up @@ -106,6 +108,10 @@ - (void)addBeforeSendBlock:(BugsnagBeforeSendBlock)block {
[(NSMutableArray *)self.beforeSendBlocks addObject:[block copy]];
}

- (void)addBeforeSendSession:(BeforeSendSession)block {
[(NSMutableArray *)self.beforeSendSessionBlocks addObject:[block copy]];
}

- (void)clearBeforeSendBlocks {
[(NSMutableArray *)self.beforeSendBlocks removeAllObjects];
}
Expand Down
8 changes: 7 additions & 1 deletion Source/BugsnagSessionTrackingApiClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,20 @@ - (void)deliverSessionsInStore:(BugsnagSessionFileStore *)store {
}
BugsnagSessionTrackingPayload *payload = [[BugsnagSessionTrackingPayload alloc] initWithSessions:sessions];
NSUInteger sessionCount = payload.sessions.count;
NSMutableDictionary *data = [payload toJson];

for (BeforeSendSession cb in self.config.beforeSendSessionBlocks) {
cb(data);
}

if (sessionCount > 0) {
NSDictionary *HTTPHeaders = @{
@"Bugsnag-Payload-Version": @"1.0",
@"Bugsnag-API-Key": apiKey,
@"Bugsnag-Sent-At": [BSG_RFC3339DateTool stringFromDate:[NSDate new]]
};
[self sendItems:sessions.count
withPayload:[payload toJson]
withPayload:data
toURL:sessionURL
headers:HTTPHeaders
onCompletion:^(NSUInteger sentCount, BOOL success, NSError *error) {
Expand Down
2 changes: 1 addition & 1 deletion Source/BugsnagSessionTrackingPayload.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

- (instancetype)initWithSessions:(NSArray<BugsnagSession *> *)sessions;

- (NSDictionary *)toJson;
- (NSMutableDictionary *)toJson;

@property NSArray<BugsnagSession *> *sessions;

Expand Down
5 changes: 2 additions & 3 deletions Source/BugsnagSessionTrackingPayload.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ - (instancetype)initWithSessions:(NSArray<BugsnagSession *> *)sessions {
}


- (NSDictionary *)toJson {

- (NSMutableDictionary *)toJson {
NSMutableDictionary *dict = [NSMutableDictionary new];
NSMutableArray *sessionData = [NSMutableArray new];

Expand All @@ -42,7 +41,7 @@ - (NSDictionary *)toJson {
NSDictionary *systemInfo = [BSG_KSSystemInfo systemInfo];
BSGDictSetSafeObject(dict, BSGParseAppState(systemInfo, [Bugsnag configuration].appVersion), @"app");
BSGDictSetSafeObject(dict, BSGParseDeviceState(systemInfo), @"device");
return [NSDictionary dictionaryWithDictionary:dict];
return dict;
}

@end

0 comments on commit 5aa055d

Please sign in to comment.