monitoring voltage and amperage on Raspberry Pi USB ports
While this is certainly doable from a theoretical point of view, and I certainly don’t want to discourage you from trying this as a fun project, it may be more hassle for you than simply (purchasing and) using an oscilloscope if you’re just wanting to root-cause your intermittent failures.
USB is a high-speed protocol. Depending on your devices, you may be looking at signal rates of 1.5, 12, 400, or 5000 Mbps. A data-synchronous power transition at anywhere near those speeds is far faster than a multimeter is able to keep up with, which is why your earlier attempt was showing fluctuation. And spurious, out-of-band current spikes on any misbehaving device in your topology could occur for arbitrarily brief periods. In both cases, your test tool needs to have a fast enough sample rate in order to not miss the offending event(s).
The tool that captures signals at high sample rates is an oscilloscope. Your proposed project can be reframed as, “a DIY oscilloscope that’s running on your DUT”. While clever and potentially cost-effective, you run three major risks in going this route:

Your acquisition engine may be too slow and/or jittery, causing you to miss your events.
The additional code, having to run at a high speed in order to maximize sample rate, can incur enough resource load to cause the behavior of the DUT to deviate so much from normal that it causes the issue to go away (or, of course, it might make the issue even more prominent instead, but the point is that it changes the behavior which you’re trying to observe and root-case).
If you aren’t careful with your probe hardware, you may inadvertently place excessive electrical load, again changing the behavior of your DUT.

(This is not to mention the potentially significant time you might need to invest in order to implement this correctly and with low enough resource utilization to make it practical, which, again, may be totally worthwhile to you for its own sake, but which is otherwise a distraction from your ultimate goal of USB stability.)
There are plenty of low-cost, used oscilloscopes out there that might have enough sample rate to catch your problem immediately. Alternatively, as a compromise between the DIY spirit and pragmatism, if you had a second Raspberry Pi you could consider building your own Pi-based oscilloscope (e.g, https://duckduckgo.com/?q=raspberry+pi+oscilloscope). This way you can avoid loading the DUT while still having a relatively low-cost solution that you can also use for future projects.
However you acquire your scope, you’d simply set up one channel to record voltage, another channel to record voltage through a shunt to give you current, and set up a trigger to stop on voltage sag. (If you had additional channels available and the scope had enough bandwidth for the highest speed device in your topology, you could also record D+/D- traces and run them through a protocol decoder to try to see if there’s a correlation with certain packet patterns, but that’s significantly more involved and requires a lot more care.)