With mains and other similar voltage, you always have to be careful about the voltage rating of your parts. With a 230Vac (rms) you already have 310V peak, and voltage surges can be much higher, and your parts have to be able to withstand those without failure. Old fashioned "regular" 1/4W THT resistor are normally limited to a working voltage somewhere between 100V and 200V, and SMT resistors (especially the smaller ones) are rated for even smaller voltages. So you have to use "special" resistors, or put multiple of them in series to get to be able to use them for high enough voltages.
If the minimum CTR of your opto is 300, and the typical is 8500, then what do you design for?
For how many of your products do you find it acceptable to fail after they have been put on the pcb, put into an enclosure and shipped to customers? (That is quite something different then a single prototype "working" on your bench.
Opto couplers also do not age well, and the CTR is expected to get lower over time.
Your optocoupler has a 3pF capacitance, and this could be a path for erroneous measurements.
Once you use proper resistors on the primary side (those are safety critical) you can put a simple low voltage capacitor directly over the optocoupler LEDs (These limit the capacitor voltage to about 1V anyway). Short peaks may go right by your big electrolytics, and ceramic capacitors are much better at filtering high frequency stuff.
Alternatively, maybe you can do some filtering in software, quite similar to debouncing mechanical switches, but you can start by measuring (ether with an oscilloscope and/or with your ESP32 itself) what sort of durations you get for the pulse widths.