diff --git a/gcloud-java-core/pom.xml b/gcloud-java-core/pom.xml index 0a426d50a21b..50155634bd11 100644 --- a/gcloud-java-core/pom.xml +++ b/gcloud-java-core/pom.xml @@ -106,7 +106,7 @@ com.google.api gax - 0.0.14 + 0.0.16 com.google.api.grpc diff --git a/gcloud-java-trace/pom.xml b/gcloud-java-trace/pom.xml new file mode 100644 index 000000000000..6796dc36415a --- /dev/null +++ b/gcloud-java-trace/pom.xml @@ -0,0 +1,126 @@ + + + 4.0.0 + gcloud-java-trace + jar + GCloud Java Stackdriver Trace + https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-trace + + Java idiomatic client for Stackdriver Trace. + + + com.google.cloud + gcloud-java-pom + 0.2.9-SNAPSHOT + + + gcloud-java-trace + + + + io.netty + netty-tcnative-boringssl-static + 1.1.33.Fork17 + + + ${project.groupId} + gcloud-java-core + ${project.version} + + + com.google.api.grpc + grpc-google-devtools-cloudtrace-v1 + 0.0.7 + + + io.grpc + grpc-all + 0.15.0 + + + com.google.auto.value + auto-value + 1.1 + + + ${project.groupId} + gcloud-java-core + ${project.version} + test-jar + test + + + junit + junit + 4.12 + test + + + org.easymock + easymock + 3.4 + test + + + + + doclint-java8-disable + + [1.8,) + + + + -Xdoclint:none + + + + + + + org.codehaus.mojo + build-helper-maven-plugin + 1.9.1 + + + generate-sources + add-source + + + generated/src/main/java + + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.10.3 + + + attach-javadocs + + jar + + + ${javadoc.opts} + + + + + + maven-compiler-plugin + + + + 3.1 + + 1.7 + 1.7 + UTF-8 + -Xlint:unchecked + + + + + diff --git a/gcloud-java-trace/src/main/java/com/google/cloud/trace/spi/v1/TraceServiceApi.java b/gcloud-java-trace/src/main/java/com/google/cloud/trace/spi/v1/TraceServiceApi.java new file mode 100644 index 000000000000..0e94787987e7 --- /dev/null +++ b/gcloud-java-trace/src/main/java/com/google/cloud/trace/spi/v1/TraceServiceApi.java @@ -0,0 +1,422 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.cloud.trace.spi.v1; + +import com.google.api.gax.core.PageAccessor; +import com.google.api.gax.grpc.ApiCallable; +import com.google.api.gax.protobuf.PathTemplate; +import com.google.devtools.cloudtrace.v1.GetTraceRequest; +import com.google.devtools.cloudtrace.v1.ListTracesRequest; +import com.google.devtools.cloudtrace.v1.ListTracesResponse; +import com.google.devtools.cloudtrace.v1.PatchTracesRequest; +import com.google.devtools.cloudtrace.v1.Trace; +import com.google.devtools.cloudtrace.v1.Traces; +import com.google.protobuf.Empty; +import io.grpc.ManagedChannel; +import java.io.Closeable; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ScheduledExecutorService; + +// AUTO-GENERATED DOCUMENTATION AND SERVICE +/** + * Service Description: This file describes an API for collecting and viewing traces and spans + * within a trace. A Trace is a collection of spans corresponding to a single + * operation or set of operations for an application. A span is an individual + * timed event which forms a node of the trace tree. Spans for a single trace + * may span multiple services. + * + *

This class provides the ability to make remote calls to the backing service through method + * calls that map to API methods. Sample code to get started: + * + *

+ * 
+ * try (TraceServiceApi traceServiceApi = TraceServiceApi.create()) {
+ *   String projectId = "";
+ *   Traces traces = Traces.newBuilder().build();
+ *   traceServiceApi.patchTraces(projectId, traces);
+ * }
+ * 
+ * 
+ * + *

Note: close() needs to be called on the traceServiceApi object to clean up resources such + * as threads. In the example above, try-with-resources is used, which automatically calls + * close(). + * + *

The surface of this class includes several types of Java methods for each of the API's methods: + * + *

    + *
  1. A "flattened" method. With this type of method, the fields of the request type have been + * converted into function parameters. It may be the case that not all fields are available + * as parameters, and not every API method will have a flattened method entry point. + *
  2. A "request object" method. This type of method only takes one parameter, a request + * object, which must be constructed before the call. Not every API method will have a request + * object method. + *
  3. A "callable" method. This type of method takes no parameters and returns an immutable + * ApiCallable object, which can be used to initiate calls to the service. + *
