sentry-cocoa icon indicating copy to clipboard operation
sentry-cocoa copied to clipboard

feat: Add Attachments in beforeSend

Open philipphofmann opened this issue 2 years ago • 5 comments

Currently, adding attachments in beforeSend is not possible, because the Cocoa SDK doesn't have hints as for example, Python does. Either we add hints or the SDK should offer another way of adding attachments in beforeSend. Furthermore, it would be nice to also drop attachments in beforeSend.

This came up in https://github.com/getsentry/sentry-cocoa/issues/1459. A user wants to add an attachment to an OOM event.

philipphofmann avatar Nov 17 '21 07:11 philipphofmann

Hints could be its own class extending a dictionary with methods for adding an attachment. Before implementing this we still need to investigate the use cases of hints in the Cocoa SDK. Look at Python and Android.

How this could look like with hints:

SentrySDK.start { options in
    options.beforeSend { event, hint in
       hint.addAttachment(Attachment())
       hint.removeAttachment()
       hint.attachments
    }
}

philipphofmann avatar Nov 24 '21 15:11 philipphofmann

How can we add attachments to any sort of crashes at all? We have logs that would be helpful in identifying why a crash happened and can't figure out how to send it.

eric avatar Mar 31 '22 01:03 eric

@eric, sadly that is currently not possible. We could add an API for capturing attachemts; then you could do

SentrySDK.start { options in
    // ...
    
    options.onCrashedLastRun = { event in
        SentrySDK.captureAttachment(...)
    }
}

But I think the proper way would be adding hints to the SDK, so you can do this in beforeSend.

philipphofmann avatar Mar 31 '22 14:03 philipphofmann

What if we use the following existing api, what is the expected behavior?

  [SentrySDK configureScope:^(SentryScope * _Nonnull scope) {
    [scope addAttachment:...];
  }];

tmm1 avatar Mar 31 '22 17:03 tmm1

@tmm1, your approach could work, but SentrySDK.start installs the SentryCrashIntegration, which then, on a background thread, will send the crash report. So it could work in most cases, but there is no guarantee. So you could do

SentrySDK.start { options in
    // ...
    
    options.onCrashedLastRun = { event in
        SentrySDK.configureScope { scope in
            scope.clearAttachments()
        }
    }
}

SentrySDK.configureScope { scope in
    scope.addAttachment(...)
}

philipphofmann avatar Apr 01 '22 07:04 philipphofmann