• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 Google LLC
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  *      https://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.trace.v2;
18 
19 import com.google.api.gax.core.BackgroundResource;
20 import com.google.api.gax.rpc.UnaryCallable;
21 import com.google.cloud.trace.v2.stub.TraceServiceStub;
22 import com.google.cloud.trace.v2.stub.TraceServiceStubSettings;
23 import com.google.devtools.cloudtrace.v2.BatchWriteSpansRequest;
24 import com.google.devtools.cloudtrace.v2.ProjectName;
25 import com.google.devtools.cloudtrace.v2.Span;
26 import com.google.protobuf.Empty;
27 import java.io.IOException;
28 import java.util.List;
29 import java.util.concurrent.TimeUnit;
30 import javax.annotation.Generated;
31 
32 // AUTO-GENERATED DOCUMENTATION AND CLASS.
33 /**
34  * Service Description: Service for collecting and viewing traces and spans within a trace.
35  *
36  * <p>A trace is a collection of spans corresponding to a single operation or a set of operations in
37  * an application.
38  *
39  * <p>A span is an individual timed event which forms a node of the trace tree. A single trace can
40  * contain spans from multiple services.
41  *
42  * <p>This class provides the ability to make remote calls to the backing service through method
43  * calls that map to API methods. Sample code to get started:
44  *
45  * <pre>{@code
46  * // This snippet has been automatically generated and should be regarded as a code template only.
47  * // It will require modifications to work:
48  * // - It may require correct/in-range values for request initialization.
49  * // - It may require specifying regional endpoints when creating the service client as shown in
50  * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
51  * try (TraceServiceClient traceServiceClient = TraceServiceClient.create()) {
52  *   ProjectName name = ProjectName.of("[PROJECT]");
53  *   List<Span> spans = new ArrayList<>();
54  *   traceServiceClient.batchWriteSpans(name, spans);
55  * }
56  * }</pre>
57  *
58  * <p>Note: close() needs to be called on the TraceServiceClient object to clean up resources such
59  * as threads. In the example above, try-with-resources is used, which automatically calls close().
60  *
61  * <p>The surface of this class includes several types of Java methods for each of the API's
62  * methods:
63  *
64  * <ol>
65  *   <li>A "flattened" method. With this type of method, the fields of the request type have been
66  *       converted into function parameters. It may be the case that not all fields are available as
67  *       parameters, and not every API method will have a flattened method entry point.
68  *   <li>A "request object" method. This type of method only takes one parameter, a request object,
69  *       which must be constructed before the call. Not every API method will have a request object
70  *       method.
71  *   <li>A "callable" method. This type of method takes no parameters and returns an immutable API
72  *       callable object, which can be used to initiate calls to the service.
73  * </ol>
74  *
75  * <p>See the individual methods for example code.
76  *
77  * <p>Many parameters require resource names to be formatted in a particular way. To assist with
78  * these names, this class includes a format method for each type of name, and additionally a parse
79  * method to extract the individual identifiers contained within names that are returned.
80  *
81  * <p>This class can be customized by passing in a custom instance of TraceServiceSettings to
82  * create(). For example:
83  *
84  * <p>To customize credentials:
85  *
86  * <pre>{@code
87  * // This snippet has been automatically generated and should be regarded as a code template only.
88  * // It will require modifications to work:
89  * // - It may require correct/in-range values for request initialization.
90  * // - It may require specifying regional endpoints when creating the service client as shown in
91  * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
92  * TraceServiceSettings traceServiceSettings =
93  *     TraceServiceSettings.newBuilder()
94  *         .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
95  *         .build();
96  * TraceServiceClient traceServiceClient = TraceServiceClient.create(traceServiceSettings);
97  * }</pre>
98  *
99  * <p>To customize the endpoint:
100  *
101  * <pre>{@code
102  * // This snippet has been automatically generated and should be regarded as a code template only.
103  * // It will require modifications to work:
104  * // - It may require correct/in-range values for request initialization.
105  * // - It may require specifying regional endpoints when creating the service client as shown in
106  * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
107  * TraceServiceSettings traceServiceSettings =
108  *     TraceServiceSettings.newBuilder().setEndpoint(myEndpoint).build();
109  * TraceServiceClient traceServiceClient = TraceServiceClient.create(traceServiceSettings);
110  * }</pre>
111  *
112  * <p>To use REST (HTTP1.1/JSON) transport (instead of gRPC) for sending and receiving requests over
113  * the wire:
114  *
115  * <pre>{@code
116  * // This snippet has been automatically generated and should be regarded as a code template only.
117  * // It will require modifications to work:
118  * // - It may require correct/in-range values for request initialization.
119  * // - It may require specifying regional endpoints when creating the service client as shown in
120  * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
121  * TraceServiceSettings traceServiceSettings = TraceServiceSettings.newHttpJsonBuilder().build();
122  * TraceServiceClient traceServiceClient = TraceServiceClient.create(traceServiceSettings);
123  * }</pre>
124  *
125  * <p>Please refer to the GitHub repository's samples for more quickstart code snippets.
126  */
127 @Generated("by gapic-generator-java")
128 public class TraceServiceClient implements BackgroundResource {
129   private final TraceServiceSettings settings;
130   private final TraceServiceStub stub;
131 
132   /** Constructs an instance of TraceServiceClient with default settings. */
create()133   public static final TraceServiceClient create() throws IOException {
134     return create(TraceServiceSettings.newBuilder().build());
135   }
136 
137   /**
138    * Constructs an instance of TraceServiceClient, using the given settings. The channels are
139    * created based on the settings passed in, or defaults for any settings that are not set.
140    */
create(TraceServiceSettings settings)141   public static final TraceServiceClient create(TraceServiceSettings settings) throws IOException {
142     return new TraceServiceClient(settings);
143   }
144 
145   /**
146    * Constructs an instance of TraceServiceClient, using the given stub for making calls. This is
147    * for advanced usage - prefer using create(TraceServiceSettings).
148    */
create(TraceServiceStub stub)149   public static final TraceServiceClient create(TraceServiceStub stub) {
150     return new TraceServiceClient(stub);
151   }
152 
153   /**
154    * Constructs an instance of TraceServiceClient, using the given settings. This is protected so
155    * that it is easy to make a subclass, but otherwise, the static factory methods should be
156    * preferred.
157    */
TraceServiceClient(TraceServiceSettings settings)158   protected TraceServiceClient(TraceServiceSettings settings) throws IOException {
159     this.settings = settings;
160     this.stub = ((TraceServiceStubSettings) settings.getStubSettings()).createStub();
161   }
162 
TraceServiceClient(TraceServiceStub stub)163   protected TraceServiceClient(TraceServiceStub stub) {
164     this.settings = null;
165     this.stub = stub;
166   }
167 
getSettings()168   public final TraceServiceSettings getSettings() {
169     return settings;
170   }
171 
getStub()172   public TraceServiceStub getStub() {
173     return stub;
174   }
175 
176   // AUTO-GENERATED DOCUMENTATION AND METHOD.
177   /**
178    * Batch writes new spans to new or existing traces. You cannot update existing spans.
179    *
180    * <p>Sample code:
181    *
182    * <pre>{@code
183    * // This snippet has been automatically generated and should be regarded as a code template only.
184    * // It will require modifications to work:
185    * // - It may require correct/in-range values for request initialization.
186    * // - It may require specifying regional endpoints when creating the service client as shown in
187    * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
188    * try (TraceServiceClient traceServiceClient = TraceServiceClient.create()) {
189    *   ProjectName name = ProjectName.of("[PROJECT]");
190    *   List<Span> spans = new ArrayList<>();
191    *   traceServiceClient.batchWriteSpans(name, spans);
192    * }
193    * }</pre>
194    *
195    * @param name Required. The name of the project where the spans belong. The format is
196    *     `projects/[PROJECT_ID]`.
197    * @param spans Required. A list of new spans. The span names must not match existing spans,
198    *     otherwise the results are undefined.
199    * @throws com.google.api.gax.rpc.ApiException if the remote call fails
200    */
batchWriteSpans(ProjectName name, List<Span> spans)201   public final void batchWriteSpans(ProjectName name, List<Span> spans) {
202     BatchWriteSpansRequest request =
203         BatchWriteSpansRequest.newBuilder()
204             .setName(name == null ? null : name.toString())
205             .addAllSpans(spans)
206             .build();
207     batchWriteSpans(request);
208   }
209 
210   // AUTO-GENERATED DOCUMENTATION AND METHOD.
211   /**
212    * Batch writes new spans to new or existing traces. You cannot update existing spans.
213    *
214    * <p>Sample code:
215    *
216    * <pre>{@code
217    * // This snippet has been automatically generated and should be regarded as a code template only.
218    * // It will require modifications to work:
219    * // - It may require correct/in-range values for request initialization.
220    * // - It may require specifying regional endpoints when creating the service client as shown in
221    * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
222    * try (TraceServiceClient traceServiceClient = TraceServiceClient.create()) {
223    *   String name = ProjectName.of("[PROJECT]").toString();
224    *   List<Span> spans = new ArrayList<>();
225    *   traceServiceClient.batchWriteSpans(name, spans);
226    * }
227    * }</pre>
228    *
229    * @param name Required. The name of the project where the spans belong. The format is
230    *     `projects/[PROJECT_ID]`.
231    * @param spans Required. A list of new spans. The span names must not match existing spans,
232    *     otherwise the results are undefined.
233    * @throws com.google.api.gax.rpc.ApiException if the remote call fails
234    */
batchWriteSpans(String name, List<Span> spans)235   public final void batchWriteSpans(String name, List<Span> spans) {
236     BatchWriteSpansRequest request =
237         BatchWriteSpansRequest.newBuilder().setName(name).addAllSpans(spans).build();
238     batchWriteSpans(request);
239   }
240 
241   // AUTO-GENERATED DOCUMENTATION AND METHOD.
242   /**
243    * Batch writes new spans to new or existing traces. You cannot update existing spans.
244    *
245    * <p>Sample code:
246    *
247    * <pre>{@code
248    * // This snippet has been automatically generated and should be regarded as a code template only.
249    * // It will require modifications to work:
250    * // - It may require correct/in-range values for request initialization.
251    * // - It may require specifying regional endpoints when creating the service client as shown in
252    * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
253    * try (TraceServiceClient traceServiceClient = TraceServiceClient.create()) {
254    *   BatchWriteSpansRequest request =
255    *       BatchWriteSpansRequest.newBuilder()
256    *           .setName(ProjectName.of("[PROJECT]").toString())
257    *           .addAllSpans(new ArrayList<Span>())
258    *           .build();
259    *   traceServiceClient.batchWriteSpans(request);
260    * }
261    * }</pre>
262    *
263    * @param request The request object containing all of the parameters for the API call.
264    * @throws com.google.api.gax.rpc.ApiException if the remote call fails
265    */
batchWriteSpans(BatchWriteSpansRequest request)266   public final void batchWriteSpans(BatchWriteSpansRequest request) {
267     batchWriteSpansCallable().call(request);
268   }
269 
270   // AUTO-GENERATED DOCUMENTATION AND METHOD.
271   /**
272    * Batch writes new spans to new or existing traces. You cannot update existing spans.
273    *
274    * <p>Sample code:
275    *
276    * <pre>{@code
277    * // This snippet has been automatically generated and should be regarded as a code template only.
278    * // It will require modifications to work:
279    * // - It may require correct/in-range values for request initialization.
280    * // - It may require specifying regional endpoints when creating the service client as shown in
281    * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
282    * try (TraceServiceClient traceServiceClient = TraceServiceClient.create()) {
283    *   BatchWriteSpansRequest request =
284    *       BatchWriteSpansRequest.newBuilder()
285    *           .setName(ProjectName.of("[PROJECT]").toString())
286    *           .addAllSpans(new ArrayList<Span>())
287    *           .build();
288    *   ApiFuture<Empty> future = traceServiceClient.batchWriteSpansCallable().futureCall(request);
289    *   // Do something.
290    *   future.get();
291    * }
292    * }</pre>
293    */
batchWriteSpansCallable()294   public final UnaryCallable<BatchWriteSpansRequest, Empty> batchWriteSpansCallable() {
295     return stub.batchWriteSpansCallable();
296   }
297 
298   // AUTO-GENERATED DOCUMENTATION AND METHOD.
299   /**
300    * Creates a new span.
301    *
302    * <p>Sample code:
303    *
304    * <pre>{@code
305    * // This snippet has been automatically generated and should be regarded as a code template only.
306    * // It will require modifications to work:
307    * // - It may require correct/in-range values for request initialization.
308    * // - It may require specifying regional endpoints when creating the service client as shown in
309    * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
310    * try (TraceServiceClient traceServiceClient = TraceServiceClient.create()) {
311    *   Span request =
312    *       Span.newBuilder()
313    *           .setName(SpanName.of("[PROJECT]", "[TRACE]", "[SPAN]").toString())
314    *           .setSpanId("spanId-896182779")
315    *           .setParentSpanId("parentSpanId1059234639")
316    *           .setDisplayName(TruncatableString.newBuilder().build())
317    *           .setStartTime(Timestamp.newBuilder().build())
318    *           .setEndTime(Timestamp.newBuilder().build())
319    *           .setAttributes(Span.Attributes.newBuilder().build())
320    *           .setStackTrace(StackTrace.newBuilder().build())
321    *           .setTimeEvents(Span.TimeEvents.newBuilder().build())
322    *           .setLinks(Span.Links.newBuilder().build())
323    *           .setStatus(Status.newBuilder().build())
324    *           .setSameProcessAsParentSpan(BoolValue.newBuilder().build())
325    *           .setChildSpanCount(Int32Value.newBuilder().build())
326    *           .build();
327    *   Span response = traceServiceClient.createSpan(request);
328    * }
329    * }</pre>
330    *
331    * @param request The request object containing all of the parameters for the API call.
332    * @throws com.google.api.gax.rpc.ApiException if the remote call fails
333    */
createSpan(Span request)334   public final Span createSpan(Span request) {
335     return createSpanCallable().call(request);
336   }
337 
338   // AUTO-GENERATED DOCUMENTATION AND METHOD.
339   /**
340    * Creates a new span.
341    *
342    * <p>Sample code:
343    *
344    * <pre>{@code
345    * // This snippet has been automatically generated and should be regarded as a code template only.
346    * // It will require modifications to work:
347    * // - It may require correct/in-range values for request initialization.
348    * // - It may require specifying regional endpoints when creating the service client as shown in
349    * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
350    * try (TraceServiceClient traceServiceClient = TraceServiceClient.create()) {
351    *   Span request =
352    *       Span.newBuilder()
353    *           .setName(SpanName.of("[PROJECT]", "[TRACE]", "[SPAN]").toString())
354    *           .setSpanId("spanId-896182779")
355    *           .setParentSpanId("parentSpanId1059234639")
356    *           .setDisplayName(TruncatableString.newBuilder().build())
357    *           .setStartTime(Timestamp.newBuilder().build())
358    *           .setEndTime(Timestamp.newBuilder().build())
359    *           .setAttributes(Span.Attributes.newBuilder().build())
360    *           .setStackTrace(StackTrace.newBuilder().build())
361    *           .setTimeEvents(Span.TimeEvents.newBuilder().build())
362    *           .setLinks(Span.Links.newBuilder().build())
363    *           .setStatus(Status.newBuilder().build())
364    *           .setSameProcessAsParentSpan(BoolValue.newBuilder().build())
365    *           .setChildSpanCount(Int32Value.newBuilder().build())
366    *           .build();
367    *   ApiFuture<Span> future = traceServiceClient.createSpanCallable().futureCall(request);
368    *   // Do something.
369    *   Span response = future.get();
370    * }
371    * }</pre>
372    */
createSpanCallable()373   public final UnaryCallable<Span, Span> createSpanCallable() {
374     return stub.createSpanCallable();
375   }
376 
377   @Override
close()378   public final void close() {
379     stub.close();
380   }
381 
382   @Override
shutdown()383   public void shutdown() {
384     stub.shutdown();
385   }
386 
387   @Override
isShutdown()388   public boolean isShutdown() {
389     return stub.isShutdown();
390   }
391 
392   @Override
isTerminated()393   public boolean isTerminated() {
394     return stub.isTerminated();
395   }
396 
397   @Override
shutdownNow()398   public void shutdownNow() {
399     stub.shutdownNow();
400   }
401 
402   @Override
awaitTermination(long duration, TimeUnit unit)403   public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException {
404     return stub.awaitTermination(duration, unit);
405   }
406 }
407