Setting up on Google Kubernetes Engine

You can send errors from your Google Kubernetes Engine applications toError Reporting in one of two ways:

Using Logging to report errors

GKE's default logging agent provides a managed solution todeploy and manage the agents that send the logs for your clusters toCloud Logging. The structure of the agent depends on the version of thecluster. For information about this agent, seeManaging GKE logs.

Error Reporting requires that exceptions or stack traces be containedin a single log entry. Most logging agents are capable of recognizing thatseveral log lines—stack frames printed each on a new line—representa stack trace and send it to Cloud Logging as a single log entry.If the agent isn't capable of reconstructing multiple lines as a single error,then use theprojects.events.report API endpoint,which allows you to control the contents of an error.

Using the Error Reporting API to write errors

The Error Reporting API provides areport endpoint for writingerror information to the service.

  1. 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

  2. Report errors to the API using either the REST API or a client library.

Samples

ASP.NET

The ASP.NET NuGet package reports uncaught exceptions fromASP.NET web applications to Error Reporting.

Install the NuGet package

To install the Stackdriver ASP.NET NuGet package in Visual Studio:

  1. Right-click your solution and selectManage NuGet packages forsolution.
  2. Select theInclude prerelease checkbox.
  3. Search for and install the package namedGoogle.Cloud.Diagnostics.AspNet.

Usage

Once you've installed the Stackdriver ASP.NET NuGet package, add thefollowing statement to your application code to start sending errors toStackdriver:

using Google.Cloud.Diagnostics.AspNet;

Add the followingHttpConfiguration code to theRegister method of your.NET web app (replacingyour-project-id with your actualproject ID to enable the reporting of exceptions:

publicstaticvoidRegister(HttpConfigurationconfig){stringprojectId="YOUR-PROJECT-ID";stringserviceName="NAME-OF-YOUR-SERVICE";stringversion="VERSION-OF-YOUR-SERVCICE";// ...// Add a catch all for the uncaught exceptions.config.Services.Add(typeof(IExceptionLogger),ErrorReportingExceptionLogger.Create(projectId,serviceName,version));// ...}

Once you've added this method to your ASP.NET application, you can view anyuncaught exceptions that occur as they get reported to Google Cloudin theError Reportingsection of the Google Cloud console.

C#

The following example can be found in theGoogleCloudPlatform/dotnet-docs-samples repo. To use it, after building the project, specify yourproject ID:

C:\...\bin\Debug>setGOOGLE_PROJECT_ID=[YOUR_PROJECT_ID]

Make sure to replace[YOUR_PROJECT_ID] with the correct value from theGoogle Cloud console.

Then, send exception data with code similar to the following:

publicclassErrorReportingSample{publicstaticvoidMain(string[]args){try{thrownewException("Generic exception for testing Stackdriver Error Reporting");}catch(Exceptione){report(e);Console.WriteLine("Stackdriver Error Report Sent");}}/// <summary>/// Create the Error Reporting service (<seealso cref="ClouderrorreportingService"/>)/// with the Application Default Credentials and the proper scopes./// See: https://developers.google.com/identity/protocols/application-default-credentials./// </summary>privatestaticClouderrorreportingServiceCreateErrorReportingClient(){// Get the Application Default Credentials.GoogleCredentialcredential=GoogleCredential.GetApplicationDefaultAsync().Result;// Add the needed scope to the credentials.credential.CreateScoped(ClouderrorreportingService.Scope.CloudPlatform);// Create the Error Reporting Service.ClouderrorreportingServiceservice=newClouderrorreportingService(newBaseClientService.Initializer{HttpClientInitializer=credential,});returnservice;}/// <summary>/// Creates a <seealso cref="ReportRequest"/> from a given exception./// </summary>privatestaticReportRequestCreateReportRequest(Exceptione){// Create the service.ClouderrorreportingServiceservice=CreateErrorReportingClient();// Get the project ID from the environement variables.stringprojectId=Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID");// Format the project id to the format Error Reporting expects. See:// https://cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events/reportstringformattedProjectId=string.Format("projects/{0}",projectId);// Add a service context to the report.  For more details see:// https://cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events#ServiceContextServiceContextserviceContext=newServiceContext(){Service="myapp",Version="8c1917a9eca3475b5a3686d1d44b52908463b989",};ReportedErrorEventerrorEvent=newReportedErrorEvent(){Message=e.ToString(),ServiceContext=serviceContext,};returnnewReportRequest(service,errorEvent,formattedProjectId);}/// <summary>/// Report an exception to the Error Reporting service./// </summary>privatestaticvoidreport(Exceptione){// Create the report and execute the request.ReportRequestrequest=CreateReportRequest(e);request.Execute();}}

Go

SeeSetting up Error Reporting for Go.

Java

SeeSetting up Error Reporting for Java.

Node.js

SeeSetting up Error Reporting for Node.js.

Ruby

SeeSetting up Error Reporting for Ruby.

Python

SeeSetting up Error Reporting for Python.

PHP

SeeSetting up Error Reporting for PHP.

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.

Note: If you see the message "Set up Error Reporting" on theError Reporting page,then your Google Cloud project has no error groups to display.

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.