The ResourceConsumption::ScopedMetricsCollector constructor tells the MetricsCollector attached to the current OperationContext to begin recording metrics. The ScopedMetricsCollector destructor then tells the MetricsCollector to stop recording metrics and merges the MetricsCollector's metrics into the global map, keyed on database name.
This normally works fine, except that it doesn't clear the MetricsCollector after merging the metrics. The code assumes the MetricsCollector will destruct after this point.
This means if you happen to encounter another ScopedMetricsCollector before the OperationContext/MetricsCollector destructs, it records more metrics on top of the ones it already collected, and then merges those erroneous stats into a possibly different database in the global metrics map. See Steps to Reproduce.