Sentry on Google Cloud Function (Python)
Sentry can be added to your Google Cloud Function to help with debugging in your application. Setting up Sentry is easy with only two steps based on the documentation provided by Sentry.
Setup Sentry
First, add the sentry-sdk
to your requirements.txt
and then use the GCP Functions integration provided by the sentry-sdk.
sentry_sdk==1.5.12
import sentry_sdk
from sentry_sdk.integrations.gcp import GcpIntegration
sentry_sdk.init(
dsn="https://<key>@sentry.io/<project>",
integrations=[GcpIntegration()],
)
Done! Time to test it out. Let's intentionally trigger some errors in cloud function.
Hmm...why no exceptions are being captured in Sentry? 🤔
What's wrong.....
Debug step 1
From the documentation:
A DSN tells a Sentry SDK where to send events so the events are associated with the correct project.
✅ Double checked to make sure I had set the correct dsn.
Debug step 2
There is a debug option that we can enabled.
import sentry_sdk
from sentry_sdk.integrations.gcp import GcpIntegration
sentry_sdk.init(
dsn="https://<key>@sentry.io/<project>",
integrations=[GcpIntegration()],
debug=True
)
After debug is enabled, sentry related logs can be seen in Google Cloud Console.
Ah ha...~! Found a line in the log that says "GcpIntegration currently supports only Python 3.7 runtime environment." (code reference). My Cloud Function is using Python 3.8, wished this warning is stated more clearly in the documentation. 😞
Possible solution
- Downgrade to Python 3.7 runtime OR
- Use the serverless_function decorator.
import sentry_sdk
from sentry_sdk.integrations.serverless import serverless_function
sentry_sdk.init(
dsn="https://<key>@sentry.io/<project>",
)
@serverless_function
def main_func():
# This is the main function
I went with option 2. Although it is a generic integration, we can still use it without any issue and could continue to use Python 3.8 in the production environment.
Thank you for reading and I hope this helps anyone that ran into a similar issue. 👋
Clap to support the author, help others find it, and make your opinion count.