Skip to content

Commit 1b47eb9

Browse files
committed
fix: move OperationType to ApiTracerContext
1 parent 97d1aac commit 1b47eb9

12 files changed

Lines changed: 44 additions & 33 deletions

gax-java/gax/src/main/java/com/google/api/gax/tracing/ApiTracerContext.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import com.google.api.core.InternalApi;
3434
import com.google.api.gax.rpc.LibraryMetadata;
35+
import com.google.api.gax.tracing.ApiTracerFactory.OperationType;
3536
import com.google.auto.value.AutoValue;
3637
import java.util.HashMap;
3738
import java.util.Map;
@@ -116,6 +117,14 @@ String rpcSystemName() {
116117
@Nullable
117118
abstract Transport transport();
118119

120+
/**
121+
* Returns the type of operation the {@link ApiTracer} is tracing.
122+
*
123+
* @return the operation type, or {@code null} if not set
124+
*/
125+
@Nullable
126+
public abstract OperationType operationType();
127+
119128
/**
120129
* @return a map of attributes to be included in attempt-level spans
121130
*/
@@ -161,6 +170,9 @@ ApiTracerContext merge(ApiTracerContext other) {
161170
if (other.transport() != null) {
162171
builder.setTransport(other.transport());
163172
}
173+
if (other.operationType() != null) {
174+
builder.setOperationType(other.operationType());
175+
}
164176
return builder.build();
165177
}
166178

@@ -184,6 +196,8 @@ public abstract static class Builder {
184196

185197
public abstract Builder setTransport(@Nullable Transport transport);
186198

199+
public abstract Builder setOperationType(@Nullable OperationType operationType);
200+
187201
public abstract ApiTracerContext build();
188202
}
189203
}

gax-java/gax/src/main/java/com/google/api/gax/tracing/ApiTracerFactory.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ default ApiTracer newTracer(
7575
return newTracer(parent, spanName, operationType);
7676
}
7777

78+
/**
79+
* Create a new {@link ApiTracer} that will be a child of the current context.
80+
*
81+
* @param parent the parent of this tracer
82+
* @param tracerContext the method-definition-specific tracer context
83+
*/
84+
default ApiTracer newTracer(ApiTracer parent, ApiTracerContext tracerContext) {
85+
return newTracer(parent, tracerContext, tracerContext.operationType());
86+
}
87+
7888
/**
7989
* @return the {@link ApiTracerContext} for this factory
8090
*/

gax-java/gax/src/main/java/com/google/api/gax/tracing/TracedBatchingCallable.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ public ApiFuture<ResponseT> futureCall(RequestT request, ApiCallContext context)
8585
// So this start a top level tracer.
8686
ApiTracer tracer;
8787
if (apiTracerContext != null) {
88-
tracer =
89-
tracerFactory.newTracer(context.getTracer(), apiTracerContext, OperationType.Batching);
88+
tracer = tracerFactory.newTracer(context.getTracer(), apiTracerContext);
9089
} else {
9190
tracer = tracerFactory.newTracer(context.getTracer(), spanName, OperationType.Batching);
9291
}

