Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Metric class, implement metric methods and tests #1091

Merged
merged 1 commit into from
Jul 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static ListOption pageToken(String pageToken) {

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

Expand Down Expand Up @@ -135,4 +135,79 @@ public static ListOption pageToken(String pageToken) {
* was not found.
*/
Future<Boolean> deleteSinkAsync(String sink);

/**
* Creates a new metric.
*
* @return the created metric
* @throws LoggingException upon failure
*/
Metric create(MetricInfo metric);

/**
* Sends a request for creating a metric. This method returns a {@code Future} object to consume
* the result. {@link Future#get()} returns the created metric.
*/
Future<Metric> createAsync(MetricInfo metric);

/**
* Updates a metric or creates one if it does not exist.
*
* @return the created metric
* @throws LoggingException upon failure
*/
Metric update(MetricInfo metric);

/**
* Sends a request for updating a metric (or creating it, if it does not exist). This method
* returns a {@code Future} object to consume the result. {@link Future#get()} returns the
* updated/created metric or {@code null} if not found.
*/
Future<Metric> updateAsync(MetricInfo metric);

/**
* Returns the requested metric or {@code null} if not found.
*
* @throws LoggingException upon failure
*/
Metric getMetric(String metric);

/**
* Sends a request for getting a metric. This method returns a {@code Future} object to consume
* the result. {@link Future#get()} returns the requested metric or {@code null} if not found.
*
* @throws LoggingException upon failure
*/
Future<Metric> getMetricAsync(String metric);

/**
* Lists the metrics. This method returns a {@link Page} object that can be used to consume
* paginated results. Use {@link ListOption} to specify the page size or the page token from which
* to start listing metrics.
*
* @throws LoggingException upon failure
*/
Page<Metric> listMetrics(ListOption... options);

/**
* Sends a request for listing metrics. This method returns a {@code Future} object to consume
* the result. {@link Future#get()} returns an {@link AsyncPage} object that can be used to
* asynchronously handle paginated results. Use {@link ListOption} to specify the page size or the
* page token from which to start listing metrics.
*/
Future<AsyncPage<Metric>> listMetricsAsync(ListOption... options);

/**
* Deletes the requested metric.
*
* @return {@code true} if the metric was deleted, {@code false} if it was not found
*/
boolean deleteMetric(String metric);

/**
* Sends a request for deleting a metric. This method returns a {@code Future} object to consume
* the result. {@link Future#get()} returns {@code true} if the metric was deleted, {@code false}
* if it was not found.
*/
Future<Boolean> deleteMetricAsync(String metric);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package com.google.cloud.logging;

import static com.google.api.client.util.Preconditions.checkArgument;
import static com.google.cloud.logging.Logging.ListOption.OptionType.PAGE_SIZE;
import static com.google.cloud.logging.Logging.ListOption.OptionType.PAGE_TOKEN;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.util.concurrent.Futures.lazyTransform;

import com.google.cloud.AsyncPage;
Expand All @@ -28,17 +28,24 @@
import com.google.cloud.PageImpl;
import com.google.cloud.logging.spi.LoggingRpc;
import com.google.cloud.logging.spi.v2.ConfigServiceV2Api;
import com.google.cloud.logging.spi.v2.MetricsServiceV2Api;
import com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.util.concurrent.Uninterruptibles;
import com.google.logging.v2.CreateLogMetricRequest;
import com.google.logging.v2.CreateSinkRequest;
import com.google.logging.v2.DeleteLogMetricRequest;
import com.google.logging.v2.DeleteSinkRequest;
import com.google.logging.v2.GetLogMetricRequest;
import com.google.logging.v2.GetSinkRequest;
import com.google.logging.v2.ListLogMetricsRequest;
import com.google.logging.v2.ListLogMetricsResponse;
import com.google.logging.v2.ListSinksRequest;
import com.google.logging.v2.ListSinksResponse;
import com.google.logging.v2.UpdateLogMetricRequest;
import com.google.logging.v2.UpdateSinkRequest;
import com.google.protobuf.Empty;

Expand Down Expand Up @@ -111,6 +118,21 @@ public Future<AsyncPage<Sink>> nextPage() {
}
}

private static class MetricPageFetcher extends BasePageFetcher<Metric> {

private static final long serialVersionUID = -316783549651771553L;

MetricPageFetcher(LoggingOptions serviceOptions, String cursor,
Map<Option.OptionType, ?> requestOptions) {
super(serviceOptions, cursor, requestOptions);
}

@Override
public Future<AsyncPage<Metric>> nextPage() {
return listMetricsAsync(serviceOptions(), requestOptions());
}
}

@Override
public Sink create(SinkInfo sink) {
return get(createAsync(sink));
Expand Down Expand Up @@ -208,6 +230,103 @@ public Future<Boolean> deleteSinkAsync(String sink) {
return lazyTransform(rpc.delete(request), EMPTY_TO_BOOLEAN_FUNCTION);
}

@Override
public Metric create(MetricInfo metric) {
return get(createAsync(metric));
}

@Override
public Future<Metric> createAsync(MetricInfo metric) {
CreateLogMetricRequest request = CreateLogMetricRequest.newBuilder()
.setProjectName(MetricsServiceV2Api.formatProjectName(options().projectId()))
.setMetric(metric.toPb())
.build();
return lazyTransform(rpc.create(request), Metric.fromPbFunction(this));
}

@Override
public Metric update(MetricInfo metric) {
return get(updateAsync(metric));
}

@Override
public Future<Metric> updateAsync(MetricInfo metric) {
UpdateLogMetricRequest request = UpdateLogMetricRequest.newBuilder()
.setMetricName(MetricsServiceV2Api.formatMetricName(options().projectId(), metric.name()))
.setMetric(metric.toPb())
.build();
return lazyTransform(rpc.update(request), Metric.fromPbFunction(this));
}

@Override
public Metric getMetric(String metric) {
return get(getMetricAsync(metric));
}

@Override
public Future<Metric> getMetricAsync(String metric) {
GetLogMetricRequest request = GetLogMetricRequest.newBuilder()
.setMetricName(MetricsServiceV2Api.formatMetricName(options().projectId(), metric))
.build();
return lazyTransform(rpc.get(request), Metric.fromPbFunction(this));
}

private static ListLogMetricsRequest listMetricsRequest(LoggingOptions serviceOptions,
Map<Option.OptionType, ?> options) {
ListLogMetricsRequest.Builder builder = ListLogMetricsRequest.newBuilder();
builder.setProjectName(MetricsServiceV2Api.formatProjectName(serviceOptions.projectId()));
Integer pageSize = PAGE_SIZE.get(options);
String pageToken = PAGE_TOKEN.get(options);
if (pageSize != null) {
builder.setPageSize(pageSize);
}
if (pageToken != null) {
builder.setPageToken(pageToken);
}
return builder.build();
}

private static Future<AsyncPage<Metric>> listMetricsAsync(final LoggingOptions serviceOptions,
final Map<Option.OptionType, ?> options) {
final ListLogMetricsRequest request = listMetricsRequest(serviceOptions, options);
Future<ListLogMetricsResponse> list = serviceOptions.rpc().list(request);
return lazyTransform(list, new Function<ListLogMetricsResponse, AsyncPage<Metric>>() {
@Override
public AsyncPage<Metric> apply(ListLogMetricsResponse listMetricsResponse) {
List<Metric> metrics = listMetricsResponse.getMetricsList() == null
? ImmutableList.<Metric>of() : Lists.transform(listMetricsResponse.getMetricsList(),
Metric.fromPbFunction(serviceOptions.service()));
String cursor = listMetricsResponse.getNextPageToken().equals("") ? null
: listMetricsResponse.getNextPageToken();
return new AsyncPageImpl<>(new MetricPageFetcher(serviceOptions, cursor, options), cursor,
metrics);
}
});
}

@Override
public Page<Metric> listMetrics(ListOption... options) {
return get(listMetricsAsync(options));
}

@Override
public Future<AsyncPage<Metric>> listMetricsAsync(ListOption... options) {
return listMetricsAsync(options(), optionMap(options));
}

@Override
public boolean deleteMetric(String metric) {
return get(deleteMetricAsync(metric));
}

@Override
public Future<Boolean> deleteMetricAsync(String metric) {
DeleteLogMetricRequest request = DeleteLogMetricRequest.newBuilder()
.setMetricName(MetricsServiceV2Api.formatMetricName(options().projectId(), metric))
.build();
return lazyTransform(rpc.delete(request), EMPTY_TO_BOOLEAN_FUNCTION);
}

@Override
public void close() throws Exception {
if (closed) {
Expand Down
Loading