Skip to content
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

feat: Add opt-in flag and ClientInterceptor to propagate trace context for Spanner end to end tracing #3162

Merged
merged 33 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
df54343
feat(spanner): Add x-goog-spanner-end-to-end-tracing header for reque…
nareshz Jun 3, 2024
b4864da
Merge branch 'main' of https://github.com/googleapis/java-spanner int…
nareshz Jun 13, 2024
290f01e
Add Grpc Telemetry client interceptor for trace context propagation
nareshz Jun 17, 2024
3c92b26
Remove print statement
nareshz Jun 17, 2024
3671173
Merge branch 'main' of https://github.com/googleapis/java-spanner int…
nareshz Jun 18, 2024
9379106
Merge branch 'main' of https://github.com/googleapis/java-spanner int…
nareshz Jun 25, 2024
5033b1b
copy grpc telemetry client interceptor code
nareshz Jul 10, 2024
d7c778a
Merge branch 'main' of https://github.com/googleapis/java-spanner int…
nareshz Jul 10, 2024
4613e29
Merge branch 'main' of https://github.com/googleapis/java-spanner int…
nareshz Jul 11, 2024
7c39b82
rename spanner option for end to end tracing
nareshz Jul 11, 2024
d2ac8b4
add test for custom client interceptor code
nareshz Jul 15, 2024
3eb5e47
Merge branch 'main' of https://github.com/googleapis/java-spanner int…
nareshz Jul 15, 2024
233acd2
resolve comments
nareshz Jul 17, 2024
71a8f39
Merge branch 'main' of https://github.com/googleapis/java-spanner int…
nareshz Aug 7, 2024
1883fed
Merge branch 'main' of https://github.com/googleapis/java-spanner int…
nareshz Aug 9, 2024
01aa679
Merge branch 'main' of https://github.com/googleapis/java-spanner int…
nareshz Aug 9, 2024
be7d426
Merge branch 'e2e-tracing-header' into grpc-telemetry-client-interceptor
nareshz Aug 9, 2024
ab71745
Make trace context interceptor conditional based on client opt-in
nareshz Aug 9, 2024
2035d59
resolve comments
nareshz Aug 9, 2024
6af5e04
reformat code
nareshz Aug 13, 2024
0aa51ff
resolve comments
nareshz Aug 14, 2024
39269c0
fix clirr build failure
nareshz Aug 14, 2024
d2fd1c8
fix lint errors
nareshz Aug 14, 2024
2b9409b
combine enable/disable fn into single fn
nareshz Aug 19, 2024
b62b3c0
Merge branch 'main' of https://github.com/googleapis/java-spanner int…
nareshz Aug 19, 2024
e12cd0f
Merge branch 'main' of https://github.com/googleapis/java-spanner int…
nareshz Aug 21, 2024
0446fb5
Merge branch 'main' of https://github.com/googleapis/java-spanner int…
nareshz Aug 21, 2024
aa729a0
Merge branch 'main' of https://github.com/googleapis/java-spanner int…
nareshz Aug 27, 2024
0febebc
Merge branch 'main' of https://github.com/googleapis/java-spanner int…
nareshz Sep 25, 2024
b3a6d31
Rename server side tracing to spanner tracing
nareshz Sep 25, 2024
117c279
Rename spanner tracing to end to end tracing
nareshz Sep 26, 2024
a982c69
fix comments
nareshz Sep 27, 2024
0a2622d
Fix lint error
nareshz Sep 27, 2024
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 @@ -23,6 +23,7 @@
import io.grpc.ClientInterceptor;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;

