Skip to content
This repository was archived by the owner on Sep 26, 2023. It is now read-only.

Commit 27bf265

Browse files
feat: pass request in ApiTracer (#1491)
* feat: pass request or response in ApiTracer * add comments * make change backward compatible * revert attemptSucceeded changes Co-authored-by: Igor Bernstein <igorbernstein@google.com>
1 parent d4222e7 commit 27bf265

9 files changed

Lines changed: 53 additions & 27 deletions

File tree

gax/src/main/java/com/google/api/gax/rpc/AttemptCallable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public ResponseT call() {
8282

8383
callContext
8484
.getTracer()
85-
.attemptStarted(externalFuture.getAttemptSettings().getOverallAttemptCount());
85+
.attemptStarted(request, externalFuture.getAttemptSettings().getOverallAttemptCount());
8686

8787
ApiFuture<ResponseT> internalFuture = callable.futureCall(request, callContext);
8888
externalFuture.setAttemptFuture(internalFuture);

gax/src/main/java/com/google/api/gax/rpc/ServerStreamingAttemptCallable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public Void call() {
229229

230230
attemptContext
231231
.getTracer()
232-
.attemptStarted(outerRetryingFuture.getAttemptSettings().getOverallAttemptCount());
232+
.attemptStarted(request, outerRetryingFuture.getAttemptSettings().getOverallAttemptCount());
233233

234234
innerCallable.call(
235235
request,

gax/src/main/java/com/google/api/gax/tracing/ApiTracer.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,21 @@ public interface ApiTracer {
8383
* start of the operation. The attemptNumber is zero based. So the initial attempt will be 0.
8484
*
8585
* @param attemptNumber the zero based sequential attempt number.
86+
* @deprecated Please use {@link #attemptStarted(Object, int)} instead.
8687
*/
88+
@Deprecated
8789
void attemptStarted(int attemptNumber);
8890

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+
*/
99+
void attemptStarted(Object request, int attemptNumber);
100+
89101
/** Adds an annotation that the attempt succeeded. */
90102
void attemptSucceeded();
91103

gax/src/main/java/com/google/api/gax/tracing/BaseApiTracer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ public void attemptStarted(int attemptNumber) {
8585
// noop
8686
}
8787

88+
@Override
89+
public void attemptStarted(Object request, int attemptNumber) {
90+
attemptStarted(attemptNumber);
91+
}
92+
8893
@Override
8994
public void attemptSucceeded() {
9095
// noop

gax/src/main/java/com/google/api/gax/tracing/OpencensusTracer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,12 @@ public void attemptStarted(int attemptNumber) {
307307
// This simply is used for state management.
308308
}
309309

310+
/** {@inheritDoc} */
311+
@Override
312+
public void attemptStarted(Object request, int attemptNumber) {
313+
attemptStarted(attemptNumber);
314+
}
315+
310316
/** {@inheritDoc} */
311317
@Override
312318
public void attemptSucceeded() {

gax/src/test/java/com/google/api/gax/retrying/AbstractRetryingExecutorTest.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import static org.junit.Assert.assertNull;
3838
import static org.junit.Assert.assertSame;
3939
import static org.junit.Assert.assertTrue;
40+
import static org.mockito.ArgumentMatchers.eq;
4041
import static org.mockito.Mockito.any;
4142
import static org.mockito.Mockito.anyInt;
4243
import static org.mockito.Mockito.times;
@@ -115,7 +116,7 @@ private RetrySettings getDefaultRetrySettings() {
115116

116117
@Test
117118
public void testSuccess() throws Exception {
118-
FailingCallable callable = new FailingCallable(0, "SUCCESS", tracer);
119+
FailingCallable callable = new FailingCallable(0, "request", "SUCCESS", tracer);
119120
RetryingExecutorWithContext<String> executor =
120121
getExecutor(getAlgorithm(getDefaultRetrySettings(), 0, null));
121122
RetryingFuture<String> future = executor.createFuture(callable, retryingContext);
@@ -124,14 +125,14 @@ public void testSuccess() throws Exception {
124125
assertFutureSuccess(future);
125126
assertEquals(0, future.getAttemptSettings().getAttemptCount());
126127

127-
verify(tracer, times(1)).attemptStarted(0);
128+
verify(tracer, times(1)).attemptStarted(eq("request"), eq(0));
128129
verify(tracer, times(1)).attemptSucceeded();
129130
verifyNoMoreInteractions(tracer);
130131
}
131132

132133
@Test
133134
public void testSuccessWithFailures() throws Exception {
134-
FailingCallable callable = new FailingCallable(5, "SUCCESS", tracer);
135+
FailingCallable callable = new FailingCallable(5, "request", "SUCCESS", tracer);
135136
RetryingExecutorWithContext<String> executor =
136137
getExecutor(getAlgorithm(getDefaultRetrySettings(), 0, null));
137138
RetryingFuture<String> future = executor.createFuture(callable, retryingContext);
@@ -140,15 +141,15 @@ public void testSuccessWithFailures() throws Exception {
140141
assertFutureSuccess(future);
141142
assertEquals(5, future.getAttemptSettings().getAttemptCount());
142143

143-
verify(tracer, times(6)).attemptStarted(anyInt());
144+
verify(tracer, times(6)).attemptStarted(eq("request"), anyInt());
144145
verify(tracer, times(5)).attemptFailed(any(Throwable.class), any(Duration.class));
145146
verify(tracer, times(1)).attemptSucceeded();
146147
verifyNoMoreInteractions(tracer);
147148
}
148149

149150
@Test
150151
public void testSuccessWithFailuresPeekGetAttempt() throws Exception {
151-
FailingCallable callable = new FailingCallable(5, "SUCCESS", tracer);
152+
FailingCallable callable = new FailingCallable(5, "request", "SUCCESS", tracer);
152153
RetryingExecutorWithContext<String> executor =
153154
getExecutor(getAlgorithm(getDefaultRetrySettings(), 0, null));
154155
RetryingFuture<String> future = executor.createFuture(callable, retryingContext);
@@ -174,7 +175,7 @@ public void testSuccessWithFailuresPeekGetAttempt() throws Exception {
174175

175176
@Test
176177
public void testMaxRetriesExceeded() throws Exception {
177-
FailingCallable callable = new FailingCallable(6, "FAILURE", tracer);
178+
FailingCallable callable = new FailingCallable(6, "request", "FAILURE", tracer);
178179
RetryingExecutorWithContext<String> executor =
179180
getExecutor(getAlgorithm(getDefaultRetrySettings(), 0, null));
180181
RetryingFuture<String> future = executor.createFuture(callable, retryingContext);
@@ -183,7 +184,7 @@ public void testMaxRetriesExceeded() throws Exception {
183184
assertFutureFail(future, CustomException.class);
184185
assertEquals(5, future.getAttemptSettings().getAttemptCount());
185186

186-
verify(tracer, times(6)).attemptStarted(anyInt());
187+
verify(tracer, times(6)).attemptStarted(eq("request"), anyInt());
187188
verify(tracer, times(5)).attemptFailed(any(Throwable.class), any(Duration.class));
188189
verify(tracer, times(1)).attemptFailedRetriesExhausted(any(Throwable.class));
189190
verifyNoMoreInteractions(tracer);
@@ -202,7 +203,7 @@ public void testTotalTimeoutExceeded() throws Exception {
202203
getExecutor(
203204
getAlgorithm(
204205
useContextRetrySettings ? getDefaultRetrySettings() : retrySettings, 0, null));
205-
FailingCallable callable = new FailingCallable(6, "FAILURE", tracer);
206+
FailingCallable callable = new FailingCallable(6, "request", "FAILURE", tracer);
206207
RetryingContext context;
207208
if (useContextRetrySettings) {
208209
context = FakeCallContext.createDefault().withTracer(tracer).withRetrySettings(retrySettings);
@@ -215,14 +216,14 @@ public void testTotalTimeoutExceeded() throws Exception {
215216
assertFutureFail(future, CustomException.class);
216217
assertTrue(future.getAttemptSettings().getAttemptCount() < 4);
217218

218-
verify(tracer, times(1)).attemptStarted(anyInt());
219+
verify(tracer, times(1)).attemptStarted(eq("request"), anyInt());
219220
verify(tracer, times(1)).attemptFailedRetriesExhausted(any(Throwable.class));
220221
verifyNoMoreInteractions(tracer);
221222
}
222223

223224
@Test
224225
public void testCancelOuterFutureBeforeStart() throws Exception {
225-
FailingCallable callable = new FailingCallable(4, "SUCCESS", tracer);
226+
FailingCallable callable = new FailingCallable(4, "request", "SUCCESS", tracer);
226227

227228
RetrySettings retrySettings =
228229
FAST_RETRY_SETTINGS
@@ -248,7 +249,7 @@ public void testCancelOuterFutureBeforeStart() throws Exception {
248249

249250
@Test
250251
public void testCancelByRetryingAlgorithm() throws Exception {
251-
FailingCallable callable = new FailingCallable(6, "FAILURE", tracer);
252+
FailingCallable callable = new FailingCallable(6, "request", "FAILURE", tracer);
252253
RetryingExecutorWithContext<String> executor =
253254
getExecutor(getAlgorithm(getDefaultRetrySettings(), 5, new CancellationException()));
254255
RetryingFuture<String> future = executor.createFuture(callable, retryingContext);
@@ -257,7 +258,7 @@ public void testCancelByRetryingAlgorithm() throws Exception {
257258
assertFutureCancel(future);
258259
assertEquals(4, future.getAttemptSettings().getAttemptCount());
259260

260-
verify(tracer, times(5)).attemptStarted(anyInt());
261+
verify(tracer, times(5)).attemptStarted(eq("request"), anyInt());
261262
// Pre-apocalypse failures
262263
verify(tracer, times(4)).attemptFailed(any(Throwable.class), any(Duration.class));
263264
// Apocalypse failure
@@ -267,7 +268,7 @@ public void testCancelByRetryingAlgorithm() throws Exception {
267268

268269
@Test
269270
public void testUnexpectedExceptionFromRetryAlgorithm() throws Exception {
270-
FailingCallable callable = new FailingCallable(6, "FAILURE", tracer);
271+
FailingCallable callable = new FailingCallable(6, "request", "FAILURE", tracer);
271272
RetryingExecutorWithContext<String> executor =
272273
getExecutor(getAlgorithm(getDefaultRetrySettings(), 5, new RuntimeException()));
273274
RetryingFuture<String> future = executor.createFuture(callable, retryingContext);
@@ -276,7 +277,7 @@ public void testUnexpectedExceptionFromRetryAlgorithm() throws Exception {
276277
assertFutureFail(future, RuntimeException.class);
277278
assertEquals(4, future.getAttemptSettings().getAttemptCount());
278279

279-
verify(tracer, times(5)).attemptStarted(anyInt());
280+
verify(tracer, times(5)).attemptStarted(eq("request"), anyInt());
280281
// Pre-apocalypse failures
281282
verify(tracer, times(4)).attemptFailed(any(Throwable.class), any(Duration.class));
282283
// Apocalypse failure
@@ -302,7 +303,7 @@ public void testPollExceptionByPollAlgorithm() throws Exception {
302303
NanoClock.getDefaultClock()));
303304

304305
RetryingExecutorWithContext<String> executor = getExecutor(retryAlgorithm);
305-
FailingCallable callable = new FailingCallable(6, "FAILURE", tracer);
306+
FailingCallable callable = new FailingCallable(6, "request", "FAILURE", tracer);
306307
RetryingContext context;
307308
if (useContextRetrySettings) {
308309
context = FakeCallContext.createDefault().withTracer(tracer).withRetrySettings(retrySettings);
@@ -315,7 +316,7 @@ public void testPollExceptionByPollAlgorithm() throws Exception {
315316
assertFutureFail(future, PollException.class);
316317
assertTrue(future.getAttemptSettings().getAttemptCount() < 4);
317318

318-
verify(tracer, times(1)).attemptStarted(anyInt());
319+
verify(tracer, times(1)).attemptStarted(eq("request"), anyInt());
319320
verify(tracer, times(1)).attemptPermanentFailure(any(PollException.class));
320321
verifyNoMoreInteractions(tracer);
321322
}

gax/src/test/java/com/google/api/gax/retrying/FailingCallable.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,12 @@ class FailingCallable implements Callable<String> {
6262
private AtomicInteger attemptsCount = new AtomicInteger(0);
6363
private final ApiTracer tracer;
6464
private final int expectedFailuresCount;
65+
private final String request;
6566
private final String result;
6667
private final CountDownLatch firstAttemptFinished = new CountDownLatch(1);
6768

68-
FailingCallable(int expectedFailuresCount, String result, ApiTracer tracer) {
69+
FailingCallable(int expectedFailuresCount, String request, String result, ApiTracer tracer) {
70+
this.request = request;
6971
this.tracer = tracer;
7072
this.expectedFailuresCount = expectedFailuresCount;
7173
this.result = result;
@@ -80,7 +82,7 @@ public String call() throws Exception {
8082
try {
8183
int attemptNumber = attemptsCount.getAndIncrement();
8284

83-
tracer.attemptStarted(attemptNumber);
85+
tracer.attemptStarted(request, attemptNumber);
8486

8587
if (attemptNumber < expectedFailuresCount) {
8688
throw new CustomException();

gax/src/test/java/com/google/api/gax/retrying/ScheduledRetryingExecutorTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void testSuccessWithFailuresPeekAttempt() throws Exception {
8888
final int maxRetries = 100;
8989

9090
ScheduledExecutorService localExecutor = Executors.newSingleThreadScheduledExecutor();
91-
FailingCallable callable = new FailingCallable(15, "SUCCESS", tracer);
91+
FailingCallable callable = new FailingCallable(15, "request", "SUCCESS", tracer);
9292

9393
RetrySettings retrySettings =
9494
FAST_RETRY_SETTINGS
@@ -139,7 +139,7 @@ public void testSuccessWithFailuresGetAttempt() throws Exception {
139139
final int maxRetries = 100;
140140

141141
ScheduledExecutorService localExecutor = Executors.newSingleThreadScheduledExecutor();
142-
FailingCallable callable = new FailingCallable(15, "SUCCESS", tracer);
142+
FailingCallable callable = new FailingCallable(15, "request", "SUCCESS", tracer);
143143
RetrySettings retrySettings =
144144
FAST_RETRY_SETTINGS
145145
.toBuilder()
@@ -192,7 +192,7 @@ public void testCancelGetAttempt() throws Exception {
192192
ScheduledExecutorService localExecutor = Executors.newSingleThreadScheduledExecutor();
193193
final int maxRetries = 100;
194194

195-
FailingCallable callable = new FailingCallable(maxRetries - 1, "SUCCESS", tracer);
195+
FailingCallable callable = new FailingCallable(maxRetries - 1, "request", "SUCCESS", tracer);
196196
RetrySettings retrySettings =
197197
FAST_RETRY_SETTINGS
198198
.toBuilder()
@@ -249,7 +249,7 @@ public void testCancelGetAttempt() throws Exception {
249249
public void testCancelOuterFutureAfterStart() throws Exception {
250250
for (int executionsCount = 0; executionsCount < EXECUTIONS_COUNT; executionsCount++) {
251251
ScheduledExecutorService localExecutor = Executors.newSingleThreadScheduledExecutor();
252-
FailingCallable callable = new FailingCallable(4, "SUCCESS", tracer);
252+
FailingCallable callable = new FailingCallable(4, "requset", "SUCCESS", tracer);
253253
RetrySettings retrySettings =
254254
FAST_RETRY_SETTINGS
255255
.toBuilder()
@@ -276,7 +276,7 @@ public void testCancelOuterFutureAfterStart() throws Exception {
276276
@Test
277277
public void testCancelIsTraced() throws Exception {
278278
ScheduledExecutorService localExecutor = Executors.newSingleThreadScheduledExecutor();
279-
FailingCallable callable = new FailingCallable(4, "SUCCESS", tracer);
279+
FailingCallable callable = new FailingCallable(4, "request", "SUCCESS", tracer);
280280
RetrySettings retrySettings =
281281
FAST_RETRY_SETTINGS
282282
.toBuilder()
@@ -305,7 +305,7 @@ public void testCancelProxiedFutureAfterStart() throws Exception {
305305
// this is a heavy test, which takes a lot of time, so only few executions.
306306
for (int executionsCount = 0; executionsCount < 2; executionsCount++) {
307307
ScheduledExecutorService localExecutor = Executors.newSingleThreadScheduledExecutor();
308-
FailingCallable callable = new FailingCallable(5, "SUCCESS", tracer);
308+
FailingCallable callable = new FailingCallable(5, "request", "SUCCESS", tracer);
309309
RetrySettings retrySettings =
310310
FAST_RETRY_SETTINGS
311311
.toBuilder()

gax/src/test/java/com/google/api/gax/tracing/TracedCallableTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public void testNonRetriedCallable() throws Exception {
111111
ApiFuture<String> future = callable.futureCall("Is your refrigerator running?", callContext);
112112

113113
verify(tracerFactory, times(1)).newTracer(parentTracer, SPAN_NAME, OperationType.Unary);
114-
verify(tracer, times(1)).attemptStarted(anyInt());
114+
verify(tracer, times(1)).attemptStarted(anyString(), anyInt());
115115
verify(tracer, times(1)).attemptSucceeded();
116116
verify(tracer, times(1)).operationSucceeded();
117117
verifyNoMoreInteractions(tracer);

0 commit comments

Comments
 (0)