Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

report spans to SkyWalking #449

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<module>sofa-tracer-plugins/sofa-tracer-okhttp-plugin</module>
<module>sofa-tracer-plugins/sofa-tracer-datasource-plugin</module>
<module>sofa-tracer-plugins/sofa-tracer-zipkin-plugin</module>
<module>sofa-tracer-plugins/sofa-tracer-skywalking-plugin</module>
<module>sofa-tracer-plugins/sofa-tracer-dubbo-plugin</module>
<module>sofa-tracer-plugins/sofa-tracer-spring-cloud-plugin</module>
<module>sofa-tracer-plugins/sofa-tracer-resttmplate-plugin</module>
Expand All @@ -40,6 +41,8 @@
<module>tracer-test/logback-test</module>
<module>tracer-test/log4j2-test</module>
<module>tracer-test/log4j-test</module>


</modules>

<properties>
Expand Down Expand Up @@ -130,6 +133,11 @@
<artifactId>sofa-tracer-zipkin-plugin</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-tracer-skywalking-plugin</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-tracer-spring-cloud-plugin</artifactId>
Expand Down Expand Up @@ -233,13 +241,25 @@
<version>1.3.1</version>
</dependency>


<!-- 对外部框架的一些插件使用 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>

<!-- HttpClient Dependency -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
Expand Down Expand Up @@ -294,11 +314,7 @@
<version>${jmh.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>

</dependencies>
</dependencyManagement>
<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@
import com.alipay.common.tracer.core.span.CommonSpanTags;
import com.alipay.common.tracer.core.span.LogData;
import com.alipay.common.tracer.core.span.SofaTracerSpan;
import com.alipay.common.tracer.core.utils.NetUtils;
import com.alipay.common.tracer.core.utils.StringUtils;
import com.alipay.sofa.common.code.LogCode2Description;
import com.alipay.sofa.tracer.plugins.dubbo.constants.AttachmentKeyConstants;
import com.alipay.sofa.tracer.plugins.dubbo.tracer.DubboConsumerSofaTracer;
import com.alipay.sofa.tracer.plugins.dubbo.tracer.DubboProviderSofaTracer;
import io.opentracing.tag.Tags;

import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -119,6 +121,15 @@ private Result doClientFilter(RpcContext rpcContext, Invoker<?> invoker, Invocat
+ methodName);
// set tags to span
appendRpcClientSpanTags(invoker, sofaTracerSpan);
InetAddress address = NetUtils.getLocalAddress();
String local_app = rpcContext.getUrl().getParameter(Constants.APPLICATION_KEY);
String instance = local_app + "@" + address.getHostAddress();
SofaTracerSpanContext parentSpanContext = sofaTracerSpan.getParentSofaTracerSpan()
.getSofaTracerSpanContext();
sofaTracerSpan.getSofaTracerSpanContext().setParentParams(parentSpanContext.getService(),
parentSpanContext.getServiceInstance(), parentSpanContext.getOperationName());
sofaTracerSpan.getSofaTracerSpanContext().setParams(local_app, instance,
service + "#" + methodName);
// do serialized and then transparent transmission to the rpc server
String serializedSpanContext = sofaTracerSpan.getSofaTracerSpanContext()
.serializeSpanContext();
Expand Down Expand Up @@ -269,6 +280,17 @@ private Result doServerFilter(Invoker<?> invoker, Invocation invocation) {
}
SofaTracerSpan sofaTracerSpan = serverReceived(invocation);
appendRpcServerSpanTags(invoker, sofaTracerSpan);
RpcContext rpcContext = RpcContext.getContext();
String service = invoker.getInterface().getName();
String methodName = rpcContext.getMethodName();
InetAddress address = NetUtils.getLocalAddress();
String local_app = rpcContext.getUrl().getParameter(Constants.APPLICATION_KEY);
String instance = local_app + "@" + address.getHostAddress();
SofaTracerSpanContext spanContext = sofaTracerSpan.getSofaTracerSpanContext();
spanContext.setParentParams(spanContext.getService(), spanContext.getServiceInstance(),
spanContext.getOperationName());
sofaTracerSpan.getSofaTracerSpanContext().setParams(local_app, instance,
service + "#" + methodName);
Result result;
Throwable exception = null;
try {
Expand Down Expand Up @@ -423,6 +445,7 @@ private void appendRpcServerSpanTags(Invoker<?> invoker, SofaTracerSpan sofaTrac
tagsStr.put(CommonSpanTags.METHOD, methodName == null ? BLANK : methodName);
String app = rpcContext.getUrl().getParameter(Constants.APPLICATION_KEY);
tagsStr.put(CommonSpanTags.REMOTE_HOST, rpcContext.getRemoteHost());
tagsStr.put(CommonSpanTags.REMOTE_PORT, String.valueOf(rpcContext.getRemotePort()));
tagsStr.put(CommonSpanTags.LOCAL_APP, app == null ? BLANK : app);
tagsStr.put(CommonSpanTags.CURRENT_THREAD_NAME, Thread.currentThread().getName());
String protocol = rpcContext.getUrl().getProtocol();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@
import com.alipay.common.tracer.core.span.CommonSpanTags;
import com.alipay.common.tracer.core.span.LogData;
import com.alipay.common.tracer.core.span.SofaTracerSpan;
import com.alipay.common.tracer.core.utils.NetUtils;
import com.alipay.common.tracer.core.utils.StringUtils;
import com.alipay.sofa.common.code.LogCode2Description;
import com.alipay.sofa.tracer.plugins.dubbo.constants.AttachmentKeyConstants;
import com.alipay.sofa.tracer.plugins.dubbo.tracer.DubboConsumerSofaTracer;
import com.alipay.sofa.tracer.plugins.dubbo.tracer.DubboProviderSofaTracer;
import io.opentracing.tag.Tags;

import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -118,6 +120,15 @@ private Result doClientFilter(RpcContext rpcContext, Invoker<?> invoker, Invocat
+ methodName);
// set tags to span
appendRpcClientSpanTags(invoker, sofaTracerSpan);
InetAddress address = NetUtils.getLocalAddress();
String local_app = rpcContext.getUrl().getParameter(Constants.APPLICATION_KEY);
String instance = local_app + "@" + address.getHostAddress();
sofaTracerSpan.getSofaTracerSpanContext().setParams(local_app, instance,
service + "#" + methodName);
SofaTracerSpanContext parentSpanContext = sofaTracerSpan.getParentSofaTracerSpan()
.getSofaTracerSpanContext();
sofaTracerSpan.getSofaTracerSpanContext().setParentParams(parentSpanContext.getService(),
parentSpanContext.getServiceInstance(), parentSpanContext.getOperationName());
// do serialized and then transparent transmission to the rpc server
String serializedSpanContext = sofaTracerSpan.getSofaTracerSpanContext()
.serializeSpanContext();
Expand Down Expand Up @@ -273,6 +284,16 @@ private Result doServerFilter(Invoker<?> invoker, Invocation invocation) {
}
SofaTracerSpan sofaTracerSpan = serverReceived(invocation);
appendRpcServerSpanTags(invoker, sofaTracerSpan);
org.apache.dubbo.rpc.RpcContext rpcContext = org.apache.dubbo.rpc.RpcContext.getContext();
String service = invoker.getInterface().getName();
String methodName = rpcContext.getMethodName();
InetAddress address = NetUtils.getLocalAddress();
String local_app = rpcContext.getUrl().getParameter(Constants.APPLICATION_KEY);
String instance = local_app + "@" + address.getHostAddress();
SofaTracerSpanContext spanContext = sofaTracerSpan.getSofaTracerSpanContext();
spanContext.setParentParams(spanContext.getService(), spanContext.getServiceInstance(),
spanContext.getOperationName());
spanContext.setParams(local_app, instance, service + "#" + methodName);
Result result;
Throwable exception = null;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.alipay.common.tracer.core.span.CommonSpanTags;
import com.alipay.common.tracer.core.span.LogData;
import com.alipay.common.tracer.core.span.SofaTracerSpan;
import com.alipay.common.tracer.core.utils.NetUtils;
import com.alipay.common.tracer.core.utils.StringUtils;
import com.alipay.sofa.common.code.LogCode2Description;
import com.alipay.sofa.tracer.plugins.dubbo.constants.AttachmentKeyConstants;
Expand All @@ -43,6 +44,7 @@
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.support.RpcUtils;

import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -153,6 +155,14 @@ private Result doClientFilter(RpcContext rpcContext, Invoker<?> invoker, Invocat
+ methodName);
// set tags to span
appendRpcClientSpanTags(invoker, sofaTracerSpan);

InetAddress address = NetUtils.getLocalAddress();
String local_app =rpcContext.getUrl().getParameter(CommonConstants.APPLICATION_KEY);
String instance = local_app + "@" + address.getHostAddress();
sofaTracerSpan.getSofaTracerSpanContext().setParams(local_app, instance, service + "#" + methodName);
SofaTracerSpanContext parentSpanContext = sofaTracerSpan.getParentSofaTracerSpan().getSofaTracerSpanContext();
sofaTracerSpan.getSofaTracerSpanContext().setParentParams(parentSpanContext.getService(), parentSpanContext.getServiceInstance(),
parentSpanContext.getOperationName());
// do serialized and then transparent transmission to the rpc server
String serializedSpanContext = sofaTracerSpan.getSofaTracerSpanContext()
.serializeSpanContext();
Expand Down Expand Up @@ -247,6 +257,16 @@ private Result doServerFilter(Invoker<?> invoker, Invocation invocation) {
}
SofaTracerSpan sofaTracerSpan = serverReceived(invocation);
appendRpcServerSpanTags(invoker, sofaTracerSpan);
RpcContext rpcContext = RpcContext.getContext();
String service = invoker.getInterface().getName();
String methodName = rpcContext.getMethodName();
InetAddress address = NetUtils.getLocalAddress();
String local_app = rpcContext.getUrl().getParameter(CommonConstants.APPLICATION_KEY);
String instance = local_app + "@" + address.getHostAddress();
SofaTracerSpanContext spanContext = sofaTracerSpan.getSofaTracerSpanContext();
spanContext.setParentParams(spanContext.getService(), spanContext.getServiceInstance(),
spanContext.getOperationName());
spanContext.setParams(local_app, instance, service + "#" + methodName);
Result result;
Throwable exception = null;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.alipay.common.tracer.core.span.CommonSpanTags;
import com.alipay.common.tracer.core.span.LogData;
import com.alipay.common.tracer.core.span.SofaTracerSpan;
import com.alipay.common.tracer.core.utils.NetUtils;
import com.alipay.common.tracer.core.utils.StringUtils;
import com.alipay.sofa.common.code.LogCode2Description;
import com.alipay.sofa.tracer.plugins.dubbo.constants.AttachmentKeyConstants;
Expand All @@ -43,6 +44,7 @@
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.support.RpcUtils;

import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -153,6 +155,13 @@ private Result doClientFilter(RpcContext rpcContext, Invoker<?> invoker, Invocat
+ methodName);
// set tags to span
appendRpcClientSpanTags(invoker, sofaTracerSpan);
InetAddress address = NetUtils.getLocalAddress();
String local_app =rpcContext.getUrl().getParameter(CommonConstants.APPLICATION_KEY);
String instance = local_app + "@" + address.getHostAddress();
sofaTracerSpan.getSofaTracerSpanContext().setParams(local_app, instance, service + "#" + methodName);
SofaTracerSpanContext parentSpanContext = sofaTracerSpan.getParentSofaTracerSpan().getSofaTracerSpanContext();
sofaTracerSpan.getSofaTracerSpanContext().setParentParams(parentSpanContext.getService(), parentSpanContext.getServiceInstance(),
parentSpanContext.getOperationName());
// do serialized and then transparent transmission to the rpc server
String serializedSpanContext = sofaTracerSpan.getSofaTracerSpanContext()
.serializeSpanContext();
Expand Down Expand Up @@ -244,6 +253,17 @@ private Result doServerFilter(Invoker<?> invoker, Invocation invocation) {
}
SofaTracerSpan sofaTracerSpan = serverReceived(invocation);
appendRpcServerSpanTags(invoker, sofaTracerSpan);

RpcContext rpcContext = RpcContext.getContext();
String service = invoker.getInterface().getName();
String methodName = rpcContext.getMethodName();
InetAddress address = NetUtils.getLocalAddress();
String local_app = rpcContext.getUrl().getParameter(CommonConstants.APPLICATION_KEY);
String instance = local_app + "@" + address.getHostAddress();
SofaTracerSpanContext spanContext = sofaTracerSpan.getSofaTracerSpanContext();
spanContext.setParentParams(spanContext.getService(), spanContext.getServiceInstance(),
spanContext.getOperationName());
spanContext.setParams(local_app, instance, service + "#" + methodName);
Result result;
Throwable exception = null;
try {
Expand Down Expand Up @@ -380,12 +400,14 @@ private void appendRpcServerSpanTags(Invoker<?> invoker, SofaTracerSpan sofaTrac
tagsStr.put(CommonSpanTags.METHOD, methodName == null ? BLANK : methodName);
String app = rpcContext.getUrl().getParameter(CommonConstants.APPLICATION_KEY);
tagsStr.put(CommonSpanTags.REMOTE_HOST, rpcContext.getRemoteHost());
tagsStr.put(CommonSpanTags.REMOTE_PORT, String.valueOf(rpcContext.getRemotePort()));
tagsStr.put(CommonSpanTags.LOCAL_APP, app == null ? BLANK : app);
tagsStr.put(CommonSpanTags.CURRENT_THREAD_NAME, Thread.currentThread().getName());
String protocol = rpcContext.getUrl().getProtocol();
tagsStr.put(CommonSpanTags.PROTOCOL, protocol == null ? BLANK : protocol);
tagsStr.put(CommonSpanTags.LOCAL_HOST, rpcContext.getLocalHost());
tagsStr.put(CommonSpanTags.LOCAL_PORT, String.valueOf(rpcContext.getLocalPort()));

}

private void appendRpcClientSpanTags(Invoker<?> invoker, SofaTracerSpan sofaTracerSpan) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
package com.alipay.sofa.tracer.plugins.httpclient.interceptor;

import com.alipay.common.tracer.core.SofaTracer;
import com.alipay.common.tracer.core.appender.self.SelfLog;
import com.alipay.common.tracer.core.configuration.SofaTracerConfiguration;
import com.alipay.common.tracer.core.registry.ExtendFormat;
import com.alipay.common.tracer.core.span.CommonSpanTags;
import com.alipay.common.tracer.core.span.SofaTracerSpan;
import com.alipay.common.tracer.core.tracer.AbstractTracer;
import com.alipay.common.tracer.core.utils.NetUtils;
import com.alipay.common.tracer.core.utils.StringUtils;
import com.alipay.sofa.tracer.plugins.httpclient.HttpClientRequestCarrier;
import org.apache.http.HttpEntity;
Expand All @@ -31,6 +33,10 @@
import org.apache.http.RequestLine;
import org.apache.http.client.methods.HttpRequestWrapper;

import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;

/**
* AbstractHttpRequestInterceptor
*
Expand Down Expand Up @@ -76,8 +82,20 @@ public void appendHttpClientRequestSpanTags(HttpRequest httpRequest,
HttpRequestWrapper httpRequestWrapper = (HttpRequestWrapper) httpRequest;
httpClientSpan.setTag(CommonSpanTags.REQUEST_URL, httpRequestWrapper.getOriginal()
.getRequestLine().getUri());
httpClientSpan.setTag(CommonSpanTags.REMOTE_HOST, httpRequestWrapper.getTarget()
.getAddress().getHostAddress());
httpClientSpan.setTag(CommonSpanTags.REMOTE_PORT,
String.valueOf(httpRequestWrapper.getTarget().getPort()));
httpClientSpan.getSofaTracerSpanContext().setPeer(
httpRequestWrapper.getTarget().getAddress().getHostAddress() + ":"
+ httpRequestWrapper.getTarget().getPort());
} else {
httpClientSpan.setTag(CommonSpanTags.REQUEST_URL, requestLine.getUri());
String[] remoteHostAndPort = parseRemoteHostAndPort(requestLine.getUri());
httpClientSpan.setTag(CommonSpanTags.REMOTE_HOST, remoteHostAndPort[0]);
httpClientSpan.setTag(CommonSpanTags.REMOTE_PORT, remoteHostAndPort[1]);
httpClientSpan.getSofaTracerSpanContext().setPeer(
remoteHostAndPort[0] + ":" + remoteHostAndPort[1]);
}
//method
httpClientSpan.setTag(CommonSpanTags.METHOD, methodName);
Expand Down Expand Up @@ -109,4 +127,19 @@ public void processHttpClientRequestCarrier(HttpRequest httpRequest, SofaTracerS
sofaTracer.inject(currentSpan.getSofaTracerSpanContext(),
ExtendFormat.Builtin.B3_HTTP_HEADERS, new HttpClientRequestCarrier(httpRequest));
}

private String[] parseRemoteHostAndPort(String url) {
String[] hostWithPort = new String[2];
URL requestUrl = null;
try {
requestUrl = new URL(url);
} catch (MalformedURLException e) {
SelfLog.error("cannot parse remote host and port. request:" + url, e);
}
InetAddress hostAddress = NetUtils.getIpAddress(requestUrl.getHost());
hostWithPort[0] = requestUrl != null ? (hostAddress == null ? requestUrl.getHost()
: hostAddress.getHostAddress()) : "";
hostWithPort[1] = String.valueOf(requestUrl != null ? requestUrl.getPort() : -1);
return hostWithPort;
}
}
Loading