nareshz marked this conversation as resolved.
Show resolved Hide resolved
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
Expand Down Expand Up @@ -51,6 +52,7 @@ public static SpannerInterceptorProvider createDefault(OpenTelemetry openTelemet
defaultInterceptorList.add(
new LoggingInterceptor(Logger.getLogger(GapicSpannerRpc.class.getName()), Level.FINER));
defaultInterceptorList.add(new HeaderInterceptor(new SpannerRpcMetrics(openTelemetry)));
defaultInterceptorList.add(new TraceContextInterceptor(openTelemetry));
nareshz marked this conversation as resolved.
Show resolved Hide resolved
return new SpannerInterceptorProvider(ImmutableList.copyOf(defaultInterceptorList));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2021 Google LLC
nareshz marked this conversation as resolved.
Show resolved Hide resolved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.spanner.spi.v1;

import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.ClientInterceptor;
import io.grpc.ForwardingClientCall.SimpleForwardingClientCall;
import io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.grpc.Status;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.context.propagation.TextMapSetter;

nareshz marked this conversation as resolved.
Show resolved Hide resolved
public class TraceContextInterceptor implements ClientInterceptor {
nareshz marked this conversation as resolved.
Show resolved Hide resolved

private final ContextPropagators propagators;

TraceContextInterceptor(OpenTelemetry openTelemetry) {
this.propagators = openTelemetry.getPropagators();
nareshz marked this conversation as resolved.
Show resolved Hide resolved
}

enum MetadataSetter implements TextMapSetter<Metadata> {
INSTANCE;

@SuppressWarnings("null")
@Override
public void set(Metadata carrier, String key, String value) {
carrier.put(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER), value);
}
}

@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method,
CallOptions callOptions, Channel next) {
return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
@Override
public void start(Listener<RespT> responseListener, Metadata headers) {
Context parentContext = Context.current();

nareshz marked this conversation as resolved.
Show resolved Hide resolved
propagators.getTextMapPropagator().inject(parentContext, headers, MetadataSetter.INSTANCE);
nareshz marked this conversation as resolved.
Show resolved Hide resolved

super.start(new SimpleForwardingClientCallListener<RespT>(responseListener) {
nareshz marked this conversation as resolved.
Show resolved Hide resolved
@Override
public void onHeaders(Metadata headers) {
super.onHeaders(headers);
}

@Override
public void onClose(Status status, Metadata trailers) {
super.onClose(status, trailers);
}
}, headers);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@
import io.grpc.auth.MoreCallCredentials;
import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder;
import io.grpc.protobuf.lite.ProtoLiteUtils;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.samplers.Sampler;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.HashMap;
Expand Down Expand Up @@ -148,6 +155,7 @@ public class GapicSpannerRpcTest {
private static String defaultUserAgent;
private static Spanner spanner;
private static boolean isRouteToLeader;
private static boolean isTraceContextPresent;

@Parameter public Dialect dialect;

Expand All @@ -158,6 +166,9 @@ public static Object[] data() {

@Before
public void startServer() throws IOException {
// Enable OpenTelemetry tracing.
SpannerOptions.enableOpenTelemetryTraces();
nareshz marked this conversation as resolved.
Show resolved Hide resolved

assumeTrue(
"Skip tests when emulator is enabled as this test interferes with the check whether the emulator is running",
System.getenv("SPANNER_EMULATOR_HOST") == null);
Expand Down Expand Up @@ -194,6 +205,9 @@ public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
if (call.getMethodDescriptor()
.equals(SpannerGrpc.getExecuteStreamingSqlMethod())
|| call.getMethodDescriptor().equals(SpannerGrpc.getExecuteSqlMethod())) {
String traceParentHeader =
headers.get(Key.of("traceparent", Metadata.ASCII_STRING_MARSHALLER));
isTraceContextPresent = (traceParentHeader != null);
String routeToLeaderHeader =
headers.get(
Key.of(
Expand Down Expand Up @@ -224,6 +238,7 @@ public void reset() throws InterruptedException {
server.awaitTermination();
}
isRouteToLeader = false;
isTraceContextPresent = false;
}

@Test
Expand Down Expand Up @@ -535,6 +550,41 @@ public void testCustomUserAgent() {
}
}

@Test
public void testTraceContextHeaderWithOpenTelemetry() {
OpenTelemetry openTelemetry = OpenTelemetrySdk.builder()
.setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance()))
.setTracerProvider(SdkTracerProvider.builder().setSampler(Sampler.alwaysOn()).build())
.build();

final SpannerOptions options = createSpannerOptions().toBuilder().setOpenTelemetry(openTelemetry).build();
try (Spanner spanner = options.getService()) {
final DatabaseClient databaseClient =
spanner.getDatabaseClient(DatabaseId.of("[PROJECT]", "[INSTANCE]", "[DATABASE]"));

try (final ResultSet rs = databaseClient.singleUse().executeQuery(SELECT1AND2)) {
rs.next();
}

assertTrue(isTraceContextPresent);
}
}

@Test
public void testTraceContextHeaderWithoutOpenTelemetry() {
final SpannerOptions options = createSpannerOptions();
try (Spanner spanner = options.getService()) {
final DatabaseClient databaseClient =
spanner.getDatabaseClient(DatabaseId.of("[PROJECT]", "[INSTANCE]", "[DATABASE]"));

try (final ResultSet rs = databaseClient.singleUse().executeQuery(SELECT1AND2)) {
rs.next();
}

assertFalse(isTraceContextPresent);
}
}

@Test
public void testRouteToLeaderHeaderForReadOnly() {
final SpannerOptions options =
Expand Down
Loading