Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add internal api for mutating session payload before sending #341

Merged
merged 3 commits into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Changelog

### Enhancements

* Add internal api for mutating session payload before sending
[#341](https://github.com/bugsnag/bugsnag-cocoa/pull/341)

* Persist breadcrumbs on disk to allow reading upon next boot in the event of an
uncatchable app termination.
* Add `+[Bugsnag appDidCrashLastLaunch]` as a helper to determine if the
Expand Down
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