-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Changes from 5 commits
1e16092
6e01d4d
fff6216
745693b
ee7606a
5db8f7e
fec28b1
d6669e5
944c70a
41a9d61
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<!-- | ||
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> | ||
</dependency> | ||
</dependencies> | ||
</project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* 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; | ||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
|
||
/** | ||
* Snippet demonstrates using the Google Cloud Error Reporting API to report a custom error event. | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most users won't be using this kind of code, please see StackDriver Logging w/ link. |
||
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] |
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[]{}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just weird. 56-57 & L61 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we are going to do this, we really should explain what's happening. e.printStackTrace(PrintWriter(StringWriter)) |
||
ReportedErrorEvent.getDefaultInstance() | ||
.toBuilder() | ||
.setMessage(stackTrace.toString()) | ||
.setMessage(sw.toString()) | ||
.build(); | ||
// default project id | ||
ProjectName projectName = ProjectName.create(ServiceOptions.getDefaultProjectId()); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,8 @@ | |
|
||
<module>dlp</module> | ||
|
||
<module>errorreporting</module> | ||
|
||
<module>iap</module> | ||
|
||
<module>kms</module> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<version>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done