From ae81663dca6ec19ae73e1047528b9203e99e893f Mon Sep 17 00:00:00 2001 From: Shashank Patidar <74622220+shashank11p@users.noreply.github.com> Date: Fri, 19 Jan 2024 22:02:36 +0530 Subject: [PATCH] add grpc mappings for blocking status code (#399) --- .../v1_6/server/GrpcServerInterceptor.java | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/instrumentation/grpc-1.6/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/grpc/v1_6/server/GrpcServerInterceptor.java b/instrumentation/grpc-1.6/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/grpc/v1_6/server/GrpcServerInterceptor.java index da965e94..f1bc3ef3 100644 --- a/instrumentation/grpc-1.6/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/grpc/v1_6/server/GrpcServerInterceptor.java +++ b/instrumentation/grpc-1.6/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/grpc/v1_6/server/GrpcServerInterceptor.java @@ -61,9 +61,9 @@ public ServerCall.Listener interceptCall( FilterResult filterResult = FilterRegistry.getFilter().evaluateRequestHeaders(currentSpan, mapHeaders); if (filterResult.shouldBlock()) { + // map http codes with grpc codes // We cannot send custom message in grpc calls - // TODO: map http codes with grpc codes. filterResult.getBlockingStatusCode() - call.close(Status.PERMISSION_DENIED, new Metadata()); + call.close(mapHttpToGrpcStatus(filterResult.getBlockingStatusCode()), new Metadata()); @SuppressWarnings("unchecked") ServerCall.Listener noop = NoopServerCallListener.INSTANCE; return noop; @@ -78,6 +78,32 @@ public ServerCall.Listener interceptCall( } } + /** + * Mapping according to https://github.com/grpc/grpc/blob/master/doc/http-grpc-status-mapping.md + */ + private static Status mapHttpToGrpcStatus(int httpStatus) { + switch (httpStatus) { + case 400: + return Status.INTERNAL; + case 401: + return Status.UNAUTHENTICATED; + case 403: + return Status.PERMISSION_DENIED; + case 404: + return Status.UNIMPLEMENTED; + case 429: + return Status.UNAVAILABLE; + case 502: + return Status.UNAVAILABLE; + case 503: + return Status.UNAVAILABLE; + case 504: + return Status.UNAVAILABLE; + default: + return Status.UNKNOWN; + } + } + static final class TracingServerCall extends ForwardingServerCall.SimpleForwardingServerCall {