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

Add ability to set attachments on a sentry event

Open izackp opened this issue 2 years ago • 3 comments

:scroll: Description

This just exposes a property on the event which will allow the user to add attachments to be sent.

:bulb: Motivation and Context

This is so I can add log data along with crashes. Its incredibly necessary in resolving not so obvious bugs.

:green_heart: How did you test it?

It's currently live in our app and seems to work.

:pencil: Checklist

You have to check all boxes before merging:

  • [ ] I reviewed the submitted code.
  • [ ] I added tests to verify the changes.
  • [ ] No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • [ ] I updated the docs if needed.
  • [ ] Review from the native team if needed.
  • [ ] No breaking change or entry added to the changelog.
  • [ ] No breaking change for hybrid SDKs or communicated to hybrid SDKs.

:crystal_ball: Next steps

izackp avatar Apr 24 '24 17:04 izackp

While this might work in your fork, we don't intend to add attachments to the event payload. Attachments are a separate thing. They get sent via extra envelope items. What is your use case for this @izackp?

If you only want to attach an attachment to a single event, you can use the local scope as mentioned in the docs:

SentrySDK.captureMessage(message: "my message", block: { scope in
	scope.addAttachment(fileAttachment)
})

philipphofmann avatar Apr 25 '24 12:04 philipphofmann

I want attachments on crash reports. Exposing a property allows me to add logs/attachments in beforeSend very easily.

I looked at the proposed solution here and it doesn't seem to work: https://github.com/getsentry/sentry-cocoa/issues/1460#issuecomment-1085515472

I guess what you really want implemented are 'hints' which are also in the java version (I believe).. so I suppose this won't get accepted.

Exact usage:

options.beforeSend = { [weak self] (newEvent:Sentry.Event?) in
    guard let myEvent = newEvent else { return nil }
    if (myEvent.exceptions == nil && myEvent.error == nil) {
        return myEvent
    }
    //if (myEvent.isAppHangEvent) { return myEvent; }
    
    myEvent.attachments = self?.attachments().map({
        return Sentry.Attachment(data: $0.data, filename: $0.fileName, contentType: $0.mimeType)
    })
    return myEvent
}

izackp avatar Apr 25 '24 19:04 izackp

Yes, exactly. We need hints (https://github.com/getsentry/sentry-cocoa/issues/2325) for this. We don't want attachments to the event payload.

philipphofmann avatar Apr 26 '24 07:04 philipphofmann

I'm closing this because, as mentioned here: https://github.com/getsentry/sentry-cocoa/pull/3896#issuecomment-2078810659, we will add hints at some point.

philipphofmann avatar Aug 13 '24 06:08 philipphofmann