Skip to content

Commit 9018c10

Browse files
authored
add callee ip in RpcContext (#65)
* feat: rename contextWithRemoteIp to contextWithRemoteCallerIp * feat: add callee ip in context before invoke rpc request * feat: add unit test * feat:fix * import issue * feat:add caller/callee port in RpcContext
1 parent acdbb07 commit 9018c10

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

trpc-core/src/main/java/com/tencent/trpc/core/filter/ConsumerInvokerHeadFilter.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Tencent is pleased to support the open source community by making tRPC available.
33
*
4-
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
4+
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
55
* All rights reserved.
66
*
77
* If you have downloaded a copy of the tRPC source code from Tencent,
@@ -20,6 +20,10 @@
2020
import com.tencent.trpc.core.rpc.RequestMeta;
2121
import com.tencent.trpc.core.rpc.Response;
2222
import com.tencent.trpc.core.rpc.RpcContext;
23+
import com.tencent.trpc.core.rpc.RpcContextValueKeys;
24+
import com.tencent.trpc.core.utils.RpcContextUtils;
25+
26+
import java.util.Optional;
2327
import java.util.concurrent.CompletableFuture;
2428
import java.util.concurrent.CompletionStage;
2529

@@ -43,6 +47,7 @@ public CompletionStage<Response> filter(Invoker<?> invoker, Request request) {
4347
ConsumerInvoker consumerInvoker = (ConsumerInvoker) invoker;
4448
// combine with DefClusterInvocationHandler#genRequest to complete the information of the request.
4549
prepareRequestInfoBeforeInvoke(request, consumerInvoker);
50+
contextWithRemoteCalleeAddr(context, request);
4651
startLog(context, request);
4752
CompletableFuture<Response> future = invoker.invoke(request).toCompletableFuture();
4853
if (logger.isDebugEnabled()) {
@@ -60,6 +65,24 @@ public CompletionStage<Response> filter(Invoker<?> invoker, Request request) {
6065
});
6166
}
6267

68+
/**
69+
* Set the request remote callee addr to RpcContext.
70+
* Caller IP: CTX_CALLEE_REMOTE_IP
71+
* Caller PORT: CTX_CALLEE_REMOTE_PORT
72+
*
73+
* @param context RpcContext
74+
* @param request Request
75+
*/
76+
private void contextWithRemoteCalleeAddr(RpcContext context, Request request) {
77+
Optional.ofNullable(request.getMeta().getRemoteAddress()).ifPresent(remoteAddr
78+
-> {
79+
RpcContextUtils.putValueMapValue(context, RpcContextValueKeys.CTX_CALLEE_REMOTE_IP,
80+
remoteAddr.getAddress().getHostAddress());
81+
RpcContextUtils.putValueMapValue(context, RpcContextValueKeys.CTX_CALLEE_REMOTE_PORT,
82+
remoteAddr.getPort());
83+
});
84+
}
85+
6386
private void prepareRequestInfoBeforeInvoke(Request request,
6487
ConsumerInvoker<?> consumerInvoker) {
6588
RequestMeta meta = request.getMeta();

trpc-core/src/main/java/com/tencent/trpc/core/filter/ProviderInvokerHeadFilter.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public int getOrder() {
5252
public CompletionStage<Response> filter(Invoker<?> invoker, Request request) {
5353
RpcServerContext serverContext = (RpcServerContext) (request.getContext());
5454
prepareRequestInfoBeforeInvoke(request, (ProviderInvoker) invoker);
55-
contextWithRemoteIp(serverContext, request);
55+
contextWithRemoteCallerAddr(serverContext, request);
5656
startLog(serverContext, request);
5757
CompletableFuture<Response> future = invoker.invoke(request).toCompletableFuture();
5858
if (logger.isDebugEnabled()) {
@@ -62,15 +62,21 @@ public CompletionStage<Response> filter(Invoker<?> invoker, Request request) {
6262
}
6363

6464
/**
65-
* Set the request remote IP to RpcServerContext, with the key as CTX_CALLER_REMOTE_IP.
65+
* Set the request remote caller addr to RpcServerContext.
66+
* Caller IP: CTX_CALLER_REMOTE_IP
67+
* Caller PORT: CTX_CALLER_REMOTE_PORT
6668
*
6769
* @param serverContext RpcServerContext
6870
* @param request Request
6971
*/
70-
private void contextWithRemoteIp(RpcServerContext serverContext, Request request) {
72+
private void contextWithRemoteCallerAddr(RpcServerContext serverContext, Request request) {
7173
Optional.ofNullable(request.getMeta().getRemoteAddress()).ifPresent(remoteAddr
72-
-> RpcContextUtils.putValueMapValue(serverContext, RpcContextValueKeys.CTX_CALLER_REMOTE_IP,
73-
remoteAddr.getAddress().getHostAddress()));
74+
-> {
75+
RpcContextUtils.putValueMapValue(serverContext, RpcContextValueKeys.CTX_CALLER_REMOTE_IP,
76+
remoteAddr.getAddress().getHostAddress());
77+
RpcContextUtils.putValueMapValue(serverContext, RpcContextValueKeys.CTX_CALLER_REMOTE_PORT,
78+
remoteAddr.getPort());
79+
});
7480

7581
}
7682

trpc-core/src/main/java/com/tencent/trpc/core/rpc/RpcContextValueKeys.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,21 @@ public class RpcContextValueKeys {
2424
*/
2525
public static final String CTX_TELEMETRY_TRACE_SPAN = "ctx_telemetry_trace_span";
2626
/**
27-
* Remote IP.
27+
* Caller Remote IP.
2828
*/
2929
public static final String CTX_CALLER_REMOTE_IP = "ctx_caller_remote_ip";
30+
/**
31+
* Caller Remote PORT.
32+
*/
33+
public static final String CTX_CALLER_REMOTE_PORT = "ctx_caller_remote_port";
34+
/**
35+
* Callee Remote IP.
36+
*/
37+
public static final String CTX_CALLEE_REMOTE_IP = "ctx_callee_remote_ip";
38+
/**
39+
* Callee Remote PORT.
40+
*/
41+
public static final String CTX_CALLEE_REMOTE_PORT = "ctx_callee_remote_port";
3042
/**
3143
* Full link timeout key.
3244
*/

trpc-proto/trpc-proto-standard/src/test/java/com/tencent/trpc/proto/standard/common/TRpcServerTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Tencent is pleased to support the open source community by making tRPC available.
33
*
4-
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
4+
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
55
* All rights reserved.
66
*
77
* If you have downloaded a copy of the tRPC source code from Tencent,
@@ -29,6 +29,7 @@
2929
import com.tencent.trpc.core.exception.TransportException;
3030
import com.tencent.trpc.core.rpc.RpcClient;
3131
import com.tencent.trpc.core.rpc.RpcClientContext;
32+
import com.tencent.trpc.core.rpc.RpcContextValueKeys;
3233
import com.tencent.trpc.core.rpc.RpcServer;
3334
import com.tencent.trpc.core.rpc.RpcServerManager;
3435
import com.tencent.trpc.core.transport.Channel;
@@ -241,6 +242,7 @@ public void serverNormalTest() {
241242
.println(">>>>>>>>>" + new String((byte[]) (context.getRspAttachMap().get("key"))));
242243
assertEquals(new String((byte[]) (context.getRspAttachMap().get("key")), Charsets.UTF_8),
243244
"abc");
245+
assertEquals("127.0.0.1",context.getValueMap().get(RpcContextValueKeys.CTX_CALLEE_REMOTE_IP));
244246
}
245247

246248
@Test

0 commit comments

Comments
 (0)