Integrate metrics support and refactor the code.

Two things really:
- Provide ability to emit metrics for API calls.
- Nicer code organization.