This is a well known passage that shows how Jesus is the good shepherd. He guides with the rod and he offers us fellowship at his table by drinking the cup of wrath that could have been ours.
King David takes note of his brokenness. He confesses his sin to the LORD and trusts that he will be forgiven. For that reason his heart finds gladness.