How can I collect metrics from CircleCI?

I want to collect per build metrics such as how long did a build take (potentially broken down by different stages) and whether a build passed or failed (broken down by fork).

I know this is possible to get this kind of info through the API, but the issue is that I would want to send the metrics for a given build at the very end of a build and ensure that it always runs regardless if the build failed or succeeded.

Is there a good way to do this? I’m thinking about using the teardown section of circle.yml but AFAIK the commands on there are currently not guaranteed to run. (In particular it seems to get skipped if the dependencies stage fails.

Thanks,
Dong