bitcoin-cli getmininginfo returns a metric
networkhashps among others. It shows how many hashes the entire Bitcoin network generates per second.
This metric however, is not directly stored within the Blockchain, resp. it is not communicated over the gossip network. Consequently it must be derived; I assume that it takes the difficulty and also checks the time it took between blocks.
But how does the algorithm behind this exactly look like?