From 765b15e026f78e0f9c4b21357b41654cfd7c5149 Mon Sep 17 00:00:00 2001 From: ascrutae Date: Sat, 11 Mar 2017 16:14:13 +0800 Subject: [PATCH] fix tomcat span without peer host and peer port issue --- .../plugin/dubbo/DubboInterceptor.java | 4 +-- .../plugin/tomcat78x/TomcatInterceptor.java | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/skywalking-sniffer/skywalking-sdk-plugin/dubbo-plugin/src/main/java/com/a/eye/skywalking/plugin/dubbo/DubboInterceptor.java b/skywalking-sniffer/skywalking-sdk-plugin/dubbo-plugin/src/main/java/com/a/eye/skywalking/plugin/dubbo/DubboInterceptor.java index 1ce09301f..c070bf887 100644 --- a/skywalking-sniffer/skywalking-sdk-plugin/dubbo-plugin/src/main/java/com/a/eye/skywalking/plugin/dubbo/DubboInterceptor.java +++ b/skywalking-sniffer/skywalking-sdk-plugin/dubbo-plugin/src/main/java/com/a/eye/skywalking/plugin/dubbo/DubboInterceptor.java @@ -55,10 +55,10 @@ public class DubboInterceptor implements InstanceMethodsAroundInterceptor { Tags.URL.set(span, generateRequestURL(requestURL, invocation)); Tags.COMPONENT.set(span, DUBBO_COMPONENT); Tags.SPAN_LAYER.asRPCFramework(span); + Tags.PEER_HOST.set(span, requestURL.getHost()); + Tags.PEER_PORT.set(span, requestURL.getPort()); if (isConsumer) { - Tags.PEER_HOST.set(span, requestURL.getHost()); - Tags.PEER_PORT.set(span, requestURL.getPort()); Tags.SPAN_KIND.set(span, Tags.SPAN_KIND_CLIENT); ContextCarrier contextCarrier = new ContextCarrier(); ContextManager.INSTANCE.inject(contextCarrier); diff --git a/skywalking-sniffer/skywalking-sdk-plugin/tomcat-7.x-8.x-plugin/src/main/java/com/a/eye/skywalking/plugin/tomcat78x/TomcatInterceptor.java b/skywalking-sniffer/skywalking-sdk-plugin/tomcat-7.x-8.x-plugin/src/main/java/com/a/eye/skywalking/plugin/tomcat78x/TomcatInterceptor.java index 07f518113..63592cfcd 100644 --- a/skywalking-sniffer/skywalking-sdk-plugin/tomcat-7.x-8.x-plugin/src/main/java/com/a/eye/skywalking/plugin/tomcat78x/TomcatInterceptor.java +++ b/skywalking-sniffer/skywalking-sdk-plugin/tomcat-7.x-8.x-plugin/src/main/java/com/a/eye/skywalking/plugin/tomcat78x/TomcatInterceptor.java @@ -43,6 +43,8 @@ public class TomcatInterceptor implements InstanceMethodsAroundInterceptor { Span span = ContextManager.INSTANCE.createSpan(request.getRequestURI()); Tags.COMPONENT.set(span, TOMCAT_COMPONENT); + Tags.PEER_HOST.set(span, fetchRequestPeerHost(request)); + Tags.PEER_PORT.set(span, request.getRemotePort()); Tags.SPAN_KIND.set(span, Tags.SPAN_KIND_SERVER); Tags.URL.set(span, request.getRequestURL().toString()); Tags.SPAN_LAYER.asHttp(span); @@ -76,4 +78,29 @@ public class TomcatInterceptor implements InstanceMethodsAroundInterceptor { Tags.ERROR.set(span, true); } + /** + * + * @param request + * @return + */ + public String fetchRequestPeerHost(HttpServletRequest request) { + String ip = request.getHeader("X-Forwarded-For"); + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("Proxy-Client-IP"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("WL-Proxy-Client-IP"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("HTTP_CLIENT_IP"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("HTTP_X_FORWARDED_FOR"); + } + if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + ip = request.getRemoteAddr(); + } + return ip; + } + }