-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1e16092
Stackdriver error reporting quickstart
jabubake 6e01d4d
updating year in license
jabubake fff6216
updating README
jabubake 745693b
adding newlines
jabubake ee7606a
Merge branch 'master' into error-reporting-sample
jabubake 5db8f7e
Merge branch 'master' into error-reporting-sample
lesv fec28b1
cleaning up pom.xml
jabubake d6669e5
cleaning up, adding context for library usage
jabubake 944c70a
Merge branch 'error-reporting-sample' of https://github.com/GoogleClo…
jabubake 41a9d61
Merge branch 'master' of https://github.com/GoogleCloudPlatform/java-…
jabubake File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
<version>4.12</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
64 changes: 64 additions & 0 deletions
64
errorreporting/src/main/java/com/example/errorreporting/QuickStart.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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. @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] |
30 changes: 30 additions & 0 deletions
30
errorreporting/src/test/java/com/example/errorreporting/QuickStartIT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]{}); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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