You can send errors from your Google Kubernetes Engine applications to Error Reporting in one of two ways:
By logging to Cloud Logging. If you're already using Cloud Logging, the only additional requirement is that your log entries be recognizable by Error Reporting. For more information on error formatting requirements, read Formatting errors in Cloud Logging.
Using the Error Reporting API. Your application can send HTTP requests using the REST API, or can make use of experimental libraries in several languages.
Using Logging to report errors
To connect your Google Kubernetes Engine applications to Error Reporting, send your exceptions or other errors to Logging, using a dedicated log name to keep your errors separate from your other logs.
For example:
Enable the Cloud Logging API.
Install the Logging agent that is appropriate for your environment. For instructions, go to Installing the Logging agent
google-fluentd
.Modify your application so that it logs exceptions and their stack traces to Logging. Choose a log name for your error information to keep it separate from other logged information.
You must include all the information for a single error or exception in the same log entry, including all the frames of any stack trace. If all the information is not together, Error Reporting might not pick up the information. You can use the structured JSON format for your log entry payloads to include different kinds of information for each error.
Java
Add the following to your pom.xml
file:
Then use code like the following to send the exception data:
Python
First, install the
fluent-logger-python
library:
sudo pip install fluent-logger
Then use code like the following to send the exception data:
Node.js
First, install the
fluent-logger-node
library:
npm install --save fluent-logger
Then use code like the following to send the exception data:
Go
First, install the
fluent-logger-golang
package:
go get -u github.com/fluent/fluent-logger-golang/
Then use code like the following to send error data:
Using the Error Reporting API to write errors
The Error Reporting API provides a report
endpoint for writing
error information to the service.
Enable the Error Reporting API.
Report errors to the API using either the REST API or a client library.
Samples
ASP.NET
The Stackdriver ASP.NET NuGet package reports uncaught exceptions from ASP.NET web applications to Error Reporting.
Install the NuGet package
To install the Stackdriver ASP.NET NuGet package in Visual Studio:
- Right-click your solution and select Manage NuGet packages for solution.
- Select the Include prerelease checkbox.
- Search for and install the package named
Google.Cloud.Diagnostics.AspNet
.
Usage
Once you've installed the Stackdriver ASP.NET NuGet package, add the following statement to your application code to start sending errors to Stackdriver:
using Google.Cloud.Diagnostics.AspNet;
Add the following HttpConfiguration
code to the Register
method of your
.NET web app (replacing your-project-id
with your actual
project ID
to enable the reporting of exceptions:
Once you've added this method to your ASP.NET application, you can view any uncaught exceptions that occur as they get reported to Google Cloud in the Error Reporting section of the Cloud Console.
C#
The following example can be found in the GoogleCloudPlatform/dotnet-docs-samples repo. To use it, after building the project, specify your project ID:
C:\...\bin\Debug> set GOOGLE_PROJECT_ID=[YOUR_PROJECT_ID]
Make sure to replace [YOUR_PROJECT_ID]
with the correct value from the
Cloud Console.
Then, send exception data with code similar to the following: