GeezerVison is showing github.com — Add snippets to Metric's javadoc, MetricSnippets class and tests · googleapis/google-cloud-java@a87305b
File treeExpand file treeCollapse file tree Open diff view settings google-cloud-examples/src main/java/com/google/cloud/examples/logging/snippets test/java/com/google/cloud/examples/logging/snippets
google-cloud-logging/src/main/java/com/google/cloud/logging
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +134Lines changed: 134 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +/* | | 2 | + * Copyright 2016 Google Inc. All Rights Reserved. | | 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 | + | | 17 | +/* | | 18 | + * EDITING INSTRUCTIONS | | 19 | + * This file is referenced in Metric's javadoc. Any change to this file should be reflected in | | 20 | + * Metric's javadoc. | | 21 | + */ | | 22 | + | | 23 | +package com.google.cloud.examples.logging.snippets; | | 24 | + | | 25 | +import com.google.cloud.logging.Metric; | | 26 | + | | 27 | +import java.util.concurrent.ExecutionException; | | 28 | +import java.util.concurrent.Future; | | 29 | + | | 30 | +/** | | 31 | + * This class contains a number of snippets for the {@link Metric} class. | | 32 | + */ | | 33 | +public class MetricSnippets { | | 34 | + | | 35 | + private final Metric metric; | | 36 | + | | 37 | + public MetricSnippets(Metric metric) { | | 38 | + this.metric = metric; | | 39 | + } | | 40 | + | | 41 | + /** | | 42 | + * Example of getting the metric's latest information. | | 43 | + */ | | 44 | + // [TARGET reload()] | | 45 | + public Metric reload() { | | 46 | + // [START reload] | | 47 | + Metric latestMetric = metric.reload(); | | 48 | + if (latestMetric == null) { | | 49 | + // the metric was not found | | 50 | + } | | 51 | + // [END reload] | | 52 | + return latestMetric; | | 53 | + } | | 54 | + | | 55 | + /** | | 56 | + * Example of asynchronously getting the metric's latest information. | | 57 | + */ | | 58 | + // [TARGET reloadAsync()] | | 59 | + public Metric reloadAsync() throws ExecutionException, InterruptedException { | | 60 | + // [START reloadAsync] | | 61 | + Future<Metric> future = metric.reloadAsync(); | | 62 | + // ... | | 63 | + Metric latestMetric = future.get(); | | 64 | + if (latestMetric == null) { | | 65 | + // the metric was not found | | 66 | + } | | 67 | + // [END reloadAsync] | | 68 | + return latestMetric; | | 69 | + } | | 70 | + | | 71 | + /** | | 72 | + * Example of updating the metric's information. | | 73 | + */ | | 74 | + // [TARGET update()] | | 75 | + public Metric update() { | | 76 | + // [START update] | | 77 | + Metric updatedMetric = metric.toBuilder() | | 78 | + .description("A more detailed description") | | 79 | + .build() | | 80 | + .update(); | | 81 | + // [END update] | | 82 | + return updatedMetric; | | 83 | + } | | 84 | + | | 85 | + /** | | 86 | + * Example of asynchronously updating the metric's information. | | 87 | + */ | | 88 | + // [TARGET updateAsync()] | | 89 | + public Metric updateAsync() throws ExecutionException, InterruptedException { | | 90 | + // [START updateAsync] | | 91 | + Future<Metric> future = metric.toBuilder() | | 92 | + .description("A more detailed description") | | 93 | + .build() | | 94 | + .updateAsync(); | | 95 | + // ... | | 96 | + Metric updatedMetric = future.get(); | | 97 | + // [END updateAsync] | | 98 | + return updatedMetric; | | 99 | + } | | 100 | + | | 101 | + /** | | 102 | + * Example of deleting the metric. | | 103 | + */ | | 104 | + // [TARGET delete()] | | 105 | + public boolean delete() { | | 106 | + // [START delete] | | 107 | + boolean deleted = metric.delete(); | | 108 | + if (deleted) { | | 109 | + // the metric was deleted | | 110 | + } else { | | 111 | + // the metric was not found | | 112 | + } | | 113 | + // [END delete] | | 114 | + return deleted; | | 115 | + } | | 116 | + | | 117 | + /** | | 118 | + * Example of asynchronously deleting the metric. | | 119 | + */ | | 120 | + // [TARGET deleteAsync()] | | 121 | + public boolean deleteAsync() throws ExecutionException, InterruptedException { | | 122 | + // [START deleteAsync] | | 123 | + Future<Boolean> future = metric.deleteAsync(); | | 124 | + // ... | | 125 | + boolean deleted = future.get(); | | 126 | + if (deleted) { | | 127 | + // the metric was deleted | | 128 | + } else { | | 129 | + // the metric was not found | | 130 | + } | | 131 | + // [END deleteAsync] | | 132 | + return deleted; | | 133 | + } | | 134 | +} |
Collapse file Copy file name to clipboardExpand all lines: google-cloud-examples/src/test/java/com/google/cloud/examples/logging/snippets/ITLoggingSnippets.java +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -45,11 +45,11 @@ public class ITLoggingSnippets { | 45 | 45 | private static final String DATASET = "dataset"; | 46 | 46 | private static final Set<String> DESCRIPTOR_TYPES = ImmutableSet.of("gce_instance", "gae_app", | 47 | 47 | "cloudsql_database", "api", "gcs_bucket", "global", "dataflow_step", "build", | 48 | | - "app_script_function", "dataproc_cluster", "ml_job", "bigquery_resource", | 49 | | - "crm_iam_policy_check", "container", "gke_cluster", "cloud_debugger_resource", | 50 | | - "http_load_balancer", "aws_ec2_instance", "client_auth_config_brand", | 51 | | - "client_auth_config_client", "logging_log", "logging_sink", "metric", "project", | 52 | | - "testservice_matrix", "service_account", "deployment", "dns_managed_zone"); | | 48 | + "app_script_function", "dataproc_cluster", "ml_job", "bigquery_resource", "container", | | 49 | + "gke_cluster", "cloud_debugger_resource", "http_load_balancer", "aws_ec2_instance", | | 50 | + "client_auth_config_brand", "client_auth_config_client", "logging_log", "logging_sink", | | 51 | + "metric", "project", "testservice_matrix", "service_account", "deployment", | | 52 | + "dns_managed_zone"); | 53 | 53 |
| 54 | 54 | private static Logging logging; | 55 | 55 | private static LoggingSnippets loggingSnippets; | |
Collapse file Copy file name to clipboard +67Lines changed: 67 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +/* | | 2 | + * Copyright 2016 Google Inc. All Rights Reserved. | | 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 | + | | 17 | +package com.google.cloud.examples.logging.snippets; | | 18 | + | | 19 | +import static org.junit.Assert.assertEquals; | | 20 | +import static org.junit.Assert.assertFalse; | | 21 | +import static org.junit.Assert.assertNotNull; | | 22 | +import static org.junit.Assert.assertTrue; | | 23 | + | | 24 | +import com.google.cloud.logging.Logging; | | 25 | +import com.google.cloud.logging.Metric; | | 26 | +import com.google.cloud.logging.MetricInfo; | | 27 | +import com.google.cloud.logging.testing.RemoteLoggingHelper; | | 28 | + | | 29 | +import org.junit.BeforeClass; | | 30 | +import org.junit.Test; | | 31 | + | | 32 | +import java.util.concurrent.ExecutionException; | | 33 | + | | 34 | +public class ITMetricSnippets { | | 35 | + | | 36 | + private static final String METRIC_NAME = RemoteLoggingHelper.formatForTest("it_metric_snippets"); | | 37 | + private static final String METRIC_FILTER = "severity>=ERROR"; | | 38 | + private static final String DESCRIPTION = "description"; | | 39 | + | | 40 | + private static MetricSnippets metricSnippets; | | 41 | + | | 42 | + @BeforeClass | | 43 | + public static void beforeClass() { | | 44 | + RemoteLoggingHelper helper = RemoteLoggingHelper.create(); | | 45 | + Logging logging = helper.options().service(); | | 46 | + MetricInfo metricInfo = MetricInfo.builder(METRIC_NAME, METRIC_FILTER) | | 47 | + .description(DESCRIPTION) | | 48 | + .build(); | | 49 | + metricSnippets = new MetricSnippets(logging.create(metricInfo)); | | 50 | + } | | 51 | + | | 52 | + @Test | | 53 | + public void testMetric() throws InterruptedException, ExecutionException { | | 54 | + Metric metric = metricSnippets.reload(); | | 55 | + assertNotNull(metric); | | 56 | + Metric updatedMetric = metricSnippets.update(); | | 57 | + assertEquals("A more detailed description", updatedMetric.description()); | | 58 | + updatedMetric = metricSnippets.reloadAsync(); | | 59 | + assertNotNull(updatedMetric); | | 60 | + assertEquals("A more detailed description", updatedMetric.description()); | | 61 | + metric.update(); | | 62 | + updatedMetric = metricSnippets.updateAsync(); | | 63 | + assertEquals("A more detailed description", updatedMetric.description()); | | 64 | + assertTrue(metricSnippets.delete()); | | 65 | + assertFalse(metricSnippets.deleteAsync()); | | 66 | + } | | 67 | +} |
Collapse file Copy file name to clipboardExpand all lines: google-cloud-logging/src/main/java/com/google/cloud/logging/Metric.java +58Lines changed: 58 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -119,6 +119,16 @@ public Logging logging() { | 119 | 119 | /** | 120 | 120 | * Deletes this metric. | 121 | 121 | * | | 122 | + * <p>Example of deleting the metric. | | 123 | + * <pre> {@code | | 124 | + * boolean deleted = metric.delete(); | | 125 | + * if (deleted) { | | 126 | + * // the metric was deleted | | 127 | + * } else { | | 128 | + * // the metric was not found | | 129 | + * } | | 130 | + * }</pre> | | 131 | + * | 122 | 132 | * @return {@code true} if the metric was deleted, {@code false} if it was not found | 123 | 133 | * @throws LoggingException upon failure | 124 | 134 | */ | @@ -131,6 +141,18 @@ public boolean delete() { | 131 | 141 | * consume the result. {@link Future#get()} returns {@code true} if the metric was deleted, | 132 | 142 | * {@code false} if it was not found. | 133 | 143 | * | | 144 | + * <p>Example of asynchronously deleting the metric. | | 145 | + * <pre> {@code | | 146 | + * Future<Boolean> future = metric.deleteAsync(); | | 147 | + * // ... | | 148 | + * boolean deleted = future.get(); | | 149 | + * if (deleted) { | | 150 | + * // the metric was deleted | | 151 | + * } else { | | 152 | + * // the metric was not found | | 153 | + * } | | 154 | + * }</pre> | | 155 | + * | 134 | 156 | * @throws LoggingException upon failure | 135 | 157 | */ | 136 | 158 | public Future<Boolean> deleteAsync() { | @@ -140,6 +162,14 @@ public Future<Boolean> deleteAsync() { | 140 | 162 | /** | 141 | 163 | * Fetches current metric's latest information. Returns {@code null} if the metric does not exist. | 142 | 164 | * | | 165 | + * <p>Example of getting the metric's latest information. | | 166 | + * <pre> {@code | | 167 | + * Metric latestMetric = metric.reload(); | | 168 | + * if (latestMetric == null) { | | 169 | + * // the metric was not found | | 170 | + * } | | 171 | + * }</pre> | | 172 | + * | 143 | 173 | * @return a {@code Metric} object with latest information or {@code null} if not found | 144 | 174 | * @throws LoggingException upon failure | 145 | 175 | */ | @@ -152,6 +182,16 @@ public Metric reload() { | 152 | 182 | * {@code Future} object to consume the result. {@link Future#get()} returns a {@code Metric} | 153 | 183 | * object with latest information or {@code null} if not found. | 154 | 184 | * | | 185 | + * <p>Example of asynchronously getting the metric's latest information. | | 186 | + * <pre> {@code | | 187 | + * Future<Metric> future = metric.reloadAsync(); | | 188 | + * // ... | | 189 | + * Metric latestMetric = future.get(); | | 190 | + * if (latestMetric == null) { | | 191 | + * // the metric was not found | | 192 | + * } | | 193 | + * }</pre> | | 194 | + * | 155 | 195 | * @throws LoggingException upon failure | 156 | 196 | */ | 157 | 197 | public Future<Metric> reloadAsync() { | @@ -161,6 +201,14 @@ public Future<Metric> reloadAsync() { | 161 | 201 | /** | 162 | 202 | * Updates current metric. If the metric does not exist, it is created. | 163 | 203 | * | | 204 | + * <p>Example of updating the metric's information. | | 205 | + * <pre> {@code | | 206 | + * Metric updatedMetric = metric.toBuilder() | | 207 | + * .description("A more detailed description") | | 208 | + * .build() | | 209 | + * .update(); | | 210 | + * }</pre> | | 211 | + * | 164 | 212 | * @return a {@code Metric} object with updated information | 165 | 213 | * @throws LoggingException upon failure | 166 | 214 | */ | @@ -173,6 +221,16 @@ public Metric update() { | 173 | 221 | * method returns a {@code Future} object to consume the result. {@link Future#get()} returns a | 174 | 222 | * {@code Metric} object with updated information. | 175 | 223 | * | | 224 | + * <p>Example of asynchronously updating the metric's information. | | 225 | + * <pre> {@code | | 226 | + * Future<Metric> future = metric.toBuilder() | | 227 | + * .description("A more detailed description") | | 228 | + * .build() | | 229 | + * .updateAsync(); | | 230 | + * // ... | | 231 | + * Metric updatedMetric = future.get(); | | 232 | + * }</pre> | | 233 | + * | 176 | 234 | * @throws LoggingException upon failure | 177 | 235 | */ | 178 | 236 | public Future<Metric> updateAsync() { | |
|