Should be as close to logic as in sentry server as seen in: https://github.com/getsentry/sentry/pull/1089