Skip to content

Commit 24c91c0

Browse files
authored
Fix Metric.update and add snippets for Metric class (#1221)
* Remove unnecessary MetricInfo parameter for Metric.update(Async) * Update snippets script to support final method parameters * Add snippets to Metric's javadoc, MetricSnippets class and tests
1 parent 7309be5 commit 24c91c0

File tree

7 files changed

+303
-22
lines changed

7 files changed

+303
-22
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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+
}

google-cloud-examples/src/test/java/com/google/cloud/examples/logging/snippets/ITLoggingSnippets.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.google.common.collect.Iterators;
3030
import com.google.common.collect.Sets;
3131

32+
import org.junit.AfterClass;
3233
import org.junit.BeforeClass;
3334
import org.junit.Rule;
3435
import org.junit.Test;
@@ -45,11 +46,11 @@ public class ITLoggingSnippets {
4546
private static final String DATASET = "dataset";
4647
private static final Set<String> DESCRIPTOR_TYPES = ImmutableSet.of("gce_instance", "gae_app",
4748
"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");
49+
"app_script_function", "dataproc_cluster", "ml_job", "bigquery_resource", "container",
50+
"gke_cluster", "cloud_debugger_resource", "http_load_balancer", "aws_ec2_instance",
51+
"client_auth_config_brand", "client_auth_config_client", "logging_log", "logging_sink",
52+
"metric", "project", "testservice_matrix", "service_account", "deployment",
53+
"dns_managed_zone");
5354

5455
private static Logging logging;
5556
private static LoggingSnippets loggingSnippets;
@@ -67,6 +68,13 @@ public static void beforeClass() {
6768
loggingSnippets = new LoggingSnippets(logging);
6869
}
6970

