• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 Google LLC
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google LLC nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 package com.google.api.gax.tracing;
31 
32 import com.google.api.core.InternalApi;
33 import org.threeten.bp.Duration;
34 
35 /**
36  * Implementations of this class trace the logical flow of a google cloud client.
37  *
38  * <p>A single instance of a tracer represents a logical operation that can be annotated throughout
39  * its lifecycle. Constructing an instance of a subclass will implicitly signal the start of a new
40  * operation.
41  *
42  * <p>For internal use only. google-cloud-java libraries should extend {@link BaseApiTracer}.
43  */
44 @InternalApi
45 public interface ApiTracer {
46 
47   /**
48    * Asks the underlying implementation to install itself as a thread local. This allows for interop
49    * between clients using gax and external resources to share the same implementation of the
50    * tracing. For example OpenCensus will install a thread local that can read by the GRPC.
51    */
inScope()52   Scope inScope();
53 
54   /**
55    * Signals that the overall operation has finished successfully. The tracer is now considered
56    * closed and should no longer be used.
57    */
operationSucceeded()58   void operationSucceeded();
59 
60   /**
61    * Signals that the operation was cancelled by the user. The tracer is now considered closed and
62    * should no longer be used.
63    */
operationCancelled()64   void operationCancelled();
65 
66   /**
67    * Signals that the overall operation has failed and no further attempts will be made. The tracer
68    * is now considered closed and should no longer be used.
69    *
70    * @param error the final error that caused the operation to fail.
71    */
operationFailed(Throwable error)72   void operationFailed(Throwable error);
73 
74   /**
75    * Annotates the operation with selected connection id from the {@code ChannelPool}.
76    *
77    * @param id the local connection identifier of the selected connection.
78    */
connectionSelected(String id)79   void connectionSelected(String id);
80 
81   /**
82    * Adds an annotation that an attempt is about to start. In general this should occur at the very
83    * start of the operation. The attemptNumber is zero based. So the initial attempt will be 0.
84    *
85    * @param attemptNumber the zero based sequential attempt number.
86    * @deprecated Please use {@link #attemptStarted(Object, int)} instead.
87    */
88   @Deprecated
attemptStarted(int attemptNumber)89   void attemptStarted(int attemptNumber);
90 
91   /**
92    * Adds an annotation that an attempt is about to start with additional information from the
93    * request. In general this should occur at the very start of the operation. The attemptNumber is
94    * zero based. So the initial attempt will be 0.
95    *
96    * @param attemptNumber the zero based sequential attempt number.
97    * @param request request of this attempt.
98    */
attemptStarted(Object request, int attemptNumber)99   void attemptStarted(Object request, int attemptNumber);
100 
101   /** Adds an annotation that the attempt succeeded. */
attemptSucceeded()102   void attemptSucceeded();
103 
104   /** Add an annotation that the attempt was cancelled by the user. */
attemptCancelled()105   void attemptCancelled();
106 
107   /**
108    * Adds an annotation that the attempt failed, but another attempt will be made after the delay.
109    *
110    * @param error the transient error that caused the attempt to fail.
111    * @param delay the amount of time to wait before the next attempt will start.
112    */
attemptFailed(Throwable error, Duration delay)113   void attemptFailed(Throwable error, Duration delay);
114 
115   /**
116    * Adds an annotation that the attempt failed and that no further attempts will be made because
117    * retry limits have been reached.
118    *
119    * @param error the last error received before retries were exhausted.
120    */
attemptFailedRetriesExhausted(Throwable error)121   void attemptFailedRetriesExhausted(Throwable error);
122 
123   /**
124    * Adds an annotation that the attempt failed and that no further attempts will be made because
125    * the last error was not retryable.
126    *
127    * @param error the error that caused the final attempt to fail.
128    */
attemptPermanentFailure(Throwable error)129   void attemptPermanentFailure(Throwable error);
130 
131   /**
132    * Signals that the initial RPC for the long running operation failed.
133    *
134    * @param error the error that caused the long running operation fail.
135    */
lroStartFailed(Throwable error)136   void lroStartFailed(Throwable error);
137 
138   /**
139    * Signals that the initial RPC successfully started the long running operation. The long running
140    * operation will now be polled for completion.
141    */
lroStartSucceeded()142   void lroStartSucceeded();
143 
144   /** Adds an annotation that a streaming response has been received. */
responseReceived()145   void responseReceived();
146 
147   /** Adds an annotation that a streaming request has been sent. */
requestSent()148   void requestSent();
149 
150   /**
151    * Adds an annotation that a batch of writes has been flushed.
152    *
153    * @param elementCount the number of elements in the batch.
154    * @param requestSize the size of the batch in bytes.
155    */
batchRequestSent(long elementCount, long requestSize)156   void batchRequestSent(long elementCount, long requestSize);
157 
158   /**
159    * A context class to be used with {@link #inScope()} and a try-with-resources block. Closing a
160    * {@link Scope} removes any context that the underlying implementation might've set in {@link
161    * #inScope()}.
162    */
163   interface Scope extends AutoCloseable {
164     @Override
close()165     void close();
166   }
167 }
168