Instrument Python apps for Error Reporting

You can send error events to Error Reporting from Pythonapplications by using theError Reporting library for Python.Use the Error Reporting library for Python to create error groups for the following cases:

  • A log bucket that contains your log entries has customer-managed encryptionkeys(CMEK).
  • The log bucket satisfies one of the following:
    • The log bucket is stored in the same project where the log entriesoriginated.
    • The log entries were routed to a project, and then that project storedthose log entries in a log bucket that it owns.
  • You want to report custom error events.

Error Reporting is integrated with some Google Cloud services,such asCloud Run functionsandApp Engine,Compute Engine, andGoogle Kubernetes Engine. Error Reporting displays the error eventsthat are logged to Cloud Logging by applications running on those services.For more information, go toRunning on Google Cloud on thispage.

You can also send error events to Error Reporting usingLogging. For information on the data formattingrequirements, readFormat a log entry to report error events.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.create permission.Learn how to grant roles.
    Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Error Reporting API .

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enable permission.Learn how to grant roles.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.create permission.Learn how to grant roles.
    Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Error Reporting API .

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enable permission.Learn how to grant roles.

    Enable the API

  8. Prepare your environment for Python development.

    Go to the Python setup guide

Install the client library

The Error Reporting library for Python lets you monitor and view error events reported byPython applications running nearly anywhere.

  1. Instantiate a virtual environment and usepip to install the package:

    pip install google-cloud-error-reporting --upgrade
  2. Import the library and instantiate a client in order to begin reporting errors:

    Python

    To authenticate to Error Reporting, set up Application Default Credentials. For more information, seeSet up authentication for a local development environment.

    #!/usr/bin/env python## Copyright 2022 Google, Inc.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at##    http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.defreport_exception():fromgoogle.cloudimporterror_reportingclient=error_reporting.Client()try:raiseException("Something went wrong")exceptException:client.report_exception()defreport_manual_error():fromgoogle.cloudimporterror_reportingclient=error_reporting.Client()client.report("An error has occurred.")if__name__=="__main__":report_exception()report_manual_error()

For more information on installation, read thedocumentation for theError Reporting library for Python. You can also report issues using theissue tracker.

Configure the client library

You can customize the behavior of Error Reporting library for Python. Seeclient library documentationfor configuration examples.

Run apps on Google Cloud

To create error groups by usingprojects.events.report, your service account requires theError Reporting Writer role (roles/errorreporting.writer).

Some Google Cloud services automatically grant theError Reporting Writer role (roles/errorreporting.writer) to the appropriateservice account. However, you must grant this role to the appropriate serviceaccount for some services.

Cloud Run and Cloud Run functions

The default service account used byCloud Run has thepermissions ofError Reporting Writer role (roles/errorreporting.writer).

The Error Reporting library for Python can be used without needing to explicitly providecredentials.

Cloud Run is configured to use Error Reportingautomatically. UnhandledJavaScript exceptions will appear in Logging and be processed byError Reporting without needing to use theError Reporting library for Python.

Note: Error Reporting automatically creates an error event whena log entry contains a stack trace and the severity level of the log entryisn't set or is set to at leastERROR.

App Engine flexible environment

App Engine grants theError Reporting Writer role (roles/errorreporting.writer)to your default service account automatically.

The Error Reporting library for Python can be used without needing to explicitly providecredentials.

Error Reporting is automatically enabled for App Engineflexible environment applications. No additional setup is required.

Note: Error log entries written tostderr are processed automatically byError Reporting, without needing to use theError Reporting library for Python directly.

Google Kubernetes Engine

To use Error Reporting with Google Kubernetes Engine,do the following:

  1. Ensure that the service account to be used by your container has beengranted theError Reporting Writer role (roles/errorreporting.writer).

    You can use either theCompute Engine default service account or a custom service account.

    For information about granting roles, seeManage access to projects, folders, and organizations.

  2. Create your cluster and grant the cluster thecloud-platformaccess scope.

    For example, the following create command specifies thecloud-platformaccess scope and a service account:

    gcloud container clusters createCLUSTER_NAME --service-accountSERVICE_ACCT_NAME --scopes=cloud-platform
Note: After you create a cluster, you can't change its service account.

Compute Engine

To use Error Reporting with Compute Engine VM instances,do the following:

  1. Ensure that the service account to be used by your VM instance has beengranted theError Reporting Writer role (roles/errorreporting.writer).

    You can use either theCompute Engine default service account or a custom service account.

    For information about granting roles, seeManage access to projects, folders, and organizations.

  2. In the Google Cloud console, go to theVM instances page:

    Go toVM instances

    If you use the search bar to find this page, then select the result whose subheading isCompute Engine.

  3. Select the VM instance that you want to receive thecloud-platformaccess scope.

  4. ClickStop, and then clickEdit.

  5. In theIdentity and API access section, select a service account thathas the Error Reporting Writer role (roles/errorreporting.writer).

  6. In theAccess scopes section,selectAllow full access to all Cloud APIs, and then save your changes.

  7. ClickStart/Resume.

Example

You can manually report an error by calling thereport method, as seen in thefollowing example:

Python

To authenticate to Error Reporting, set up Application Default Credentials. For more information, seeSet up authentication.

#!/usr/bin/env python## Copyright 2022 Google, Inc.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at##    http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.defreport_exception():fromgoogle.cloudimporterror_reportingclient=error_reporting.Client()try:raiseException("Something went wrong")exceptException:client.report_exception()defreport_manual_error():fromgoogle.cloudimporterror_reportingclient=error_reporting.Client()client.report("An error has occurred.")if__name__=="__main__":report_exception()report_manual_error()

Run apps in a local development environment

To use the Error Reporting library for Python in a local development environment,such as running the library on your own workstation, you must provideyour Error Reporting library for Python with the local application default credentials.For more information, seeAuthenticate to Error Reporting.

To use the Python samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.

    Install the Google Cloud CLI.

    If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    If you're using a local shell, then create local authentication credentials for your user account:

    gcloudauthapplication-defaultlogin

    You don't need to do this if you're using Cloud Shell.

    Note: If the gcloud CLI prints a warning that your account doesn't have theserviceusage.services.use permission, then some gcloud CLI commands and client libraries might not work. Ask an administrator to grant you the Service Usage Consumer IAM role (roles/serviceusage.serviceUsageConsumer), then run the following command:

    gcloudauthapplication-defaultset-quota-projectPROJECT_ID

    If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

For more information, see Set up authentication for a local development environment.

Theprojects.events.report method also supports API keys. If you want to use API keys for authentication, you do not need to set up a local Application Default Credentials file. For more information, seeCreate an API key in the Google Cloud authentication documentation.

View error groups

In the Google Cloud console, go to theError Reporting page:

Go toError Reporting

You can also find this page by using the search bar.

For more information, seeView and filter error groups.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.