Integrate metrics support and refactor the code.

Two things really:
- Provide ability to emit metrics for API calls.
- Nicer code organization.
"the error will happen" reads a little strangely to me. How about:

`// Response body must be JSON-marshallable, otherwise this function will write an Internal Server Error to w.`