If you use klipper you can use this script (by zellneralex) to calculate the filament length used since the last manual reset. Obviously it works with a single spool, if you switch spools it doesn't work.
If you want to know how much filament is left in the spool, the exact formula based on inner radius of the spool $r_{int}$ and outer radius of the spool when new $r_{ext}$ and based on the current outer radius of the remaining filament $x$ should be:
$$ 100 \left( \frac{x-r_{int}}{r_{ext}-r_{int}} \right)^2 $$
You can see that you get 100% when $x=r_{ext}$ (spool is new) and 0% when $x=r_{int}$.
It's a simple integration in $(x-x_0)\,dx$.
For those using Klipper, there is also a module for Moonraker which keeps track automatically of multiple filaments: https://github.com/Donkie/Spoolman