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

beforeSend callback is not called for cached events after going from offline mode to online mode #2034

Open
hgadalrab opened this issue May 6, 2024 · 10 comments
Assignees

Comments

@hgadalrab
Copy link

Platform

Flutter Mobile

Obfuscation

Disabled

Debug Info

Disabled

Doctor

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.19.3, on macOS 14.4.1 23E224 darwin-arm64, locale en-AT)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 15.3)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2023.1)
[✓] Connected device (5 available)
! Error: Browsing on the local area network for M’s iPhone. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
The device must be opted into Developer Mode to connect wirelessly. (code -27)
[✓] Network resources

• No issues found!

Version

8.1.0

Steps to Reproduce

  1. add beforeSend callback to the options
  2. switch to offline mode
  3. send event
  4. beforeSend is called but the request fails due to the network connection
  5. switch to online mode

---- Code ----
await SentryFlutter.init((options) {
options
..dsn = dsn
..addIntegration(LoggingIntegration(minEventLevel: Level.WARNING))
..beforeSend = ((event, hint) {
print('Sending event with id ${event.eventId} and message: ${event.message?.formatted}');
return event;
});
});

Timer.periodic(const Duration(seconds: 5), (timer) {
logger.warning('Sending event At ${DateTime.now()}');
});

Expected Result

beforeSend is called before the request is sent to Sentry

Actual Result

beforeSend is not called and the request is sent to Sentry

Are you willing to submit a PR?

None

@buenaflor
Copy link
Contributor

hi thanks for raising this, we'll have a look

@denrase
Copy link
Collaborator

denrase commented May 7, 2024

The beforeSend callback is the last place where you can mutate or drop events before they are being sent to sentry. See Filtering Error Events for more info.

On Flutter event is sent through the iOS & Android native SDKs. To my knowledge, we will drop an event if it fails to send due to a lack of connection. Maybe @philipphofmann has some insight into how iOS is handling connection failures?

In any case, calling beforeSend again is not correct, as its intent is to either modify or drop an event before sending it to Sentry.

@philipphofmann
Copy link
Member

That is intended behavior. SDKs only call the beforeSend callback once. It's meant to modify the event before the SDKs send it to Sentry. Users don't have to worry about their apps being online or offline. SDKs ensure to send events to sentry once apps are online again.

Why do you expect the SDK to call beforeSend again when the device is online again @hgadalrab?

@hgadalrab
Copy link
Author

@philipphofmann Due to this issue #1753, I had so many cached events which are also too old. I couldn't find another way to drop these events except for this callback, which, I think, should be called before sending to Sentry for the second time.

@vaind
Copy link
Collaborator

vaind commented May 8, 2024

I believe you can have a one-time code to empty the cache directory from within your app code (you can check file times). The linked issue should be fixed since v8.

@hgadalrab
Copy link
Author

The linked issue is fixed in v8, yes, but the fix doesn't apply to the old events. So, if I had 100 events, and updated to v8, the 100 events will be kept in the cache.

If there is no other solution provided by Sentry, I will have to do this, but I expected a better solution e.g, the beforeSend callback or providing access to the existing events.

@buenaflor
Copy link
Contributor

I believe you can have a one-time code to empty the cache directory from within your app code

Unfortunately I believe this is the only solution we can currently offer currently

@philipphofmann
Copy link
Member

So, if I had 100 events, and updated to v8, the 100 events will be kept in the cache.

@buenaflor, can't we do some simple logic to check for old events in that cache and delete them? I guess more people could run into that problem.

@hgadalrab
Copy link
Author

@buenaflor Although it would be good to have a callback every time the event is sent to Sentry, but it would also be nice to have what you suggested. @philipphofmann

@buenaflor
Copy link
Contributor

Would you have an idea of the size of scope of this? @denrase

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Status: Needs Discussion
Development

No branches or pull requests

5 participants