Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 324e029

Browse files
authoredJun 27, 2017
Error reporting sample for App Engine Flex (GoogleCloudPlatform#730)
1 parent 5600781 commit 324e029

File tree

5 files changed

+235
-0
lines changed

5 files changed

+235
-0
lines changed
 

‎flexible/errorreporting/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Error reporting sample for Google App Engine Flexible
2+
3+
[Stackdriver Error Reporting][error-reporting] Stackdriver Error Reporting counts, analyzes and aggregates the crashes in your running cloud services.
4+
A [centralized error management interface](https://console.cloud.google.com/errors) displays the results with sorting and filtering capabilities.
5+
6+
This sample Java application demonstrates how errors are automatically sent to Error reporting in applications running in [App Engine Flex environment][ae-flex].
7+
It also demonstrates how to send custom error events using the Error Reporting API.
8+
9+
[error-reporting]: https://cloud.google.com/error-reporting/
10+
[ae-flex]: https://cloud.google.com/appengine/docs/flexible/java
11+
[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java
12+
13+
## Setup
14+
15+
1. Install [Maven](http://maven.apache.org/).
16+
1. Install and initialize [GCloud SDK](https://cloud.google.com/sdk/downloads).
17+
1. [Enable](https://console.cloud.google.com/apis/api/clouderrorreporting.googleapis.com/overview) Stack Driver Error Reporting API.
18+
(Note : only required for logging custom events using the Error Reporting API)
19+
20+
## Build
21+
Build your project with:
22+
```
23+
mvn clean package
24+
```
25+
26+
## Local testing
27+
1. Authorize the local environment
28+
```
29+
gcloud auth application-default login
30+
```
31+
For local testing, we will be using the [Jetty Maven plugin](http://www.eclipse.org/jetty/documentation/9.4.x/jetty-maven-plugin.html).
32+
Run:
33+
```
34+
mvn jetty:run
35+
```
36+
Access [http://localhost:8080/error](http://localhost:8080/error) endpoint.
37+
38+
After accessing the `/error` endpoint, check the [error reporting console](https://console.cloud.google.com/errors).
39+
Confirm that you see the custom error reported using the error reporting API.
40+
41+
## Deploy
42+
Run:
43+
```
44+
mvn appengine:deploy
45+
```
46+
Access [https://YOUR_PROJECT_ID.appspot.com/error] endpoint.
47+
48+
After accessing the `/error` endpoint, check the [error reporting console](https://console.cloud.google.com/errors).
49+
Confirm that you see:
50+
1. IllegalArgumentException logged via the standard logging framework.
51+
1. Custom error reported using the error reporting API.
52+
1. Runtime exception.

‎flexible/errorreporting/pom.xml

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<!--
2+
Copyright 2017 Google Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
<!-- [START project] -->
17+
<project>
18+
<modelVersion>4.0.0</modelVersion>
19+
<packaging>war</packaging>
20+
<version>1.0-SNAPSHOT</version>
21+
<groupId>com.example.flexible</groupId>
22+
<artifactId>flexible-error-reporting</artifactId>
23+
24+
<parent>
25+
<artifactId>doc-samples</artifactId>
26+
<groupId>com.google.cloud</groupId>
27+
<version>1.0.0</version>
28+
<relativePath>../..</relativePath>
29+
</parent>
30+
31+
<properties>
32+
<appengine.maven.plugin>1.3.1</appengine.maven.plugin>
33+
<maven.compiler.target>1.8</maven.compiler.target>
34+
<maven.compiler.source>1.8</maven.compiler.source>
35+
<failOnMissingWebXml>false</failOnMissingWebXml>
36+
<jetty.maven.plugin>9.4.6.v20170531</jetty.maven.plugin>
37+
</properties>
38+
39+
<!-- Temporary workaround for known issue : https://github.com/GoogleCloudPlatform/google-cloud-java/issues/2192 -->
40+
<dependencyManagement>
41+
<dependencies>
42+
<dependency>
43+
<groupId>com.google.auth</groupId>
44+
<artifactId>google-auth-library-credentials</artifactId>
45+
<version>0.6.1</version>
46+
</dependency>
47+
<dependency>
48+
<groupId>com.google.auth</groupId>
49+
<artifactId>google-auth-library-oauth2-http</artifactId>
50+
<version>0.6.1</version>
51+
</dependency>
52+
</dependencies>
53+
</dependencyManagement>
54+
55+
<dependencies>
56+
<dependency>
57+
<groupId>javax.servlet</groupId>
58+
<artifactId>javax.servlet-api</artifactId>
59+
<version>3.1.0</version>
60+
<type>jar</type>
61+
<scope>provided</scope>
62+
</dependency>
63+
<dependency>
64+
<groupId>com.google.cloud</groupId>
65+
<artifactId>google-cloud-errorreporting</artifactId>
66+
<version>0.20.0-alpha</version>
67+
</dependency>
68+
</dependencies>
69+
70+
<build>
71+
<!-- for hot reload of the web application -->
72+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
73+
<plugins>
74+
<plugin>
75+
<groupId>com.google.cloud.tools</groupId>
76+
<artifactId>appengine-maven-plugin</artifactId>
77+
<version>${appengine.maven.plugin}</version>
78+
<configuration>
79+
<!-- deploy configuration -->
80+
</configuration>
81+
</plugin>
82+
<plugin>
83+
<groupId>org.eclipse.jetty</groupId>
84+
<artifactId>jetty-maven-plugin</artifactId>
85+
<version>${jetty.maven.plugin}</version>
86+
</plugin>
87+
</plugins>
88+
</build>
89+
</project>
90+
<!-- [END project] -->
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2017 Google Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
# [START appyaml]
14+
runtime: java
15+
env: flex
16+
17+
handlers:
18+
- url: /.*
19+
script: this field is required, but ignored
20+
# [END appyaml]
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Copyright 2017 Google Inc.
3+
*
4+
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5+
* except in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* <p>http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
10+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
* express or implied. See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package com.example.flexible.errorreporting;
15+
16+
import com.google.cloud.ServiceOptions;
17+
import com.google.cloud.errorreporting.v1beta1.ReportErrorsServiceClient;
18+
import com.google.devtools.clouderrorreporting.v1beta1.ProjectName;
19+
import com.google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent;
20+
21+
import java.io.IOException;
22+
import java.io.PrintWriter;
23+
import java.io.StringWriter;
24+
import java.util.logging.Level;
25+
import java.util.logging.Logger;
26+
import javax.servlet.ServletException;
27+
import javax.servlet.annotation.WebServlet;
28+
import javax.servlet.http.HttpServlet;
29+
import javax.servlet.http.HttpServletRequest;
30+
import javax.servlet.http.HttpServletResponse;
31+
32+
// [START flex_error_reporting]
33+
@WebServlet(name = "Error reporting", value = "/error")
34+
public class ErrorReportingExample extends HttpServlet {
35+
36+
private Logger logger = Logger.getLogger(ErrorReportingExample.class.getName());
37+
38+
@Override
39+
public void doGet(HttpServletRequest req, HttpServletResponse resp)
40+
throws IOException, ServletException {
41+
42+
// errors logged to stderr / Cloud logging with exceptions are automatically reported.
43+
logger.log(Level.SEVERE, "exception using log framework", new IllegalArgumentException());
44+
45+
// use the error-reporting client library to log custom error events
46+
logCustomErrorEvent();
47+
48+
// runtime exceptions are also automatically picked up by error reporting.
49+
throw new RuntimeException("this is a runtime exception");
50+
}
51+
52+
private void logCustomErrorEvent() {
53+
try (ReportErrorsServiceClient reportErrorsServiceClient = ReportErrorsServiceClient.create()) {
54+
// custom exception logged using the API
55+
Exception e = new Exception("custom event reported using the API");
56+
// Events reported using the API must contain a stack trace message
57+
StringWriter stackTrace = new StringWriter();
58+
e.printStackTrace(new PrintWriter(stackTrace));
59+
ReportedErrorEvent errorEvent =
60+
ReportedErrorEvent.getDefaultInstance()
61+
.toBuilder()
62+
.setMessage(stackTrace.toString())
63+
.build();
64+
// default project id
65+
ProjectName projectName = ProjectName.create(ServiceOptions.getDefaultProjectId());
66+
reportErrorsServiceClient.reportErrorEvent(projectName, errorEvent);
67+
} catch (Exception e) {
68+
logger.log(Level.SEVERE, "Exception encountered logging custom event", e);
69+
}
70+
}
71+
}
72+
// [END flex_error_reporting]

‎pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
<module>flexible/cron</module>
5858
<module>flexible/datastore</module>
5959
<module>flexible/disk</module>
60+
<module>flexible/errorreporting</module>
6061
<module>flexible/extending-runtime</module>
6162
<module>flexible/helloworld</module>
6263
<module>flexible/mailgun</module>

0 commit comments

Comments
 (0)
Failed to load comments.