copied some code from codahale metrics