Skip to content

Commit

Permalink
fix grpc
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Cole <[email protected]>
  • Loading branch information
Adrian Cole committed May 14, 2024
1 parent 9f4b012 commit d1fd3d3
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 203 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Here are the example projects you can try:
* Trace Configuration: [Brave API](https://github.com/openzipkin/brave/tree/master/brave#setup) [XML](jersey2-cassandra3/src/main/webapp/WEB-INF/tracing.xml)

* [netty4-grpc](netty4-grpc) `BRAVE_EXAMPLE=netty4-grpc docker-compose up`
* Runtime: Netty 4.1, Google gRPC 1.34 , SLF4J 1.7, JRE 21
* Runtime: Netty 4.1, Google gRPC 1.63 , SLF4J 1.7, JRE 21
* Trace Instrumentation: [Netty Codec HTTP](https://github.com/openzipkin/brave/tree/master/instrumentation/netty-codec-http), [Google gRPC](https://github.com/openzipkin/brave/tree/master/instrumentation/grpc), [SLF4J](https://github.com/openzipkin/brave/tree/master/context/slf4j)
* Trace Configuration: [Brave API](https://github.com/openzipkin/brave/tree/master/brave#setup) [Java](netty4-grpc/src/main/java/brave/example/TracingConfiguration.java)

Expand Down
4 changes: 2 additions & 2 deletions netty4-grpc/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Tracing Example: Netty 4.1/Google gRPC 1.34/JRE 21
## Tracing Example: Netty 4.1/Google gRPC 1.63/JRE 21

Instead of servlet, the frontend is a Netty HTTP handler. The backend is Google gRPC 1.31.
Both services run as a normal Java application.
Expand All @@ -7,7 +7,7 @@ Both services run as a normal Java application.
* [brave.example.Backend](src/main/java/brave/example/Backend.java) - Google gRPC server
* [brave.example.TracingConfiguration](src/main/java/brave/example/TracingConfiguration.java) - Configures trace instrumentation

Here's an example screen shot:
Here's an example screenshot:
![screen shot](https://user-images.githubusercontent.com/64215/102683005-a2986500-4208-11eb-8258-92cc02f9310b.png)

This example also shows Brave's `BaggagePropagation`. If you make a request to the frontend with
Expand Down
9 changes: 7 additions & 2 deletions netty4-grpc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<packaging>jar</packaging>

<name>brave-example-netty4-grpc</name>
<description>Tracing Example: Netty 4.1/Google gRPC 1.34/JRE 21</description>
<description>Tracing Example: Netty 4.1/Google gRPC 1.63/JRE 21</description>

<properties>
<jre.version>21</jre.version>
Expand All @@ -33,6 +33,11 @@
<artifactId>grpc-services</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
Expand Down Expand Up @@ -63,7 +68,7 @@
</dependency>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-context-slf4j</artifactId>
<artifactId>brave-context-log4j12</artifactId>
</dependency>

<!-- The below are needed to report traces to http://127.0.0.1:9411/api/v2/spans -->
Expand Down
2 changes: 1 addition & 1 deletion netty4-grpc/src/main/java/brave/example/Backend.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static void main(String[] args) throws Exception {

/**
* Google's gRPC impl disallows reading headers inside a server handler. This copies a header into
* the context so we can see if it was propagated or not.
* the context, so we can see if it was propagated or not.
*/
static final class CopyHeaderToGrpcContext implements ServerInterceptor {
final Key<String> contextKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import brave.baggage.BaggagePropagation;
import brave.baggage.BaggagePropagationConfig;
import brave.baggage.CorrelationScopeConfig;
import brave.context.slf4j.MDCScopeDecorator;
import brave.context.log4j12.MDCScopeDecorator;
import brave.grpc.GrpcTracing;
import brave.http.HttpTracing;
import brave.netty.http.NettyHttpTracing;
Expand Down
79 changes: 42 additions & 37 deletions netty4-grpc/src/main/java/brave/example/generated/BackendGrpc.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
package brave.example.generated;

import static io.grpc.MethodDescriptor.generateFullMethodName;
import static io.grpc.stub.ClientCalls.asyncBidiStreamingCall;
import static io.grpc.stub.ClientCalls.asyncClientStreamingCall;
import static io.grpc.stub.ClientCalls.asyncServerStreamingCall;
import static io.grpc.stub.ClientCalls.asyncUnaryCall;
import static io.grpc.stub.ClientCalls.blockingServerStreamingCall;
import static io.grpc.stub.ClientCalls.blockingUnaryCall;
import static io.grpc.stub.ClientCalls.futureUnaryCall;
import static io.grpc.stub.ServerCalls.asyncBidiStreamingCall;
import static io.grpc.stub.ServerCalls.asyncClientStreamingCall;
import static io.grpc.stub.ServerCalls.asyncServerStreamingCall;
import static io.grpc.stub.ServerCalls.asyncUnaryCall;
import static io.grpc.stub.ServerCalls.asyncUnimplementedStreamingCall;
import static io.grpc.stub.ServerCalls.asyncUnimplementedUnaryCall;

/**
*/
@javax.annotation.Generated(
value = "by gRPC proto compiler (version 1.31.1)",
value = "by gRPC proto compiler (version 1.63.0)",
comments = "Source: backend.proto")
@io.grpc.stub.annotations.GrpcGenerated
public final class BackendGrpc {

private BackendGrpc() {}

public static final String SERVICE_NAME = "brave.example.Backend";
public static final java.lang.String SERVICE_NAME = "brave.example.Backend";

// Static method descriptors that strictly reflect the proto.
private static volatile io.grpc.MethodDescriptor<brave.example.generated.BackendProto.Empty,
Expand Down Expand Up @@ -104,31 +92,32 @@ public BackendFutureStub newStub(io.grpc.Channel channel, io.grpc.CallOptions ca

/**
*/
public static abstract class BackendImplBase implements io.grpc.BindableService {
public interface AsyncService {

/**
*/
public void printDate(brave.example.generated.BackendProto.Empty request,
default void printDate(brave.example.generated.BackendProto.Empty request,
io.grpc.stub.StreamObserver<brave.example.generated.BackendProto.Reply> responseObserver) {
asyncUnimplementedUnaryCall(getPrintDateMethod(), responseObserver);
io.grpc.stub.ServerCalls.asyncUnimplementedUnaryCall(getPrintDateMethod(), responseObserver);
}
}

/**
* Base class for the server implementation of the service Backend.
*/
public static abstract class BackendImplBase
implements io.grpc.BindableService, AsyncService {

@java.lang.Override public final io.grpc.ServerServiceDefinition bindService() {
return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor())
.addMethod(
getPrintDateMethod(),
asyncUnaryCall(
new MethodHandlers<
brave.example.generated.BackendProto.Empty,
brave.example.generated.BackendProto.Reply>(
this, METHODID_PRINT_DATE)))
.build();
return BackendGrpc.bindService(this);
}
}

/**
* A stub to allow clients to do asynchronous rpc calls to service Backend.
*/
public static final class BackendStub extends io.grpc.stub.AbstractAsyncStub<BackendStub> {
public static final class BackendStub
extends io.grpc.stub.AbstractAsyncStub<BackendStub> {
private BackendStub(
io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
super(channel, callOptions);
Expand All @@ -144,14 +133,16 @@ protected BackendStub build(
*/
public void printDate(brave.example.generated.BackendProto.Empty request,
io.grpc.stub.StreamObserver<brave.example.generated.BackendProto.Reply> responseObserver) {
asyncUnaryCall(
io.grpc.stub.ClientCalls.asyncUnaryCall(
getChannel().newCall(getPrintDateMethod(), getCallOptions()), request, responseObserver);
}
}

/**
* A stub to allow clients to do synchronous rpc calls to service Backend.
*/
public static final class BackendBlockingStub extends io.grpc.stub.AbstractBlockingStub<BackendBlockingStub> {
public static final class BackendBlockingStub
extends io.grpc.stub.AbstractBlockingStub<BackendBlockingStub> {
private BackendBlockingStub(
io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
super(channel, callOptions);
Expand All @@ -166,14 +157,16 @@ protected BackendBlockingStub build(
/**
*/
public brave.example.generated.BackendProto.Reply printDate(brave.example.generated.BackendProto.Empty request) {
return blockingUnaryCall(
return io.grpc.stub.ClientCalls.blockingUnaryCall(
getChannel(), getPrintDateMethod(), getCallOptions(), request);
}
}

/**
* A stub to allow clients to do ListenableFuture-style rpc calls to service Backend.
*/
public static final class BackendFutureStub extends io.grpc.stub.AbstractFutureStub<BackendFutureStub> {
public static final class BackendFutureStub
extends io.grpc.stub.AbstractFutureStub<BackendFutureStub> {
private BackendFutureStub(
io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
super(channel, callOptions);
Expand All @@ -189,7 +182,7 @@ protected BackendFutureStub build(
*/
public com.google.common.util.concurrent.ListenableFuture<brave.example.generated.BackendProto.Reply> printDate(
brave.example.generated.BackendProto.Empty request) {
return futureUnaryCall(
return io.grpc.stub.ClientCalls.futureUnaryCall(
getChannel().newCall(getPrintDateMethod(), getCallOptions()), request);
}
}
Expand All @@ -201,10 +194,10 @@ private static final class MethodHandlers<Req, Resp> implements
io.grpc.stub.ServerCalls.ServerStreamingMethod<Req, Resp>,
io.grpc.stub.ServerCalls.ClientStreamingMethod<Req, Resp>,
io.grpc.stub.ServerCalls.BidiStreamingMethod<Req, Resp> {
private final BackendImplBase serviceImpl;
private final AsyncService serviceImpl;
private final int methodId;

MethodHandlers(BackendImplBase serviceImpl, int methodId) {
MethodHandlers(AsyncService serviceImpl, int methodId) {
this.serviceImpl = serviceImpl;
this.methodId = methodId;
}
Expand Down Expand Up @@ -233,6 +226,18 @@ public io.grpc.stub.StreamObserver<Req> invoke(
}
}

public static final io.grpc.ServerServiceDefinition bindService(AsyncService service) {
return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor())
.addMethod(
getPrintDateMethod(),
io.grpc.stub.ServerCalls.asyncUnaryCall(
new MethodHandlers<
brave.example.generated.BackendProto.Empty,
brave.example.generated.BackendProto.Reply>(
service, METHODID_PRINT_DATE)))
.build();
}

private static abstract class BackendBaseDescriptorSupplier
implements io.grpc.protobuf.ProtoFileDescriptorSupplier, io.grpc.protobuf.ProtoServiceDescriptorSupplier {
BackendBaseDescriptorSupplier() {}
Expand All @@ -256,9 +261,9 @@ private static final class BackendFileDescriptorSupplier
private static final class BackendMethodDescriptorSupplier
extends BackendBaseDescriptorSupplier
implements io.grpc.protobuf.ProtoMethodDescriptorSupplier {
private final String methodName;
private final java.lang.String methodName;

BackendMethodDescriptorSupplier(String methodName) {
BackendMethodDescriptorSupplier(java.lang.String methodName) {
this.methodName = methodName;
}

Expand Down
Loading

0 comments on commit d1fd3d3

Please sign in to comment.