Google Cloud Functions

Learn about using Sentry with Google Cloud Functions.

Add the Sentry SDK to your requirements.txt:

requirements.txt
Copied
 sentry-sdk>=0.17.1

The GCP integration currently supports only the Python 3.7 runtime environment. If you try to run it with a newer version (such as 3.9), an error message will be displayed (with debug mode on).

Limited Sentry support is available on newer Python runtimes by directly enabling integrations for supported frameworks and/or using the generic serverless integration.

You can use the Google Cloud Functions integration for the Python SDK like this:

In addition to capturing errors, you can monitor interactions between multiple services or applications by enabling tracing. You can also collect and analyze performance profiles from real users with profiling.

Select which Sentry features you'd like to install in addition to Error Monitoring to get the corresponding installation and configuration instructions below.

Copied
import sentry_sdk
from sentry_sdk.integrations.gcp import GcpIntegration

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    traces_sample_rate=1.0,
    # Set profiles_sample_rate to 1.0 to profile 100%
    # of sampled transactions.
    # We recommend adjusting this value in production.
    profiles_sample_rate=1.0,
    integrations=[
        GcpIntegration(),
    ],
)

def http_function_entrypoint(request):
    # ...

Check out Sentry's GCP sample apps for detailed examples.

The timeout warning reports an issue when the function execution time is near the configured timeout.

To enable the warning, update the SDK initialization to set timeout_warning to true:

Copied
sentry_sdk.init(
    # ...
    integrations=[
        GcpIntegration(
            timeout_warning=True
        ),
    ],
)

The timeout warning is sent only if the timeout in the Cloud Function configuration is set to a value greater than one second.

With the Google Cloud Functions integration enabled, the Python SDK will:

  • Automatically report all events from your Cloud Functions

  • You can modify the transaction sample rate using traces_sample_rate.

  • Issues reports automatically include:

    • A link to the stackdriver logs
    • Function details
    • sys.argv for the function
    • Function execution time (in milliseconds)
    • Function configured time (in seconds)
    • You can add more data as described here
  • Request data is attached to all events: HTTP method, URL, headers, form data, JSON payloads. Sentry excludes raw bodies and multipart file uploads. Sentry also excludes personally identifiable information (such as user ids, usernames, cookies, authorization headers, IP addresses) unless you set send_default_pii to True.

  • Each request has a separate scope. Changes to the scope within a view, for example setting a tag, will only apply to events sent as part of the request being handled.

Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").