Skip to content

Commit 5497008

Browse files
authored
Add Metric class, implement metric methods and tests (#1091)
1 parent add9489 commit 5497008

File tree

5 files changed

+996
-6
lines changed

5 files changed

+996
-6
lines changed

gcloud-java-logging/src/main/java/com/google/cloud/logging/Logging.java

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static ListOption pageToken(String pageToken) {
7171

7272
/**
7373
* Sends a request for creating a sink. This method returns a {@code Future} object to consume the
74-
* result. {@link Future#get()} returns the created sink or {@code null} if not found.
74+
* result. {@link Future#get()} returns the created sink.
7575
*/
7676
Future<Sink> createAsync(SinkInfo sink);
7777

@@ -135,4 +135,79 @@ public static ListOption pageToken(String pageToken) {
135135
* was not found.
136136
*/
137137
Future<Boolean> deleteSinkAsync(String sink);
138+
139+
/**
140+
* Creates a new metric.
141+
*
142+
* @return the created metric
143+
* @throws LoggingException upon failure
144+
*/
145+
Metric create(MetricInfo metric);
146+
147+
/**
148+
* Sends a request for creating a metric. This method returns a {@code Future} object to consume
149+
* the result. {@link Future#get()} returns the created metric.
150+
*/
151+
Future<Metric> createAsync(MetricInfo metric);
152+
153+
/**
154+
* Updates a metric or creates one if it does not exist.
155+
*
156+
* @return the created metric
157+
* @throws LoggingException upon failure
158+
*/
159+
Metric update(MetricInfo metric);
160+
161+
/**
162+
* Sends a request for updating a metric (or creating it, if it does not exist). This method
163+
* returns a {@code Future} object to consume the result. {@link Future#get()} returns the
164+
* updated/created metric or {@code null} if not found.
165+
*/
166+
Future<Metric> updateAsync(MetricInfo metric);
167+
168+
/**
169+
* Returns the requested metric or {@code null} if not found.
170+
*
171+
* @throws LoggingException upon failure
172+
*/
173+
Metric getMetric(String metric);
174+
175+
/**
176+
* Sends a request for getting a metric. This method returns a {@code Future} object to consume
177+
* the result. {@link Future#get()} returns the requested metric or {@code null} if not found.
178+
*
179+
* @throws LoggingException upon failure
180+
*/
181+
Future<Metric> getMetricAsync(String metric);
182+
183+
/**
184+
* Lists the metrics. This method returns a {@link Page} object that can be used to consume
185+
* paginated results. Use {@link ListOption} to specify the page size or the page token from which
186+
* to start listing metrics.
187+
*
188+
* @throws LoggingException upon failure
189+
*/
190+
Page<Metric> listMetrics(ListOption... options);
191+
192+
/**
193+
* Sends a request for listing metrics. This method returns a {@code Future} object to consume
194+
* the result. {@link Future#get()} returns an {@link AsyncPage} object that can be used to
195+
* asynchronously handle paginated results. Use {@link ListOption} to specify the page size or the
196+
* page token from which to start listing metrics.
197+
*/
198+
Future<AsyncPage<Metric>> listMetricsAsync(ListOption... options);
199+
200+
/**
201+
* Deletes the requested metric.
202+
*
203+
* @return {@code true} if the metric was deleted, {@code false} if it was not found
204+
*/
205+
boolean deleteMetric(String metric);
206+
207+
/**
208+
* Sends a request for deleting a metric. This method returns a {@code Future} object to consume
209+
* the result. {@link Future#get()} returns {@code true} if the metric was deleted, {@code false}
210+
* if it was not found.
211+
*/
212+
Future<Boolean> deleteMetricAsync(String metric);
138213
}

gcloud-java-logging/src/main/java/com/google/cloud/logging/LoggingImpl.java

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
package com.google.cloud.logging;
1818

19-
import static com.google.api.client.util.Preconditions.checkArgument;
2019
import static com.google.cloud.logging.Logging.ListOption.OptionType.PAGE_SIZE;
2120
import static com.google.cloud.logging.Logging.ListOption.OptionType.PAGE_TOKEN;
21+
import static com.google.common.base.Preconditions.checkArgument;
2222
import static com.google.common.util.concurrent.Futures.lazyTransform;
2323

2424
import com.google.cloud.AsyncPage;
@@ -28,17 +28,24 @@
2828
import com.google.cloud.PageImpl;
2929
import com.google.cloud.logging.spi.LoggingRpc;
3030
import com.google.cloud.logging.spi.v2.ConfigServiceV2Api;
31+
import com.google.cloud.logging.spi.v2.MetricsServiceV2Api;
3132
import com.google.common.base.Function;
3233
import com.google.common.base.Throwables;
3334
import com.google.common.collect.ImmutableList;
3435
import com.google.common.collect.Lists;
3536
import com.google.common.collect.Maps;
3637
import com.google.common.util.concurrent.Uninterruptibles;
38+
import com.google.logging.v2.CreateLogMetricRequest;
3739
import com.google.logging.v2.CreateSinkRequest;
40+
import com.google.logging.v2.DeleteLogMetricRequest;
3841
import com.google.logging.v2.DeleteSinkRequest;
42+
import com.google.logging.v2.GetLogMetricRequest;
3943
import com.google.logging.v2.GetSinkRequest;
44+
import com.google.logging.v2.ListLogMetricsRequest;
45+
import com.google.logging.v2.ListLogMetricsResponse;
4046
import com.google.logging.v2.ListSinksRequest;
4147
import com.google.logging.v2.ListSinksResponse;
48+
import com.google.logging.v2.UpdateLogMetricRequest;
4249
import com.google.logging.v2.UpdateSinkRequest;
4350
import com.google.protobuf.Empty;
4451

@@ -111,6 +118,21 @@ public Future<AsyncPage<Sink>> nextPage() {
111118
}
112119
}
113120

121+
private static class MetricPageFetcher extends BasePageFetcher<Metric> {
122+
123+
private static final long serialVersionUID = -316783549651771553L;
124+
125+
MetricPageFetcher(LoggingOptions serviceOptions, String cursor,
126+
Map<Option.OptionType, ?> requestOptions) {
127+
super(serviceOptions, cursor, requestOptions);
128+
}
129+
130+
@Override
131+
public Future<AsyncPage<Metric>> nextPage() {
132+
return listMetricsAsync(serviceOptions(), requestOptions());
133+
}
134+
}
135+
114136
@Override
115137
public Sink create(SinkInfo sink) {
116138
return get(createAsync(sink));
@@ -208,6 +230,103 @@ public Future<Boolean> deleteSinkAsync(String sink) {
208230
return lazyTransform(rpc.delete(request), EMPTY_TO_BOOLEAN_FUNCTION);
209231
}
210232

233+
@Override
234+
public Metric create(MetricInfo metric) {
235+
return get(createAsync(metric));
236+
}
237+
238+
@Override
239+
public Future<Metric> createAsync(MetricInfo metric) {
240+
CreateLogMetricRequest request = CreateLogMetricRequest.newBuilder()
241+
.setProjectName(MetricsServiceV2Api.formatProjectName(options().projectId()))
242+
.setMetric(metric.toPb())
243+
.build();
244+
return lazyTransform(rpc.create(request), Metric.fromPbFunction(this));
245+
}
246+
247+
@Override
248+
public Metric update(MetricInfo metric) {
249+
return get(updateAsync(metric));
250+
}
251+
252+
@Override
253+
public Future<Metric> updateAsync(MetricInfo metric) {
254+
UpdateLogMetricRequest request = UpdateLogMetricRequest.newBuilder()
255+
.setMetricName(MetricsServiceV2Api.formatMetricName(options().projectId(), metric.name()))
256+
.setMetric(metric.toPb())
257+
.build();
258+
return lazyTransform(rpc.update(request), Metric.fromPbFunction(this));
259+
}
260+
261+
@Override
262+
public Metric getMetric(String metric) {
263+
return get(getMetricAsync(metric));
264+
}
265+
266+
@Override
267+
public Future<Metric> getMetricAsync(String metric) {
268+
GetLogMetricRequest request = GetLogMetricRequest.newBuilder()
269+
.setMetricName(MetricsServiceV2Api.formatMetricName(options().projectId(), metric))
270+
.build();
271+
return lazyTransform(rpc.get(request), Metric.fromPbFunction(this));
272+
}
273+
274+
private static ListLogMetricsRequest listMetricsRequest(LoggingOptions serviceOptions,
275+
Map<Option.OptionType, ?> options) {
276+
ListLogMetricsRequest.Builder builder = ListLogMetricsRequest.newBuilder();
277+
builder.setProjectName(MetricsServiceV2Api.formatProjectName(serviceOptions.projectId()));
278+
Integer pageSize = PAGE_SIZE.get(options);
279+
String pageToken = PAGE_TOKEN.get(options);
280+
if (pageSize != null) {
281+
builder.setPageSize(pageSize);
282+
}
283+
if (pageToken != null) {
284+
builder.setPageToken(pageToken);
285+
}
286+
return builder.build();
287+
}
288+
289+
private static Future<AsyncPage<Metric>> listMetricsAsync(final LoggingOptions serviceOptions,
290+
final Map<Option.OptionType, ?> options) {
291+
final ListLogMetricsRequest request = listMetricsRequest(serviceOptions, options);
292+
Future<ListLogMetricsResponse> list = serviceOptions.rpc().list(request);
293+
return lazyTransform(list, new Function<ListLogMetricsResponse, AsyncPage<Metric>>() {
294+
@Override
295+
public AsyncPage<Metric> apply(ListLogMetricsResponse listMetricsResponse) {
296+
List<Metric> metrics = listMetricsResponse.getMetricsList() == null
297+
? ImmutableList.<Metric>of() : Lists.transform(listMetricsResponse.getMetricsList(),
298+
Metric.fromPbFunction(serviceOptions.service()));
299+
String cursor = listMetricsResponse.getNextPageToken().equals("") ? null
300+
: listMetricsResponse.getNextPageToken();
301+
return new AsyncPageImpl<>(new MetricPageFetcher(serviceOptions, cursor, options), cursor,
302+
metrics);
303+
}
304+
});
305+
}
306+
307+
@Override
308+
public Page<Metric> listMetrics(ListOption... options) {
309+
return get(listMetricsAsync(options));
310+
}
311+
312+
@Override
313+
public Future<AsyncPage<Metric>> listMetricsAsync(ListOption... options) {
314+
return listMetricsAsync(options(), optionMap(options));
315+
}
316+
317+
@Override
318+
public boolean deleteMetric(String metric) {
319+
return get(deleteMetricAsync(metric));
320+
}
321+
322+
@Override
323+
public Future<Boolean> deleteMetricAsync(String metric) {
324+
DeleteLogMetricRequest request = DeleteLogMetricRequest.newBuilder()
325+
.setMetricName(MetricsServiceV2Api.formatMetricName(options().projectId(), metric))
326+
.build();
327+
return lazyTransform(rpc.delete(request), EMPTY_TO_BOOLEAN_FUNCTION);
328+
}
329+
211330
@Override
212331
public void close() throws Exception {
213332
if (closed) {

0 commit comments

Comments
 (0)