Skip to content

Commit e52d936

Browse files
authored
Error reporting quickstart (#854)
1 parent d1e23ca commit e52d936

File tree

11 files changed

+206
-36
lines changed

11 files changed

+206
-36
lines changed

errorreporting/README.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Stackdriver Error Reporting sample
2+
3+
[Stackdriver Error Reporting][error-reporting] Stackdriver Error Reporting counts, analyzes and aggregates the crashes in your running cloud services.
4+
A [centralized error management interface](https://console.cloud.google.com/errors) displays the results with sorting and filtering capabilities.
5+
6+
This sample Java application demonstrates how to send custom error events using the [Error Reporting API][api-ref-docs].
7+
Note: Runtime exceptions and stack traces are automatically sent to Error reporting in applications running in [App Engine Flex environment][ae-flex].
8+
9+
[ae-flex]: https://cloud.google.com/appengine/docs/flexible/java
10+
[error-reporting]: https://cloud.google.com/error-reporting/
11+
[api-ref-docs]: https://googlecloudplatform.github.io/google-cloud-java/latest/apidocs/index.html?com/google/cloud/errorreporting/v1beta1/package-summary.html
12+
13+
## Setup
14+
15+
1. Install [Maven](http://maven.apache.org/).
16+
1. [Enable](https://console.cloud.google.com/apis/api/clouderrorreporting.googleapis.com/overview) Stack Driver Error Reporting API.
17+
18+
## Build
19+
20+
Build your project with:
21+
```
22+
mvn clean package
23+
```
24+
25+
## Local testing
26+
27+
1. [Create a service account](https://cloud.google.com/docs/authentication/getting-started#creating_the_service_account)
28+
and set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable.
29+
2. Run
30+
```
31+
mvn clean verify
32+
```
33+
Check the [error reporting console](https://console.cloud.google.com/errors).
34+
35+
Confirm that you see the custom errors reported using the Error Reporting API.

errorreporting/pom.xml

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<!--
2+
Copyright 2017 Google Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
17+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
18+
<modelVersion>4.0.0</modelVersion>
19+
<groupId>com.google.cloud.logging.samples</groupId>
20+
<artifactId>cloud-logging-samples</artifactId>
21+
<packaging>pom</packaging>
22+
23+
<parent>
24+
<artifactId>doc-samples</artifactId>
25+
<groupId>com.google.cloud</groupId>
26+
<version>1.0.0</version>
27+
<relativePath>..</relativePath>
28+
</parent>
29+
30+
<properties>
31+
<maven.compiler.target>1.8</maven.compiler.target>
32+
<maven.compiler.source>1.8</maven.compiler.source>
33+
</properties>
34+
35+
<dependencies>
36+
<dependency>
37+
<groupId>com.google.cloud</groupId>
38+
<artifactId>google-cloud-errorreporting</artifactId>
39+
<version>0.24.0-alpha</version>
40+
</dependency>
41+
<!-- Test dependencies -->
42+
<dependency>
43+
<groupId>junit</groupId>
44+
<artifactId>junit</artifactId>
45+
<version>4.12</version>
46+
<scope>test</scope>
47+
</dependency>
48+
</dependencies>
49+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Copyright 2017 Google Inc.
3+
*
4+
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5+
* except in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* <p>http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
10+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
* express or implied. See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package com.example.errorreporting;
16+
17+
//[START errorreporting_quickstart]
18+
import com.google.cloud.ServiceOptions;
19+
import com.google.cloud.errorreporting.v1beta1.ReportErrorsServiceClient;
20+
import com.google.devtools.clouderrorreporting.v1beta1.ErrorContext;
21+
import com.google.devtools.clouderrorreporting.v1beta1.ProjectName;
22+
import com.google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent;
23+
import com.google.devtools.clouderrorreporting.v1beta1.SourceLocation;
24+
25+
/**
26+
* Snippet demonstrates using the Stackdriver Error Reporting API to report a custom error event.
27+
28+
* This library is not required on App Engine, errors written to stderr are automatically written
29+
* to Stackdriver Error Reporting.
30+
* It is also not required if you are writing logs to Stackdriver Logging.
31+
* Errors written to Stackdriver Logging that contain an exception or stack trace
32+
* are automatically written out to Stackdriver Error Reporting.
33+
*/
34+
public class QuickStart {
35+
public static void main(String[] args) throws Exception {
36+
37+
// Google Cloud Platform Project ID
38+
String projectId = (args.length > 0) ? args[0] : ServiceOptions.getDefaultProjectId();
39+
ProjectName projectName = ProjectName.create(projectId);
40+
41+
// Instantiate an Error Reporting Client
42+
try (ReportErrorsServiceClient reportErrorsServiceClient = ReportErrorsServiceClient.create()) {
43+
44+
// Custom error events require an error reporting location as well.
45+
ErrorContext errorContext = ErrorContext.newBuilder()
46+
.setReportLocation(SourceLocation.newBuilder()
47+
.setFilePath("Test.java")
48+
.setLineNumber(10)
49+
.setFunctionName("myMethod")
50+
.build())
51+
.build();
52+
53+
//Report a custom error event
54+
ReportedErrorEvent customErrorEvent = ReportedErrorEvent.getDefaultInstance()
55+
.toBuilder()
56+
.setMessage("custom error event")
57+
.setContext(errorContext)
58+
.build();
59+
// Report an event synchronously, use .reportErrorEventCallable for asynchronous reporting.
60+
reportErrorsServiceClient.reportErrorEvent(projectName, customErrorEvent);
61+
}
62+
}
63+
}
64+
// [END errorreporting_quickstart]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Copyright 2017 Google Inc.
3+
*
4+
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5+
* except in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* <p>http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
10+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
* express or implied. See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package com.example.errorreporting;
16+
17+
import org.junit.Test;
18+
import org.junit.runner.RunWith;
19+
import org.junit.runners.JUnit4;
20+
21+
@RunWith(JUnit4.class)
22+
@SuppressWarnings("AbbreviationAsWordInName")
23+
public class QuickStartIT {
24+
25+
@Test
26+
public void testQuickStart() throws Exception {
27+
// Ensure quick start runs without any exception
28+
QuickStart.main(new String[]{});
29+
}
30+
}

flexible/errorreporting/README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@ Build your project with:
2424
```
2525

2626
## Local testing
27-
1. Authorize the local environment
28-
```
29-
gcloud auth application-default login
30-
```
27+
[Create a service account](https://cloud.google.com/docs/authentication/getting-started#creating_the_service_account)
28+
and set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable.
29+
3130
For local testing, we will be using the [Jetty Maven plugin](http://www.eclipse.org/jetty/documentation/9.4.x/jetty-maven-plugin.html).
3231
Run:
3332
```

flexible/errorreporting/pom.xml

-16
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,6 @@
3636
<jetty.maven.plugin>9.4.6.v20170531</jetty.maven.plugin>
3737
</properties>
3838

39-
<!-- Temporary workaround for known issue : https://github.com/GoogleCloudPlatform/google-cloud-java/issues/2192 -->
40-
<dependencyManagement>
41-
<dependencies>
42-
<dependency>
43-
<groupId>com.google.auth</groupId>
44-
<artifactId>google-auth-library-credentials</artifactId>
45-
<version>0.8.0</version>
46-
</dependency>
47-
<dependency>
48-
<groupId>com.google.auth</groupId>
49-
<artifactId>google-auth-library-oauth2-http</artifactId>
50-
<version>0.8.0</version>
51-
</dependency>
52-
</dependencies>
53-
</dependencyManagement>
54-
5539
<dependencies>
5640
<dependency>
5741
<groupId>javax.servlet</groupId>

flexible/errorreporting/src/main/java/com/example/flexible/errorreporting/ErrorReportingExample.java

+20-13
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515

1616
import com.google.cloud.ServiceOptions;
1717
import com.google.cloud.errorreporting.v1beta1.ReportErrorsServiceClient;
18+
import com.google.devtools.clouderrorreporting.v1beta1.ErrorContext;
1819
import com.google.devtools.clouderrorreporting.v1beta1.ProjectName;
1920
import com.google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent;
2021

22+
import com.google.devtools.clouderrorreporting.v1beta1.SourceLocation;
2123
import java.io.IOException;
2224
import java.io.PrintWriter;
2325
import java.io.StringWriter;
@@ -42,28 +44,33 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp)
4244
// errors logged to stderr / Cloud logging with exceptions are automatically reported.
4345
logger.log(Level.SEVERE, "exception using log framework", new IllegalArgumentException());
4446

45-
// use the error-reporting client library to log custom error events
47+
// use the error-reporting client library only if you require logging custom error events.
4648
logCustomErrorEvent();
4749

48-
// runtime exceptions are also automatically picked up by error reporting.
50+
// runtime exceptions are also automatically reported.
4951
throw new RuntimeException("this is a runtime exception");
5052
}
5153

5254
private void logCustomErrorEvent() {
5355
try (ReportErrorsServiceClient reportErrorsServiceClient = ReportErrorsServiceClient.create()) {
54-
// custom exception logged using the API
55-
Exception e = new Exception("custom event reported using the API");
56-
// Events reported using the API must contain a stack trace message
57-
StringWriter stackTrace = new StringWriter();
58-
e.printStackTrace(new PrintWriter(stackTrace));
59-
ReportedErrorEvent errorEvent =
60-
ReportedErrorEvent.getDefaultInstance()
61-
.toBuilder()
62-
.setMessage(stackTrace.toString())
63-
.build();
56+
// Custom error events require an error reporting location as well.
57+
ErrorContext errorContext = ErrorContext.newBuilder()
58+
.setReportLocation(SourceLocation.newBuilder()
59+
.setFilePath("Test.java")
60+
.setLineNumber(10)
61+
.setFunctionName("myMethod")
62+
.build())
63+
.build();
64+
//Report a custom error event
65+
ReportedErrorEvent customErrorEvent = ReportedErrorEvent.getDefaultInstance()
66+
.toBuilder()
67+
.setMessage("custom error event")
68+
.setContext(errorContext)
69+
.build();
70+
6471
// default project id
6572
ProjectName projectName = ProjectName.create(ServiceOptions.getDefaultProjectId());
66-
reportErrorsServiceClient.reportErrorEvent(projectName, errorEvent);
73+
reportErrorsServiceClient.reportErrorEvent(projectName, customErrorEvent);
6774
} catch (Exception e) {
6875
logger.log(Level.SEVERE, "Exception encountered logging custom event", e);
6976
}

logging/cloud-client/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ Build your project with:
3131
-Dexec.args="my-log"
3232

3333

34-
Logs can also viewed using the [Logs Viewer Console](https://pantheon.corp.google.com/logs/viewer).
34+
Logs can also viewed using the [Logs Viewer Console](https://console.cloud.google.com/logs/viewer).

logging/jul/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ provides an example of enhancing log entries with additional labels.
3434
mvn exec:java -Dexec.mainClass=com.example.logging.jul.Quickstart \
3535
-Dexec.args="-Djava.util.logging.file=src/main/resources/logging.properties"
3636

37-
Logs can be viewed using the [Logs Viewer Console](https://pantheon.corp.google.com/logs/viewer).
37+
Logs can be viewed using the [Logs Viewer Console](https://console.cloud.google.com/logs/viewer).

logging/logback/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ provides an example of enhancing log entries with additional labels.
3535
## Writing log entries
3636
mvn exec:java -Dexec.mainClass=com.example.logging.logback.Quickstart
3737

38-
Logs can be viewed using the [Logs Viewer Console](https://pantheon.corp.google.com/logs/viewer).
38+
Logs can be viewed using the [Logs Viewer Console](https://console.cloud.google.com/logs/viewer).

pom.xml

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454

5555
<module>dlp</module>
5656

57+
<module>errorreporting</module>
58+
5759
<module>iap</module>
5860

5961
<module>kms</module>

0 commit comments

Comments
 (0)