|
16 | 16 |
|
17 | 17 | package com.google.cloud.logging;
|
18 | 18 |
|
19 |
| -import static com.google.api.client.util.Preconditions.checkArgument; |
20 | 19 | import static com.google.cloud.logging.Logging.ListOption.OptionType.PAGE_SIZE;
|
21 | 20 | import static com.google.cloud.logging.Logging.ListOption.OptionType.PAGE_TOKEN;
|
| 21 | +import static com.google.common.base.Preconditions.checkArgument; |
22 | 22 | import static com.google.common.util.concurrent.Futures.lazyTransform;
|
23 | 23 |
|
24 | 24 | import com.google.cloud.AsyncPage;
|
|
28 | 28 | import com.google.cloud.PageImpl;
|
29 | 29 | import com.google.cloud.logging.spi.LoggingRpc;
|
30 | 30 | import com.google.cloud.logging.spi.v2.ConfigServiceV2Api;
|
| 31 | +import com.google.cloud.logging.spi.v2.MetricsServiceV2Api; |
31 | 32 | import com.google.common.base.Function;
|
32 | 33 | import com.google.common.base.Throwables;
|
33 | 34 | import com.google.common.collect.ImmutableList;
|
34 | 35 | import com.google.common.collect.Lists;
|
35 | 36 | import com.google.common.collect.Maps;
|
36 | 37 | import com.google.common.util.concurrent.Uninterruptibles;
|
| 38 | +import com.google.logging.v2.CreateLogMetricRequest; |
37 | 39 | import com.google.logging.v2.CreateSinkRequest;
|
| 40 | +import com.google.logging.v2.DeleteLogMetricRequest; |
38 | 41 | import com.google.logging.v2.DeleteSinkRequest;
|
| 42 | +import com.google.logging.v2.GetLogMetricRequest; |
39 | 43 | import com.google.logging.v2.GetSinkRequest;
|
| 44 | +import com.google.logging.v2.ListLogMetricsRequest; |
| 45 | +import com.google.logging.v2.ListLogMetricsResponse; |
40 | 46 | import com.google.logging.v2.ListSinksRequest;
|
41 | 47 | import com.google.logging.v2.ListSinksResponse;
|
| 48 | +import com.google.logging.v2.UpdateLogMetricRequest; |
42 | 49 | import com.google.logging.v2.UpdateSinkRequest;
|
43 | 50 | import com.google.protobuf.Empty;
|
44 | 51 |
|
@@ -111,6 +118,21 @@ public Future<AsyncPage<Sink>> nextPage() {
|
111 | 118 | }
|
112 | 119 | }
|
113 | 120 |
|
| 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 | + |
114 | 136 | @Override
|
115 | 137 | public Sink create(SinkInfo sink) {
|
116 | 138 | return get(createAsync(sink));
|
@@ -208,6 +230,103 @@ public Future<Boolean> deleteSinkAsync(String sink) {
|
208 | 230 | return lazyTransform(rpc.delete(request), EMPTY_TO_BOOLEAN_FUNCTION);
|
209 | 231 | }
|
210 | 232 |
|
| 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 | + |
211 | 330 | @Override
|
212 | 331 | public void close() throws Exception {
|
213 | 332 | if (closed) {
|
|
0 commit comments