|
| 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] |
0 commit comments