gax-java/gax/src/main/java/com/google/api/gax/tracing/TracedBidiCallable.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ public ClientStream<RequestT> internalCall(
8787

8888
ApiTracer tracer;
8989
if (apiTracerContext != null) {
90-
tracer =
91-
tracerFactory.newTracer(
92-
context.getTracer(), apiTracerContext, OperationType.BidiStreaming);
90+
tracer = tracerFactory.newTracer(context.getTracer(), apiTracerContext);
9391
} else {
9492
tracer = tracerFactory.newTracer(context.getTracer(), spanName, OperationType.BidiStreaming);
9593
}

gax-java/gax/src/main/java/com/google/api/gax/tracing/TracedClientStreamingCallable.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ public ApiStreamObserver<RequestT> clientStreamingCall(
8585

8686
ApiTracer tracer;
8787
if (apiTracerContext != null) {
88-
tracer =
89-
tracerFactory.newTracer(
90-
context.getTracer(), apiTracerContext, OperationType.ClientStreaming);
88+
tracer = tracerFactory.newTracer(context.getTracer(), apiTracerContext);
9189
} else {
9290
tracer =
9391
tracerFactory.newTracer(context.getTracer(), spanName, OperationType.ClientStreaming);

gax-java/gax/src/main/java/com/google/api/gax/tracing/TracedServerStreamingCallable.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ public void call(
8181

8282
ApiTracer tracer;
8383
if (apiTracerContext != null) {
84-
tracer =
85-
tracerFactory.newTracer(
86-
context.getTracer(), apiTracerContext, OperationType.ServerStreaming);
84+
tracer = tracerFactory.newTracer(context.getTracer(), apiTracerContext);
8785
} else {
8886
tracer =
8987
tracerFactory.newTracer(context.getTracer(), spanName, OperationType.ServerStreaming);

gax-java/gax/src/main/java/com/google/api/gax/tracing/TracedUnaryCallable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public TracedUnaryCallable(
8282
public ApiFuture<ResponseT> futureCall(RequestT request, ApiCallContext context) {
8383
ApiTracer tracer;
8484
if (apiTracerContext != null) {
85-
tracer = tracerFactory.newTracer(context.getTracer(), apiTracerContext, OperationType.Unary);
85+
tracer = tracerFactory.newTracer(context.getTracer(), apiTracerContext);
8686
} else {
8787
tracer = tracerFactory.newTracer(context.getTracer(), spanName, OperationType.Unary);
8888
}

gax-java/gax/src/test/java/com/google/api/gax/tracing/TracedBatchingCallableTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class TracedBatchingCallableTest {
6060
.setFullMethodName("FakeClient/FakeRpc")
6161
.setTransport(Transport.GRPC)
6262
.setLibraryMetadata(LibraryMetadata.empty())
63+
.setOperationType(OperationType.Batching)
6364
.build();
6465

6566
@Mock private ApiTracerFactory tracerFactory;
@@ -74,8 +75,7 @@ class TracedBatchingCallableTest {
7475
void init(boolean useContext) {
7576
// Wire the mock tracer factory
7677
if (useContext) {
77-
when(tracerFactory.newTracer(
78-
any(ApiTracer.class), any(ApiTracerContext.class), eq(OperationType.Batching)))
78+
when(tracerFactory.newTracer(any(ApiTracer.class), any(ApiTracerContext.class)))
7979
.thenReturn(tracer);
8080
tracedBatchingCallable =
8181
new TracedBatchingCallable<>(
@@ -102,8 +102,7 @@ void testRootTracerCreated(boolean useContext) {
102102
init(useContext);
103103
tracedBatchingCallable.futureCall("test", callContext);
104104
if (useContext) {
105-
verify(tracerFactory, times(1))
106-
.newTracer(callContext.getTracer(), TRACER_CONTEXT, OperationType.Batching);
105+
verify(tracerFactory, times(1)).newTracer(callContext.getTracer(), TRACER_CONTEXT);
107106
} else {
108107
verify(tracerFactory, times(1))
109108
.newTracer(callContext.getTracer(), SPAN_NAME, OperationType.Batching);

gax-java/gax/src/test/java/com/google/api/gax/tracing/TracedBidiCallableTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
import static com.google.common.truth.Truth.assertThat;
3333
import static org.mockito.ArgumentMatchers.any;
34-
import static org.mockito.ArgumentMatchers.eq;
3534
import static org.mockito.Mockito.times;
3635
import static org.mockito.Mockito.verify;
3736
import static org.mockito.Mockito.when;
@@ -65,6 +64,7 @@ class TracedBidiCallableTest {
6564
.setFullMethodName("fake-client/fake-method")
6665
.setTransport(Transport.GRPC)
6766
.setLibraryMetadata(LibraryMetadata.empty())
67+
.setOperationType(OperationType.BidiStreaming)
6868
.build();
6969

7070
private FakeBidiObserver outerObserver;
@@ -84,8 +84,7 @@ void init(boolean useContext) {
8484
outerCallContext = FakeCallContext.createDefault();
8585

8686
if (useContext) {
87-
when(tracerFactory.newTracer(
88-
any(ApiTracer.class), any(ApiTracerContext.class), eq(OperationType.BidiStreaming)))
87+
when(tracerFactory.newTracer(any(ApiTracer.class), any(ApiTracerContext.class)))
8988
.thenReturn(tracer);
9089
} else {
9190
when(tracerFactory.newTracer(parentTracer, SPAN_NAME, OperationType.BidiStreaming))
@@ -108,8 +107,7 @@ void testTracerCreated(boolean useContext) {
108107
tracedCallable.call(outerObserver, outerCallContext);
109108

110109
if (useContext) {
111-
verify(tracerFactory, times(1))
112-
.newTracer(parentTracer, TRACER_CONTEXT, OperationType.BidiStreaming);
110+
verify(tracerFactory, times(1)).newTracer(parentTracer, TRACER_CONTEXT);
113111
} else {
114112
verify(tracerFactory, times(1))
115113
.newTracer(parentTracer, SPAN_NAME, OperationType.BidiStreaming);

gax-java/gax/src/test/java/com/google/api/gax/tracing/TracedClientStreamingCallableTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131

3232
import static com.google.common.truth.Truth.assertThat;
3333
import static org.mockito.ArgumentMatchers.any;
34-
import static org.mockito.ArgumentMatchers.eq;
3534
import static org.mockito.Mockito.times;
3635
import static org.mockito.Mockito.verify;
3736
import static org.mockito.Mockito.when;
@@ -65,6 +64,7 @@ class TracedClientStreamingCallableTest {
6564
.setFullMethodName("fake-client/fake-method")
6665
.setTransport(Transport.GRPC)
6766
.setLibraryMetadata(LibraryMetadata.empty())
67+
.setOperationType(OperationType.ClientStreaming)
6868
.build();
6969

7070
@Mock private ApiTracerFactory tracerFactory;
@@ -81,8 +81,7 @@ void init(boolean useContext) {
8181
callContext = FakeCallContext.createDefault();
8282
innerCallable = new FakeClientCallable();
8383
if (useContext) {
84-
when(tracerFactory.newTracer(
85-
any(ApiTracer.class), any(ApiTracerContext.class), eq(OperationType.ClientStreaming)))
84+
when(tracerFactory.newTracer(any(ApiTracer.class), any(ApiTracerContext.class)))
8685
.thenReturn(tracer);
8786
tracedCallable =
8887
new TracedClientStreamingCallable<>(innerCallable, tracerFactory, TRACER_CONTEXT);
@@ -100,8 +99,7 @@ void testTracerCreated(boolean useContext) {
10099
tracedCallable.clientStreamingCall(outerResponseObsever, callContext);
101100

102101
if (useContext) {
103-
verify(tracerFactory, times(1))
104-
.newTracer(parentTracer, TRACER_CONTEXT, OperationType.ClientStreaming);
102+
verify(tracerFactory, times(1)).newTracer(parentTracer, TRACER_CONTEXT);
105103
} else {
106104
verify(tracerFactory, times(1))
107105
.newTracer(parentTracer, SPAN_NAME, OperationType.ClientStreaming);

0 commit comments

Comments
 (0)