If an error occurs in the app, the app sends a message with the stacktrace and an anonymous user id to the Sentry Cloud.
There is no such thing as an "an anonymous user id". It either identifies a user, then it is personal data, or it doesn't, then it's just random byte garbage. Since your app is contacting this cloud service directly, not via your servers as a proxy, you also exposing the customers IP address to a third party.
Is this legal or do I need user consent for this?
An IP address is PII, exposing it to a third part needs consent. Given that your app would be perfectly usable if this feature did not exist, you cannot claim it is absolutely neccessary for the operation (like sending the IP address to your own servers, which is simply how the internet works). If you would send all that to your own servers, strip the user id and ip address and send just the stacktrace devoid of any personal information to a third party, I would say you are safe. But the user id and ip address are PII that you need consent for, before sending them to a third party.
Normally, this is done in the fine print of the terms of use one has to accept before being able to sign up for an account in the app.
If you don't have an app with accounts, you could simply let the user chose to do this (or not) at the first app start. That is more user friendly and it established explicit consent to either do it, or not.