Skip to content

Commit

Permalink
Fixed AsyncServerInterceptor to be compatible with OpenTelmetry (#5938
Browse files Browse the repository at this point in the history
)

Motivation:

There was a bug in `AsyncServerInterceptor` where a
`ForwardingServerCall` was not unwrapped properly via reflection.
Because of that, OpenTelemetry's `TracingServerCall` that wraps the
existing `ServerCall` using `ForwardingServerCall` was rejected by the
following condition.

https://github.com/line/armeria/blob/6dd5cd18dcdc32431316f272128287b2037249d3/grpc/src/main/java/com/linecorp/armeria/server/grpc/DeferredListener.java#L51-L52

Modifications:

- Use the correct reflection API to unsafely access
`ForwardingServerCall.delegate()`.

Result:

- Fix a bug where `AsyncServerInterceptor` is incompatible with the
OpenTelemetry gRPC agent.
- Closes #5937
  • Loading branch information
ikhoon authored Nov 7, 2024
1 parent 2ea65f5 commit d1491ca
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Method;

import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.internal.server.grpc.AbstractServerCall;
Expand All @@ -34,8 +34,9 @@ final class ServerCallUtil {

static {
try {
delegateMH = MethodHandles.lookup().findVirtual(ForwardingServerCall.class, "delegate",
MethodType.methodType(ServerCall.class));
final Method delegate = ForwardingServerCall.class.getDeclaredMethod("delegate");
delegate.setAccessible(true);
delegateMH = MethodHandles.lookup().unreflect(delegate);
} catch (NoSuchMethodException | IllegalAccessException e) {
delegateMH = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import com.google.common.util.concurrent.MoreExecutors;

Expand All @@ -44,6 +46,7 @@

import io.grpc.CompressorRegistry;
import io.grpc.DecompressorRegistry;
import io.grpc.ForwardingServerCall.SimpleForwardingServerCall;
import io.grpc.ServerCall;
import io.netty.channel.EventLoop;
import testing.grpc.Messages.SimpleRequest;
Expand All @@ -60,10 +63,14 @@ void shouldHaveRequestContextInThread() {
AsyncServerInterceptor.class.getName());
}

@Test
void shouldLazilyExecuteCallbacks() {
@ValueSource(booleans = { true, false })
@ParameterizedTest
void shouldLazilyExecuteCallbacks(boolean wrap) {
final EventLoop eventLoop = CommonPools.workerGroup().next();
final UnaryServerCall<SimpleRequest, SimpleResponse> serverCall = newServerCall(eventLoop, null);
ServerCall<SimpleRequest, SimpleResponse> serverCall = newServerCall(eventLoop, null);
if (wrap) {
serverCall = new SimpleForwardingServerCall<SimpleRequest, SimpleResponse>(serverCall) {};
}
assertListenerEvents(serverCall, eventLoop);

final Executor blockingExecutor =
Expand Down

0 comments on commit d1491ca

Please sign in to comment.