Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ OperationCallable<RequestT, ResponseT, MetadataT> createOperationCallable(
// Create a sub-trace for the initial RPC that starts the operation.
UnaryCallable<RequestT, OperationSnapshot> tracedInitialCallable =
new TracedOperationInitialCallable<>(
initialCallable, clientContext.getTracerFactory(), initialSpanName);
initialCallable, clientContext.getTracerFactory(), tracerContext);

LongRunningClient longRunningClient = new GrpcLongRunningClient(operationsStub);
OperationCallable<RequestT, ResponseT, MetadataT> operationCallable =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public class TracedOperationInitialCallable<RequestT>
public TracedOperationInitialCallable(
UnaryCallable<RequestT, OperationSnapshot> innerCallable,
ApiTracerFactory tracedFactory,
SpanName spanName) {
super(innerCallable, tracedFactory, spanName);
ApiTracerContext apiTracerContext) {
super(innerCallable, tracedFactory, apiTracerContext, null);
Comment thread
blakeli0 marked this conversation as resolved.
Outdated
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,11 @@
public class TracedUnaryCallable<RequestT, ResponseT> extends UnaryCallable<RequestT, ResponseT> {
private final UnaryCallable<RequestT, ResponseT> innerCallable;
private final ApiTracerFactory tracerFactory;
private final SpanName spanName;

Check warning on line 53 in sdk-platform-java/gax-java/gax/src/main/java/com/google/api/gax/tracing/TracedUnaryCallable.java

View check run for this annotation

SonarQubeCloud / [gapic-generator-java-root] SonarCloud Code Analysis

Remove this unused "spanName" private field.

See more on https://sonarcloud.io/project/issues?id=googleapis_google-cloud-java_showcase&issues=AZ3XLMqIz0DJLEl1205E&open=AZ3XLMqIz0DJLEl1205E&pullRequest=12948
@Nullable private final ApiTracerContext apiTracerContext;
private final ApiTracerContext apiTracerContext;
Comment thread
blakeli0 marked this conversation as resolved.
Outdated
@Nullable private final ResourceNameExtractor<RequestT> resourceNameExtractor;

public TracedUnaryCallable(
UnaryCallable<RequestT, ResponseT> innerCallable,
ApiTracerFactory tracerFactory,
SpanName spanName) {
this.innerCallable = innerCallable;
this.tracerFactory = tracerFactory;
this.spanName = spanName;
this.apiTracerContext = null;
this.resourceNameExtractor = null;
}


public TracedUnaryCallable(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider adding a 3-argument constructor for TracedUnaryCallable that omits the ResourceNameExtractor. This would simplify usage in cases where an extractor is not needed, such as in TracedOperationInitialCallable and various test cases.

  public TracedUnaryCallable(
      UnaryCallable<RequestT, ResponseT> innerCallable,
      ApiTracerFactory tracerFactory,
      ApiTracerContext apiTracerContext) {
    this(innerCallable, tracerFactory, apiTracerContext, null);
  }

UnaryCallable<RequestT, ResponseT> innerCallable,
Expand All @@ -86,15 +77,10 @@
*/
@Override
public ApiFuture<ResponseT> futureCall(RequestT request, ApiCallContext context) {
ApiTracer tracer;
if (apiTracerContext != null) {
tracer =
tracerFactory.newTracer(
context.getTracer(),
apiTracerContext.withResourceNameExtractor(request, resourceNameExtractor));
} else {
tracer = tracerFactory.newTracer(context.getTracer(), spanName, OperationType.Unary);
}
ApiTracer tracer =
tracerFactory.newTracer(
context.getTracer(),
apiTracerContext.withResourceNameExtractor(request, resourceNameExtractor));
TraceFinisher<ResponseT> finisher = new TraceFinisher<>(tracer);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.eq;

Check warning on line 35 in sdk-platform-java/gax-java/gax/src/test/java/com/google/api/gax/tracing/TracedCallableTest.java

View check run for this annotation

SonarQubeCloud / [gapic-generator-java-root] SonarCloud Code Analysis

Remove this unused import 'org.mockito.Mockito.eq'.

See more on https://sonarcloud.io/project/issues?id=googleapis_google-cloud-java_showcase&issues=AZ3XLMpFz0DJLEl1205C&open=AZ3XLMpFz0DJLEl1205C&pullRequest=12948
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
Expand All @@ -46,6 +46,8 @@
import com.google.api.gax.rpc.UnaryCallSettings;
import com.google.api.gax.rpc.UnaryCallable;
import com.google.api.gax.rpc.testing.FakeCallContext;
import com.google.api.gax.rpc.LibraryMetadata;
import com.google.api.gax.tracing.ApiTracerContext.Transport;
import com.google.api.gax.tracing.ApiTracerFactory.OperationType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -55,7 +57,14 @@

@ExtendWith(MockitoExtension.class)
class TracedCallableTest {
private static final SpanName SPAN_NAME = SpanName.of("FakeClient", "FakeRpc");

Check warning on line 60 in sdk-platform-java/gax-java/gax/src/test/java/com/google/api/gax/tracing/TracedCallableTest.java

View check run for this annotation

SonarQubeCloud / [gapic-generator-java-root] SonarCloud Code Analysis

Remove this unused "SPAN_NAME" private field.

See more on https://sonarcloud.io/project/issues?id=googleapis_google-cloud-java_showcase&issues=AZ3XLMpFz0DJLEl1205D&open=AZ3XLMpFz0DJLEl1205D&pullRequest=12948
private static final ApiTracerContext TRACER_CONTEXT =
ApiTracerContext.newBuilder()
.setFullMethodName("FakeClient/FakeRpc")
.setTransport(Transport.GRPC)
.setLibraryMetadata(LibraryMetadata.empty())
.setOperationType(OperationType.Unary)
.build();

@Mock private ApiTracerFactory tracerFactory;
private ApiTracer parentTracer;
Expand All @@ -71,8 +80,7 @@
parentTracer = BaseApiTracer.getInstance();

// Wire the mock tracer factory
when(tracerFactory.newTracer(
any(ApiTracer.class), any(SpanName.class), eq(OperationType.Unary)))
when(tracerFactory.newTracer(any(ApiTracer.class), any(ApiTracerContext.class)))
.thenReturn(tracer);

// Wire the mock inner callable
Expand All @@ -87,7 +95,7 @@
UnaryCallSettings<Object, Object> callSettings) {
UnaryCallable<String, String> callable =
Callables.retrying(innerCallable, callSettings, clientContext);
return new TracedUnaryCallable<>(callable, tracerFactory, SPAN_NAME);
return new TracedUnaryCallable<>(callable, tracerFactory, TRACER_CONTEXT, null);
Comment thread
blakeli0 marked this conversation as resolved.
Outdated
}

@Test
Expand All @@ -102,7 +110,7 @@

ApiFuture<String> future = callable.futureCall("Is your refrigerator running?", callContext);

verify(tracerFactory, times(1)).newTracer(parentTracer, SPAN_NAME, OperationType.Unary);
verify(tracerFactory, times(1)).newTracer(parentTracer, TRACER_CONTEXT);
verify(tracer, times(1)).attemptStarted(anyString(), anyInt());
verify(tracer, times(1)).attemptSucceeded();
verify(tracer, times(1)).operationSucceeded();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@
import com.google.api.gax.tracing.ApiTracerFactory.OperationType;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;

Check warning on line 50 in sdk-platform-java/gax-java/gax/src/test/java/com/google/api/gax/tracing/TracedUnaryCallableTest.java

View check run for this annotation

SonarQubeCloud / [gapic-generator-java-root] SonarCloud Code Analysis

Remove this unused import 'org.junit.jupiter.params.ParameterizedTest'.

See more on https://sonarcloud.io/project/issues?id=googleapis_google-cloud-java_showcase&issues=AZ3XLMk6z0DJLEl1204_&open=AZ3XLMk6z0DJLEl1204_&pullRequest=12948
import org.junit.jupiter.params.provider.ValueSource;

Check warning on line 51 in sdk-platform-java/gax-java/gax/src/test/java/com/google/api/gax/tracing/TracedUnaryCallableTest.java

View check run for this annotation

SonarQubeCloud / [gapic-generator-java-root] SonarCloud Code Analysis

Remove this unused import 'org.junit.jupiter.params.provider.ValueSource'.

See more on https://sonarcloud.io/project/issues?id=googleapis_google-cloud-java_showcase&issues=AZ3XLMk6z0DJLEl1205A&open=AZ3XLMk6z0DJLEl1205A&pullRequest=12948
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class TracedUnaryCallableTest {
private static final SpanName SPAN_NAME = SpanName.of("FakeClient", "FakeRpc");

Check warning on line 58 in sdk-platform-java/gax-java/gax/src/test/java/com/google/api/gax/tracing/TracedUnaryCallableTest.java

View check run for this annotation

SonarQubeCloud / [gapic-generator-java-root] SonarCloud Code Analysis

Remove this unused "SPAN_NAME" private field.

See more on https://sonarcloud.io/project/issues?id=googleapis_google-cloud-java_showcase&issues=AZ3XLMk6z0DJLEl1205B&open=AZ3XLMk6z0DJLEl1205B&pullRequest=12948
private static final ApiTracerContext TRACER_CONTEXT =
ApiTracerContext.newBuilder()
.setFullMethodName("FakeClient/FakeRpc")
Expand All @@ -73,21 +73,14 @@
private TracedUnaryCallable<String, String> tracedUnaryCallable;
private FakeCallContext callContext;

void init(boolean useContext) {
void init() {
parentTracer = BaseApiTracer.getInstance();

// Wire the mock tracer factory
if (useContext) {
when(tracerFactory.newTracer(any(ApiTracer.class), any(ApiTracerContext.class)))
.thenReturn(tracer);
tracedUnaryCallable =
new TracedUnaryCallable<>(innerCallable, tracerFactory, TRACER_CONTEXT, null);
} else {
when(tracerFactory.newTracer(
any(ApiTracer.class), any(SpanName.class), eq(OperationType.Unary)))
.thenReturn(tracer);
tracedUnaryCallable = new TracedUnaryCallable<>(innerCallable, tracerFactory, SPAN_NAME);
}
when(tracerFactory.newTracer(any(ApiTracer.class), any(ApiTracerContext.class)))
.thenReturn(tracer);
tracedUnaryCallable =
new TracedUnaryCallable<>(innerCallable, tracerFactory, TRACER_CONTEXT, null);
Comment thread
blakeli0 marked this conversation as resolved.
Outdated

// Wire the mock inner callable
innerResult = SettableApiFuture.create();
Expand All @@ -96,16 +89,11 @@
callContext = FakeCallContext.createDefault();
}

@ParameterizedTest
@ValueSource(booleans = {false, true})
void testTracerCreated(boolean useContext) {
init(useContext);
@Test
void testTracerCreated() {
init();
tracedUnaryCallable.futureCall("test", callContext);
if (useContext) {
verify(tracerFactory, times(1)).newTracer(parentTracer, TRACER_CONTEXT);
} else {
verify(tracerFactory, times(1)).newTracer(parentTracer, SPAN_NAME, OperationType.Unary);
}
verify(tracerFactory, times(1)).newTracer(parentTracer, TRACER_CONTEXT);
}

@Test
Expand All @@ -131,40 +119,36 @@
assertThat(contextCaptor.getValue().operationType()).isEqualTo(OperationType.Unary);
}

@ParameterizedTest
@ValueSource(booleans = {false, true})
void testOperationFinish(boolean useContext) {
init(useContext);
@Test
void testOperationFinish() {
init();
innerResult.set("successful result");
tracedUnaryCallable.futureCall("test", callContext);

verify(tracer, times(1)).operationSucceeded();
}

@ParameterizedTest
@ValueSource(booleans = {false, true})
void testOperationCancelled(boolean useContext) {
init(useContext);
@Test
void testOperationCancelled() {
init();
innerResult.cancel(true);
tracedUnaryCallable.futureCall("test", callContext);
verify(tracer, times(1)).operationCancelled();
}

@ParameterizedTest
@ValueSource(booleans = {false, true})
void testOperationFailed(boolean useContext) {
init(useContext);
@Test
void testOperationFailed() {
init();
RuntimeException fakeError = new RuntimeException("fake error");
innerResult.setException(fakeError);
tracedUnaryCallable.futureCall("test", callContext);

verify(tracer, times(1)).operationFailed(fakeError);
}

@ParameterizedTest
@ValueSource(booleans = {false, true})
void testSyncError(boolean useContext) {
init(useContext);
@Test
void testSyncError() {
init();
RuntimeException fakeError = new RuntimeException("fake error");

// Reset the irrelevant expectations from setup. (only needed to silence the warnings).
Expand Down
Loading