A publicly accessible HTTP endpoint, deployed to Google Cloud Functions, to serve as a Vercel log drain to ingest logs into Google Cloud Logging. This only converts some fields from the Vercel log to their Cloud Logging counterparts.
(1) Generate a secret key of length 32 (minimum length allowed by Vercel):
python -c 'import secrets; print(secrets.token_hex(16))'
(2) Create file env-vars.yaml
based on env-vars.yaml.example and set all variables except VERCEL_VERIFICATION_KEY.
(3) Deploy to google cloud so that you can obtain the public URL:
gcloud functions deploy vercel-log-drain \
--gen2 \
--runtime=python312 \
--region=us-east4 \
--trigger-http \
--max-instances 10 \
--concurrency=10 \
--cpu=1 \
--memory="256MB" \
--allow-unauthenticated \
--env-vars-file=env-vars.yaml \
--source=. \
--entry-point=on_log
... wait for command to complete and copy the Cloud Function URL.
(4) Create a log drain in Vercel with the following options:
- Delivery Format: JSON
- Custom Secret: secret generated in step 1
- Endpoint: The URL of the Cloud Function deployed in step 3
When you enter the Endpoint URL, Vercel will ask you to verify that you own the URL. Set VERCEL_VERIFICATION_KEY in env-vars.yaml
and redeploy (step 3). Wait for the deployment to finish then click Verify in Vercel.
(5) Redeploy (step 3)
Local testing:
- Start the server
GCP_PROJECT="TODO" SECRET_KEY="TODO" functions-framework-python --target=on_log
- Make a request
curl localhost:8080
Run tests:
python -m tests.test_transform
Useful documentation:
- Local testing https://cloud.google.com/functions/docs/create-deploy-http-python#build_and_test_your_function_locally
- Cloud Functions Python runtime https://cloud.google.com/functions/docs/concepts/python-runtime
- Vercel log drain auth https://vercel.com/docs/rest-api#securing-your-log-drains
- Cloud Logging LogEntry https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry