-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add opt-in flag and ClientInterceptor to propagate trace contex…
…t for Spanner end to end tracing (#3162) * feat(spanner): Add x-goog-spanner-end-to-end-tracing header for requests to SpanFE * Add Grpc Telemetry client interceptor for trace context propagation * Remove print statement * copy grpc telemetry client interceptor code * rename spanner option for end to end tracing * add test for custom client interceptor code * resolve comments * Make trace context interceptor conditional based on client opt-in * resolve comments * reformat code * resolve comments * fix clirr build failure * fix lint errors * combine enable/disable fn into single fn * Rename server side tracing to spanner tracing * Rename spanner tracing to end to end tracing * fix comments * Fix lint error
- Loading branch information
Showing
10 changed files
with
364 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...-cloud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/TraceContextInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* 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.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.propagation.TextMapPropagator; | ||
import io.opentelemetry.context.propagation.TextMapSetter; | ||
|
||
/** | ||
* Intercepts all gRPC calls and injects trace context related headers to propagate trace context to | ||
* Spanner. This class takes reference from OpenTelemetry's JAVA instrumentation library for gRPC. | ||
* https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/9ecf7965aa455d41ea8cc0761b6c6b6eeb106324/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/TracingClientInterceptor.java#L27 | ||
*/ | ||
class TraceContextInterceptor implements ClientInterceptor { | ||
|
||
private final TextMapPropagator textMapPropagator; | ||
|
||
TraceContextInterceptor(OpenTelemetry openTelemetry) { | ||
this.textMapPropagator = openTelemetry.getPropagators().getTextMapPropagator(); | ||
} | ||
|
||
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); | ||
} | ||
} | ||
|
||
private static final class NoopSimpleForwardingClientCallListener<RespT> | ||
extends SimpleForwardingClientCallListener<RespT> { | ||
public NoopSimpleForwardingClientCallListener(ClientCall.Listener<RespT> responseListener) { | ||
super(responseListener); | ||
} | ||
} | ||
|
||
@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(); | ||
textMapPropagator.inject(parentContext, headers, MetadataSetter.INSTANCE); | ||
super.start(new NoopSimpleForwardingClientCallListener<RespT>(responseListener), headers); | ||
} | ||
}; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerOptionsHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* 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; | ||
|
||
/** Helper to configure SpannerOptions for tests. */ | ||
public class SpannerOptionsHelper { | ||
|
||
/** | ||
* Resets the activeTracingFramework. This variable is used for internal testing, and is not a | ||
* valid production scenario. | ||
*/ | ||
public static void resetActiveTracingFramework() { | ||
SpannerOptions.resetActiveTracingFramework(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.