From 1e160924213654c87458d6282e0f07a2f327416e Mon Sep 17 00:00:00 2001 From: Jisha Abubaker Date: Tue, 19 Sep 2017 14:19:23 -0700 Subject: [PATCH 1/6] Stackdriver error reporting quickstart --- errorreporting/README.md | 35 ++++++++++ errorreporting/pom.xml | 47 +++++++++++++ .../example/errorreporting/QuickStart.java | 70 +++++++++++++++++++ .../example/errorreporting/QuickStartIT.java | 30 ++++++++ flexible/errorreporting/README.md | 7 +- .../errorreporting/ErrorReportingExample.java | 7 +- logging/cloud-client/README.md | 2 +- logging/jul/README.md | 2 +- logging/logback/README.md | 2 +- pom.xml | 2 + 10 files changed, 193 insertions(+), 11 deletions(-) create mode 100644 errorreporting/README.md create mode 100644 errorreporting/pom.xml create mode 100644 errorreporting/src/main/java/com/example/errorreporting/QuickStart.java create mode 100644 errorreporting/src/test/java/com/example/errorreporting/QuickStartIT.java diff --git a/errorreporting/README.md b/errorreporting/README.md new file mode 100644 index 00000000000..4dff547264b --- /dev/null +++ b/errorreporting/README.md @@ -0,0 +1,35 @@ +# Stack Driver 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. \ No newline at end of file diff --git a/errorreporting/pom.xml b/errorreporting/pom.xml new file mode 100644 index 00000000000..3bf056e2c7e --- /dev/null +++ b/errorreporting/pom.xml @@ -0,0 +1,47 @@ + + + 4.0.0 + com.google.cloud.logging.samples + cloud-logging-samples + pom + + + doc-samples + com.google.cloud + 1.0.0 + .. + + + + 1.8 + 1.8 + + + + + com.google.cloud + google-cloud-errorreporting + 0.24.0-alpha + + + + junit + junit + + + diff --git a/errorreporting/src/main/java/com/example/errorreporting/QuickStart.java b/errorreporting/src/main/java/com/example/errorreporting/QuickStart.java new file mode 100644 index 00000000000..2b0c710d36f --- /dev/null +++ b/errorreporting/src/main/java/com/example/errorreporting/QuickStart.java @@ -0,0 +1,70 @@ +/** + * 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. + */ + +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; +import java.io.PrintWriter; +import java.io.StringWriter; + +/** + * Snippet demonstrates using the Google Cloud Error Reporting API to report a custom error event. + */ +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); + + // Report a stack trace from an exception using the API + // Stack trace / exception reporting does not require source location + Exception e = new Exception("custom event reported using the API"); + StringWriter sw = new StringWriter(); + e.printStackTrace(new PrintWriter(sw)); + ReportedErrorEvent stackTraceEvent = ReportedErrorEvent.getDefaultInstance() + .toBuilder() + .setMessage(sw.toString()) + .build(); + reportErrorsServiceClient.reportErrorEvent(projectName, stackTraceEvent); + } + } +} +// [END errorreporting_quickstart] diff --git a/errorreporting/src/test/java/com/example/errorreporting/QuickStartIT.java b/errorreporting/src/test/java/com/example/errorreporting/QuickStartIT.java new file mode 100644 index 00000000000..e26c1657073 --- /dev/null +++ b/errorreporting/src/test/java/com/example/errorreporting/QuickStartIT.java @@ -0,0 +1,30 @@ +/** + * 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. + */ + +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[]{}); + } +} \ No newline at end of file diff --git a/flexible/errorreporting/README.md b/flexible/errorreporting/README.md index 089cbfb8fb3..b27ba8ca1f0 100644 --- a/flexible/errorreporting/README.md +++ b/flexible/errorreporting/README.md @@ -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: ``` diff --git a/flexible/errorreporting/src/main/java/com/example/flexible/errorreporting/ErrorReportingExample.java b/flexible/errorreporting/src/main/java/com/example/flexible/errorreporting/ErrorReportingExample.java index 38a7c86d666..4f14c59c58b 100644 --- a/flexible/errorreporting/src/main/java/com/example/flexible/errorreporting/ErrorReportingExample.java +++ b/flexible/errorreporting/src/main/java/com/example/flexible/errorreporting/ErrorReportingExample.java @@ -53,13 +53,12 @@ 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)); + StringWriter sw = new StringWriter(); + e.printStackTrace(new PrintWriter(sw)); ReportedErrorEvent errorEvent = ReportedErrorEvent.getDefaultInstance() .toBuilder() - .setMessage(stackTrace.toString()) + .setMessage(sw.toString()) .build(); // default project id ProjectName projectName = ProjectName.create(ServiceOptions.getDefaultProjectId()); diff --git a/logging/cloud-client/README.md b/logging/cloud-client/README.md index 2b0c8630204..25b17a1d750 100644 --- a/logging/cloud-client/README.md +++ b/logging/cloud-client/README.md @@ -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). diff --git a/logging/jul/README.md b/logging/jul/README.md index 90f91b14c2d..641317b9c52 100644 --- a/logging/jul/README.md +++ b/logging/jul/README.md @@ -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). diff --git a/logging/logback/README.md b/logging/logback/README.md index b12c0c9890b..d7dcaeb74ce 100644 --- a/logging/logback/README.md +++ b/logging/logback/README.md @@ -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). diff --git a/pom.xml b/pom.xml index 7c2e85970d8..988ea203237 100644 --- a/pom.xml +++ b/pom.xml @@ -54,6 +54,8 @@ dlp + errorreporting + iap kms From 6e01d4d28817ae5b4f7c195eec80064afee8291f Mon Sep 17 00:00:00 2001 From: Jisha Abubaker Date: Tue, 19 Sep 2017 14:21:06 -0700 Subject: [PATCH 2/6] updating year in license --- errorreporting/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/errorreporting/pom.xml b/errorreporting/pom.xml index 3bf056e2c7e..bf25f55dcc8 100644 --- a/errorreporting/pom.xml +++ b/errorreporting/pom.xml @@ -1,5 +1,5 @@ - - - - com.google.auth - google-auth-library-credentials - 0.7.1 - - - com.google.auth - google-auth-library-oauth2-http - 0.7.1 - - - - javax.servlet @@ -63,7 +47,7 @@ com.google.cloud google-cloud-errorreporting - 0.21.1-alpha + 0.24.0-alpha From d6669e57d464f40204653514b8a5246abd969c18 Mon Sep 17 00:00:00 2001 From: Jisha Abubaker Date: Tue, 19 Sep 2017 15:43:31 -0700 Subject: [PATCH 6/6] cleaning up, adding context for library usage --- .../example/errorreporting/QuickStart.java | 22 +++++-------- .../errorreporting/ErrorReportingExample.java | 32 ++++++++++++------- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/errorreporting/src/main/java/com/example/errorreporting/QuickStart.java b/errorreporting/src/main/java/com/example/errorreporting/QuickStart.java index 2b0c710d36f..4b495ddddfe 100644 --- a/errorreporting/src/main/java/com/example/errorreporting/QuickStart.java +++ b/errorreporting/src/main/java/com/example/errorreporting/QuickStart.java @@ -21,11 +21,15 @@ import com.google.devtools.clouderrorreporting.v1beta1.ProjectName; import com.google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent; import com.google.devtools.clouderrorreporting.v1beta1.SourceLocation; -import java.io.PrintWriter; -import java.io.StringWriter; /** - * Snippet demonstrates using the Google Cloud Error Reporting API to report a custom error event. + * 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 + * 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 { @@ -45,6 +49,7 @@ public static void main(String[] args) throws Exception { .setFunctionName("myMethod") .build()) .build(); + //Report a custom error event ReportedErrorEvent customErrorEvent = ReportedErrorEvent.getDefaultInstance() .toBuilder() @@ -53,17 +58,6 @@ public static void main(String[] args) throws Exception { .build(); // Report an event synchronously, use .reportErrorEventCallable for asynchronous reporting. reportErrorsServiceClient.reportErrorEvent(projectName, customErrorEvent); - - // Report a stack trace from an exception using the API - // Stack trace / exception reporting does not require source location - Exception e = new Exception("custom event reported using the API"); - StringWriter sw = new StringWriter(); - e.printStackTrace(new PrintWriter(sw)); - ReportedErrorEvent stackTraceEvent = ReportedErrorEvent.getDefaultInstance() - .toBuilder() - .setMessage(sw.toString()) - .build(); - reportErrorsServiceClient.reportErrorEvent(projectName, stackTraceEvent); } } } diff --git a/flexible/errorreporting/src/main/java/com/example/flexible/errorreporting/ErrorReportingExample.java b/flexible/errorreporting/src/main/java/com/example/flexible/errorreporting/ErrorReportingExample.java index 4f14c59c58b..1b9f4eebcd2 100644 --- a/flexible/errorreporting/src/main/java/com/example/flexible/errorreporting/ErrorReportingExample.java +++ b/flexible/errorreporting/src/main/java/com/example/flexible/errorreporting/ErrorReportingExample.java @@ -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; @@ -42,27 +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"); - StringWriter sw = new StringWriter(); - e.printStackTrace(new PrintWriter(sw)); - ReportedErrorEvent errorEvent = - ReportedErrorEvent.getDefaultInstance() - .toBuilder() - .setMessage(sw.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); }