-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Implement ContextStorageOverride for opentelemetry context bridge #11523
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,27 @@ | ||
plugins { | ||
id "java-library" | ||
id "maven-publish" | ||
|
||
id "ru.vyarus.animalsniffer" | ||
} | ||
|
||
description = 'gRPC: ContextStorageOverride' | ||
|
||
dependencies { | ||
api project(':grpc-api') | ||
implementation libraries.opentelemetry.api | ||
|
||
testImplementation libraries.junit, | ||
libraries.opentelemetry.sdk.testing, | ||
libraries.assertj.core | ||
testImplementation 'junit:junit:4.13.1'// opentelemetry.sdk.testing uses compileOnly for assertj | ||
|
||
signature libraries.signature.java | ||
signature libraries.signature.android | ||
} | ||
|
||
tasks.named("jar").configure { | ||
manifest { | ||
attributes('Automatic-Module-Name': 'io.grpc.override') | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
contextstorage/src/main/java/io/grpc/override/ContextStorageOverride.java
This file contains hidden or 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,46 @@ | ||
/* | ||
* Copyright 2024 The gRPC Authors | ||
* | ||
* 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 | ||
* | ||
* http://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 io.grpc.override; | ||
|
||
import io.grpc.Context; | ||
|
||
/** | ||
* Including this class in your dependencies will override the default gRPC context storage using | ||
* reflection. It is a bridge between {@link io.grpc.Context} and | ||
* {@link io.opentelemetry.context.Context}, i.e. propagating io.grpc.context.Context also | ||
* propagates io.opentelemetry.context, and propagating io.opentelemetry.context will also propagate | ||
* io.grpc.context. | ||
*/ | ||
public final class ContextStorageOverride extends Context.Storage { | ||
|
||
private final Context.Storage delegate = new OpenTelemetryContextStorage(); | ||
|
||
@Override | ||
public Context doAttach(Context toAttach) { | ||
return delegate.doAttach(toAttach); | ||
} | ||
|
||
@Override | ||
public void detach(Context toDetach, Context toRestore) { | ||
delegate.detach(toDetach, toRestore); | ||
} | ||
|
||
@Override | ||
public Context current() { | ||
return delegate.current(); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
contextstorage/src/main/java/io/grpc/override/OpenTelemetryContextStorage.java
This file contains hidden or 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 The gRPC Authors | ||
* | ||
* 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 | ||
* | ||
* http://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 io.grpc.override; | ||
|
||
import io.grpc.Context; | ||
import io.opentelemetry.context.ContextKey; | ||
import io.opentelemetry.context.Scope; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* A Context.Storage implementation that attaches io.grpc.context to OpenTelemetry's context and | ||
* io.opentelemetry.context is also saved in the io.grpc.context. | ||
* Bridge between {@link io.grpc.Context} and {@link io.opentelemetry.context.Context}. | ||
*/ | ||
final class OpenTelemetryContextStorage extends Context.Storage { | ||
private static final Logger logger = Logger.getLogger( | ||
OpenTelemetryContextStorage.class.getName()); | ||
|
||
private static final io.grpc.Context.Key<io.opentelemetry.context.Context> OTEL_CONTEXT_OVER_GRPC | ||
= io.grpc.Context.key("otel-context-over-grpc"); | ||
private static final Context.Key<Scope> OTEL_SCOPE = Context.key("otel-scope"); | ||
private static final ContextKey<io.grpc.Context> GRPC_CONTEXT_OVER_OTEL = | ||
ContextKey.named("grpc-context-over-otel"); | ||
|
||
@Override | ||
@SuppressWarnings("MustBeClosedChecker") | ||
public Context doAttach(Context toAttach) { | ||
io.grpc.Context previous = current(); | ||
io.opentelemetry.context.Context otelContext = OTEL_CONTEXT_OVER_GRPC.get(toAttach); | ||
if (otelContext == null) { | ||
otelContext = io.opentelemetry.context.Context.current(); | ||
} | ||
Scope scope = otelContext.with(GRPC_CONTEXT_OVER_OTEL, toAttach).makeCurrent(); | ||
return previous.withValue(OTEL_SCOPE, scope); | ||
} | ||
|
||
@Override | ||
public void detach(Context toDetach, Context toRestore) { | ||
Scope scope = OTEL_SCOPE.get(toRestore); | ||
if (scope == null) { | ||
logger.log( | ||
Level.SEVERE, "Detaching context which was not attached.", | ||
new Throwable().fillInStackTrace()); | ||
YifeiZhuang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
scope.close(); | ||
} | ||
} | ||
|
||
@Override | ||
public Context current() { | ||
io.opentelemetry.context.Context otelCurrent = io.opentelemetry.context.Context.current(); | ||
io.grpc.Context grpcCurrent = otelCurrent.get(GRPC_CONTEXT_OVER_OTEL); | ||
if (grpcCurrent == null) { | ||
ejona86 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
grpcCurrent = Context.ROOT; | ||
} | ||
return grpcCurrent.withValue(OTEL_CONTEXT_OVER_GRPC, otelCurrent); | ||
} | ||
} |
144 changes: 144 additions & 0 deletions
144
contextstorage/src/test/java/io/grpc/override/OpenTelemetryContextStorageTest.java
This file contains hidden or 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,144 @@ | ||
/* | ||
* Copyright 2024 The gRPC Authors | ||
* | ||
* 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 | ||
* | ||
* http://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 io.grpc.override; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNull; | ||
|
||
import com.google.common.util.concurrent.SettableFuture; | ||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.api.trace.Tracer; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.ContextKey; | ||
import io.opentelemetry.context.Scope; | ||
import io.opentelemetry.sdk.testing.junit4.OpenTelemetryRule; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import org.junit.Assert; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class OpenTelemetryContextStorageTest { | ||
@Rule | ||
public final OpenTelemetryRule openTelemetryRule = OpenTelemetryRule.create(); | ||
private Tracer tracerRule = openTelemetryRule.getOpenTelemetry().getTracer( | ||
"context-storage-test"); | ||
private final io.grpc.Context.Key<String> username = io.grpc.Context.key("username"); | ||
private final ContextKey<String> password = ContextKey.named("password"); | ||
|
||
@Test | ||
public void grpcContextPropagation() throws Exception { | ||
final Span parentSpan = tracerRule.spanBuilder("test-context").startSpan(); | ||
final SettableFuture<Span> spanPropagated = SettableFuture.create(); | ||
final SettableFuture<String> grpcContextPropagated = SettableFuture.create(); | ||
final SettableFuture<Span> spanDetached = SettableFuture.create(); | ||
final SettableFuture<String> grpcContextDetached = SettableFuture.create(); | ||
|
||
io.grpc.Context grpcContext; | ||
try (Scope scope = Context.current().with(parentSpan).makeCurrent()) { | ||
grpcContext = io.grpc.Context.current().withValue(username, "jeff"); | ||
} | ||
new Thread(new Runnable() { | ||
@Override | ||
public void run() { | ||
io.grpc.Context previous = grpcContext.attach(); | ||
try { | ||
grpcContextPropagated.set(username.get(io.grpc.Context.current())); | ||
spanPropagated.set(Span.fromContext(io.opentelemetry.context.Context.current())); | ||
} finally { | ||
grpcContext.detach(previous); | ||
spanDetached.set(Span.fromContext(io.opentelemetry.context.Context.current())); | ||
grpcContextDetached.set(username.get(io.grpc.Context.current())); | ||
} | ||
} | ||
}).start(); | ||
Assert.assertEquals(spanPropagated.get(5, TimeUnit.SECONDS), parentSpan); | ||
Assert.assertEquals(grpcContextPropagated.get(5, TimeUnit.SECONDS), "jeff"); | ||
Assert.assertEquals(spanDetached.get(5, TimeUnit.SECONDS), Span.getInvalid()); | ||
Assert.assertNull(grpcContextDetached.get(5, TimeUnit.SECONDS)); | ||
} | ||
|
||
@Test | ||
public void otelContextPropagation() throws Exception { | ||
final SettableFuture<String> grpcPropagated = SettableFuture.create(); | ||
final AtomicReference<String> otelPropagation = new AtomicReference<>(); | ||
|
||
io.grpc.Context grpcContext = io.grpc.Context.current().withValue(username, "jeff"); | ||
io.grpc.Context previous = grpcContext.attach(); | ||
Context original = Context.current().with(password, "valentine"); | ||
try { | ||
new Thread( | ||
() -> { | ||
try (Scope scope = original.makeCurrent()) { | ||
otelPropagation.set(Context.current().get(password)); | ||
grpcPropagated.set(username.get(io.grpc.Context.current())); | ||
} | ||
} | ||
).start(); | ||
} finally { | ||
grpcContext.detach(previous); | ||
} | ||
Assert.assertEquals(grpcPropagated.get(5, TimeUnit.SECONDS), "jeff"); | ||
Assert.assertEquals(otelPropagation.get(), "valentine"); | ||
} | ||
|
||
@Test | ||
public void grpcOtelMix() { | ||
io.grpc.Context grpcContext = io.grpc.Context.current().withValue(username, "jeff"); | ||
Context otelContext = Context.current().with(password, "valentine"); | ||
Assert.assertNull(username.get(io.grpc.Context.current())); | ||
Assert.assertNull(Context.current().get(password)); | ||
io.grpc.Context previous = grpcContext.attach(); | ||
try { | ||
assertEquals(username.get(io.grpc.Context.current()), "jeff"); | ||
try (Scope scope = otelContext.makeCurrent()) { | ||
Assert.assertEquals(Context.current().get(password), "valentine"); | ||
assertNull(username.get(io.grpc.Context.current())); | ||
|
||
io.grpc.Context grpcContext2 = io.grpc.Context.current().withValue(username, "frank"); | ||
io.grpc.Context previous2 = grpcContext2.attach(); | ||
try { | ||
assertEquals(username.get(io.grpc.Context.current()), "frank"); | ||
Assert.assertEquals(Context.current().get(password), "valentine"); | ||
} finally { | ||
grpcContext2.detach(previous2); | ||
} | ||
assertNull(username.get(io.grpc.Context.current())); | ||
Assert.assertEquals(Context.current().get(password), "valentine"); | ||
} | ||
} finally { | ||
grpcContext.detach(previous); | ||
} | ||
Assert.assertNull(username.get(io.grpc.Context.current())); | ||
Assert.assertNull(Context.current().get(password)); | ||
} | ||
|
||
@Test | ||
public void grpcContextDetachError() { | ||
io.grpc.Context grpcContext = io.grpc.Context.current().withValue(username, "jeff"); | ||
io.grpc.Context previous = grpcContext.attach(); | ||
try { | ||
previous.detach(grpcContext); | ||
assertEquals(username.get(io.grpc.Context.current()), "jeff"); | ||
} finally { | ||
grpcContext.detach(previous); | ||
} | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
|
@@ -76,6 +76,7 @@ include ":grpc-istio-interop-testing" | |
include ":grpc-inprocess" | ||
include ":grpc-util" | ||
include ":grpc-opentelemetry" | ||
include ":grpc-context-storage-override" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The name of the package is clearly io.grpc.context-related. However, nothing speaks to "otel" in the name. |
||
|
||
project(':grpc-api').projectDir = "$rootDir/api" as File | ||
project(':grpc-core').projectDir = "$rootDir/core" as File | ||
|
@@ -111,6 +112,7 @@ project(':grpc-istio-interop-testing').projectDir = "$rootDir/istio-interop-test | |
project(':grpc-inprocess').projectDir = "$rootDir/inprocess" as File | ||
project(':grpc-util').projectDir = "$rootDir/util" as File | ||
project(':grpc-opentelemetry').projectDir = "$rootDir/opentelemetry" as File | ||
project(':grpc-context-storage-override').projectDir = "$rootDir/contextstorage" as File | ||
|
||
if (settings.hasProperty('skipCodegen') && skipCodegen.toBoolean()) { | ||
println '*** Skipping the build of codegen and compilation of proto files because skipCodegen=true' | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.