71+
@AfterClass
72+
public static void afterClass() throws Exception {
73+
if (logging != null) {
74+
logging.close();
75+
}
76+
}
77+
7078
@Test
7179
public void testSink() throws ExecutionException, InterruptedException {
7280
String sinkName1 = RemoteLoggingHelper.formatForTest("sink_name1");
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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.AfterClass;
30+
import org.junit.BeforeClass;
31+
import org.junit.Test;
32+
33+
import java.util.concurrent.ExecutionException;
34+
35+
public class ITMetricSnippets {
36+
37+
private static final String METRIC_NAME = RemoteLoggingHelper.formatForTest("it_metric_snippets");
38+
private static final String METRIC_FILTER = "severity>=ERROR";
39+
private static final String DESCRIPTION = "description";
40+
private static final String UPDATED_DESCRIPTION = "A more detailed description";
41+
42+
private static Logging logging;
43+
private static MetricSnippets metricSnippets;
44+
45+
@BeforeClass
46+
public static void beforeClass() {
47+
RemoteLoggingHelper helper = RemoteLoggingHelper.create();
48+
logging = helper.options().service();
49+
MetricInfo metricInfo = MetricInfo.builder(METRIC_NAME, METRIC_FILTER)
50+
.description(DESCRIPTION)
51+
.build();
52+
metricSnippets = new MetricSnippets(logging.create(metricInfo));
53+
}
54+
55+
@AfterClass
56+
public static void afterClass() throws Exception {
57+
if (logging != null) {
58+
logging.close();
59+
}
60+
}
61+
62+
@Test
63+
public void testMetric() throws InterruptedException, ExecutionException {
64+
Metric metric = metricSnippets.reload();
65+
assertNotNull(metric);
66+
Metric updatedMetric = metricSnippets.update();
67+
assertEquals(UPDATED_DESCRIPTION, updatedMetric.description());
68+
updatedMetric = metricSnippets.reloadAsync();
69+
assertNotNull(updatedMetric);
70+
assertEquals(UPDATED_DESCRIPTION, updatedMetric.description());
71+
metric.update();
72+
updatedMetric = metricSnippets.updateAsync();
73+
assertEquals(UPDATED_DESCRIPTION, updatedMetric.description());
74+
assertTrue(metricSnippets.delete());
75+
assertFalse(metricSnippets.deleteAsync());
76+
}
77+
}

google-cloud-logging/src/main/java/com/google/cloud/logging/Metric.java

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ public Logging logging() {
119119
/**
120120
* Deletes this metric.
121121
*
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+
*
122132
* @return {@code true} if the metric was deleted, {@code false} if it was not found
123133
* @throws LoggingException upon failure
124134
*/
@@ -131,6 +141,18 @@ public boolean delete() {
131141
* consume the result. {@link Future#get()} returns {@code true} if the metric was deleted,
132142
* {@code false} if it was not found.
133143
*
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+
*
134156
* @throws LoggingException upon failure
135157
*/
136158
public Future<Boolean> deleteAsync() {
@@ -140,6 +162,14 @@ public Future<Boolean> deleteAsync() {
140162
/**
141163
* Fetches current metric's latest information. Returns {@code null} if the metric does not exist.
142164
*
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+
*
143173
* @return a {@code Metric} object with latest information or {@code null} if not found
144174
* @throws LoggingException upon failure
145175
*/
@@ -152,6 +182,16 @@ public Metric reload() {
152182
* {@code Future} object to consume the result. {@link Future#get()} returns a {@code Metric}
153183
* object with latest information or {@code null} if not found.
154184
*
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+
*
155195
* @throws LoggingException upon failure
156196
*/
157197
public Future<Metric> reloadAsync() {
@@ -161,22 +201,40 @@ public Future<Metric> reloadAsync() {
161201
/**
162202
* Updates current metric. If the metric does not exist, it is created.
163203
*
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+
*
164212
* @return a {@code Metric} object with updated information
165213
* @throws LoggingException upon failure
166214
*/
167-
public Metric update(MetricInfo metricInfo) {
168-
return logging.update(metricInfo);
215+
public Metric update() {
216+
return logging.update(this);
169217
}
170218

171219
/**
172220
* Sends a request to update current metric. If the metric does not exist, it is created. This
173221
* method returns a {@code Future} object to consume the result. {@link Future#get()} returns a
174222
* {@code Metric} object with updated information.
175223
*
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+
*
176234
* @throws LoggingException upon failure
177235
*/
178-
public Future<Metric> updateAsync(MetricInfo metricInfo) {
179-
return logging.updateAsync(metricInfo);
236+
public Future<Metric> updateAsync() {
237+
return logging.updateAsync(this);
180238
}
181239

182240
private void readObject(ObjectInputStream input) throws IOException, ClassNotFoundException {

google-cloud-logging/src/test/java/com/google/cloud/logging/BaseSystemTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,11 @@ public void testCreateGetUpdateAndDeleteMetric() {
245245
assertEquals("severity>=ERROR", metric.filter());
246246
assertEquals("description", metric.description());
247247
assertEquals(metric, logging().getMetric(name));
248-
metric = metric.update(metric.toBuilder()
248+
metric = metric.toBuilder()
249249
.description("newDescription")
250250
.filter("severity>=WARNING")
251-
.build());
251+
.build()
252+
.update();
252253
assertEquals(name, metric.name());
253254
assertEquals("severity>=WARNING", metric.filter());
254255
assertEquals("newDescription", metric.description());
@@ -267,10 +268,11 @@ public void testCreateGetUpdateAndDeleteMetricAsync()
267268
assertEquals("severity>=ERROR", metric.filter());
268269
assertEquals("description", metric.description());
269270
assertEquals(metric, logging().getMetricAsync(name).get());
270-
metric = metric.updateAsync(metric.toBuilder()
271+
metric = metric.toBuilder()
271272
.description("newDescription")
272273
.filter("severity>=WARNING")
273-
.build()).get();
274+
.build()
275+
.updateAsync().get();
274276
assertEquals(name, metric.name());
275277
assertEquals("severity>=WARNING", metric.filter());
276278
assertEquals("newDescription", metric.description());

0 commit comments

Comments
 (0)