How do I query the transition history of a Jira Issue?

I want to know how long an issue is in a particular state? Also when it entered that state? etc. I want to produce metrics and analyze how an issue transitions through its lifecycle, where the bottle necks are, etc.