Skip to content
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

Error reporting sample for App Engine Flex #730

Merged
merged 5 commits into from
Jun 27, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions flexible/errorreporting/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Error reporting sample for Google App Engine Flexible

[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 errors are automatically sent to Error reporting in applications running in [App Engine Flex environment][ae-flex].
It also demonstrates how to send custom error events using the Error Reporting API.

[error-reporting]: https://cloud.google.com/error-reporting/
[ae-flex]: https://cloud.google.com/appengine/docs/flexible/java
[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java

## Setup

1. Install [Maven](http://maven.apache.org/).
1. Install and initialize [GCloud SDK](https://cloud.google.com/sdk/downloads).
1. [Enable](https://console.cloud.google.com/apis/api/clouderrorreporting.googleapis.com/overview) Stack Driver Error Reporting API.
(Note : only required for logging custom events using the Error Reporting API)

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

## Local testing
1. Authorize the local environment
```
gcloud auth application-default login
```
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:
```
mvn jetty:run
```
Access [http://localhost:8080/error](http://localhost:8080/error) endpoint.

After accessing the `/error` endpoint, check the [error reporting console](https://console.cloud.google.com/errors).
Confirm that you see the custom error reported using the error reporting API.

## Deploy
Run:
```
mvn appengine:deploy
```
Access [https://YOUR_PROJECT_ID.appspot.com/error] endpoint.

After accessing the `/error` endpoint, check the [error reporting console](https://console.cloud.google.com/errors).
Confirm that you see:
1. IllegalArgumentException logged via the standard logging framework.
1. Custom error reported using the error reporting API.
1. Runtime exception.
90 changes: 90 additions & 0 deletions flexible/errorreporting/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<!--
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.
-->
<!-- [START project] -->
<project>
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<groupId>com.example.flexible</groupId>
<artifactId>flexible-error-reporting</artifactId>

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

<properties>
<appengine.maven.plugin>1.3.1</appengine.maven.plugin>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<failOnMissingWebXml>false</failOnMissingWebXml>
<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.6.1</version>
</dependency>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
<version>0.6.1</version>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-errorreporting</artifactId>
<version>0.20.0-alpha</version>
</dependency>
</dependencies>

<build>
<!-- for hot reload of the web application -->
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
<plugins>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>${appengine.maven.plugin}</version>
<configuration>
<!-- deploy configuration -->
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.maven.plugin}</version>
</plugin>
</plugins>
</build>
</project>
<!-- [END project] -->
20 changes: 20 additions & 0 deletions flexible/errorreporting/src/main/appengine/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 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.
# [START appyaml]
runtime: java
env: flex

handlers:
- url: /.*
script: this field is required, but ignored
# [END appyaml]
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* 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.flexible.errorreporting;

import com.google.cloud.ServiceOptions;
import com.google.cloud.errorreporting.v1beta1.ReportErrorsServiceClient;
import com.google.devtools.clouderrorreporting.v1beta1.ProjectName;
import com.google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// [START flex_error_reporting]
@WebServlet(name = "Error reporting", value = "/error")
public class ErrorReportingExample extends HttpServlet {

private Logger logger = Logger.getLogger(ErrorReportingExample.class.getName());

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {

// exceptions logged to stdout/stderr or Cloud logging gets automatically picked up by error reporting.
logger.log(Level.SEVERE, "exception using log framework", new IllegalArgumentException());

// use the error-reporting client library to log custom error events
logCustomErrorEvent();

// runtime exceptions are also automatically picked up by error reporting.
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();
// default project id
ProjectName projectName = ProjectName.create(ServiceOptions.getDefaultProjectId());
reportErrorsServiceClient.reportErrorEvent(projectName, errorEvent);
} catch (Exception e) {
logger.log(Level.SEVERE, "Exception encountered logging custom event", e);
}
}
}
// [END flex_error_reporting]
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<module>flexible/cron</module>
<module>flexible/datastore</module>
<module>flexible/disk</module>
<module>flexible/errorreporting</module>
<module>flexible/extending-runtime</module>
<module>flexible/helloworld</module>
<module>flexible/mailgun</module>
Expand Down