It turns out that the issue was not code-related, but a Cloud Run configuration related. The service account for Cloud Run must have the role Cloud Trace Agent.
Cloud Trace Agent