Instrument Go apps for Error Reporting

You can send error events to Error Reporting from Goapplications by using theError Reporting package for Go.Use the Error Reporting package for Go 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

Install the client library

The Error Reporting package for Go lets you monitor and view error events reported byGo applications running nearly anywhere.

  1. Usego get to install the package:

    go get cloud.google.com/go/errorreporting

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

Configure the client library

You can customize the behavior of the Error Reporting package for Go. Seethe godoc.

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 package for Go 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 package for Go.

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 package for Go 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 package for Go 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

The following sample demonstrates using the Go client libraryto report a custom error event:

// Sample errorreporting_quickstart contains is a quickstart// example for the Google Cloud Error Reporting API.packagemainimport("context""errors""log""os""cloud.google.com/go/errorreporting")varerrorClient*errorreporting.Clientfuncmain(){// Set your Google Cloud Platform project ID via environment or explicitlyprojectID:=os.Getenv("GOOGLE_CLOUD_PROJECT")args:=os.Args[1:]iflen(args) >0 &&args[0]!=""{projectID=args[0]}ctx:=context.Background()varerrerrorerrorClient,err=errorreporting.NewClient(ctx,projectID,errorreporting.Config{ServiceName:"errorreporting_quickstart",ServiceVersion:"0.0.0",OnError:func(errerror){log.Printf("Could not report the error: %v",err)},})iferr!=nil{log.Fatal(err)}defererrorClient.Close()err=errors.New("something went wrong")iferr!=nil{logAndPrintError(err)return}}funclogAndPrintError(errerror){/// Client autopopulates the error context of the error. For more details about the context see:/// https://cloud.google.com/error-reporting/reference/rest/v1beta1/ErrorContexterrorClient.Report(errorreporting.Entry{Error:err,})log.Print(err)}

See thegodoc for more examples of how to reportpanics and errors.

Run apps in a local development environment

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

To use the Go 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.