Skip to content

Commit 30e75c6

Browse files
author
Anuraag Agrawal
authored
Add server interceptor to end in javaagent because they are run in reverse order. (#1383)
1 parent ee0e0a9 commit 30e75c6

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

instrumentation/grpc-1.5/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_5/GrpcServerBuilderInstrumentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static void addInterceptor(
4848
}
4949
}
5050
if (shouldRegister) {
51-
interceptors.add(0, TracingServerInterceptor.newInterceptor());
51+
interceptors.add(TracingServerInterceptor.newInterceptor());
5252
}
5353
}
5454
}

instrumentation/grpc-1.5/testing/src/main/groovy/io/opentelemetry/instrumentation/grpc/v1_5/AbstractGrpcTest.groovy

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import io.grpc.Channel
1818
import io.grpc.ClientCall
1919
import io.grpc.ClientInterceptor
2020
import io.grpc.Context
21+
import io.grpc.Contexts
2122
import io.grpc.ManagedChannel
2223
import io.grpc.ManagedChannelBuilder
2324
import io.grpc.Metadata
@@ -323,25 +324,26 @@ abstract class AbstractGrpcTest extends InstrumentationSpecification {
323324
def port = PortUtils.randomOpenPort()
324325
Server server
325326
server = configureServer(ServerBuilder.forPort(port)
326-
.addService(greeter))
327+
.addService(greeter)
327328
.intercept(new ServerInterceptor() {
328329
@Override
329330
<ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
330-
def ctx = Context.current().withValue(key, "meow")
331-
def oldCtx = ctx.attach()
332-
try {
333-
return next.startCall(call, headers)
334-
} finally {
335-
ctx.detach(oldCtx)
331+
if (!TracingContextUtils.getSpan(Context.current()).getContext().isValid()) {
332+
throw new AssertionError((Object) "span not attached in server interceptor")
336333
}
334+
def ctx = Context.current().withValue(key, "meow")
335+
return Contexts.interceptCall(ctx, call, headers, next)
337336
}
338-
})
337+
}))
339338
.build().start()
340339
ManagedChannelBuilder channelBuilder
341340
channelBuilder = configureClient(ManagedChannelBuilder.forAddress("localhost", port))
342341
.intercept(new ClientInterceptor() {
343342
@Override
344343
<ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
344+
if (!TracingContextUtils.getSpan(Context.current()).getContext().isValid()) {
345+
throw new AssertionError((Object) "span not attached in client interceptor")
346+
}
345347
def ctx = Context.current().withValue(key, "meow")
346348
def oldCtx = ctx.attach()
347349
try {

0 commit comments

Comments
 (0)