Skip to content

Error reporting quickstart #854

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Sep 20, 2017
35 changes: 35 additions & 0 deletions errorreporting/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Stackdriver Error Reporting sample

[Stackdriver Error Reporting][error-reporting] Stackdriver Error Reporting counts, analyzes and aggregates the crashes in your running cloud services.
A [centralized error management interface](https://console.cloud.google.com/errors) displays the results with sorting and filtering capabilities.

This sample Java application demonstrates how to send custom error events using the [Error Reporting API][api-ref-docs].
Note: Runtime exceptions and stack traces are automatically sent to Error reporting in applications running in [App Engine Flex environment][ae-flex].

[ae-flex]: https://cloud.google.com/appengine/docs/flexible/java
[error-reporting]: https://cloud.google.com/error-reporting/
[api-ref-docs]: https://googlecloudplatform.github.io/google-cloud-java/latest/apidocs/index.html?com/google/cloud/errorreporting/v1beta1/package-summary.html

## Setup

1. Install [Maven](http://maven.apache.org/).
1. [Enable](https://console.cloud.google.com/apis/api/clouderrorreporting.googleapis.com/overview) Stack Driver Error Reporting API.

## Build

Build your project with:
```
mvn clean package
```

## Local testing

1. [Create a service account](https://cloud.google.com/docs/authentication/getting-started#creating_the_service_account)
and set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable.
2. Run
```
mvn clean verify
```
Check the [error reporting console](https://console.cloud.google.com/errors).

Confirm that you see the custom errors reported using the Error Reporting API.
49 changes: 49 additions & 0 deletions errorreporting/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!--
Copyright 2017 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.cloud.logging.samples</groupId>
<artifactId>cloud-logging-samples</artifactId>
<packaging>pom</packaging>

<parent>
<artifactId>doc-samples</artifactId>
<groupId>com.google.cloud</groupId>
<version>1.0.0</version>
<relativePath>..</relativePath>
</parent>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>

<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-errorreporting</artifactId>
<version>0.24.0-alpha</version>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<version>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Copyright 2017 Google Inc.
*
* <p>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
*
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* <p>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.
*/

package com.example.errorreporting;

//[START errorreporting_quickstart]
import com.google.cloud.ServiceOptions;
import com.google.cloud.errorreporting.v1beta1.ReportErrorsServiceClient;
import com.google.devtools.clouderrorreporting.v1beta1.ErrorContext;
import com.google.devtools.clouderrorreporting.v1beta1.ProjectName;
import com.google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent;
import com.google.devtools.clouderrorreporting.v1beta1.SourceLocation;

/**
* Snippet demonstrates using the Stackdriver Error Reporting API to report a custom error event.

* This library is not required on App Engine, errors written to stderr are automatically written
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lesv PTAL if this suffices, I removed the stack trace reporting as well to make it clearer that using this API is an extreme rare case.

* to Stackdriver Error Reporting.
* It is also not required if you are writing logs to Stackdriver Logging.
* Errors written to Stackdriver Logging that contain an exception or stack trace
* are automatically written out to Stackdriver Error Reporting.
*/
public class QuickStart {
public static void main(String[] args) throws Exception {

// Google Cloud Platform Project ID
String projectId = (args.length > 0) ? args[0] : ServiceOptions.getDefaultProjectId();
ProjectName projectName = ProjectName.create(projectId);

// Instantiate an Error Reporting Client
try (ReportErrorsServiceClient reportErrorsServiceClient = ReportErrorsServiceClient.create()) {

// Custom error events require an error reporting location as well.
ErrorContext errorContext = ErrorContext.newBuilder()
.setReportLocation(SourceLocation.newBuilder()
.setFilePath("Test.java")
.setLineNumber(10)
.setFunctionName("myMethod")
.build())
.build();

//Report a custom error event
ReportedErrorEvent customErrorEvent = ReportedErrorEvent.getDefaultInstance()
.toBuilder()
.setMessage("custom error event")
.setContext(errorContext)
.build();
// Report an event synchronously, use .reportErrorEventCallable for asynchronous reporting.
reportErrorsServiceClient.reportErrorEvent(projectName, customErrorEvent);
}
}
}
// [END errorreporting_quickstart]
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright 2017 Google Inc.
*
* <p>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
*
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* <p>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.
*/

package com.example.errorreporting;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
@SuppressWarnings("AbbreviationAsWordInName")
public class QuickStartIT {

@Test
public void testQuickStart() throws Exception {
// Ensure quick start runs without any exception
QuickStart.main(new String[]{});
}
}
7 changes: 3 additions & 4 deletions flexible/errorreporting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ Build your project with:
```

## Local testing
1. Authorize the local environment
```
gcloud auth application-default login
```
[Create a service account](https://cloud.google.com/docs/authentication/getting-started#creating_the_service_account)
and set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable.

For local testing, we will be using the [Jetty Maven plugin](http://www.eclipse.org/jetty/documentation/9.4.x/jetty-maven-plugin.html).
Run:
```
Expand Down
16 changes: 0 additions & 16 deletions flexible/errorreporting/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,6 @@
<jetty.maven.plugin>9.4.6.v20170531</jetty.maven.plugin>
</properties>

<!-- Temporary workaround for known issue : https://github.com/GoogleCloudPlatform/google-cloud-java/issues/2192 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-credentials</artifactId>
<version>0.8.0</version>
</dependency>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
<version>0.8.0</version>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@

import com.google.cloud.ServiceOptions;
import com.google.cloud.errorreporting.v1beta1.ReportErrorsServiceClient;
import com.google.devtools.clouderrorreporting.v1beta1.ErrorContext;
import com.google.devtools.clouderrorreporting.v1beta1.ProjectName;
import com.google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent;

import com.google.devtools.clouderrorreporting.v1beta1.SourceLocation;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
Expand All @@ -42,28 +44,33 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp)
// errors logged to stderr / Cloud logging with exceptions are automatically reported.
logger.log(Level.SEVERE, "exception using log framework", new IllegalArgumentException());

// use the error-reporting client library to log custom error events
// use the error-reporting client library only if you require logging custom error events.
logCustomErrorEvent();

// runtime exceptions are also automatically picked up by error reporting.
// runtime exceptions are also automatically reported.
throw new RuntimeException("this is a runtime exception");
}

private void logCustomErrorEvent() {
try (ReportErrorsServiceClient reportErrorsServiceClient = ReportErrorsServiceClient.create()) {
// custom exception logged using the API
Exception e = new Exception("custom event reported using the API");
// Events reported using the API must contain a stack trace message
StringWriter stackTrace = new StringWriter();
e.printStackTrace(new PrintWriter(stackTrace));
ReportedErrorEvent errorEvent =
ReportedErrorEvent.getDefaultInstance()
.toBuilder()
.setMessage(stackTrace.toString())
.build();
// Custom error events require an error reporting location as well.
ErrorContext errorContext = ErrorContext.newBuilder()
.setReportLocation(SourceLocation.newBuilder()
.setFilePath("Test.java")
.setLineNumber(10)
.setFunctionName("myMethod")
.build())
.build();
//Report a custom error event
ReportedErrorEvent customErrorEvent = ReportedErrorEvent.getDefaultInstance()
.toBuilder()
.setMessage("custom error event")
.setContext(errorContext)
.build();

// default project id
ProjectName projectName = ProjectName.create(ServiceOptions.getDefaultProjectId());
reportErrorsServiceClient.reportErrorEvent(projectName, errorEvent);
reportErrorsServiceClient.reportErrorEvent(projectName, customErrorEvent);
} catch (Exception e) {
logger.log(Level.SEVERE, "Exception encountered logging custom event", e);
}
Expand Down
2 changes: 1 addition & 1 deletion logging/cloud-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ Build your project with:
-Dexec.args="my-log"


Logs can also viewed using the [Logs Viewer Console](https://pantheon.corp.google.com/logs/viewer).
Logs can also viewed using the [Logs Viewer Console](https://console.cloud.google.com/logs/viewer).
2 changes: 1 addition & 1 deletion logging/jul/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ provides an example of enhancing log entries with additional labels.
mvn exec:java -Dexec.mainClass=com.example.logging.jul.Quickstart \
-Dexec.args="-Djava.util.logging.file=src/main/resources/logging.properties"

Logs can be viewed using the [Logs Viewer Console](https://pantheon.corp.google.com/logs/viewer).
Logs can be viewed using the [Logs Viewer Console](https://console.cloud.google.com/logs/viewer).
2 changes: 1 addition & 1 deletion logging/logback/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ provides an example of enhancing log entries with additional labels.
## Writing log entries
mvn exec:java -Dexec.mainClass=com.example.logging.logback.Quickstart

Logs can be viewed using the [Logs Viewer Console](https://pantheon.corp.google.com/logs/viewer).
Logs can be viewed using the [Logs Viewer Console](https://console.cloud.google.com/logs/viewer).
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@

<module>dlp</module>

<module>errorreporting</module>

<module>iap</module>

<module>kms</module>
Expand Down