Skip to content

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 10 commits into from
Sep 20, 2017
35 changes: 35 additions & 0 deletions errorreporting/README.md
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.
47 changes: 47 additions & 0 deletions errorreporting/pom.xml
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>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<version>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

</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.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The 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[]{});
}
}
7 changes: 3 additions & 4 deletions flexible/errorreporting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just weird. 56-57 & L61

Copy link
Contributor

Choose a reason for hiding this comment

The 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))
.setMessage(StringWriter.toString())

ReportedErrorEvent.getDefaultInstance()
.toBuilder()
.setMessage(stackTrace.toString())
.setMessage(sw.toString())
.build();
// default project id
ProjectName projectName = ProjectName.create(ServiceOptions.getDefaultProjectId());
Expand Down
2 changes: 1 addition & 1 deletion logging/cloud-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
2 changes: 1 addition & 1 deletion logging/jul/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
2 changes: 1 addition & 1 deletion logging/logback/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@

<module>dlp</module>

<module>errorreporting</module>

<module>iap</module>

<module>kms</module>
Expand Down