diff --git a/joylive-plugin/joylive-router/joylive-router-grpc/src/main/java/com/jd/live/agent/plugin/router/gprc/request/GrpcRequest.java b/joylive-plugin/joylive-router/joylive-router-grpc/src/main/java/com/jd/live/agent/plugin/router/gprc/request/GrpcRequest.java index 6e3776e72..4eeb1fce5 100644 --- a/joylive-plugin/joylive-router/joylive-router-grpc/src/main/java/com/jd/live/agent/plugin/router/gprc/request/GrpcRequest.java +++ b/joylive-plugin/joylive-router/joylive-router-grpc/src/main/java/com/jd/live/agent/plugin/router/gprc/request/GrpcRequest.java @@ -31,6 +31,9 @@ import java.net.InetSocketAddress; import java.net.SocketAddress; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; /** * An interface representing a gRPC request. @@ -81,6 +84,21 @@ public void setHeader(String key, String value) { request.setHeader(key, value); } + @Override + public String getHeader(String key) { + return request.getHeaders().get(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER)); + } + + @Override + public List getHeaders(String key) { + Iterable iterable = request.getHeaders().getAll(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER)); + if (iterable == null) { + return null; + } + return StreamSupport.stream(iterable.spliterator(), false) + .collect(Collectors.toList()); + } + @SuppressWarnings("unchecked") @Override public E getEndpoint() { diff --git a/joylive-plugin/joylive-transmission/joylive-transmission-grpc/src/main/java/com/jd/live/agent/plugin/transmission/grpc/interceptor/ClientCallImplInterceptor.java b/joylive-plugin/joylive-transmission/joylive-transmission-grpc/src/main/java/com/jd/live/agent/plugin/transmission/grpc/interceptor/ClientCallImplInterceptor.java index c843c953c..35d27902a 100644 --- a/joylive-plugin/joylive-transmission/joylive-transmission-grpc/src/main/java/com/jd/live/agent/plugin/transmission/grpc/interceptor/ClientCallImplInterceptor.java +++ b/joylive-plugin/joylive-transmission/joylive-transmission-grpc/src/main/java/com/jd/live/agent/plugin/transmission/grpc/interceptor/ClientCallImplInterceptor.java @@ -28,7 +28,21 @@ public void onEnter(ExecutableContext ctx) { } private void attachTag(Metadata metadata) { - RequestContext.cargos(tag -> metadata.put(Metadata.Key.of(tag.getKey(), Metadata.ASCII_STRING_MARSHALLER), tag.getValue())); + RequestContext.cargos(tag -> { + // If the tag key and value already exists in the metadata, do not add it again. + if (metadata.containsKey(Metadata.Key.of(tag.getKey(), Metadata.ASCII_STRING_MARSHALLER))) { + Iterable iterable = metadata.getAll(Metadata.Key.of(tag.getKey(), Metadata.ASCII_STRING_MARSHALLER)); + if (iterable != null) { + for (String value : iterable) { + if (value.equals(tag.getValue())) { + return; + } + } + } + } + metadata.put(Metadata.Key.of(tag.getKey(), Metadata.ASCII_STRING_MARSHALLER), tag.getValue()); + } + ); } }