Skip to content

Commit

Permalink
Makes it possible to read commonly defined gRPC data
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Cole committed Apr 28, 2020
1 parent f3a6d98 commit 1959291
Show file tree
Hide file tree
Showing 14 changed files with 396 additions and 256 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package brave.grpc;

import brave.rpc.RpcClientRequest;
import brave.rpc.RpcTracing;
import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
Expand All @@ -27,35 +26,19 @@
/**
* Allows access gRPC specific aspects of a client request during sampling and parsing.
*
* <p>Here's an example that adds default tags, and if gRPC, the {@linkplain
* MethodDescriptor#getType() method type}:
* <pre>{@code
* Tag<GrpcClientRequest> methodType = new Tag<GrpcClientRequest>("grpc.method_type") {
* protected String parseValue(GrpcClientRequest input, TraceContext context) {
* return input.methodDescriptor().getType().name();
* }
* };
* rpcTracing = rpcTracingBuilder.clientResponseParser((res, context, span) -> {
* RpcResponseParser.DEFAULT.parse(res, context, span);
* if (res instanceof GrpcClientRequest) {
* methodType.tag((GrpcClientRequest) res, span);
* }
* }).build();
* }</pre>
*
* @see GrpcClientResponse
* @see RpcTracing#clientRequestParser()
* @see GrpcRequest for a parsing example
* @since 5.12
*/
public final class GrpcClientRequest extends RpcClientRequest {
public final class GrpcClientRequest extends RpcClientRequest implements GrpcRequest {
final Map<String, Key<String>> nameToKey;
final MethodDescriptor<?, ?> methodDescriptor;
final CallOptions callOptions;
final ClientCall<?, ?> call;
final Metadata headers;

GrpcClientRequest(Map<String, Key<String>> nameToKey, MethodDescriptor<?, ?> methodDescriptor,
CallOptions callOptions, ClientCall<?, ?> call, Metadata headers) {
CallOptions callOptions, ClientCall<?, ?> call, Metadata headers) {
if (nameToKey == null) throw new NullPointerException("nameToKey == null");
if (methodDescriptor == null) throw new NullPointerException("methodDescriptor == null");
if (callOptions == null) throw new NullPointerException("callOptions == null");
Expand Down Expand Up @@ -88,7 +71,7 @@ public final class GrpcClientRequest extends RpcClientRequest {
*
* @since 5.12
*/
public MethodDescriptor<?, ?> methodDescriptor() {
@Override public MethodDescriptor<?, ?> methodDescriptor() {
return methodDescriptor;
}

Expand Down Expand Up @@ -118,7 +101,7 @@ public CallOptions callOptions() {
*
* @since 5.12
*/
public Metadata headers() {
@Override public Metadata headers() {
return headers;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,30 @@

import brave.internal.Nullable;
import brave.rpc.RpcClientResponse;
import brave.rpc.RpcTracing;
import io.grpc.ClientCall;
import io.grpc.Metadata;
import io.grpc.Status;

/**
* Allows access gRPC specific aspects of a client response for parsing.
*
* <p>Here's an example that adds default tags, and if gRPC, the Java result:
* <pre>{@code
* rpcTracing = rpcTracingBuilder
* .clientResponseParser((res, context, span) -> {
* RpcResponseParser.DEFAULT.parse(res, context, span);
* if (res instanceof DubboResponse) {
* DubboResponse dubboResponse = (DubboResponse) res;
* if (res.result() != null) {
* tagJavaResult(res.result().value());
* }
* }
* }).build();
* }</pre>
*
* @see GrpcClientRequest
* @see RpcTracing#clientResponseParser()
* @see GrpcResponse for a parsing example
* @since 5.12
*/
public final class GrpcClientResponse extends RpcClientResponse {
public final class GrpcClientResponse extends RpcClientResponse implements GrpcResponse {
final GrpcClientRequest request;
final Metadata headers;
final Status status;
final Metadata trailers;

GrpcClientResponse(GrpcClientRequest request, Status status, Metadata trailers) {
GrpcClientResponse(GrpcClientRequest request, Metadata headers, Status status,
Metadata trailers) {
if (request == null) throw new NullPointerException("request == null");
if (headers == null) throw new NullPointerException("headers == null");
if (status == null) throw new NullPointerException("status == null");
if (trailers == null) throw new NullPointerException("trailers == null");
this.headers = headers;
this.request = request;
this.status = status;
this.trailers = trailers;
Expand Down Expand Up @@ -78,12 +67,21 @@ public final class GrpcClientResponse extends RpcClientResponse {
return status.getCode().name();
}

/**
* Returns a copy of headers passed to {@link ClientCall.Listener#onHeaders(Metadata)}.
*
* @since 5.12
*/
@Override public Metadata headers() {
return headers;
}

/**
* Returns the status passed to {@link ClientCall.Listener#onClose(Status, Metadata)}.
*
* @since 5.12
*/
public Status status() {
@Override public Status status() {
return status;
}

Expand All @@ -92,7 +90,7 @@ public Status status() {
*
* @since 5.12
*/
public Metadata trailers() {
@Override public Metadata trailers() {
return trailers;
}
}
54 changes: 54 additions & 0 deletions instrumentation/grpc/src/main/java/brave/grpc/GrpcRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2013-2020 The OpenZipkin 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 brave.grpc;

import brave.rpc.RpcTracing;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;

/**
* Allows access gRPC specific aspects of a client or server request during sampling and parsing.
*
* <p>Here's an example that adds default tags, and if gRPC, the {@linkplain
* MethodDescriptor#getType() method type}:
* <pre>{@code
* Tag<GrpcRequest> methodType = new Tag<GrpcRequest>("grpc.method_type") {
* @Override protected String parseValue(GrpcRequest input, TraceContext context) {
* return input.methodDescriptor().getType().name();
* }
* };
*
* RpcRequestParser addMethodType = (req, context, span) -> {
* RpcRequestParser.DEFAULT.parse(req, context, span);
* if (req instanceof GrpcRequest) methodType.tag((GrpcRequest) req, span);
* };
*
* grpcTracing = GrpcTracing.create(RpcTracing.newBuilder(tracing)
* .clientRequestParser(addMethodType)
* .serverRequestParser(addMethodType).build());
* }</pre>
*
* @see GrpcResponse
* @see GrpcClientRequest
* @see GrpcServerRequest
* @see RpcTracing#clientRequestParser()
* @see RpcTracing#serverRequestParser()
* @since 5.12
*/
// NOTE: gRPC is Java 1.7+, so we cannot add methods to this later
public interface GrpcRequest {
MethodDescriptor<?, ?> methodDescriptor();

Metadata headers();
}
55 changes: 55 additions & 0 deletions instrumentation/grpc/src/main/java/brave/grpc/GrpcResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2013-2020 The OpenZipkin 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 brave.grpc;

import brave.rpc.RpcTracing;
import io.grpc.Metadata;
import io.grpc.Status;

/**
* Allows access gRPC specific aspects of a client or server response for parsing.
*
* <p>Here's an example that adds default tags, and if gRPC, the response encoding:
* <pre>{@code
* Tag<GrpcResponse> responseEncoding = new Tag<GrpcResponse>("grpc.response_encoding") {
* @Override protected String parseValue(GrpcResponse input, TraceContext context) {
* return input.headers().get(GrpcUtil.MESSAGE_ENCODING_KEY);
* }
* };
*
* RpcResponseParser addResponseEncoding = (res, context, span) -> {
* RpcResponseParser.DEFAULT.parse(res, context, span);
* if (res instanceof GrpcResponse) responseEncoding.tag((GrpcResponse) res, span);
* };
*
* grpcTracing = GrpcTracing.create(RpcTracing.newBuilder(tracing)
* .clientResponseParser(addResponseEncoding);
* .serverResponseParser(addResponseEncoding).build());
* }</pre>
*
* @see GrpcRequest
* @see GrpcClientResponse
* @see GrpcServerResponse
* @see RpcTracing#clientResponseParser()
* @see RpcTracing#serverResponseParser()
* @since 5.12
*/
// NOTE: gRPC is Java 1.7+, so we cannot add methods to this later
public interface GrpcResponse {
Metadata headers();

Status status();

Metadata trailers();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package brave.grpc;

import brave.rpc.RpcServerRequest;
import brave.rpc.RpcTracing;
import io.grpc.Metadata;
import io.grpc.Metadata.Key;
import io.grpc.MethodDescriptor;
Expand All @@ -25,27 +24,11 @@
/**
* Allows access gRPC specific aspects of a server request during sampling and parsing.
*
* <p>Here's an example that adds default tags, and if gRPC, the {@linkplain
* MethodDescriptor#getType() method type}:
* <pre>{@code
* Tag<GrpcServerRequest> methodType = new Tag<GrpcServerRequest>("grpc.method_type") {
* protected String parseValue(GrpcServerRequest input, TraceContext context) {
* return input.call().getMethodDescriptor().getType().name();
* }
* };
* rpcTracing = rpcTracingBuilder.serverResponseParser((res, context, span) -> {
* RpcResponseParser.DEFAULT.parse(res, context, span);
* if (res instanceof GrpcServerRequest) {
* methodType.tag((GrpcServerRequest) res, span);
* }
* }).build();
* }</pre>
*
* @see GrpcServerResponse
* @see RpcTracing#serverRequestParser()
* @see GrpcRequest for a parsing example
* @since 5.12
*/
public class GrpcServerRequest extends RpcServerRequest {
public class GrpcServerRequest extends RpcServerRequest implements GrpcRequest {
final Map<String, Key<String>> nameToKey;
final ServerCall<?, ?> call;
final Metadata headers;
Expand Down Expand Up @@ -83,12 +66,21 @@ public class GrpcServerRequest extends RpcServerRequest {
return call;
}

/**
* Returns {@linkplain ServerCall#getMethodDescriptor()}} from the {@link #call()}.
*
* @since 5.12
*/
@Override public MethodDescriptor<?, ?> methodDescriptor() {
return call.getMethodDescriptor();
}

/**
* Returns the {@linkplain Metadata headers} passed to {@link ServerInterceptor#interceptCall}.
*
* @since 5.12
*/
public Metadata headers() {
@Override public Metadata headers() {
return headers;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,30 @@

import brave.internal.Nullable;
import brave.rpc.RpcServerResponse;
import brave.rpc.RpcTracing;
import io.grpc.Metadata;
import io.grpc.ServerCall;
import io.grpc.Status;

/**
* Allows access gRPC specific aspects of a client response for parsing.
* Allows access gRPC specific aspects of a server response for parsing.
*
* @see GrpcServerRequest
* @see RpcTracing#serverResponseParser()
* @see GrpcResponse for a parsing example
* @since 5.12
*/
public final class GrpcServerResponse extends RpcServerResponse {
public final class GrpcServerResponse extends RpcServerResponse implements GrpcResponse {
final GrpcServerRequest request;
final Metadata headers;
final Status status;
final Metadata trailers;

GrpcServerResponse(GrpcServerRequest request, Status status, Metadata trailers) {
GrpcServerResponse(GrpcServerRequest request, Metadata headers, Status status,
Metadata trailers) {
if (request == null) throw new NullPointerException("request == null");
if (headers == null) throw new NullPointerException("headers == null");
if (status == null) throw new NullPointerException("status == null");
if (trailers == null) throw new NullPointerException("trailers == null");
this.headers = headers;
this.request = request;
this.status = status;
this.trailers = trailers;
Expand Down Expand Up @@ -65,11 +68,20 @@ public final class GrpcServerResponse extends RpcServerResponse {
}

/**
* Returns the status passed to{@link ServerCall#close(Status, Metadata)}.
* Returns a copy of headers passed to {@link ServerCall#sendHeaders(Metadata)}.
*
* @since 5.12
*/
public Status status() {
@Override public Metadata headers() {
return headers;
}

/**
* Returns the status passed to {@link ServerCall#close(Status, Metadata)}.
*
* @since 5.12
*/
@Override public Status status() {
return status;
}

Expand All @@ -78,7 +90,7 @@ public Status status() {
*
* @since 5.12
*/
public Metadata trailers() {
@Override public Metadata trailers() {
return trailers;
}
}
Loading

0 comments on commit 1959291

Please sign in to comment.