+ * + *

See the individual methods for example code. + * + *

Many parameters require resource names to be formatted in a particular way. To assist + * with these names, this class includes a format method for each type of name, and additionally + * a parse method to extract the individual identifiers contained within names that are + * returned. + * + *

This class can be customized by passing in a custom instance of TraceServiceSettings to + * create(). For example: + * + *

+ * 
+ * TraceServiceSettings traceServiceSettings = TraceServiceSettings.defaultBuilder()
+ *     .provideChannelWith(myCredentials)
+ *     .build();
+ * TraceServiceApi traceServiceApi = TraceServiceApi.create(traceServiceSettings);
+ * 
+ * 
+ */ +@javax.annotation.Generated("by GAPIC") +public class TraceServiceApi implements AutoCloseable { + private final TraceServiceSettings settings; + private final ManagedChannel channel; + private final ScheduledExecutorService executor; + private final List closeables = new ArrayList<>(); + + private final ApiCallable patchTracesCallable; + private final ApiCallable getTraceCallable; + private final ApiCallable listTracesCallable; + private final ApiCallable> listTracesPagedCallable; + + public final TraceServiceSettings getSettings() { + return settings; + } + + /** + * Constructs an instance of TraceServiceApi with default settings. + */ + public static final TraceServiceApi create() throws IOException { + return create(TraceServiceSettings.defaultBuilder().build()); + } + + /** + * Constructs an instance of TraceServiceApi, using the given settings. + * The channels are created based on the settings passed in, or defaults for any + * settings that are not set. + */ + public static final TraceServiceApi create(TraceServiceSettings settings) throws IOException { + return new TraceServiceApi(settings); + } + + /** + * Constructs an instance of TraceServiceApi, using the given settings. + * This is protected so that it easy to make a subclass, but otherwise, the static + * factory methods should be preferred. + */ + protected TraceServiceApi(TraceServiceSettings settings) throws IOException { + this.settings = settings; + this.executor = settings.getExecutorProvider().getOrBuildExecutor(); + this.channel = settings.getChannelProvider().getOrBuildChannel(this.executor); + + this.patchTracesCallable = + ApiCallable.create(settings.patchTracesSettings(), this.channel, this.executor); + this.getTraceCallable = + ApiCallable.create(settings.getTraceSettings(), this.channel, this.executor); + this.listTracesCallable = + ApiCallable.create(settings.listTracesSettings(), this.channel, this.executor); + this.listTracesPagedCallable = + ApiCallable.createPagedVariant(settings.listTracesSettings(), this.channel, this.executor); + + if (settings.getChannelProvider().shouldAutoClose()) { + closeables.add( + new Closeable() { + @Override + public void close() throws IOException { + channel.shutdown(); + } + }); + } + if (settings.getExecutorProvider().shouldAutoClose()) { + closeables.add( + new Closeable() { + @Override + public void close() throws IOException { + executor.shutdown(); + } + }); + } + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Sends new traces to Cloud Trace or updates existing traces. If the ID of + * a trace that you send matches that of an existing trace, any fields + * in the existing trace and its spans are overwritten by the provided values, + * and any new fields provided are merged with the existing trace data. If the + * ID does not match, a new trace is created. + * + * Sample code: + *

+   * try (TraceServiceApi traceServiceApi = TraceServiceApi.create()) {
+   *   String projectId = "";
+   *   Traces traces = Traces.newBuilder().build();
+   *   traceServiceApi.patchTraces(projectId, traces);
+   * }
+   * 
+ * + * @param projectId ID of the Cloud project where the trace data is stored. + * @param traces The body of the message. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final void patchTraces(String projectId, Traces traces) { + PatchTracesRequest request = + PatchTracesRequest.newBuilder().setProjectId(projectId).setTraces(traces).build(); + patchTraces(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Sends new traces to Cloud Trace or updates existing traces. If the ID of + * a trace that you send matches that of an existing trace, any fields + * in the existing trace and its spans are overwritten by the provided values, + * and any new fields provided are merged with the existing trace data. If the + * ID does not match, a new trace is created. + * + * Sample code: + *

+   * try (TraceServiceApi traceServiceApi = TraceServiceApi.create()) {
+   *   String projectId = "";
+   *   Traces traces = Traces.newBuilder().build();
+   *   PatchTracesRequest request = PatchTracesRequest.newBuilder()
+   *     .setProjectId(projectId)
+   *     .setTraces(traces)
+   *     .build();
+   *   traceServiceApi.patchTraces(request);
+   * }
+   * 
+ * + * @param request The request object containing all of the parameters for the API call. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final void patchTraces(PatchTracesRequest request) { + patchTracesCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Sends new traces to Cloud Trace or updates existing traces. If the ID of + * a trace that you send matches that of an existing trace, any fields + * in the existing trace and its spans are overwritten by the provided values, + * and any new fields provided are merged with the existing trace data. If the + * ID does not match, a new trace is created. + * + * Sample code: + *

+   * try (TraceServiceApi traceServiceApi = TraceServiceApi.create()) {
+   *   String projectId = "";
+   *   Traces traces = Traces.newBuilder().build();
+   *   PatchTracesRequest request = PatchTracesRequest.newBuilder()
+   *     .setProjectId(projectId)
+   *     .setTraces(traces)
+   *     .build();
+   *   ListenableFuture<Void> future = traceServiceApi.patchTracesCallable().futureCall(request);
+   *   // Do something
+   *   future.get();
+   * }
+   * 
+ */ + public final ApiCallable patchTracesCallable() { + return patchTracesCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Gets a single trace by its ID. + * + * Sample code: + *

+   * try (TraceServiceApi traceServiceApi = TraceServiceApi.create()) {
+   *   String projectId = "";
+   *   String traceId = "";
+   *   Trace response = traceServiceApi.getTrace(projectId, traceId);
+   * }
+   * 
+ * + * @param projectId ID of the Cloud project where the trace data is stored. + * @param traceId ID of the trace to return. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final Trace getTrace(String projectId, String traceId) { + GetTraceRequest request = + GetTraceRequest.newBuilder().setProjectId(projectId).setTraceId(traceId).build(); + return getTrace(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Gets a single trace by its ID. + * + * Sample code: + *

+   * try (TraceServiceApi traceServiceApi = TraceServiceApi.create()) {
+   *   String projectId = "";
+   *   String traceId = "";
+   *   GetTraceRequest request = GetTraceRequest.newBuilder()
+   *     .setProjectId(projectId)
+   *     .setTraceId(traceId)
+   *     .build();
+   *   Trace response = traceServiceApi.getTrace(request);
+   * }
+   * 
+ * + * @param request The request object containing all of the parameters for the API call. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + private final Trace getTrace(GetTraceRequest request) { + return getTraceCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Gets a single trace by its ID. + * + * Sample code: + *

+   * try (TraceServiceApi traceServiceApi = TraceServiceApi.create()) {
+   *   String projectId = "";
+   *   String traceId = "";
+   *   GetTraceRequest request = GetTraceRequest.newBuilder()
+   *     .setProjectId(projectId)
+   *     .setTraceId(traceId)
+   *     .build();
+   *   ListenableFuture<Trace> future = traceServiceApi.getTraceCallable().futureCall(request);
+   *   // Do something
+   *   Trace response = future.get();
+   * }
+   * 
+ */ + public final ApiCallable getTraceCallable() { + return getTraceCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Returns of a list of traces that match the specified filter conditions. + * + * Sample code: + *

+   * try (TraceServiceApi traceServiceApi = TraceServiceApi.create()) {
+   *   String projectId = "";
+   *   for (Trace element : traceServiceApi.listTraces(projectId)) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * 
+ * + * @param projectId ID of the Cloud project where the trace data is stored. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final PageAccessor listTraces(String projectId) { + ListTracesRequest request = ListTracesRequest.newBuilder().setProjectId(projectId).build(); + return listTraces(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Returns of a list of traces that match the specified filter conditions. + * + * Sample code: + *

+   * try (TraceServiceApi traceServiceApi = TraceServiceApi.create()) {
+   *   String projectId = "";
+   *   ListTracesRequest request = ListTracesRequest.newBuilder()
+   *     .setProjectId(projectId)
+   *     .build();
+   *   for (Trace element : traceServiceApi.listTraces(request)) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * 
+ * + * @param request The request object containing all of the parameters for the API call. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final PageAccessor listTraces(ListTracesRequest request) { + return listTracesPagedCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Returns of a list of traces that match the specified filter conditions. + * + * Sample code: + *

+   * try (TraceServiceApi traceServiceApi = TraceServiceApi.create()) {
+   *   String projectId = "";
+   *   ListTracesRequest request = ListTracesRequest.newBuilder()
+   *     .setProjectId(projectId)
+   *     .build();
+   *   ListenableFuture<PageAccessor<Trace>> future = traceServiceApi.listTracesPagedCallable().futureCall(request);
+   *   // Do something
+   *   for (Trace element : future.get()) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * 
+ */ + public final ApiCallable> listTracesPagedCallable() { + return listTracesPagedCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Returns of a list of traces that match the specified filter conditions. + * + * Sample code: + *

+   * try (TraceServiceApi traceServiceApi = TraceServiceApi.create()) {
+   *   String projectId = "";
+   *   ListTracesRequest request = ListTracesRequest.newBuilder()
+   *     .setProjectId(projectId)
+   *     .build();
+   *   while (true) {
+   *     ListTracesResponse response = traceServiceApi.listTracesCallable().call(request);
+   *     for (Trace element : response.getTracesList()) {
+   *       // doThingsWith(element);
+   *     }
+   *     String nextPageToken = response.getNextPageToken();
+   *     if (!Strings.isNullOrEmpty(nextPageToken)) {
+   *       request = request.toBuilder().setPageToken(nextPageToken).build();
+   *     } else {
+   *       break;
+   *     }
+   *   }
+   * }
+   * 
+ */ + public final ApiCallable listTracesCallable() { + return listTracesCallable; + } + + /** + * Initiates an orderly shutdown in which preexisting calls continue but new calls are immediately + * cancelled. + */ + @Override + public final void close() throws Exception { + for (AutoCloseable closeable : closeables) { + closeable.close(); + } + } +} diff --git a/gcloud-java-trace/src/main/java/com/google/cloud/trace/spi/v1/TraceServiceSettings.java b/gcloud-java-trace/src/main/java/com/google/cloud/trace/spi/v1/TraceServiceSettings.java new file mode 100644 index 000000000000..145adeffed56 --- /dev/null +++ b/gcloud-java-trace/src/main/java/com/google/cloud/trace/spi/v1/TraceServiceSettings.java @@ -0,0 +1,384 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package com.google.cloud.trace.spi.v1; + +import com.google.api.gax.core.ConnectionSettings; +import com.google.api.gax.core.RetrySettings; +import com.google.api.gax.grpc.ApiCallSettings; +import com.google.api.gax.grpc.PageStreamingCallSettings; +import com.google.api.gax.grpc.PageStreamingDescriptor; +import com.google.api.gax.grpc.ServiceApiSettings; +import com.google.api.gax.grpc.SimpleCallSettings; +import com.google.auth.Credentials; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; +import com.google.devtools.cloudtrace.v1.GetTraceRequest; +import com.google.devtools.cloudtrace.v1.ListTracesRequest; +import com.google.devtools.cloudtrace.v1.ListTracesResponse; +import com.google.devtools.cloudtrace.v1.PatchTracesRequest; +import com.google.devtools.cloudtrace.v1.Trace; +import com.google.devtools.cloudtrace.v1.TraceServiceGrpc; +import com.google.protobuf.Empty; +import io.grpc.ManagedChannel; +import io.grpc.Status; +import java.io.IOException; +import java.util.List; +import java.util.concurrent.ScheduledExecutorService; +import org.joda.time.Duration; + +// AUTO-GENERATED DOCUMENTATION AND CLASS +/** + * Settings class to configure an instance of {@link TraceServiceApi}. + * + *

The default instance has everything set to sensible defaults: + * + *

    + *
  • The default service address (cloudtrace.googleapis.com) and default port (443) + * are used. + *
  • Credentials are acquired automatically through Application Default Credentials. + *
  • Retries are configured for idempotent methods but not for non-idempotent methods. + *
+ * + *

The builder of this class is recursive, so contained classes are themselves builders. + * When build() is called, the tree of builders is called to create the complete settings + * object. For example, to set the total timeout of patchTraces to 30 seconds: + * + *

+ * 
+ * TraceServiceSettings.Builder traceServiceSettingsBuilder =
+ *     TraceServiceSettings.defaultBuilder();
+ * traceServiceSettingsBuilder.patchTracesSettings().getRetrySettingsBuilder()
+ *     .setTotalTimeout(Duration.standardSeconds(30));
+ * TraceServiceSettings traceServiceSettings = traceServiceSettingsBuilder.build();
+ * 
+ * 
+ */ +@javax.annotation.Generated("by GAPIC") +public class TraceServiceSettings extends ServiceApiSettings { + /** + * The default address of the service. + */ + private static final String DEFAULT_SERVICE_ADDRESS = "cloudtrace.googleapis.com"; + + /** + * The default port of the service. + */ + private static final int DEFAULT_SERVICE_PORT = 443; + + /** + * The default scopes of the service. + */ + private static final ImmutableList DEFAULT_SERVICE_SCOPES = + ImmutableList.builder() + .add("https://www.googleapis.com/auth/cloud-platform") + .add("https://www.googleapis.com/auth/trace.append") + .add("https://www.googleapis.com/auth/trace.readonly") + .build(); + + /** + * The default connection settings of the service. + */ + public static final ConnectionSettings DEFAULT_CONNECTION_SETTINGS = + ConnectionSettings.newBuilder() + .setServiceAddress(DEFAULT_SERVICE_ADDRESS) + .setPort(DEFAULT_SERVICE_PORT) + .provideCredentialsWith(DEFAULT_SERVICE_SCOPES) + .build(); + + private final SimpleCallSettings patchTracesSettings; + private final SimpleCallSettings getTraceSettings; + private final PageStreamingCallSettings + listTracesSettings; + + /** + * Returns the object with the settings used for calls to patchTraces. + */ + public SimpleCallSettings patchTracesSettings() { + return patchTracesSettings; + } + + /** + * Returns the object with the settings used for calls to getTrace. + */ + public SimpleCallSettings getTraceSettings() { + return getTraceSettings; + } + + /** + * Returns the object with the settings used for calls to listTraces. + */ + public PageStreamingCallSettings + listTracesSettings() { + return listTracesSettings; + } + + /** + * Returns the default service address. + */ + public static String getDefaultServiceAddress() { + return DEFAULT_SERVICE_ADDRESS; + } + + /** + * Returns the default service port. + */ + public static int getDefaultServicePort() { + return DEFAULT_SERVICE_PORT; + } + + /** + * Returns the default service scopes. + */ + public static ImmutableList getDefaultServiceScopes() { + return DEFAULT_SERVICE_SCOPES; + } + + /** + * Returns a builder for this class with recommended defaults. + */ + public static Builder defaultBuilder() { + return Builder.createDefault(); + } + + /** + * Returns a new builder for this class. + */ + public static Builder newBuilder() { + return new Builder(); + } + + /** + * Returns a builder containing all the values of this settings class. + */ + public Builder toBuilder() { + return new Builder(this); + } + + private TraceServiceSettings(Builder settingsBuilder) throws IOException { + super( + settingsBuilder.getChannelProvider(), + settingsBuilder.getExecutorProvider(), + settingsBuilder.getGeneratorName(), + settingsBuilder.getGeneratorVersion(), + settingsBuilder.getClientLibName(), + settingsBuilder.getClientLibVersion()); + + patchTracesSettings = settingsBuilder.patchTracesSettings().build(); + getTraceSettings = settingsBuilder.getTraceSettings().build(); + listTracesSettings = settingsBuilder.listTracesSettings().build(); + } + + private static PageStreamingDescriptor + LIST_TRACES_PAGE_STR_DESC = + new PageStreamingDescriptor() { + @Override + public Object emptyToken() { + return ""; + } + + @Override + public ListTracesRequest injectToken(ListTracesRequest payload, Object token) { + return ListTracesRequest.newBuilder(payload).setPageToken((String) token).build(); + } + + @Override + public Object extractNextToken(ListTracesResponse payload) { + return payload.getNextPageToken(); + } + + @Override + public Iterable extractResources(ListTracesResponse payload) { + return payload.getTracesList(); + } + }; + + /** + * Builder for TraceServiceSettings. + */ + public static class Builder extends ServiceApiSettings.Builder { + private final ImmutableList methodSettingsBuilders; + + private SimpleCallSettings.Builder patchTracesSettings; + private SimpleCallSettings.Builder getTraceSettings; + private PageStreamingCallSettings.Builder + listTracesSettings; + + private static final ImmutableMap> RETRYABLE_CODE_DEFINITIONS; + + static { + ImmutableMap.Builder> definitions = ImmutableMap.builder(); + definitions.put( + "idempotent", + Sets.immutableEnumSet( + Lists.newArrayList( + Status.Code.DEADLINE_EXCEEDED, Status.Code.UNAVAILABLE))); + definitions.put("non_idempotent", Sets.immutableEnumSet(Lists.newArrayList())); + RETRYABLE_CODE_DEFINITIONS = definitions.build(); + } + + private static final ImmutableMap RETRY_PARAM_DEFINITIONS; + + static { + ImmutableMap.Builder definitions = ImmutableMap.builder(); + RetrySettings.Builder settingsBuilder = null; + settingsBuilder = + RetrySettings.newBuilder() + .setInitialRetryDelay(Duration.millis(100L)) + .setRetryDelayMultiplier(1.2) + .setMaxRetryDelay(Duration.millis(1000L)) + .setInitialRpcTimeout(Duration.millis(20000L)) + .setRpcTimeoutMultiplier(1.5) + .setMaxRpcTimeout(Duration.millis(30000L)) + .setTotalTimeout(Duration.millis(45000L)); + definitions.put("default", settingsBuilder); + RETRY_PARAM_DEFINITIONS = definitions.build(); + } + + private Builder() { + super(DEFAULT_CONNECTION_SETTINGS); + + patchTracesSettings = SimpleCallSettings.newBuilder(TraceServiceGrpc.METHOD_PATCH_TRACES); + + getTraceSettings = SimpleCallSettings.newBuilder(TraceServiceGrpc.METHOD_GET_TRACE); + + listTracesSettings = + PageStreamingCallSettings.newBuilder( + TraceServiceGrpc.METHOD_LIST_TRACES, LIST_TRACES_PAGE_STR_DESC); + + methodSettingsBuilders = + ImmutableList.of( + patchTracesSettings, getTraceSettings, listTracesSettings); + } + + private static Builder createDefault() { + Builder builder = new Builder(); + + builder + .patchTracesSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .getTraceSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .listTracesSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + return builder; + } + + private Builder(TraceServiceSettings settings) { + super(settings); + + patchTracesSettings = settings.patchTracesSettings.toBuilder(); + getTraceSettings = settings.getTraceSettings.toBuilder(); + listTracesSettings = settings.listTracesSettings.toBuilder(); + + methodSettingsBuilders = + ImmutableList.of( + patchTracesSettings, getTraceSettings, listTracesSettings); + } + + @Override + protected ConnectionSettings getDefaultConnectionSettings() { + return DEFAULT_CONNECTION_SETTINGS; + } + + @Override + public Builder provideExecutorWith(ScheduledExecutorService executor, boolean shouldAutoClose) { + super.provideExecutorWith(executor, shouldAutoClose); + return this; + } + + @Override + public Builder provideChannelWith(ManagedChannel channel, boolean shouldAutoClose) { + super.provideChannelWith(channel, shouldAutoClose); + return this; + } + + @Override + public Builder provideChannelWith(ConnectionSettings settings) { + super.provideChannelWith(settings); + return this; + } + + @Override + public Builder provideChannelWith(Credentials credentials) { + super.provideChannelWith(credentials); + return this; + } + + @Override + public Builder provideChannelWith(List scopes) { + super.provideChannelWith(scopes); + return this; + } + + @Override + public Builder setGeneratorHeader(String name, String version) { + super.setGeneratorHeader(name, version); + return this; + } + + @Override + public Builder setClientLibHeader(String name, String version) { + super.setClientLibHeader(name, version); + return this; + } + + /** + * Applies the given settings to all of the API methods in this service. Only + * values that are non-null will be applied, so this method is not capable + * of un-setting any values. + */ + public Builder applyToAllApiMethods(ApiCallSettings.Builder apiCallSettings) throws Exception { + super.applyToAllApiMethods(methodSettingsBuilders, apiCallSettings); + return this; + } + + /** + * Returns the builder for the settings used for calls to patchTraces. + */ + public SimpleCallSettings.Builder patchTracesSettings() { + return patchTracesSettings; + } + + /** + * Returns the builder for the settings used for calls to getTrace. + */ + public SimpleCallSettings.Builder getTraceSettings() { + return getTraceSettings; + } + + /** + * Returns the builder for the settings used for calls to listTraces. + */ + public PageStreamingCallSettings.Builder + listTracesSettings() { + return listTracesSettings; + } + + @Override + public TraceServiceSettings build() throws IOException { + return new TraceServiceSettings(this); + } + } +} diff --git a/gcloud-java-trace/src/main/java/com/google/cloud/trace/spi/v1/package-info.java b/gcloud-java-trace/src/main/java/com/google/cloud/trace/spi/v1/package-info.java new file mode 100644 index 000000000000..32b3625ef678 --- /dev/null +++ b/gcloud-java-trace/src/main/java/com/google/cloud/trace/spi/v1/package-info.java @@ -0,0 +1,42 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +/** + * A client to Google Cloud Trace API. + * + * The interfaces provided are listed below, along with a usage sample + * + * =============== + * TraceServiceApi + * =============== + * + * Service Description: This file describes an API for collecting and viewing traces and spans + * within a trace. A Trace is a collection of spans corresponding to a single + * operation or set of operations for an application. A span is an individual + * timed event which forms a node of the trace tree. Spans for a single trace + * may span multiple services. + * + * Sample for TraceServiceApi: + *
+ * 
+ * try (TraceServiceApi traceServiceApi = TraceServiceApi.create()) {
+ *   String projectId = "";
+ *   Traces traces = Traces.newBuilder().build();
+ *   traceServiceApi.patchTraces(projectId, traces);
+ * }
+ * 
+ * 
+ * + */ +package com.google.cloud.trace.spi.v1; diff --git a/gcloud-java-trace/src/test/java/com/google/cloud/trace/spi/v1/MockTraceService.java b/gcloud-java-trace/src/test/java/com/google/cloud/trace/spi/v1/MockTraceService.java new file mode 100644 index 000000000000..dab3938d506f --- /dev/null +++ b/gcloud-java-trace/src/test/java/com/google/cloud/trace/spi/v1/MockTraceService.java @@ -0,0 +1,50 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.cloud.trace.spi.v1; + +import com.google.api.gax.testing.MockGrpcService; +import com.google.devtools.cloudtrace.v1.TraceServiceGrpc; +import com.google.protobuf.GeneratedMessage; +import io.grpc.ServerServiceDefinition; +import java.util.List; + +@javax.annotation.Generated("by GAPIC") +public class MockTraceService implements MockGrpcService { + private MockTraceServiceImpl serviceImpl; + + public MockTraceService() { + serviceImpl = new MockTraceServiceImpl(); + } + + @Override + public List getRequests() { + return serviceImpl.getRequests(); + } + + @Override + public void setResponses(List responses) { + serviceImpl.setResponses(responses); + } + + @Override + public ServerServiceDefinition getServiceDefinition() { + return TraceServiceGrpc.bindService(serviceImpl); + } + + @Override + public void reset() { + serviceImpl.reset(); + } +} diff --git a/gcloud-java-trace/src/test/java/com/google/cloud/trace/spi/v1/MockTraceServiceImpl.java b/gcloud-java-trace/src/test/java/com/google/cloud/trace/spi/v1/MockTraceServiceImpl.java new file mode 100644 index 000000000000..e1d2ea4929f0 --- /dev/null +++ b/gcloud-java-trace/src/test/java/com/google/cloud/trace/spi/v1/MockTraceServiceImpl.java @@ -0,0 +1,79 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.cloud.trace.spi.v1; + +import com.google.common.collect.Lists; +import com.google.devtools.cloudtrace.v1.GetTraceRequest; +import com.google.devtools.cloudtrace.v1.ListTracesRequest; +import com.google.devtools.cloudtrace.v1.ListTracesResponse; +import com.google.devtools.cloudtrace.v1.PatchTracesRequest; +import com.google.devtools.cloudtrace.v1.Trace; +import com.google.devtools.cloudtrace.v1.TraceServiceGrpc.TraceService; +import com.google.protobuf.Empty; +import com.google.protobuf.GeneratedMessage; +import io.grpc.stub.StreamObserver; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +@javax.annotation.Generated("by GAPIC") +public class MockTraceServiceImpl implements TraceService { + private ArrayList requests; + private Queue responses; + + public MockTraceServiceImpl() { + requests = new ArrayList<>(); + responses = new LinkedList<>(); + } + + public List getRequests() { + return requests; + } + + public void setResponses(List responses) { + this.responses = Lists.newLinkedList(responses); + } + + public void reset() { + requests = new ArrayList<>(); + responses = new LinkedList<>(); + } + + @Override + public void listTraces( + ListTracesRequest request, StreamObserver responseObserver) { + ListTracesResponse response = (ListTracesResponse) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void getTrace(GetTraceRequest request, StreamObserver responseObserver) { + Trace response = (Trace) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void patchTraces(PatchTracesRequest request, StreamObserver responseObserver) { + Empty response = (Empty) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } +} diff --git a/gcloud-java-trace/src/test/java/com/google/cloud/trace/spi/v1/TraceServiceTest.java b/gcloud-java-trace/src/test/java/com/google/cloud/trace/spi/v1/TraceServiceTest.java new file mode 100644 index 000000000000..9af39602f843 --- /dev/null +++ b/gcloud-java-trace/src/test/java/com/google/cloud/trace/spi/v1/TraceServiceTest.java @@ -0,0 +1,151 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.cloud.trace.spi.v1; + +import com.google.api.gax.core.PageAccessor; +import com.google.api.gax.testing.MockGrpcService; +import com.google.api.gax.testing.MockServiceHelper; +import com.google.common.collect.Lists; +import com.google.devtools.cloudtrace.v1.GetTraceRequest; +import com.google.devtools.cloudtrace.v1.ListTracesRequest; +import com.google.devtools.cloudtrace.v1.ListTracesResponse; +import com.google.devtools.cloudtrace.v1.PatchTracesRequest; +import com.google.devtools.cloudtrace.v1.Trace; +import com.google.devtools.cloudtrace.v1.Traces; +import com.google.protobuf.Empty; +import com.google.protobuf.GeneratedMessage; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +@javax.annotation.Generated("by GAPIC") +public class TraceServiceTest { + private static MockTraceService mockTraceService; + private static MockServiceHelper serviceHelper; + private TraceServiceApi api; + + @BeforeClass + public static void startStaticServer() { + mockTraceService = new MockTraceService(); + serviceHelper = + new MockServiceHelper("in-process-1", Arrays.asList(mockTraceService)); + serviceHelper.start(); + } + + @AfterClass + public static void stopServer() { + serviceHelper.stop(); + } + + @Before + public void setUp() throws IOException { + serviceHelper.reset(); + TraceServiceSettings settings = + TraceServiceSettings.defaultBuilder() + .provideChannelWith(serviceHelper.createChannel(), true) + .build(); + api = TraceServiceApi.create(settings); + } + + @After + public void tearDown() throws Exception { + api.close(); + } + + @Test + @SuppressWarnings("all") + public void patchTracesTest() { + Empty expectedResponse = Empty.newBuilder().build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockTraceService.setResponses(expectedResponses); + + String projectId = "projectId-1969970175"; + Traces traces = Traces.newBuilder().build(); + + api.patchTraces(projectId, traces); + + List actualRequests = mockTraceService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + PatchTracesRequest actualRequest = (PatchTracesRequest) actualRequests.get(0); + + Assert.assertEquals(projectId, actualRequest.getProjectId()); + Assert.assertEquals(traces, actualRequest.getTraces()); + } + + @Test + @SuppressWarnings("all") + public void getTraceTest() { + String projectId2 = "projectId2939242356"; + String traceId2 = "traceId2987826376"; + Trace expectedResponse = + Trace.newBuilder().setProjectId(projectId2).setTraceId(traceId2).build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockTraceService.setResponses(expectedResponses); + + String projectId = "projectId-1969970175"; + String traceId = "traceId1270300245"; + + Trace actualResponse = api.getTrace(projectId, traceId); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockTraceService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + GetTraceRequest actualRequest = (GetTraceRequest) actualRequests.get(0); + + Assert.assertEquals(projectId, actualRequest.getProjectId()); + Assert.assertEquals(traceId, actualRequest.getTraceId()); + } + + @Test + @SuppressWarnings("all") + public void listTracesTest() { + Trace tracesElement = Trace.newBuilder().build(); + List traces = Arrays.asList(tracesElement); + String nextPageToken = "nextPageToken-1530815211"; + ListTracesResponse expectedResponse = + ListTracesResponse.newBuilder() + .addAllTraces(traces) + .setNextPageToken(nextPageToken) + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockTraceService.setResponses(expectedResponses); + + String projectId = "projectId-1969970175"; + + PageAccessor pageAccessor = api.listTraces(projectId); + + // PageAccessor will not make actual request until it is being used. + // Add all the pages here in order to make grpc requests. + List resources = Lists.newArrayList(pageAccessor.getPageValues()); + Assert.assertEquals(1, resources.size()); + Assert.assertEquals(expectedResponse.getTracesList().get(0), resources.get(0)); + + List actualRequests = mockTraceService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + ListTracesRequest actualRequest = (ListTracesRequest) actualRequests.get(0); + + Assert.assertEquals(projectId, actualRequest.getProjectId()); + } +} diff --git a/pom.xml b/pom.xml index 8002ac3338d1..172ae71777cf 100644 --- a/pom.xml +++ b/pom.xml @@ -95,6 +95,7 @@ gcloud-java gcloud-java-bigquery + gcloud-java-trace gcloud-java-compute gcloud-java-contrib gcloud-java-core