Make metrics filter thread-safe

This is an attempt to make the metrics filter thread-safe by using thread-safe data structures for variables that change during runtime. It would be great if someone can review this to see if I catched everything. Thanks!