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

Fix grpc instrumentation of callbacks #4097

Merged
merged 4 commits into from
Sep 13, 2021
Merged
Changes from 2 commits
Commits
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 @@ -5,6 +5,7 @@

package io.opentelemetry.instrumentation.grpc.v1_6

import com.google.common.util.concurrent.MoreExecutors
import example.GreeterGrpc
import example.Helloworld
import io.grpc.BindableService
Expand Down Expand Up @@ -143,7 +144,117 @@ abstract class AbstractGrpcTest extends InstrumentationSpecification {
paramName << ["some name", "some other name"]
}

def "test callback"() {
def "test ListenableFuture callback"() {
setup:
BindableService greeter = new GreeterGrpc.GreeterImplBase() {
@Override
void sayHello(
final Helloworld.Request req, final StreamObserver<Helloworld.Response> responseObserver) {
final Helloworld.Response reply = Helloworld.Response.newBuilder().setMessage("Hello $req.name").build()
responseObserver.onNext(reply)
responseObserver.onCompleted()
}
}
def port = PortUtils.findOpenPort()
Server server = configureServer(ServerBuilder.forPort(port).addService(greeter)).build().start()
ManagedChannelBuilder channelBuilder = configureClient(ManagedChannelBuilder.forAddress("localhost", port))

// Depending on the version of gRPC usePlainText may or may not take an argument.
try {
channelBuilder.usePlaintext()
} catch (MissingMethodException e) {
channelBuilder.usePlaintext(true)
}
ManagedChannel channel = channelBuilder.build()
GreeterGrpc.GreeterFutureStub client = GreeterGrpc.newFutureStub(channel)

when:
AtomicReference<Helloworld.Response> response = new AtomicReference<>()
AtomicReference<Throwable> error = new AtomicReference<>()
CountDownLatch latch = new CountDownLatch(1)
runWithSpan("parent") {
def future = client.sayHello(Helloworld.Request.newBuilder().setName("test").build())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably worth using the idiomatic API

def future = Futures.transform(
  client.sayHelo(...)
  response -> {
    runWithSpan("child");
    return response;
  },
  MoreExecutors.directExecutor());

try {
  response.set(future.get());
 } catch (Throwable t) {
  error.set(t);
}

future.addListener({
try {
response.set(future.get())
} catch (Exception e) {
error.set(e);
}
runWithSpan("child") {}
latch.countDown()
}, MoreExecutors.directExecutor())
}

latch.await(10, TimeUnit.SECONDS)

then:
error.get() == null
response.get().message == "Hello test"

assertTraces(1) {
trace(0, 4) {
span(0) {
name "parent"
kind SpanKind.INTERNAL
hasNoParent()
}
span(1) {
name "example.Greeter/SayHello"
kind CLIENT
childOf span(0)
event(0) {
eventName "message"
attributes {
"message.type" "SENT"
"message.id" 1
}
}
attributes {
"${SemanticAttributes.RPC_SYSTEM.key}" "grpc"
"${SemanticAttributes.RPC_SERVICE.key}" "example.Greeter"
"${SemanticAttributes.RPC_METHOD.key}" "SayHello"
"${SemanticAttributes.NET_TRANSPORT}" SemanticAttributes.NetTransportValues.IP_TCP
"${SemanticAttributes.RPC_GRPC_STATUS_CODE}" Status.Code.OK.value()
}
}
span(2) {
name "example.Greeter/SayHello"
kind SERVER
childOf span(1)
event(0) {
eventName "message"
attributes {
"message.type" "RECEIVED"
"message.id" 1
}
}
attributes {
"${SemanticAttributes.RPC_SYSTEM.key}" "grpc"
"${SemanticAttributes.RPC_SERVICE.key}" "example.Greeter"
"${SemanticAttributes.RPC_METHOD.key}" "SayHello"
"${SemanticAttributes.NET_PEER_IP.key}" "127.0.0.1"
// "localhost" on linux, "127.0.0.1" on windows
"${SemanticAttributes.NET_PEER_NAME.key}" { it == "localhost" || it == "127.0.0.1" }
"${SemanticAttributes.NET_PEER_PORT.key}" Long
"${SemanticAttributes.NET_TRANSPORT.key}" SemanticAttributes.NetTransportValues.IP_TCP
"${SemanticAttributes.RPC_GRPC_STATUS_CODE.key}" Status.Code.OK.value()
}
}
span(3) {
name "child"
kind SpanKind.INTERNAL
childOf span(0)
}
}
}

cleanup:
channel?.shutdownNow()?.awaitTermination(10, TimeUnit.SECONDS)
server?.shutdownNow()?.awaitTermination()
}


def "test onCompleted callback"() {
setup:
BindableService greeter = new GreeterGrpc.GreeterImplBase() {
@Override
Expand Down