GeezerVison is showing github.com — feat(gax): Implement trace context extraction and injection with integration test by westarle · Pull Request #12625 · googleapis/google-cloud-java
feat(gax): Implement trace context extraction and injection with integration test
#12625
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement . We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from all commits
Commits
File filter
Filter by extension
.java
(10)
.xml
(1)
All 2 file types selected
Only manifest files
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -90,9 +90,19 @@ public static <RequestT, ResponseT> ClientCall<RequestT, ResponseT> newCall(
channel = ((ChannelPool) channel).getChannel(grpcContext.getChannelAffinity());
}
if (!grpcContext.getExtraHeaders().isEmpty()) {
ClientInterceptor interceptor =
MetadataUtils.newAttachHeadersInterceptor(grpcContext.getMetadata());
java.util.Map<String, String> traceContext = new java.util.HashMap<>();
grpcContext.getTracer().injectTraceContext(traceContext);
if (!grpcContext.getExtraHeaders().isEmpty() || !traceContext.isEmpty()) {
Metadata metadata = new Metadata();
metadata.merge(grpcContext.getMetadata());
for (java.util.Map.Entry<String, String> entry : traceContext.entrySet()) {
Metadata.Key<String> key =
Metadata.Key.of(entry.getKey(), Metadata.ASCII_STRING_MARSHALLER);
metadata.removeAll(key);
metadata.put(key, entry.getValue());
}
ClientInterceptor interceptor = MetadataUtils.newAttachHeadersInterceptor(metadata);
channel = ClientInterceptors.intercept(channel, interceptor);
}
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -340,4 +340,48 @@ void testUniverseDomainNotReady_shouldRetry() throws IOException {
Truth.assertThat(exception.isRetryable()).isTrue();
Mockito.verify(mockChannel, Mockito.never()).newCall(descriptor, callOptions);
}
@Test
void testTraceContextHeaders() throws IOException {
Metadata emptyHeaders = new Metadata();
MethodDescriptor<Color, Money> descriptor = FakeServiceGrpc.METHOD_RECOGNIZE;
@SuppressWarnings("unchecked")
ClientCall<Color, Money> mockClientCall = Mockito.mock(ClientCall.class);
@SuppressWarnings("unchecked")
ClientCall.Listener<Money> mockListener = Mockito.mock(ClientCall.Listener.class);
Channel mockChannel = Mockito.mock(ManagedChannel.class);
com.google.api.gax.tracing.ApiTracer mockTracer =
Mockito.mock(com.google.api.gax.tracing.ApiTracer.class);
Mockito.doAnswer(
invocation -> {
java.util.Map<String, String> carrier = invocation.getArgument(0);
carrier.put("traceparent", "00-00000000000000000000000000000001-0000000000000002-01");
return null;
})
.when(mockTracer)
.injectTraceContext(Mockito.anyMap());
Mockito.doAnswer(
invocation -> {
Metadata clientCallHeaders = (Metadata) invocation.getArguments()[1];
Metadata.Key<String> traceparentKey =
Metadata.Key.of("traceparent", Metadata.ASCII_STRING_MARSHALLER);
assertThat(clientCallHeaders.getAll(traceparentKey))
.containsExactly("00-00000000000000000000000000000001-0000000000000002-01");
return null;
})
.when(mockClientCall)
.start(Mockito.<ClientCall.Listener<Money>>any(), Mockito.<Metadata>any());
Mockito.when(mockChannel.newCall(Mockito.eq(descriptor), Mockito.<CallOptions>any()))
.thenReturn(mockClientCall);
GrpcCallContext context = defaultCallContext.withChannel(mockChannel).withTracer(mockTracer);
GrpcClientCalls.newCall(descriptor, context).start(mockListener, emptyHeaders);
}
}
Original file line number
Diff line number
Diff line change
Expand Up
@@ -80,15 +80,25 @@ public static <RequestT, ResponseT> HttpJsonClientCall<RequestT, ResponseT> newC
return httpJsonContext.getChannel().newCall(methodDescriptor, httpJsonContext.getCallOptions());
}
static HttpJsonMetadata getMetadataWithTraceContext(HttpJsonCallContext context) {
java.util.Map<String, String> traceHeaders = new java.util.HashMap<>();
context.getTracer().injectTraceContext(traceHeaders);
java.util.Map<String, java.util.List<String>> finalHeaders =
new java.util.HashMap<>(context.getExtraHeaders());
for (java.util.Map.Entry<String, String> entry : traceHeaders.entrySet()) {
finalHeaders.put(entry.getKey(), java.util.Collections.singletonList(entry.getValue()));
}
return HttpJsonMetadata.newBuilder().build().withHeaders(finalHeaders);
}
static <RequestT, ResponseT> ApiFuture<ResponseT> futureUnaryCall(
HttpJsonClientCall<RequestT, ResponseT> clientCall,
RequestT request,
HttpJsonCallContext context) {
// Start the call
HttpJsonFuture<ResponseT> future = new HttpJsonFuture<>(clientCall);
clientCall.start(
new FutureListener<>(future),
HttpJsonMetadata.newBuilder().build().withHeaders(context.getExtraHeaders()));
clientCall.start(new FutureListener<>(future), getMetadataWithTraceContext(context));
// Send the request
try {
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -139,4 +139,35 @@ void testUniverseDomainNotReady_shouldRetry() throws IOException {
.isEqualTo(HttpJsonStatusCode.Code.UNAUTHENTICATED);
Mockito.verify(mockChannel, Mockito.never()).newCall(descriptor, callOptions);
}
@Test
void testGetMetadataWithTraceContext() {
com.google.api.gax.tracing.ApiTracer mockTracer =
Mockito.mock(com.google.api.gax.tracing.ApiTracer.class);
Mockito.doAnswer(
invocation -> {
java.util.Map<String, String> carrier = invocation.getArgument(0);
carrier.put("traceparent", "00-00000000000000000000000000000001-0000000000000002-01");
return null;
})
.when(mockTracer)
.injectTraceContext(Mockito.anyMap());
java.util.Map<String, java.util.List<String>> extraHeaders = new java.util.HashMap<>();
extraHeaders.put("existing-header", java.util.Collections.singletonList("existing-value"));
HttpJsonCallContext context =
(HttpJsonCallContext)
HttpJsonCallContext.createDefault()
.withTracer(mockTracer)
.withExtraHeaders(extraHeaders);
HttpJsonMetadata metadata = HttpJsonClientCalls.getMetadataWithTraceContext(context);
assertThat(metadata.getHeaders()).containsKey("existing-header");
assertThat(metadata.getHeaders().get("existing-header").toString()).contains("existing-value");
assertThat(metadata.getHeaders()).containsKey("traceparent");
assertThat(metadata.getHeaders().get("traceparent").toString())
.contains("00-00000000000000000000000000000001-0000000000000002-01");
}
}
Original file line number
Diff line number
Diff line change
Expand Up
@@ -196,6 +196,9 @@ default void requestSent() {}
default void batchRequestSent(long elementCount, long requestSize) {}
;
/** Extract the trace context from the tracer and add it to the given headers map. */
default void injectTraceContext(java.util.Map<String, String> carrier) {}
/**
* Annotates the attempt with the full resolved HTTP URL. Only relevant for HTTP transport.
*
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -213,4 +213,11 @@ public void batchRequestSent(long elementCount, long requestSize) {
child.batchRequestSent(elementCount, requestSize);
}
}
@Override
public void injectTraceContext(java.util.Map<String, String> carrier) {
for (ApiTracer child : children) {
child.injectTraceContext(carrier);
}
}
}
Original file line number
Diff line number
Diff line change
Expand Up
@@ -54,6 +54,23 @@ public class SpanTracer implements ApiTracer {
private final ApiTracerContext apiTracerContext;
private Span attemptSpan;
@Override
public void injectTraceContext(java.util.Map<String, String> carrier) {
if (attemptSpan != null) {
io.opentelemetry.context.Context context =
io.opentelemetry.context.Context.current().with(attemptSpan);
io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator.getInstance()
.inject(
context,
carrier,
(c, k, v) -> {
if (c != null) {
c.put(k, v);
}
});
}
}
/**
* Creates a new instance of {@code SpanTracer}.
*
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -270,4 +270,13 @@ void testBatchRequestSent() {
verify(child1).batchRequestSent(10L, 100L);
verify(child2).batchRequestSent(10L, 100L);
}
@Test
void testInjectTraceContext() {
java.util.Map<String, String> carrier = new java.util.HashMap<>();
compositeTracer.injectTraceContext(carrier);
InOrder inOrder = inOrder(child1, child2);
inOrder.verify(child1).injectTraceContext(carrier);
inOrder.verify(child2).injectTraceContext(carrier);
}
}
Original file line number
Diff line number
Diff line change
Expand Up
@@ -602,4 +602,27 @@ public RedirectException(String message) {
}
private static class UnknownClientException extends RuntimeException {}
@Test
void testInjectTraceContext_addsHeaders() {
io.opentelemetry.api.trace.SpanContext mockSpanContext =
io.opentelemetry.api.trace.SpanContext.create(
"00000000000000000000000000000001",
"0000000000000002",
io.opentelemetry.api.trace.TraceFlags.getSampled(),
io.opentelemetry.api.trace.TraceState.getDefault());
io.opentelemetry.api.trace.Span realSpan =
io.opentelemetry.api.trace.Span.wrap(mockSpanContext);
when(spanBuilder.startSpan()).thenReturn(realSpan);
spanTracer = new SpanTracer(tracer, ApiTracerContext.empty(), ATTEMPT_SPAN_NAME);
spanTracer.attemptStarted(new Object(), 1);
java.util.Map<String, String> carrier = new java.util.HashMap<>();
spanTracer.injectTraceContext(carrier);
assertThat(carrier).containsKey("traceparent");
assertThat(carrier.get("traceparent")).contains("00000000000000000000000000000001");
assertThat(carrier.get("traceparent")).contains("0000000000000002");
}
}
Original file line number
Diff line number
Diff line change
Expand Up
@@ -17,7 +17,7 @@
</parent>
<properties>
<gapic-showcase.version>0.36.2 </gapic-showcase.version>
<gapic-showcase.version>0.39.0 </gapic-showcase.version>
<!-- This is the last version supporting slf4j 1.x, do not upgrade -->
<slf4j1-logback.version>1.2.13</slf4j1-logback.version>
<slf4j2-logback.version>1.5.25</slf4j2-logback.version>
Expand Down
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.