The reason astronomers can tell how far the light gets shifted is because chemical elements, like hydrogen and oxygen, have unique fingerprints of light that no other element has. Astronomers use spectroscopy to analyse the light from an object (galaxy or star}. Once they know that, they check to see the difference between where the spectral lines are compared to where they normally are. From that, they can tell whether it is moving toward us or away from us, and also how fast it is going. The faster it goes, the farther the spectral lines are from their normal position in the spectrum.