add metrics. add metrics tests. fix some bugs