Is it possible to get the length of a ray inside an object with nodes?
It is always the way you ask. Ray length is the distance from previous bounce or refraction.

What might be a bit confusing - you have to apply toning to the inner side (backfacing) of your object, because outside rays return distance from other objects - as ray begins from camera, not from light source.

As far as mapping goes - approaches are different - for instance: http://lesterbanks.com/2012/10/using-blenders-raylength-node-to-create-a-re-usable-custom-absorption-shader/