From 974d2841740a9693f7bb5617c26a7c1510a8f999 Mon Sep 17 00:00:00 2001 From: zzuzh Date: Thu, 8 Jul 2021 17:19:26 +0800 Subject: [PATCH 1/2] report spans to SkyWalking (#443) --- pom.xml | 26 +- .../plugins/dubbo/DubboSofaTracerFilter.java | 1 + .../plugins/dubbo/DubboSofaTracerFilter.java | 1 + .../sofa-tracer-skywalking-plugin/pom.xml | 97 ++++++ .../SkywalkingSpanRemoteReporter.java | 67 ++++ .../adapter/SkywalkingSegmentAdapter.java | 295 ++++++++++++++++++ .../SkywalkingReportRegisterBean.java | 57 ++++ .../skywalking/model/KeyStringValuePair.java | 38 +++ .../tracer/plugins/skywalking/model/Log.java | 38 +++ .../plugins/skywalking/model/RefType.java | 30 ++ .../plugins/skywalking/model/Segment.java | 49 +++ .../skywalking/model/SegmentReference.java | 43 +++ .../tracer/plugins/skywalking/model/Span.java | 64 ++++ .../plugins/skywalking/model/SpanLayer.java | 37 +++ .../plugins/skywalking/model/SpanType.java | 30 ++ .../properties/SkywalkingProperties.java | 28 ++ .../skywalking/reporter/AsyncReporter.java | 244 +++++++++++++++ .../plugins/skywalking/reporter/Message.java | 121 +++++++ .../sender/SkywalkingRestTemplateSender.java | 49 +++ .../utils/ComponentName2ComponentId.java | 61 ++++ .../utils/ComponentName2SpanLayer.java | 52 +++ .../SkywalkingReportRegisterBeanTest.java | 42 +++ .../SkywalkingRestTemplateSenderTest.java | 67 ++++ .../java/SkywalkingSegmentAdapterTest.java | 81 +++++ .../SkywalkingSpanRemoteReporterTest.java | 72 +++++ .../src/test/resources/sofa.tracer.properties | 3 + .../src/test/resources/spring-bean.xml | 15 + .../zipkin/adapter/ZipkinV2SpanAdapter.java | 1 + .../alipay/common/tracer/core/SofaTracer.java | 1 - .../constants/ComponentNameConstants.java | 8 + .../core/constants/SofaTracerConstant.java | 4 + .../context/span/SofaTracerSpanContext.java | 1 + .../tracer/core/span/CommonSpanTags.java | 4 +- .../tracer/core/tracer/AbstractTracer.java | 4 +- .../common/tracer/core/utils}/NetUtils.java | 5 +- tracer-sofa-boot-starter/pom.xml | 8 + ...SkywalkingSofaTracerAutoConfiguration.java | 48 +++ .../SkywalkingSofaTracerProperties.java | 57 ++++ .../main/resources/META-INF/spring.factories | 1 + 39 files changed, 1839 insertions(+), 11 deletions(-) create mode 100644 sofa-tracer-plugins/sofa-tracer-skywalking-plugin/pom.xml create mode 100644 sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/SkywalkingSpanRemoteReporter.java create mode 100644 sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/adapter/SkywalkingSegmentAdapter.java create mode 100644 sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/initialize/SkywalkingReportRegisterBean.java create mode 100644 sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/KeyStringValuePair.java create mode 100644 sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/Log.java create mode 100644 sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/RefType.java create mode 100644 sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/Segment.java create mode 100644 sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/SegmentReference.java create mode 100644 sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/Span.java create mode 100644 sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/SpanLayer.java create mode 100644 sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/SpanType.java create mode 100644 sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/properties/SkywalkingProperties.java create mode 100644 sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/reporter/AsyncReporter.java create mode 100644 sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/reporter/Message.java create mode 100644 sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/sender/SkywalkingRestTemplateSender.java create mode 100644 sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/utils/ComponentName2ComponentId.java create mode 100644 sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/utils/ComponentName2SpanLayer.java create mode 100644 sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/test/java/SkywalkingReportRegisterBeanTest.java create mode 100644 sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/test/java/SkywalkingRestTemplateSenderTest.java create mode 100644 sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/test/java/SkywalkingSegmentAdapterTest.java create mode 100644 sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/test/java/SkywalkingSpanRemoteReporterTest.java create mode 100644 sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/test/resources/sofa.tracer.properties create mode 100644 sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/test/resources/spring-bean.xml rename {sofa-tracer-plugins/sofa-tracer-zipkin-plugin/src/main/java/com/alipay/sofa/tracer/plugins/zipkin/adapter => tracer-core/src/main/java/com/alipay/common/tracer/core/utils}/NetUtils.java (97%) create mode 100644 tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/skywalking/configuration/SkywalkingSofaTracerAutoConfiguration.java create mode 100644 tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/skywalking/properties/SkywalkingSofaTracerProperties.java diff --git a/pom.xml b/pom.xml index 9764545bf..1fdefc518 100644 --- a/pom.xml +++ b/pom.xml @@ -22,6 +22,7 @@ sofa-tracer-plugins/sofa-tracer-okhttp-plugin sofa-tracer-plugins/sofa-tracer-datasource-plugin sofa-tracer-plugins/sofa-tracer-zipkin-plugin + sofa-tracer-plugins/sofa-tracer-skywalking-plugin sofa-tracer-plugins/sofa-tracer-dubbo-plugin sofa-tracer-plugins/sofa-tracer-spring-cloud-plugin sofa-tracer-plugins/sofa-tracer-resttmplate-plugin @@ -40,6 +41,8 @@ tracer-test/logback-test tracer-test/log4j2-test tracer-test/log4j-test + + @@ -130,6 +133,11 @@ sofa-tracer-zipkin-plugin ${project.version} + + com.alipay.sofa + sofa-tracer-skywalking-plugin + ${project.version} + com.alipay.sofa sofa-tracer-spring-cloud-plugin @@ -233,6 +241,7 @@ 1.3.1 + javax.servlet @@ -240,6 +249,17 @@ 3.1.0 provided + + org.projectlombok + lombok + 1.16.10 + + + com.alibaba + fastjson + 1.2.51 + + org.apache.httpcomponents @@ -294,11 +314,7 @@ ${jmh.version} test - - com.alibaba - fastjson - 1.2.51 - + diff --git a/sofa-tracer-plugins/sofa-tracer-dubbo-2.6.x-plugin/src/main/java/com/alipay/sofa/tracer/plugins/dubbo/DubboSofaTracerFilter.java b/sofa-tracer-plugins/sofa-tracer-dubbo-2.6.x-plugin/src/main/java/com/alipay/sofa/tracer/plugins/dubbo/DubboSofaTracerFilter.java index 28c223f60..d72fd076c 100644 --- a/sofa-tracer-plugins/sofa-tracer-dubbo-2.6.x-plugin/src/main/java/com/alipay/sofa/tracer/plugins/dubbo/DubboSofaTracerFilter.java +++ b/sofa-tracer-plugins/sofa-tracer-dubbo-2.6.x-plugin/src/main/java/com/alipay/sofa/tracer/plugins/dubbo/DubboSofaTracerFilter.java @@ -423,6 +423,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(); diff --git a/sofa-tracer-plugins/sofa-tracer-dubbo-plugin/src/main/java/com/alipay/sofa/tracer/plugins/dubbo/DubboSofaTracerFilter.java b/sofa-tracer-plugins/sofa-tracer-dubbo-plugin/src/main/java/com/alipay/sofa/tracer/plugins/dubbo/DubboSofaTracerFilter.java index cb9d76fb9..e97f93aa5 100644 --- a/sofa-tracer-plugins/sofa-tracer-dubbo-plugin/src/main/java/com/alipay/sofa/tracer/plugins/dubbo/DubboSofaTracerFilter.java +++ b/sofa-tracer-plugins/sofa-tracer-dubbo-plugin/src/main/java/com/alipay/sofa/tracer/plugins/dubbo/DubboSofaTracerFilter.java @@ -380,6 +380,7 @@ 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(); diff --git a/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/pom.xml b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/pom.xml new file mode 100644 index 000000000..42b21a8ac --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/pom.xml @@ -0,0 +1,97 @@ + + + + tracer-all-parent + com.alipay.sofa + 3.1.1 + ../../pom.xml + + 4.0.0 + + sofa-tracer-skywalking-plugin + + + 8 + 8 + + + + org.springframework + spring-beans + + + org.springframework + spring-web + + + com.alipay.sofa + tracer-core + + + com.alipay.sofa + tracer-extensions + + + org.springframework + spring-context + + + + org.jmockit + jmockit + test + + + org.mockito + mockito-core + test + + + org.jmockit + jmockit-coverage + test + + + junit + junit + test + + + commons-io + commons-io + test + + + org.springframework.boot + spring-boot-starter-web + test + + + com.alibaba + fastjson + test + + + org.springframework.boot + spring-boot-starter-test + test + + + io.micrometer + micrometer-core + 1.1.1 + compile + + + org.projectlombok + lombok + + + com.alibaba + fastjson + + + + \ No newline at end of file diff --git a/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/SkywalkingSpanRemoteReporter.java b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/SkywalkingSpanRemoteReporter.java new file mode 100644 index 000000000..1f46bde73 --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/SkywalkingSpanRemoteReporter.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.tracer.plugins.skywalking; + +import com.alibaba.fastjson.JSON; +import com.alipay.common.tracer.core.listener.SpanReportListener; +import com.alipay.common.tracer.core.span.SofaTracerSpan; +import com.alipay.sofa.tracer.plugins.skywalking.adapter.SkywalkingSegmentAdapter; +import com.alipay.sofa.tracer.plugins.skywalking.reporter.AsyncReporter; +import com.alipay.sofa.tracer.plugins.skywalking.sender.SkywalkingRestTemplateSender; +import com.alipay.sofa.tracer.plugins.skywalking.model.Segment; + +import org.springframework.web.client.RestTemplate; + +import java.io.Closeable; +import java.io.Flushable; +import java.io.IOException; + +/** + * SkywalkingSpanRemoteReporter + * @author zhaochen + */ +public class SkywalkingSpanRemoteReporter implements SpanReportListener, Closeable, Flushable { + private AsyncReporter reporter; + private SkywalkingRestTemplateSender sender; + private SkywalkingSegmentAdapter adapter; + + public SkywalkingSpanRemoteReporter(String baseUrl, int maxBufferSize) { + adapter = new SkywalkingSegmentAdapter(); + sender = new SkywalkingRestTemplateSender(new RestTemplate(), baseUrl); + reporter = new AsyncReporter(maxBufferSize, sender); + } + + @Override + public void onSpanReport(SofaTracerSpan sofaTracerSpan) { + if (sofaTracerSpan == null || !sofaTracerSpan.getSofaTracerSpanContext().isSampled()) { + return; + } + Segment segment = adapter.convertToSkywalkingSegment(sofaTracerSpan); + int segmentSize = JSON.toJSONString(segment).length(); + reporter.report(segment, segmentSize); + } + + @Override + public void close() throws IOException { + reporter.close(); + } + + @Override + public void flush() throws IOException { + reporter.flush(); + } +} diff --git a/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/adapter/SkywalkingSegmentAdapter.java b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/adapter/SkywalkingSegmentAdapter.java new file mode 100644 index 000000000..68b00e694 --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/adapter/SkywalkingSegmentAdapter.java @@ -0,0 +1,295 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.tracer.plugins.skywalking.adapter; + +import com.alipay.common.tracer.core.constants.ComponentNameConstants; +import com.alipay.common.tracer.core.constants.SofaTracerConstant; +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.tracer.plugins.skywalking.model.Segment; +import com.alipay.sofa.tracer.plugins.skywalking.model.Span; +import com.alipay.sofa.tracer.plugins.skywalking.model.SpanType; +import com.alipay.sofa.tracer.plugins.skywalking.model.SpanLayer; +import com.alipay.sofa.tracer.plugins.skywalking.model.SegmentReference; +import com.alipay.sofa.tracer.plugins.skywalking.model.Log; +import com.alipay.sofa.tracer.plugins.skywalking.model.RefType; +import com.alipay.sofa.tracer.plugins.skywalking.utils.ComponentName2ComponentId; +import com.alipay.sofa.tracer.plugins.skywalking.utils.ComponentName2SpanLayer; + +import java.net.InetAddress; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * SkywalkingSegmentAdapter + * @author zhaochen + */ + +public class SkywalkingSegmentAdapter { + /** + * convert sofaTracerSpan to segment in Skywalking + * @param sofaTracerSpan + * @return the segment in Skywalking + */ + public Segment convertToSkywalkingSegment(SofaTracerSpan sofaTracerSpan) { + if (sofaTracerSpan == null) { + return null; + } + Segment segment = new Segment(); + segment.setTraceSegmentId(generateSegmentId(sofaTracerSpan)); + segment.setTraceId(sofaTracerSpan.getSofaTracerSpanContext().getTraceId()); + segment.setSizeLimited(false); + segment.setService(constructServiceName(sofaTracerSpan)); + segment.setServiceInstance(constructServiceInstanceName(sofaTracerSpan)); + segment.addSpan(constructSpan(sofaTracerSpan)); + return segment; + } + + /** + * generate segmentId traceId + FNV64HashCode(SpanId) + 0/1 + * the client and server span generate by dubbo or sofaRpc share the same traceId and spanId, + * so we need to append 0(server),1(client) to the end of segmentId. + * @param sofaTracerSpan + * @return segmentId + */ + private String generateSegmentId(SofaTracerSpan sofaTracerSpan) { + return sofaTracerSpan.getSofaTracerSpanContext().getTraceId() + + FNV64HashCode(sofaTracerSpan.getSofaTracerSpanContext().getSpanId()) + + (sofaTracerSpan.isServer() ? SofaTracerConstant.SERVER : SofaTracerConstant.CLIENT); + } + + /** + * construct EntrySpan or ExitSpan determined by the type of sofaTracerSpan + * @param sofaTracerSpan + * @return EntrySpan or ExitSpan + */ + private Span constructSpan(SofaTracerSpan sofaTracerSpan) { + Span span = new Span(); + // only have one span every segment + span.setSpanId(0); + span.setParentSpanId(-1); + span.setStartTime(sofaTracerSpan.getStartTime()); + span.setEndTime(sofaTracerSpan.getEndTime()); + span.setOperationName(sofaTracerSpan.getOperationName()); + if (sofaTracerSpan.isServer()) { + span.setSpanType(SpanType.Entry); + } else { + span.setSpanType(SpanType.Exit); + } + //map tracerType in sofaTracer to SpanLayer in skyWalking + span.setSpanLayer(ComponentName2SpanLayer.map.get(sofaTracerSpan.getSofaTracer() + .getTracerType())); + + //map tracerType in sofaTracer to ComponentId in skyWalking + span.setComponentId(getComponentId(sofaTracerSpan)); + span.setError(!isWebHttpClientSuccess(sofaTracerSpan.getTagsWithStr().get( + CommonSpanTags.RESULT_CODE))); + span.setSkipAnalysis(true); + span = convertSpanTags(sofaTracerSpan, span); + convertSpanLogs(sofaTracerSpan, span); + // if has patentId then need to add segmentReference + if (!StringUtils.isBlank(sofaTracerSpan.getSofaTracerSpanContext().getParentId())) { + span = addSegmentReference(sofaTracerSpan, span); + } + // Dubbo + String remoteHost = sofaTracerSpan.getTagsWithStr().get(CommonSpanTags.REMOTE_HOST); + String remotePort = sofaTracerSpan.getTagsWithStr().get(CommonSpanTags.REMOTE_PORT); + // sofaRpc + String remoteIp = sofaTracerSpan.getTagsWithStr().get("remote.ip"); + + if (remoteHost != null && remotePort != null) { + span.setPeer(remoteHost + ":" + remotePort); + } + // if the span is formed by sofaRPC, we can only get ip of the server to generate networkAddressUsedAtPeer + if (sofaTracerSpan.getSofaTracer().getTracerType().equals(ComponentNameConstants.SOFA_RPC) + && remoteIp != null) { + span.setPeer(remoteIp.split(":")[0]); + } + return span; + } + + /** + * construct serviceName + * @param sofaTracerSpan + * @return serviceName + */ + private String constructServiceName(SofaTracerSpan sofaTracerSpan) { + return sofaTracerSpan.getTagsWithStr().get(CommonSpanTags.LOCAL_APP); + } + + /** + * ServiceInstanceName + * @param sofaTracerSpan + * @return instanceName + */ + private String constructServiceInstanceName(SofaTracerSpan sofaTracerSpan) { + InetAddress localIpAddress = NetUtils.getLocalAddress(); + return constructServiceName(sofaTracerSpan) + "@" + localIpAddress.getHostAddress(); + } + + /** + * get parentSegmentId + * @param sofaTracerSpan + * @return parentSegmentId + */ + private String getParentSegmentId(SofaTracerSpan sofaTracerSpan) { + String traceId = sofaTracerSpan.getSofaTracerSpanContext().getTraceId(); + //in sofaRPC and Dubbo server->server is possible + // if the span is the server span of RPC, then it's parentSegmentId is traceId + FNV64HashCode(spanId) + SofaTracerConstant.CLIENT + if (sofaTracerSpan.isServer() + && ComponentName2SpanLayer.map.get(sofaTracerSpan.getSofaTracer().getTracerType()) + .equals(SpanLayer.RPCFramework)) { + // server and client span share the same traceId and spanId + return traceId + FNV64HashCode(sofaTracerSpan.getSofaTracerSpanContext().getSpanId()) + + SofaTracerConstant.CLIENT; + } + return traceId + + FNV64HashCode(sofaTracerSpan.getSofaTracerSpanContext().getParentId()) + + (sofaTracerSpan.isServer() ? SofaTracerConstant.CLIENT : SofaTracerConstant.SERVER); + + } + + /** + * convert tags + * @param sofaTracerSpan + * @param swSpan span in SkyWalking format + * @return span with tags in SkyWalking format + */ + private Span convertSpanTags(SofaTracerSpan sofaTracerSpan, Span swSpan) { + Map tags = new LinkedHashMap<>(); + tags.putAll(sofaTracerSpan.getTagsWithStr()); + tags.putAll(sofaTracerSpan.getTagsWithBool()); + tags.putAll(sofaTracerSpan.getTagsWithNumber()); + for (Map.Entry tag : tags.entrySet()) { + swSpan.addTag(tag.getKey(), tag.getValue().toString()); + } + return swSpan; + } + + /** + * + * @param sofaTracerSpan + * @param swSpan + * @return span with logs in SkyWalking format + */ + private Span convertSpanLogs(SofaTracerSpan sofaTracerSpan, Span swSpan) { + List logs = sofaTracerSpan.getLogs(); + for (LogData sofaLog : logs) { + Log log = new Log(); + log.setTime(sofaLog.getTime()); + //KeyStringValuePair + for (Map.Entry entry : sofaLog.getFields().entrySet()) { + log.addLogs(entry.getKey(), entry.getValue().toString()); + } + swSpan.addLog(log); + } + return swSpan; + } + + /** + * when sofaTracer Span is not root span we add add reference to point to parent segment + * @param sofaTracerSpan + * @param swSpan + * @return span with segment reference in SkyWalking format + */ + private Span addSegmentReference(SofaTracerSpan sofaTracerSpan, Span swSpan) { + SegmentReference segmentReference = new SegmentReference(); + //default set to crossProcess + segmentReference.setRefType(RefType.CrossProcess); + segmentReference.setTraceId(sofaTracerSpan.getSofaTracerSpanContext().getTraceId()); + segmentReference.setParentTraceSegmentId(getParentSegmentId(sofaTracerSpan)); + //because there is only one span in each segment so parentId is 0 + segmentReference.setParentSpanId(0); + String networkAddressUsedAtPeer = getNetworkAddressUsedAtPeer(sofaTracerSpan); + if (networkAddressUsedAtPeer != null) { + segmentReference.setNetworkAddressUsedAtPeer(networkAddressUsedAtPeer); + } + swSpan.addSegmentReference(segmentReference); + return swSpan; + } + + private String getNetworkAddressUsedAtPeer(SofaTracerSpan sofaTracerSpan) { + // if is sofaRpc get localIp + if (sofaTracerSpan.getSofaTracer().getTracerType().equals(ComponentNameConstants.SOFA_RPC)) { + return NetUtils.getLocalIpv4(); + } + Map strTags = sofaTracerSpan.getTagsWithStr(); + String host = strTags.get(CommonSpanTags.LOCAL_HOST); + String port = strTags.get(CommonSpanTags.LOCAL_PORT); + if (host != null && port != null) { + return host + ":" + port; + } + return null; + } + + private int getComponentId(SofaTracerSpan sofaTracerSpan) { + String tracerType = sofaTracerSpan.getSofaTracer().getTracerType(); + final int UNKNOWN = ComponentName2ComponentId.componentName2IDMap.get("UNKNOWN"); + if (StringUtils.isBlank(tracerType)) { + return UNKNOWN; + } + // specific type of database instead of datasource + if (tracerType.equals(ComponentNameConstants.DATA_SOURCE)) { + String database = sofaTracerSpan.getTagsWithStr().get("database.type"); + if (StringUtils.isBlank(database)) { + return UNKNOWN; + } + if (ComponentName2ComponentId.componentName2IDMap.containsKey(database)) { + return ComponentName2ComponentId.componentName2IDMap.get(database); + } + return UNKNOWN; + } + if (ComponentName2ComponentId.componentName2IDMap.containsKey(tracerType)) { + return ComponentName2ComponentId.componentName2IDMap.get(tracerType); + } + return UNKNOWN; + + } + + private boolean isHttpOrMvcSuccess(String resultCode) { + return resultCode.charAt(0) == '1' || resultCode.charAt(0) == '2' + || "302".equals(resultCode.trim()) || ("301".equals(resultCode.trim())); + } + + private boolean isWebHttpClientSuccess(String resultCode) { + return StringUtils.isNotBlank(resultCode) + && (isHttpOrMvcSuccess(resultCode) || SofaTracerConstant.RESULT_CODE_SUCCESS + .equals(resultCode)); + } + + /** + * from http://en.wikipedia.org/wiki/Fowler_Noll_Vo_hash + * + * @param data String data + * @return fnv hash code + */ + public static long FNV64HashCode(String data) { + //hash FNVHash64 : http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-param + long hash = 0xcbf29ce484222325L; + for (int i = 0; i < data.length(); ++i) { + char c = data.charAt(i); + hash ^= c; + hash *= 0x100000001b3L; + } + return hash; + } + +} diff --git a/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/initialize/SkywalkingReportRegisterBean.java b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/initialize/SkywalkingReportRegisterBean.java new file mode 100644 index 000000000..2039a4d15 --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/initialize/SkywalkingReportRegisterBean.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.tracer.plugins.skywalking.initialize; + +import com.alipay.common.tracer.core.configuration.SofaTracerConfiguration; +import com.alipay.common.tracer.core.listener.SpanReportListener; +import com.alipay.common.tracer.core.listener.SpanReportListenerHolder; +import com.alipay.common.tracer.core.utils.StringUtils; +import com.alipay.sofa.tracer.plugins.skywalking.SkywalkingSpanRemoteReporter; +import com.alipay.sofa.tracer.plugins.skywalking.properties.SkywalkingProperties; +import org.springframework.beans.factory.InitializingBean; + +import java.util.ArrayList; +import java.util.List; + +/** + * SkywalkingReportRegisterBean + * @author zhaochen + */ +public class SkywalkingReportRegisterBean implements InitializingBean { + @Override + public void afterPropertiesSet() { + // if do not match report condition,it will be return right now + boolean enabled = false; + String enabledStr = SofaTracerConfiguration + .getProperty(SkywalkingProperties.SKYWALKING_IS_ENABLED_KEY); + if (StringUtils.isNotBlank(enabledStr) && "true".equalsIgnoreCase(enabledStr)) { + enabled = true; + } + if (!enabled) { + return; + } + String baseUrl = SofaTracerConfiguration.getProperty( + SkywalkingProperties.SKYWALKING_BASE_URL_KEY, "http://localhost:12800"); + int maxBufferSize = SofaTracerConfiguration.getIntegerDefaultIfNull( + SkywalkingProperties.SKYWALKING_MAX_BUFFER_SIZE_KEY, 10000); + SpanReportListener spanReportListener = new SkywalkingSpanRemoteReporter(baseUrl, + maxBufferSize); + List spanReportListenerList = new ArrayList(); + spanReportListenerList.add(spanReportListener); + SpanReportListenerHolder.addSpanReportListeners(spanReportListenerList); + } +} diff --git a/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/KeyStringValuePair.java b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/KeyStringValuePair.java new file mode 100644 index 000000000..62fcff836 --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/KeyStringValuePair.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.tracer.plugins.skywalking.model; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; + +/** + * KeyStringValuePair + * @author zhaochen + */ +@Getter +@Setter +@AllArgsConstructor +public class KeyStringValuePair { + private String key; + private String value; + + @Override + public String toString() { + return "KeyStringValuePair{" + "key='" + key + '\'' + ", value='" + value + '\'' + '}'; + } +} diff --git a/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/Log.java b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/Log.java new file mode 100644 index 000000000..1c5caff5f --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/Log.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.tracer.plugins.skywalking.model; + +import lombok.Getter; +import lombok.Setter; + +import java.util.LinkedList; +import java.util.List; + +/** + * Log + * @author zhaochen + */ +@Getter +@Setter +public class Log { + private Long time; + private List data = new LinkedList<>(); + + public void addLogs(String key, String value) { + data.add(new KeyStringValuePair(key, value)); + } +} diff --git a/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/RefType.java b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/RefType.java new file mode 100644 index 000000000..1fab1aff0 --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/RefType.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.tracer.plugins.skywalking.model; + +/** + * RefType + * @author zhaochen + */ +public enum RefType { + CrossProcess(0), CrossThread(1), UNRECOGNIZED(-1); + private int id; + + RefType(int id) { + this.id = id; + } +} diff --git a/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/Segment.java b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/Segment.java new file mode 100644 index 000000000..420fbd529 --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/Segment.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.tracer.plugins.skywalking.model; + +import lombok.Getter; +import lombok.Setter; + +import java.util.LinkedList; +import java.util.List; + +/** + * Segment + * @author zhaochen + */ +@Getter +@Setter +public class Segment { + private String traceId; + private String traceSegmentId; + private List spans = new LinkedList<>(); + // each service name is displayed as a node in the topology view. + // Indicators obtained from the SPAN under the same service name are aggregated as indicators of the service + private String service; + private String serviceInstance; + // Whether the segment includes all tracked spans. + // In the production environment tracked, some tasks could include too many spans for one request context, such as a batch update for a cache, or an async job. + // The agent/SDK could optimize or ignore some tracked spans for better performance. + // In this case, the value should be flagged as TRUE. + private boolean isSizeLimited; + + public void addSpan(Span span) { + spans.add(span); + } + +} diff --git a/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/SegmentReference.java b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/SegmentReference.java new file mode 100644 index 000000000..c522c6a50 --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/SegmentReference.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.tracer.plugins.skywalking.model; + +import lombok.Getter; +import lombok.Setter; + +/** + * SegmentReference + * @author zhaochen + */ +@Setter +@Getter +public class SegmentReference { + private RefType refType; + private String traceId; + private String parentTraceSegmentId; + private int parentSpanId; + private String parentService; + private String parentServiceInstance; + private String parentEndpoint; + // The network address, including ip/hostname and port, which is used in the client side. + // Such as Client --> use 127.0.11.8:913 -> Server + // then, in the reference of entry span reported by Server, the value of this field is 127.0.11.8:913. + // This plays the important role in the SkyWalking STAM(Streaming Topology Analysis Method) + // For more details, read https://wu-sheng.github.io/STAM/ + private String networkAddressUsedAtPeer; + +} diff --git a/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/Span.java b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/Span.java new file mode 100644 index 000000000..34556feb3 --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/Span.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.tracer.plugins.skywalking.model; + +import com.alibaba.fastjson.annotation.JSONField; +import lombok.Getter; +import lombok.Setter; + +import java.util.LinkedList; +import java.util.List; + +/** + * Span + * @author zhaochen + */ +@Getter +@Setter +public class Span { + //so the detail of span : + //https://github.com/apache/skywalking-data-collect-protocol/blob/e626ee04850703c220f64b642d2893fa65572943/language-agent/Tracing.proto + private int spanId; + private int parentSpanId; + private Long startTime; + private Long endTime; + private List refs = new LinkedList<>(); + private String operationName; + private String peer; + private SpanType spanType; + // Span layer represent the component tech stack, related to the network tech. + private SpanLayer spanLayer; + private int componentId; + @JSONField(name = "isError") + private boolean isError; + private List tags = new LinkedList<>(); + private List logs = new LinkedList<>(); + private boolean skipAnalysis; + + public void addSegmentReference(SegmentReference segmentReference) { + refs.add(segmentReference); + } + + public void addTag(String key, String value) { + tags.add(new KeyStringValuePair(key, value)); + } + + public void addLog(Log log) { + logs.add(log); + } + +} diff --git a/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/SpanLayer.java b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/SpanLayer.java new file mode 100644 index 000000000..49e2575ce --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/SpanLayer.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.tracer.plugins.skywalking.model; + +/** + * SpanLayer + * @author zhaochen + */ +public enum SpanLayer { + + // need to user lowercase for uploading segment + Unknown(0), Database(1), RPCFramework(2), Http(3), MQ(4), Cache(5); + + private int code; + + SpanLayer(int code) { + this.code = code; + } + + public int getCode() { + return this.code; + } +} diff --git a/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/SpanType.java b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/SpanType.java new file mode 100644 index 000000000..79f8f3a58 --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/model/SpanType.java @@ -0,0 +1,30 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.tracer.plugins.skywalking.model; + +/** + * SpanType + * @author zhaochen + */ +public enum SpanType { + Entry(0), Exit(1), Local(2), UNRECOGNIZED(-1); + private int id; + + SpanType(int id) { + this.id = id; + } +} diff --git a/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/properties/SkywalkingProperties.java b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/properties/SkywalkingProperties.java new file mode 100644 index 000000000..377600b71 --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/properties/SkywalkingProperties.java @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.tracer.plugins.skywalking.properties; + +/** + * SkywalkingProperties + * @author zhaochen + */ +public class SkywalkingProperties { + public static final String SKYWALKING_IS_ENABLED_KEY = "com.alipay.sofa.tracer.skywalking.enabled"; + public static final String SKYWALKING_BASE_URL_KEY = "com.alipay.sofa.tracer.skywalking.baseUrl"; + public static final String SKYWALKING_MAX_BUFFER_SIZE_KEY = "com.alipay.sofa.tracer.skywalking.maxBufferSize"; + +} diff --git a/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/reporter/AsyncReporter.java b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/reporter/AsyncReporter.java new file mode 100644 index 000000000..3c122156c --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/reporter/AsyncReporter.java @@ -0,0 +1,244 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.tracer.plugins.skywalking.reporter; + +import com.alipay.common.tracer.core.appender.self.SelfLog; +import com.alipay.sofa.tracer.plugins.skywalking.sender.SkywalkingRestTemplateSender; +import com.alipay.sofa.tracer.plugins.skywalking.model.Segment; +import java.io.Closeable; +import java.io.Flushable; +import java.io.IOException; +import java.util.Arrays; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.ReentrantLock; + +/** + * AsyncReporter + * @author zhaochen + */ +public class AsyncReporter implements Closeable, Flushable { + final Segment[] segments; + final int[] sizesInBytes; + final int maxBufferSize; // the max num of segment + SkywalkingRestTemplateSender sender; + int count = 0; //how many segment in the array + int writePos = 0; + int readPos = 0; + int messageMaxBytes = 2 * 1024 * 1024; // max packetSize is 2M + long messageTimeoutNanos = TimeUnit.SECONDS.toNanos(1); + long closeTimeoutNanos = TimeUnit.SECONDS.toNanos(1); + final AtomicBoolean closed = new AtomicBoolean(false); + final ReentrantLock lock = new ReentrantLock(false); + final Condition available = lock.newCondition(); + final CountDownLatch close = new CountDownLatch(1); + + /** + * when create a new reporter will start a new thread to flush segment into one message, + * when the message size bigger than threshold or remaining time no more than 0, it's time to + * send the message + * @param maxBufferSize the size of the segment buffer + * @param sender sender to send the message + */ + public AsyncReporter(int maxBufferSize, SkywalkingRestTemplateSender sender) { + this.maxBufferSize = maxBufferSize; + this.segments = new Segment[maxBufferSize]; + this.sizesInBytes = new int[maxBufferSize]; + this.sender = sender; + // many segments are bundled into one message + final Message message = new Message(messageMaxBytes, messageTimeoutNanos); + final Thread flushThread = new Thread("AsyncReporter") { + @Override + public void run() { + try { + while (!closed.get()) { + flush(message); + } + } catch (RuntimeException | Error e) { + SelfLog.warn("Unexpected error flushing spans", e); + } finally { + if (count > 0) { + SelfLog.warn("Dropped " + count + " spans due to AsyncReporter.close()"); + } + close.countDown(); + } + } + }; + flushThread.setDaemon(true); + flushThread.start(); + } + + public void report(Segment segment, int segmentByteSize) { + if (segment == null) + throw new NullPointerException("segment == null"); + //if already closed or the segment Array is full just dropping the segment + if (closed.get() || !addSegment(segment, segmentByteSize)) { + SelfLog.warn("Dropped one span because reporter is close or buffer queue is full "); + } + } + + /** + * a new thread will call this function in a synchronous loop until the reporter is closed + * @param message + */ + + public void flush(Message message) { + if (closed.get()) + throw new IllegalStateException("closed"); + // drain segment in the segment array as many as possible + drainTo(message, message.remainingNanos()); + if (!message.isReady() && !closed.get()) + return; + // send the message + try { + if (!sender.post(message.getMessage())) { + int count = message.getCount(); + SelfLog.warn("Dropped " + count + " spans, result code is not 2XX"); + } + } catch (RuntimeException | Error t) { + // In failure case, we increment messages and spans dropped. + int count = message.getCount(); + SelfLog.warn("Dropped " + count + " spans", t); + // Raise in case the sender was closed out-of-band. + if (t instanceof IllegalStateException) + throw (IllegalStateException) t; + } finally { + message.reset(); + } + } + + /** + * add segment to the segment array + * @param segment segment to add + * @param segmentByteSize the size of segment to add + * @return whether the operation is successful + */ + private boolean addSegment(Segment segment, int segmentByteSize) { + lock.lock(); + try { + if (count == maxBufferSize) + return false; + segments[writePos] = segment; + sizesInBytes[writePos] = segmentByteSize; + writePos++; + //back to the head + if (writePos == maxBufferSize) + writePos = 0; + count++; + // add successfully, alert any drainers + available.signal(); + return true; + } finally { + lock.unlock(); + } + } + + /** + * clear the data buffered + * @return how many segments are removed + */ + private int clear() { + lock.lock(); + try { + int result = count; + count = readPos = writePos = 0; + Arrays.fill(segments, null); + Arrays.fill(sizesInBytes, 0); + return result; + } finally { + lock.unlock(); + } + } + + /** + * Blocks for up to nanosTimeout for spans to appear. Then, consume as many as possible. + * @param message message to receive the segment + * @param nanosTimeout remaining time to send the message + * @return how many segments are add to message + */ + int drainTo(Message message, long nanosTimeout) { + try { + lock.lockInterruptibly(); + try { + long nanosLeft = nanosTimeout; + while (count == 0) { + if (nanosLeft <= 0) + return 0; + nanosLeft = available.awaitNanos(nanosLeft); + } + return doDrain(message); + } finally { + lock.unlock(); + } + } catch (InterruptedException e) { + return 0; + } + } + + /** + * add segments to the message as many as possible + * @param message + * @return the count of segment added to message + */ + int doDrain(Message message) { + int drainedCount = 0; + while (drainedCount < count) { + Segment next = segments[readPos]; + int nextSizeInBytes = sizesInBytes[readPos]; + if (next == null) + break; + if (message.offer(next, nextSizeInBytes)) { + drainedCount++; + segments[readPos] = null; + sizesInBytes[readPos] = 0; + if (++readPos == segments.length) + readPos = 0; // circle back to the front of the array + } else { + break; + } + } + count -= drainedCount; + return drainedCount; + } + + @Override + public void close() throws IOException { + if (!closed.compareAndSet(false, true)) + return; // already closed + try { + // wait for in-flight spans to send + if (!close.await(closeTimeoutNanos, TimeUnit.NANOSECONDS)) { + SelfLog.warn("Timed out waiting for in-flight spans to send"); + } + } catch (InterruptedException e) { + SelfLog.warn("Interrupted waiting for in-flight spans to send"); + Thread.currentThread().interrupt(); + } + int count = clear(); + if (count > 0) { + SelfLog.warn("Dropped " + count + " spans due to AsyncReporter.close()"); + } + } + + @Override + public void flush() throws IOException { + // timeoutNanos equals 0, so will send immediately + flush(new Message(messageMaxBytes, 0L)); + } +} diff --git a/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/reporter/Message.java b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/reporter/Message.java new file mode 100644 index 000000000..18f3acaec --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/reporter/Message.java @@ -0,0 +1,121 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.tracer.plugins.skywalking.reporter; + +import com.alipay.sofa.tracer.plugins.skywalking.model.Segment; + +import java.util.ArrayList; + +/** + * Message + * @author zhaochen + */ +public class Message { + final int maxBytes; + // max time to wait for sending next message + final long timeoutNanos; + final ArrayList message = new ArrayList<>(); + boolean full = false; + // whether there is more than one segment in the message + boolean hasAtLeastOneSegment = false; + int messageSizeInBytes; + long deadlineNanoTime; + // how many segments contains in the message + int count = 0; + + public Message(int maxBytes, Long timeoutNanos) { + this.maxBytes = maxBytes; + this.timeoutNanos = timeoutNanos; + // contains [] + this.messageSizeInBytes = 2; + } + + /** + * add segment to the message + * @param next segment to add + * @param nextSizeInBytes the size of segment to be add + * @return whether add successfully + */ + public boolean offer(Segment next, int nextSizeInBytes) { + int x = countMessageSizeInBytes(nextSizeInBytes); + int y = maxBytes; + int includingNextVsMaxBytes = (x < y) ? -1 : ((x == y) ? 0 : 1); + if (includingNextVsMaxBytes > 0) { + full = true; + return false; // can't fit the next message into this buffer + } + addSegmentToMessage(next); + messageSizeInBytes = x; + count++; + if (includingNextVsMaxBytes == 0) + full = true; + return true; + } + + private int countMessageSizeInBytes(int nextSizeInBytes) { + // if there is more than one segment in the message need to add one comma + return messageSizeInBytes + nextSizeInBytes + (hasAtLeastOneSegment ? 1 : 0); + } + + public void reset() { + messageSizeInBytes = 2; + hasAtLeastOneSegment = false; + deadlineNanoTime = 0; + full = false; + count = 0; + message.clear(); + } + + public ArrayList getMessage() { + return this.message; + } + + private void addSegmentToMessage(Segment next) { + this.message.add(next); + if (!hasAtLeastOneSegment) { + hasAtLeastOneSegment = true; + } + } + + /** + * the time remaining to send the message + * @return remaining time to send message + */ + long remainingNanos() { + if (message.isEmpty()) { + deadlineNanoTime = System.nanoTime() + timeoutNanos; + } + return Math.max(deadlineNanoTime - System.nanoTime(), 0); + } + + /** + * if buffer is full or overtime send the message + * @return whether ready to send message + */ + boolean isReady() { + return full || remainingNanos() <= 0; + } + + /** + * get how many segment contains in the message + * @return + */ + int getCount() { + return this.count; + } + +} diff --git a/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/sender/SkywalkingRestTemplateSender.java b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/sender/SkywalkingRestTemplateSender.java new file mode 100644 index 000000000..cd5fa1bad --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/sender/SkywalkingRestTemplateSender.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.tracer.plugins.skywalking.sender; + +import com.alibaba.fastjson.JSON; +import com.alipay.sofa.tracer.plugins.skywalking.model.Segment; +import org.springframework.http.*; +import org.springframework.web.client.RestTemplate; + +import java.net.URI; +import java.util.List; + +/** + * SkywalkingRestTemplateSender + * @author zhaochen + */ +public class SkywalkingRestTemplateSender { + private RestTemplate restTemplate; + private String url; + + public SkywalkingRestTemplateSender(RestTemplate restTemplate, String baseUrl) { + this.restTemplate = restTemplate; + this.url = baseUrl + (baseUrl.endsWith("/") ? "" : "/") + "v3/segments"; + } + + public boolean post(List segments) { + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setContentType(MediaType.APPLICATION_JSON); + String json = JSON.toJSONString(segments); + RequestEntity requestEntity = new RequestEntity(json, httpHeaders, + HttpMethod.POST, URI.create(this.url)); + ResponseEntity response = this.restTemplate.exchange(requestEntity, String.class); + return response.getStatusCode().is2xxSuccessful(); + } +} diff --git a/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/utils/ComponentName2ComponentId.java b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/utils/ComponentName2ComponentId.java new file mode 100644 index 000000000..52cae8b28 --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/utils/ComponentName2ComponentId.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.tracer.plugins.skywalking.utils; + +import com.alipay.common.tracer.core.constants.ComponentNameConstants; + +import java.util.HashMap; + +/** + * map ComponentName in SOFATracer to ComponentId in SkyWalking + * @author zhaochen + */ +public class ComponentName2ComponentId { + public static final HashMap componentName2IDMap = new HashMap<>(); + static { + //componentId in SkyWalking: https://github.com/apache/skywalking/blob/master/oap-server/server-bootstrap/src/main/resources/component-libraries.yml + componentName2IDMap.put(ComponentNameConstants.UNKNOWN, 0); + componentName2IDMap.put(ComponentNameConstants.H2, 4); + componentName2IDMap.put(ComponentNameConstants.MYSQL, 5); + componentName2IDMap.put(ComponentNameConstants.ORACLE, 6); + componentName2IDMap.put(ComponentNameConstants.REDIS, 7); + componentName2IDMap.put(ComponentNameConstants.MONGODB, 9); + componentName2IDMap.put(ComponentNameConstants.MEMCACHED, 20); + componentName2IDMap.put(ComponentNameConstants.SOFA_RPC, 43); + componentName2IDMap.put(ComponentNameConstants.DUBBO_CLIENT, 3); + componentName2IDMap.put(ComponentNameConstants.DUBBO_SERVER, 3); + componentName2IDMap.put(ComponentNameConstants.HTTP_CLIENT, 2); + componentName2IDMap.put(ComponentNameConstants.OK_HTTP, 12); + componentName2IDMap.put(ComponentNameConstants.REST_TEMPLATE, 13); + componentName2IDMap.put(ComponentNameConstants.SPRING_MVC, 14); + componentName2IDMap.put(ComponentNameConstants.FEIGN_CLIENT, 11); + componentName2IDMap.put(ComponentNameConstants.KAFKAMQ_CONSUMER, 41); + componentName2IDMap.put(ComponentNameConstants.KAFKAMQ_SEND, 40); + componentName2IDMap.put(ComponentNameConstants.ROCKETMQ_CONSUMER, 39); + componentName2IDMap.put(ComponentNameConstants.ROCKETMQ_SEND, 38); + componentName2IDMap.put(ComponentNameConstants.RABBITMQ_CONSUMER, 53); + componentName2IDMap.put(ComponentNameConstants.RABBITMQ_SEND, 52); + componentName2IDMap.put(ComponentNameConstants.MONGO_CLIENT, 42); + componentName2IDMap.put(ComponentNameConstants.REDIS, 7); + //following componentId doesn't defined in SkyWalking mark it as unknown + componentName2IDMap.put(ComponentNameConstants.DATA_SOURCE, 0); + componentName2IDMap.put(ComponentNameConstants.FLEXIBLE, 0); + componentName2IDMap.put(ComponentNameConstants.MSG_PUB, 0); + componentName2IDMap.put(ComponentNameConstants.MSG_SUB, 0); + + } +} diff --git a/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/utils/ComponentName2SpanLayer.java b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/utils/ComponentName2SpanLayer.java new file mode 100644 index 000000000..528e4088b --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/utils/ComponentName2SpanLayer.java @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.tracer.plugins.skywalking.utils; + +import com.alipay.common.tracer.core.constants.ComponentNameConstants; +import com.alipay.sofa.tracer.plugins.skywalking.model.SpanLayer; + +import java.util.HashMap; + +/** + * map componentName in sofaTracer to SpanLayer in SkyWalking + * @author zhaochen + */ +public class ComponentName2SpanLayer { + public static final HashMap map = new HashMap<>(); + static { + map.put(ComponentNameConstants.DATA_SOURCE, SpanLayer.Database); + map.put(ComponentNameConstants.DUBBO_CLIENT, SpanLayer.RPCFramework); + map.put(ComponentNameConstants.DUBBO_SERVER, SpanLayer.RPCFramework); + map.put(ComponentNameConstants.SOFA_RPC, SpanLayer.RPCFramework); + map.put(ComponentNameConstants.HTTP_CLIENT, SpanLayer.Http); + map.put(ComponentNameConstants.OK_HTTP, SpanLayer.Http); + map.put(ComponentNameConstants.REST_TEMPLATE, SpanLayer.Http); + map.put(ComponentNameConstants.SPRING_MVC, SpanLayer.Http); + map.put(ComponentNameConstants.FLEXIBLE, SpanLayer.Http); + map.put(ComponentNameConstants.FEIGN_CLIENT, SpanLayer.Http); + map.put(ComponentNameConstants.KAFKAMQ_CONSUMER, SpanLayer.MQ); + map.put(ComponentNameConstants.KAFKAMQ_SEND, SpanLayer.MQ); + map.put(ComponentNameConstants.ROCKETMQ_CONSUMER, SpanLayer.MQ); + map.put(ComponentNameConstants.ROCKETMQ_SEND, SpanLayer.MQ); + map.put(ComponentNameConstants.RABBITMQ_CONSUMER, SpanLayer.MQ); + map.put(ComponentNameConstants.RABBITMQ_SEND, SpanLayer.MQ); + map.put(ComponentNameConstants.MSG_PUB, SpanLayer.MQ); + map.put(ComponentNameConstants.MSG_SUB, SpanLayer.MQ); + map.put(ComponentNameConstants.MONGO_CLIENT, SpanLayer.Cache); + map.put(ComponentNameConstants.REDIS, SpanLayer.Cache); + } +} diff --git a/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/test/java/SkywalkingReportRegisterBeanTest.java b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/test/java/SkywalkingReportRegisterBeanTest.java new file mode 100644 index 000000000..eb52a140f --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/test/java/SkywalkingReportRegisterBeanTest.java @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import com.alipay.common.tracer.core.listener.SpanReportListenerHolder; +import com.alipay.sofa.tracer.plugins.skywalking.initialize.SkywalkingReportRegisterBean; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * SkywalkingReportRegisterBeanTest + * @author zhaochen + */ +public class SkywalkingReportRegisterBeanTest { + private ClassPathXmlApplicationContext applicationContext; + + @Before + public void init() { + applicationContext = new ClassPathXmlApplicationContext("spring-bean.xml"); + } + + @Test + public void testAfterPropertiesSet() { + Object reportRegisterBean = applicationContext.getBean("SkywalkingReportRegisterBean"); + Assert.assertTrue(reportRegisterBean instanceof SkywalkingReportRegisterBean); + Assert.assertTrue(SpanReportListenerHolder.getSpanReportListenersHolder().size() > 0); + } +} diff --git a/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/test/java/SkywalkingRestTemplateSenderTest.java b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/test/java/SkywalkingRestTemplateSenderTest.java new file mode 100644 index 000000000..da45778d5 --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/test/java/SkywalkingRestTemplateSenderTest.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import com.alipay.common.tracer.core.SofaTracer; +import com.alipay.common.tracer.core.constants.ComponentNameConstants; +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.sofa.tracer.plugins.skywalking.SkywalkingSpanRemoteReporter; +import com.alipay.sofa.tracer.plugins.skywalking.adapter.SkywalkingSegmentAdapter; +import com.alipay.sofa.tracer.plugins.skywalking.model.Segment; +import com.alipay.sofa.tracer.plugins.skywalking.sender.SkywalkingRestTemplateSender; +import io.opentracing.tag.Tags; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.springframework.web.client.RestTemplate; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * SkywalkingRestTemplateSenderTest + * @author zhaochen + */ +public class SkywalkingRestTemplateSenderTest { + private final String tracerType = ComponentNameConstants.REDIS; + private SkywalkingSegmentAdapter adapter = new SkywalkingSegmentAdapter(); + private SofaTracer sofaTracer; + private SkywalkingRestTemplateSender sender; + SofaTracerSpan sofaTracerSpan; + + @Before + public void init() throws InterruptedException { + sender = new SkywalkingRestTemplateSender(new RestTemplate(), "http://127.0.0.1:12800"); + sofaTracer = new SofaTracer.Builder(tracerType).withTag("tracer", "SofaTraceZipkinTest") + .build(); + sofaTracerSpan = (SofaTracerSpan) this.sofaTracer.buildSpan("http//asynictest.com").start(); + sofaTracerSpan.setTag("tagsStrkey", "tagsStrVal"); + // mock process + Thread.sleep(30); + sofaTracerSpan.setEndTime(System.currentTimeMillis()); + } + + @Test + public void testPost() { + ArrayList segments = new ArrayList<>(); + Segment segment = adapter.convertToSkywalkingSegment(sofaTracerSpan); + segments.add(segment); + Assert.assertTrue(sender.post(segments)); + } +} diff --git a/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/test/java/SkywalkingSegmentAdapterTest.java b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/test/java/SkywalkingSegmentAdapterTest.java new file mode 100644 index 000000000..b35f6fe80 --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/test/java/SkywalkingSegmentAdapterTest.java @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import com.alibaba.fastjson.JSON; +import com.alipay.common.tracer.core.SofaTracer; +import com.alipay.common.tracer.core.constants.ComponentNameConstants; +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.sofa.tracer.plugins.skywalking.adapter.SkywalkingSegmentAdapter; +import com.alipay.sofa.tracer.plugins.skywalking.model.Segment; + +import com.alipay.sofa.tracer.plugins.skywalking.model.Span; +import com.alipay.sofa.tracer.plugins.skywalking.model.SpanLayer; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +/** + * SkywalkingSegmentAdapterTest + * @author zhaochen + */ +public class SkywalkingSegmentAdapterTest { + private SkywalkingSegmentAdapter adapter = new SkywalkingSegmentAdapter(); + + private final String tracerType = ComponentNameConstants.DATA_SOURCE; + + private SofaTracer sofaTracer; + + private SofaTracerSpan sofaTracerSpan; + + @Before + public void init() throws InterruptedException { + sofaTracer = new SofaTracer.Builder(tracerType).withTag("tracer", "SofaTraceZipkinTest") + .build(); + sofaTracerSpan = (SofaTracerSpan) this.sofaTracer.buildSpan("SofaTracerSpanTest").start(); + sofaTracerSpan.setTag("tagsStrkey", "tagsStrVal"); + sofaTracerSpan.setTag("tagsBooleankey", true); + sofaTracerSpan.setTag("tagsNumkey", 2018); + sofaTracerSpan.setBaggageItem("baggageKey", "baggageVal"); + sofaTracerSpan.setTag(CommonSpanTags.LOCAL_APP, "SofaTracerSpanTest"); + Map logMap = new HashMap(); + logMap.put("logKey", "logVal"); + LogData logData = new LogData(System.currentTimeMillis(), logMap); + sofaTracerSpan.log(logData); + // mock process + Thread.sleep(30); + sofaTracerSpan.setEndTime(System.currentTimeMillis()); + } + + @Test + public void testConvertToSegment() { + Segment segment = adapter.convertToSkywalkingSegment(sofaTracerSpan); + Assert.assertTrue(segment != null); + Span span = segment.getSpans().get(0); + Assert.assertTrue(span.getOperationName().equalsIgnoreCase( + sofaTracerSpan.getOperationName())); + Assert.assertTrue(span.getTags().size() == 4); + Assert.assertTrue(segment.getTraceId().contains( + sofaTracerSpan.getSofaTracerSpanContext().getTraceId())); + Assert.assertTrue(span.getLogs().size() == 1); + Assert.assertTrue(span.getSpanLayer().equals(SpanLayer.Database)); + Assert.assertTrue(span.getComponentId() == 0); + } +} diff --git a/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/test/java/SkywalkingSpanRemoteReporterTest.java b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/test/java/SkywalkingSpanRemoteReporterTest.java new file mode 100644 index 000000000..3ab483855 --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/test/java/SkywalkingSpanRemoteReporterTest.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import com.alipay.common.tracer.core.SofaTracer; +import com.alipay.common.tracer.core.constants.ComponentNameConstants; +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.sofa.tracer.plugins.skywalking.SkywalkingSpanRemoteReporter; + +import io.opentracing.tag.Tags; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +/** + * SkywalkingSpanRemoteReporterTest + * @author zhaochen + */ +public class SkywalkingSpanRemoteReporterTest { + private SkywalkingSpanRemoteReporter reporter; + + private final String tracerType = ComponentNameConstants.REDIS; + + private SofaTracer sofaTracer; + + private SofaTracerSpan sofaTracerSpan; + + @Before + public void init() throws InterruptedException { + reporter = new SkywalkingSpanRemoteReporter("http://127.0.0.1:12800", 10000); + + sofaTracer = new SofaTracer.Builder(tracerType).withTag("tracer", "SofaTraceZipkinTest") + .build(); + sofaTracerSpan = (SofaTracerSpan) this.sofaTracer.buildSpan("http//asynictest.com").start(); + sofaTracerSpan.setTag("tagsStrkey", "tagsStrVal"); + sofaTracerSpan.setTag("tagsBooleankey", true); + sofaTracerSpan.setTag("tagsBooleankey", 2018); + sofaTracerSpan.setTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER); + sofaTracerSpan.setBaggageItem("baggageKey", "baggageVal"); + sofaTracerSpan.setTag(CommonSpanTags.LOCAL_APP, "TESTABC"); + Map logMap = new HashMap(); + logMap.put("logKey", "logVal"); + LogData logData = new LogData(System.currentTimeMillis(), logMap); + sofaTracerSpan.log(logData); + // mock process + Thread.sleep(30); + sofaTracerSpan.setEndTime(System.currentTimeMillis()); + } + + @Test + public void testOnSpanReport() { + reporter.onSpanReport(sofaTracerSpan); + } + +} diff --git a/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/test/resources/sofa.tracer.properties b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/test/resources/sofa.tracer.properties new file mode 100644 index 000000000..95ac53944 --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/test/resources/sofa.tracer.properties @@ -0,0 +1,3 @@ +com.alipay.sofa.tracer.skywalking.enabled=true +com.alipay.sofa.tracer.skywalking.baseUrl=http://localhost:12800 +com.alipay.sofa.tracer.skywalking.maxBufferSize=10000 diff --git a/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/test/resources/spring-bean.xml b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/test/resources/spring-bean.xml new file mode 100644 index 000000000..20b687aa4 --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/test/resources/spring-bean.xml @@ -0,0 +1,15 @@ + + + + + + diff --git a/sofa-tracer-plugins/sofa-tracer-zipkin-plugin/src/main/java/com/alipay/sofa/tracer/plugins/zipkin/adapter/ZipkinV2SpanAdapter.java b/sofa-tracer-plugins/sofa-tracer-zipkin-plugin/src/main/java/com/alipay/sofa/tracer/plugins/zipkin/adapter/ZipkinV2SpanAdapter.java index 3f415ba81..c36def178 100644 --- a/sofa-tracer-plugins/sofa-tracer-zipkin-plugin/src/main/java/com/alipay/sofa/tracer/plugins/zipkin/adapter/ZipkinV2SpanAdapter.java +++ b/sofa-tracer-plugins/sofa-tracer-zipkin-plugin/src/main/java/com/alipay/sofa/tracer/plugins/zipkin/adapter/ZipkinV2SpanAdapter.java @@ -20,6 +20,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 io.opentracing.tag.Tags; import zipkin2.Endpoint; diff --git a/tracer-core/src/main/java/com/alipay/common/tracer/core/SofaTracer.java b/tracer-core/src/main/java/com/alipay/common/tracer/core/SofaTracer.java index d834e394d..9404db3d8 100644 --- a/tracer-core/src/main/java/com/alipay/common/tracer/core/SofaTracer.java +++ b/tracer-core/src/main/java/com/alipay/common/tracer/core/SofaTracer.java @@ -341,7 +341,6 @@ private SofaTracerSpanContext createRootSpanContext() { private SofaTracerSpanContext createChildContext() { SofaTracerSpanContext preferredReference = preferredReference(); - SofaTracerSpanContext sofaTracerSpanContext = new SofaTracerSpanContext( preferredReference.getTraceId(), preferredReference.nextChildContextId(), preferredReference.getSpanId(), preferredReference.isSampled()); diff --git a/tracer-core/src/main/java/com/alipay/common/tracer/core/constants/ComponentNameConstants.java b/tracer-core/src/main/java/com/alipay/common/tracer/core/constants/ComponentNameConstants.java index 887dcdc5d..d81081e2e 100644 --- a/tracer-core/src/main/java/com/alipay/common/tracer/core/constants/ComponentNameConstants.java +++ b/tracer-core/src/main/java/com/alipay/common/tracer/core/constants/ComponentNameConstants.java @@ -60,5 +60,13 @@ public class ComponentNameConstants { public static final String MONGO_CLIENT = "mongo-client"; public static final String REDIS = "redis"; + // the tracerType of sofaRpc is RPC_TRACER + public static final String SOFA_RPC = "RPC_TRACER"; + public static final String UNKNOWN = "UNKNOWN"; + public static final String H2 = "h2"; + public static final String MYSQL = "mysql"; + public static final String ORACLE = "oracle"; + public static final String MONGODB = "mongodb"; + public static final String MEMCACHED = "memcached"; } diff --git a/tracer-core/src/main/java/com/alipay/common/tracer/core/constants/SofaTracerConstant.java b/tracer-core/src/main/java/com/alipay/common/tracer/core/constants/SofaTracerConstant.java index f09e86c97..bff5427d3 100644 --- a/tracer-core/src/main/java/com/alipay/common/tracer/core/constants/SofaTracerConstant.java +++ b/tracer-core/src/main/java/com/alipay/common/tracer/core/constants/SofaTracerConstant.java @@ -122,4 +122,8 @@ public class SofaTracerConstant { public static final String STAT_FLAG_FAILS = DIGEST_FLAG_FAILS; public static final String SPACE_ID = "sofa-tracer"; + + public static final int SERVER = 0; + + public static final int CLIENT = 1; } diff --git a/tracer-core/src/main/java/com/alipay/common/tracer/core/context/span/SofaTracerSpanContext.java b/tracer-core/src/main/java/com/alipay/common/tracer/core/context/span/SofaTracerSpanContext.java index 728098b4c..7711e7c57 100644 --- a/tracer-core/src/main/java/com/alipay/common/tracer/core/context/span/SofaTracerSpanContext.java +++ b/tracer-core/src/main/java/com/alipay/common/tracer/core/context/span/SofaTracerSpanContext.java @@ -234,6 +234,7 @@ public String serializeSpanContext() { * @param deserializeValue deserialize string, format: tcid:0,spid:1 * @return SofaTracerSpanContext */ + public static SofaTracerSpanContext deserializeFromString(String deserializeValue) { if (StringUtils.isBlank(deserializeValue)) { return SofaTracerSpanContext.rootStart(); diff --git a/tracer-core/src/main/java/com/alipay/common/tracer/core/span/CommonSpanTags.java b/tracer-core/src/main/java/com/alipay/common/tracer/core/span/CommonSpanTags.java index 240fabedd..1e9fd476e 100644 --- a/tracer-core/src/main/java/com/alipay/common/tracer/core/span/CommonSpanTags.java +++ b/tracer-core/src/main/java/com/alipay/common/tracer/core/span/CommonSpanTags.java @@ -16,6 +16,8 @@ */ package com.alipay.common.tracer.core.span; +import io.opentracing.tag.Tags; + /** * some common SpanTags * @author luoguimu123 @@ -35,7 +37,7 @@ public class CommonSpanTags { public static final String REMOTE_APP = "remote.app"; /** - * CURRENT_THREAD_NAME records handler result + * RESULT_CODE records handler result */ public static final String RESULT_CODE = "result.code"; diff --git a/tracer-core/src/main/java/com/alipay/common/tracer/core/tracer/AbstractTracer.java b/tracer-core/src/main/java/com/alipay/common/tracer/core/tracer/AbstractTracer.java index 9bba3052a..0833bd916 100644 --- a/tracer-core/src/main/java/com/alipay/common/tracer/core/tracer/AbstractTracer.java +++ b/tracer-core/src/main/java/com/alipay/common/tracer/core/tracer/AbstractTracer.java @@ -101,7 +101,7 @@ protected Reporter generateReporter(AbstractSofaTracerStatisticReporter statRepo protected abstract AbstractSofaTracerStatisticReporter generateServerStatReporter(); /** - * Stage CS , This stage will produce a new span + * Stage CS , This stage will produc a new span * If there is a span in the current sofaTraceContext, it is the parent of the current Span * * @param operationName as span name @@ -128,6 +128,7 @@ public SofaTracerSpan clientSend(String operationName) { } clientSpan = this.errorRecover(bizBaggage, sysBaggage); } finally { + if (clientSpan != null) { clientSpan.setTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT); clientSpan.setTag(CommonSpanTags.CURRENT_THREAD_NAME, Thread.currentThread() @@ -196,6 +197,7 @@ public SofaTracerSpan serverReceive() { * @param sofaTracerSpanContext The context to restore * @return SofaTracerSpan */ + public SofaTracerSpan serverReceive(SofaTracerSpanContext sofaTracerSpanContext) { SofaTracerSpan newSpan = null; // pop LogContext diff --git a/sofa-tracer-plugins/sofa-tracer-zipkin-plugin/src/main/java/com/alipay/sofa/tracer/plugins/zipkin/adapter/NetUtils.java b/tracer-core/src/main/java/com/alipay/common/tracer/core/utils/NetUtils.java similarity index 97% rename from sofa-tracer-plugins/sofa-tracer-zipkin-plugin/src/main/java/com/alipay/sofa/tracer/plugins/zipkin/adapter/NetUtils.java rename to tracer-core/src/main/java/com/alipay/common/tracer/core/utils/NetUtils.java index 3d533bc41..094ed4073 100644 --- a/sofa-tracer-plugins/sofa-tracer-zipkin-plugin/src/main/java/com/alipay/sofa/tracer/plugins/zipkin/adapter/NetUtils.java +++ b/tracer-core/src/main/java/com/alipay/common/tracer/core/utils/NetUtils.java @@ -14,9 +14,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alipay.sofa.tracer.plugins.zipkin.adapter; +package com.alipay.common.tracer.core.utils; -import com.alipay.common.tracer.core.utils.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -30,7 +29,7 @@ * * @author GengZhang */ -class NetUtils { +public class NetUtils { /** * slf4j Logger for this class */ diff --git a/tracer-sofa-boot-starter/pom.xml b/tracer-sofa-boot-starter/pom.xml index 4af66ef6e..e9543e299 100644 --- a/tracer-sofa-boot-starter/pom.xml +++ b/tracer-sofa-boot-starter/pom.xml @@ -59,6 +59,10 @@ com.alipay.sofa sofa-tracer-zipkin-plugin + + com.alipay.sofa + sofa-tracer-skywalking-plugin + com.alipay.sofa sofa-tracer-flexible-plugin @@ -280,5 +284,9 @@ 1.3.1 test + + org.projectlombok + lombok + diff --git a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/skywalking/configuration/SkywalkingSofaTracerAutoConfiguration.java b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/skywalking/configuration/SkywalkingSofaTracerAutoConfiguration.java new file mode 100644 index 000000000..e3169a03a --- /dev/null +++ b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/skywalking/configuration/SkywalkingSofaTracerAutoConfiguration.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.tracer.boot.skywalking.configuration; + +import com.alipay.sofa.tracer.boot.skywalking.properties.SkywalkingSofaTracerProperties; +import com.alipay.sofa.tracer.plugins.skywalking.SkywalkingSpanRemoteReporter; +import com.alipay.sofa.tracer.plugins.zipkin.ZipkinSofaTracerSpanRemoteReporter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +/** + * SkywalkingSofaTracerAutoConfiguration + * @author zhaochen + */ +@Configuration +@EnableConfigurationProperties(SkywalkingSofaTracerProperties.class) +@ConditionalOnProperty(value = "com.alipay.sofa.tracer.skywalking.enabled", matchIfMissing = false) +public class SkywalkingSofaTracerAutoConfiguration { + @Autowired + private SkywalkingSofaTracerProperties skywalkingSofaTracerProperties; + + @Bean + @ConditionalOnMissingBean + public SkywalkingSpanRemoteReporter skywalkingSpanRemoteReporter() { + return new SkywalkingSpanRemoteReporter(skywalkingSofaTracerProperties.getBaseUrl(), + skywalkingSofaTracerProperties.getMaxBufferSize()); + } + +} diff --git a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/skywalking/properties/SkywalkingSofaTracerProperties.java b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/skywalking/properties/SkywalkingSofaTracerProperties.java new file mode 100644 index 000000000..f4a3c1c46 --- /dev/null +++ b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/skywalking/properties/SkywalkingSofaTracerProperties.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.tracer.boot.skywalking.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * SkywalkingSofaTracerProperties + * @author zhaochen + */ +@ConfigurationProperties("com.alipay.sofa.tracer.skywalking") +public class SkywalkingSofaTracerProperties { + + private String baseUrl = "http://localhost:12800/"; + private boolean enabled = false; + // size of the buffer + private int maxBufferSize = 10000; + + public String getBaseUrl() { + return this.baseUrl; + } + + public boolean isEnabled() { + return this.enabled; + } + + public void setBaseUrl(String baseUrl) { + this.baseUrl = baseUrl; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public void setMaxBufferSize(int maxBufferSize) { + this.maxBufferSize = maxBufferSize; + } + + public int getMaxBufferSize() { + return this.maxBufferSize; + } + +} diff --git a/tracer-sofa-boot-starter/src/main/resources/META-INF/spring.factories b/tracer-sofa-boot-starter/src/main/resources/META-INF/spring.factories index 648a2e920..8f6a82a5a 100644 --- a/tracer-sofa-boot-starter/src/main/resources/META-INF/spring.factories +++ b/tracer-sofa-boot-starter/src/main/resources/META-INF/spring.factories @@ -2,6 +2,7 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.alipay.sofa.tracer.boot.configuration.SofaTracerAutoConfiguration,\ com.alipay.sofa.tracer.boot.springmvc.configuration.OpenTracingSpringMvcAutoConfiguration,\ com.alipay.sofa.tracer.boot.zipkin.configuration.ZipkinSofaTracerAutoConfiguration,\ +com.alipay.sofa.tracer.boot.skywalking.configuration.SkywalkingSofaTracerAutoConfiguration,\ com.alipay.sofa.tracer.boot.datasource.configuration.SofaTracerDataSourceAutoConfiguration,\ com.alipay.sofa.tracer.boot.springcloud.configuration.SofaTracerFeignClientAutoConfiguration,\ com.alipay.sofa.tracer.boot.message.configuration.SpringMessageAutoConfiguration,\ From 297fb86f96ec3a5a22db88e9b6321e4659766bae Mon Sep 17 00:00:00 2001 From: zzuzh Date: Tue, 14 Sep 2021 20:47:04 +0800 Subject: [PATCH 2/2] do not skip analysize --- .../plugins/dubbo/DubboSofaTracerFilter.java | 22 +++ .../dubbo26x/DubboSofaTracerFilter.java | 21 +++ .../dubbo27x/DubboSofaTracerFilter.java | 20 +++ .../plugins/dubbo/DubboSofaTracerFilter.java | 21 +++ .../AbstractHttpRequestInterceptor.java | 33 ++++ .../mongodb/SofaTracerCommandListener.java | 12 +- .../SofaTracerOkHttpInterceptor.java | 9 ++ .../AsyncRestTemplateRequestInterceptor.java | 8 + .../interceptor/RestTemplateInterceptor.java | 9 ++ .../SofaTracerConsumeMessageHook.java | 5 + .../SofaTracerSendMessageHook.java | 5 + .../adapter/SkywalkingSegmentAdapter.java | 76 ++++++--- .../utils/ComponentName2ComponentId.java | 8 +- .../feign/SofaTracerFeignClient.java | 7 +- .../springmvc/SpringMvcSofaTracerFilter.java | 2 +- .../alipay/common/tracer/core/SofaTracer.java | 17 ++- .../context/span/SofaTracerSpanContext.java | 144 ++++++++++++++++-- .../registry/AbstractTextB3Formatter.java | 88 ++++++++++- .../tracer/core/span/SofaTracerSpan.java | 2 + .../common/tracer/core/utils/NetUtils.java | 14 ++ .../SofaTracerConfigurationListener.java | 4 +- 21 files changed, 473 insertions(+), 54 deletions(-) diff --git a/sofa-tracer-plugins/sofa-tracer-dubbo-2.6.x-plugin/src/main/java/com/alipay/sofa/tracer/plugins/dubbo/DubboSofaTracerFilter.java b/sofa-tracer-plugins/sofa-tracer-dubbo-2.6.x-plugin/src/main/java/com/alipay/sofa/tracer/plugins/dubbo/DubboSofaTracerFilter.java index d72fd076c..6b3a974c0 100644 --- a/sofa-tracer-plugins/sofa-tracer-dubbo-2.6.x-plugin/src/main/java/com/alipay/sofa/tracer/plugins/dubbo/DubboSofaTracerFilter.java +++ b/sofa-tracer-plugins/sofa-tracer-dubbo-2.6.x-plugin/src/main/java/com/alipay/sofa/tracer/plugins/dubbo/DubboSofaTracerFilter.java @@ -40,6 +40,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; @@ -47,6 +48,7 @@ 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; @@ -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(); @@ -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 { diff --git a/sofa-tracer-plugins/sofa-tracer-dubbo-common-plugin/src/main/java/com/alipay/sofa/tracer/plugins/dubbo26x/DubboSofaTracerFilter.java b/sofa-tracer-plugins/sofa-tracer-dubbo-common-plugin/src/main/java/com/alipay/sofa/tracer/plugins/dubbo26x/DubboSofaTracerFilter.java index 075b255df..dbd7eaf97 100644 --- a/sofa-tracer-plugins/sofa-tracer-dubbo-common-plugin/src/main/java/com/alipay/sofa/tracer/plugins/dubbo26x/DubboSofaTracerFilter.java +++ b/sofa-tracer-plugins/sofa-tracer-dubbo-common-plugin/src/main/java/com/alipay/sofa/tracer/plugins/dubbo26x/DubboSofaTracerFilter.java @@ -40,6 +40,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; @@ -47,6 +48,7 @@ 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; @@ -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(); @@ -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 { diff --git a/sofa-tracer-plugins/sofa-tracer-dubbo-common-plugin/src/main/java/com/alipay/sofa/tracer/plugins/dubbo27x/DubboSofaTracerFilter.java b/sofa-tracer-plugins/sofa-tracer-dubbo-common-plugin/src/main/java/com/alipay/sofa/tracer/plugins/dubbo27x/DubboSofaTracerFilter.java index 82280abbf..157364274 100644 --- a/sofa-tracer-plugins/sofa-tracer-dubbo-common-plugin/src/main/java/com/alipay/sofa/tracer/plugins/dubbo27x/DubboSofaTracerFilter.java +++ b/sofa-tracer-plugins/sofa-tracer-dubbo-common-plugin/src/main/java/com/alipay/sofa/tracer/plugins/dubbo27x/DubboSofaTracerFilter.java @@ -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; @@ -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; @@ -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(); @@ -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 { diff --git a/sofa-tracer-plugins/sofa-tracer-dubbo-plugin/src/main/java/com/alipay/sofa/tracer/plugins/dubbo/DubboSofaTracerFilter.java b/sofa-tracer-plugins/sofa-tracer-dubbo-plugin/src/main/java/com/alipay/sofa/tracer/plugins/dubbo/DubboSofaTracerFilter.java index e97f93aa5..3c388eead 100644 --- a/sofa-tracer-plugins/sofa-tracer-dubbo-plugin/src/main/java/com/alipay/sofa/tracer/plugins/dubbo/DubboSofaTracerFilter.java +++ b/sofa-tracer-plugins/sofa-tracer-dubbo-plugin/src/main/java/com/alipay/sofa/tracer/plugins/dubbo/DubboSofaTracerFilter.java @@ -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; @@ -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; @@ -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(); @@ -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 { @@ -387,6 +407,7 @@ private void appendRpcServerSpanTags(Invoker invoker, SofaTracerSpan sofaTrac 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) { diff --git a/sofa-tracer-plugins/sofa-tracer-httpclient-plugin/src/main/java/com/alipay/sofa/tracer/plugins/httpclient/interceptor/AbstractHttpRequestInterceptor.java b/sofa-tracer-plugins/sofa-tracer-httpclient-plugin/src/main/java/com/alipay/sofa/tracer/plugins/httpclient/interceptor/AbstractHttpRequestInterceptor.java index f909a897c..d3e44d217 100644 --- a/sofa-tracer-plugins/sofa-tracer-httpclient-plugin/src/main/java/com/alipay/sofa/tracer/plugins/httpclient/interceptor/AbstractHttpRequestInterceptor.java +++ b/sofa-tracer-plugins/sofa-tracer-httpclient-plugin/src/main/java/com/alipay/sofa/tracer/plugins/httpclient/interceptor/AbstractHttpRequestInterceptor.java @@ -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; @@ -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 * @@ -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); @@ -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; + } } diff --git a/sofa-tracer-plugins/sofa-tracer-mongodb-plugin/src/main/java/com/alipay/sofa/tracer/plugins/mongodb/SofaTracerCommandListener.java b/sofa-tracer-plugins/sofa-tracer-mongodb-plugin/src/main/java/com/alipay/sofa/tracer/plugins/mongodb/SofaTracerCommandListener.java index 814611bb8..518bfca58 100644 --- a/sofa-tracer-plugins/sofa-tracer-mongodb-plugin/src/main/java/com/alipay/sofa/tracer/plugins/mongodb/SofaTracerCommandListener.java +++ b/sofa-tracer-plugins/sofa-tracer-mongodb-plugin/src/main/java/com/alipay/sofa/tracer/plugins/mongodb/SofaTracerCommandListener.java @@ -20,7 +20,9 @@ import com.alipay.common.tracer.core.holder.SofaTraceContextHolder; import com.alipay.common.tracer.core.span.CommonSpanTags; import com.alipay.common.tracer.core.span.SofaTracerSpan; +import com.alipay.common.tracer.core.utils.NetUtils; import com.alipay.sofa.tracer.plugins.mongodb.tracers.MongoClientTracer; +import com.mongodb.ServerAddress; import com.mongodb.event.CommandFailedEvent; import com.mongodb.event.CommandListener; import com.mongodb.event.CommandStartedEvent; @@ -29,6 +31,7 @@ import java.io.PrintWriter; import java.io.StringWriter; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.util.HashMap; import java.util.Map; @@ -115,11 +118,13 @@ private void finishSpan(SofaTracerSpan sofaTracerSpan, Throwable throwable) { private void decorate(SofaTracerSpan span, CommandStartedEvent event) { String command = event.getCommandName(); + String host = event.getConnectionDescription().getServerAddress().getHost(); + InetAddress hostAddress = NetUtils.getIpAddress(host); span.setTag(Tags.COMPONENT.getKey(), COMPONENT_NAME); span.setTag(Tags.DB_STATEMENT.getKey(), event.getCommand().toString()); span.setTag(Tags.DB_INSTANCE.getKey(), event.getDatabaseName()); - span.setTag(Tags.PEER_HOSTNAME.getKey(), event.getConnectionDescription() - .getServerAddress().getHost()); + span.setTag(Tags.PEER_HOSTNAME.getKey(), + hostAddress == null ? host : hostAddress.getHostAddress()); InetSocketAddress address = event.getConnectionDescription().getServerAddress() .getSocketAddress(); if (address != null) { @@ -127,6 +132,9 @@ private void decorate(SofaTracerSpan span, CommandStartedEvent event) { } span.setTag(Tags.PEER_PORT.getKey(), event.getConnectionDescription().getServerAddress() .getPort()); + ServerAddress serverAddress = event.getConnectionDescription().getServerAddress(); + span.getSofaTracerSpanContext().setPeer( + serverAddress.getHost() + ":" + serverAddress.getPort()); span.setTag(Tags.DB_TYPE.getKey(), "mongodb"); span.setTag(CommonSpanTags.METHOD, command); span.setTag(CommonSpanTags.LOCAL_APP, applicationName); diff --git a/sofa-tracer-plugins/sofa-tracer-okhttp-plugin/src/main/java/com/alipay/sofa/tracer/plugins/okhttp/interceptor/SofaTracerOkHttpInterceptor.java b/sofa-tracer-plugins/sofa-tracer-okhttp-plugin/src/main/java/com/alipay/sofa/tracer/plugins/okhttp/interceptor/SofaTracerOkHttpInterceptor.java index 01513df4f..d61e73109 100644 --- a/sofa-tracer-plugins/sofa-tracer-okhttp-plugin/src/main/java/com/alipay/sofa/tracer/plugins/okhttp/interceptor/SofaTracerOkHttpInterceptor.java +++ b/sofa-tracer-plugins/sofa-tracer-okhttp-plugin/src/main/java/com/alipay/sofa/tracer/plugins/okhttp/interceptor/SofaTracerOkHttpInterceptor.java @@ -22,6 +22,7 @@ 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.okhttp.OkHttpRequestCarrier; import okhttp3.Headers; @@ -29,6 +30,7 @@ import okhttp3.Response; import java.io.IOException; +import java.net.InetAddress; import java.util.List; /** @@ -71,6 +73,12 @@ private Request appendOkHttpRequestSpanTags(Request request, SofaTracerSpan sofa sofaTracerSpan.setTag(CommonSpanTags.LOCAL_APP, appName == null ? StringUtils.EMPTY_STRING : appName); sofaTracerSpan.setTag(CommonSpanTags.REQUEST_URL, request.url().toString()); + InetAddress ipAddress = NetUtils.getIpAddress(request.url().host()); + String host = ipAddress == null ? request.url().host() : ipAddress.getHostAddress(); + String port = String.valueOf(request.url().port()); + sofaTracerSpan.setTag(CommonSpanTags.REMOTE_HOST, host); + sofaTracerSpan.setTag(CommonSpanTags.REMOTE_PORT, port); + sofaTracerSpan.getSofaTracerSpanContext().setPeer(host + ":" + port); //method sofaTracerSpan.setTag(CommonSpanTags.METHOD, methodName); @@ -99,4 +107,5 @@ public Request injectCarrier(Request request, SofaTracerSpan currentSpan) { Request.Builder requestBuilder = request.newBuilder(); return requestBuilder.headers(headerBuilder.build()).build(); } + } diff --git a/sofa-tracer-plugins/sofa-tracer-resttmplate-plugin/src/main/java/com/sofa/alipay/tracer/plugins/rest/interceptor/AsyncRestTemplateRequestInterceptor.java b/sofa-tracer-plugins/sofa-tracer-resttmplate-plugin/src/main/java/com/sofa/alipay/tracer/plugins/rest/interceptor/AsyncRestTemplateRequestInterceptor.java index 87ec1731f..e9e7f4cc5 100644 --- a/sofa-tracer-plugins/sofa-tracer-resttmplate-plugin/src/main/java/com/sofa/alipay/tracer/plugins/rest/interceptor/AsyncRestTemplateRequestInterceptor.java +++ b/sofa-tracer-plugins/sofa-tracer-resttmplate-plugin/src/main/java/com/sofa/alipay/tracer/plugins/rest/interceptor/AsyncRestTemplateRequestInterceptor.java @@ -23,6 +23,7 @@ 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.sofa.alipay.tracer.plugins.rest.RestTemplateRequestCarrier; import io.opentracing.tag.Tags; @@ -35,6 +36,7 @@ import org.springframework.util.concurrent.ListenableFutureCallback; import java.io.IOException; +import java.net.InetAddress; import java.util.List; /** @@ -163,6 +165,12 @@ private void appendRestTemplateRequestSpanTags(HttpRequest request, //targetAppName sofaTracerSpan.setTag(CommonSpanTags.REMOTE_APP, StringUtils.EMPTY_STRING); sofaTracerSpan.setTag(CommonSpanTags.REQUEST_URL, request.getURI().toString()); + InetAddress ipAddress = NetUtils.getIpAddress(request.getURI().getHost()); + String host = ipAddress == null ? request.getURI().getHost() : ipAddress.getHostAddress(); + String port = String.valueOf(request.getURI().getPort()); + sofaTracerSpan.setTag(CommonSpanTags.REMOTE_HOST, host); + sofaTracerSpan.setTag(CommonSpanTags.REMOTE_PORT, port); + sofaTracerSpan.getSofaTracerSpanContext().setPeer(host + ":" + port); //method sofaTracerSpan.setTag(CommonSpanTags.METHOD, methodName); HttpHeaders headers = request.getHeaders(); diff --git a/sofa-tracer-plugins/sofa-tracer-resttmplate-plugin/src/main/java/com/sofa/alipay/tracer/plugins/rest/interceptor/RestTemplateInterceptor.java b/sofa-tracer-plugins/sofa-tracer-resttmplate-plugin/src/main/java/com/sofa/alipay/tracer/plugins/rest/interceptor/RestTemplateInterceptor.java index 0e23208a1..52928b6dc 100644 --- a/sofa-tracer-plugins/sofa-tracer-resttmplate-plugin/src/main/java/com/sofa/alipay/tracer/plugins/rest/interceptor/RestTemplateInterceptor.java +++ b/sofa-tracer-plugins/sofa-tracer-resttmplate-plugin/src/main/java/com/sofa/alipay/tracer/plugins/rest/interceptor/RestTemplateInterceptor.java @@ -25,6 +25,7 @@ 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.sofa.alipay.tracer.plugins.rest.RestTemplateRequestCarrier; import io.opentracing.tag.Tags; @@ -35,6 +36,7 @@ import org.springframework.http.client.ClientHttpResponse; import java.io.IOException; +import java.net.InetAddress; import java.util.List; /** @@ -126,6 +128,12 @@ private void appendRestTemplateRequestSpanTags(HttpRequest request, sofaTracerSpan.setTag(CommonSpanTags.REQUEST_URL, request.getURI().toString()); //method sofaTracerSpan.setTag(CommonSpanTags.METHOD, methodName); + InetAddress ipAddress = NetUtils.getIpAddress(request.getURI().getHost()); + String host = ipAddress == null ? request.getURI().getHost() : ipAddress.getHostAddress(); + String port = String.valueOf(request.getURI().getPort()); + sofaTracerSpan.setTag(CommonSpanTags.REMOTE_HOST, host); + sofaTracerSpan.setTag(CommonSpanTags.REMOTE_PORT, port); + sofaTracerSpan.getSofaTracerSpanContext().setPeer(host + ":" + port); HttpHeaders headers = request.getHeaders(); //reqSize if (headers != null && headers.containsKey("Content-Length")) { @@ -146,4 +154,5 @@ public void injectCarrier(HttpRequest request, SofaTracerSpan currentSpan) { sofaTracer.inject(currentSpan.getSofaTracerSpanContext(), ExtendFormat.Builtin.B3_HTTP_HEADERS, new RestTemplateRequestCarrier(request)); } + } diff --git a/sofa-tracer-plugins/sofa-tracer-rocketmq-plugin/src/main/java/com/alipay/sofa/tracer/plugins/rocketmq/interceptor/SofaTracerConsumeMessageHook.java b/sofa-tracer-plugins/sofa-tracer-rocketmq-plugin/src/main/java/com/alipay/sofa/tracer/plugins/rocketmq/interceptor/SofaTracerConsumeMessageHook.java index fc1b82af9..21d5a1e11 100644 --- a/sofa-tracer-plugins/sofa-tracer-rocketmq-plugin/src/main/java/com/alipay/sofa/tracer/plugins/rocketmq/interceptor/SofaTracerConsumeMessageHook.java +++ b/sofa-tracer-plugins/sofa-tracer-rocketmq-plugin/src/main/java/com/alipay/sofa/tracer/plugins/rocketmq/interceptor/SofaTracerConsumeMessageHook.java @@ -87,5 +87,10 @@ private void appendTags(ConsumeMessageContext context, MessageExt msg, SofaTrace span.setTag("broker", brokerName); span.setTag(CommonSpanTags.MSG_ID, msg.getMsgId()); span.setTag("status", context.getStatus()); + String[] remote = msg.getStoreHost().toString().split(":"); + span.setTag(CommonSpanTags.REMOTE_HOST, remote[0].startsWith("/") ? remote[0].substring(1) + : remote[0]); + span.setTag(CommonSpanTags.REMOTE_PORT, remote[1]); + span.getSofaTracerSpanContext().setPeer(remote[0] + ":" + remote[1]); } } diff --git a/sofa-tracer-plugins/sofa-tracer-rocketmq-plugin/src/main/java/com/alipay/sofa/tracer/plugins/rocketmq/interceptor/SofaTracerSendMessageHook.java b/sofa-tracer-plugins/sofa-tracer-rocketmq-plugin/src/main/java/com/alipay/sofa/tracer/plugins/rocketmq/interceptor/SofaTracerSendMessageHook.java index b43b2cfb0..139ac9719 100644 --- a/sofa-tracer-plugins/sofa-tracer-rocketmq-plugin/src/main/java/com/alipay/sofa/tracer/plugins/rocketmq/interceptor/SofaTracerSendMessageHook.java +++ b/sofa-tracer-plugins/sofa-tracer-rocketmq-plugin/src/main/java/com/alipay/sofa/tracer/plugins/rocketmq/interceptor/SofaTracerSendMessageHook.java @@ -57,6 +57,8 @@ public String hookName() { @Override public void sendMessageBefore(SendMessageContext context) { SofaTracerSpan span = rocketMQSendTracer.clientSend("mq-message-send"); + String[] remote = context.getBrokerAddr().split(":"); + span.getSofaTracerSpanContext().setPeer(remote[0] + ":" + remote[1]); // put spanContext to message context.getMessage().putUserProperty("SOFA_TRACER_CONTEXT", span.getSofaTracerSpanContext().serializeSpanContext()); @@ -73,9 +75,12 @@ private void appendTags(SendMessageContext context, SofaTracerSpan span) { MessageType msgType = context.getMsgType(); Message message = context.getMessage(); SendResult sendResult = context.getSendResult(); + String[] remote = context.getBrokerAddr().split(":"); span.setTag("msgType", msgType.name()); span.setTag("bornHost", context.getBornHost()); span.setTag("brokerAddr", context.getBrokerAddr()); + span.setTag(CommonSpanTags.REMOTE_HOST, remote[0]); + span.setTag(CommonSpanTags.REMOTE_PORT, remote[1]); span.setTag("producerGroup", context.getProducerGroup()); span.setTag(CommonSpanTags.MSG_TOPIC, message.getTopic()); span.setTag(CommonSpanTags.MSG_ID, sendResult.getMsgId()); diff --git a/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/adapter/SkywalkingSegmentAdapter.java b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/adapter/SkywalkingSegmentAdapter.java index 68b00e694..1bf33b4a7 100644 --- a/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/adapter/SkywalkingSegmentAdapter.java +++ b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/adapter/SkywalkingSegmentAdapter.java @@ -18,9 +18,11 @@ import com.alipay.common.tracer.core.constants.ComponentNameConstants; import com.alipay.common.tracer.core.constants.SofaTracerConstant; +import com.alipay.common.tracer.core.context.span.SofaTracerSpanContext; 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.span.SofaTracerSpanReferenceRelationship; import com.alipay.common.tracer.core.utils.NetUtils; import com.alipay.common.tracer.core.utils.StringUtils; import com.alipay.sofa.tracer.plugins.skywalking.model.Segment; @@ -32,6 +34,8 @@ import com.alipay.sofa.tracer.plugins.skywalking.model.RefType; import com.alipay.sofa.tracer.plugins.skywalking.utils.ComponentName2ComponentId; import com.alipay.sofa.tracer.plugins.skywalking.utils.ComponentName2SpanLayer; +import io.opentracing.References; +import io.opentracing.tag.Tags; import java.net.InetAddress; import java.util.LinkedHashMap; @@ -71,8 +75,13 @@ public Segment convertToSkywalkingSegment(SofaTracerSpan sofaTracerSpan) { * @return segmentId */ private String generateSegmentId(SofaTracerSpan sofaTracerSpan) { - return sofaTracerSpan.getSofaTracerSpanContext().getTraceId() - + FNV64HashCode(sofaTracerSpan.getSofaTracerSpanContext().getSpanId()) + String prefix = sofaTracerSpan.getSofaTracerSpanContext().getTraceId() + + FNV64HashCode(sofaTracerSpan.getSofaTracerSpanContext().getSpanId()); + // when tracerType equals flexible-biz, span kind is always client + if (sofaTracerSpan.getSofaTracer().getTracerType().equals(ComponentNameConstants.FLEXIBLE)) { + return prefix + SofaTracerConstant.SERVER; + } + return prefix + (sofaTracerSpan.isServer() ? SofaTracerConstant.SERVER : SofaTracerConstant.CLIENT); } @@ -100,24 +109,28 @@ private Span constructSpan(SofaTracerSpan sofaTracerSpan) { //map tracerType in sofaTracer to ComponentId in skyWalking span.setComponentId(getComponentId(sofaTracerSpan)); - span.setError(!isWebHttpClientSuccess(sofaTracerSpan.getTagsWithStr().get( - CommonSpanTags.RESULT_CODE))); - span.setSkipAnalysis(true); + span.setError(sofaTracerSpan.getTagsWithStr().containsKey("error")); + span.setSkipAnalysis(false); span = convertSpanTags(sofaTracerSpan, span); convertSpanLogs(sofaTracerSpan, span); // if has patentId then need to add segmentReference if (!StringUtils.isBlank(sofaTracerSpan.getSofaTracerSpanContext().getParentId())) { span = addSegmentReference(sofaTracerSpan, span); } - // Dubbo String remoteHost = sofaTracerSpan.getTagsWithStr().get(CommonSpanTags.REMOTE_HOST); String remotePort = sofaTracerSpan.getTagsWithStr().get(CommonSpanTags.REMOTE_PORT); // sofaRpc String remoteIp = sofaTracerSpan.getTagsWithStr().get("remote.ip"); - + // mongodb + String peerHost = sofaTracerSpan.getTagsWithStr().get(Tags.PEER_HOSTNAME.getKey()); + String peerPort = String.valueOf(sofaTracerSpan.getTagsWithNumber().get( + Tags.PEER_PORT.getKey())); if (remoteHost != null && remotePort != null) { span.setPeer(remoteHost + ":" + remotePort); } + if (peerHost != null && peerPort != null) { + span.setPeer(peerHost + ":" + peerPort); + } // if the span is formed by sofaRPC, we can only get ip of the server to generate networkAddressUsedAtPeer if (sofaTracerSpan.getSofaTracer().getTracerType().equals(ComponentNameConstants.SOFA_RPC) && remoteIp != null) { @@ -211,6 +224,7 @@ private Span convertSpanLogs(SofaTracerSpan sofaTracerSpan, Span swSpan) { * @return span with segment reference in SkyWalking format */ private Span addSegmentReference(SofaTracerSpan sofaTracerSpan, Span swSpan) { + SofaTracerSpanContext spanContext = sofaTracerSpan.getSofaTracerSpanContext(); SegmentReference segmentReference = new SegmentReference(); //default set to crossProcess segmentReference.setRefType(RefType.CrossProcess); @@ -218,6 +232,10 @@ private Span addSegmentReference(SofaTracerSpan sofaTracerSpan, Span swSpan) { segmentReference.setParentTraceSegmentId(getParentSegmentId(sofaTracerSpan)); //because there is only one span in each segment so parentId is 0 segmentReference.setParentSpanId(0); + segmentReference.setParentService(spanContext.getParentService()); + segmentReference.setParentServiceInstance(spanContext.getParentServiceInstance()); + segmentReference.setParentEndpoint(spanContext.getParentOperationName()); + String networkAddressUsedAtPeer = getNetworkAddressUsedAtPeer(sofaTracerSpan); if (networkAddressUsedAtPeer != null) { segmentReference.setNetworkAddressUsedAtPeer(networkAddressUsedAtPeer); @@ -231,11 +249,20 @@ private String getNetworkAddressUsedAtPeer(SofaTracerSpan sofaTracerSpan) { if (sofaTracerSpan.getSofaTracer().getTracerType().equals(ComponentNameConstants.SOFA_RPC)) { return NetUtils.getLocalIpv4(); } - Map strTags = sofaTracerSpan.getTagsWithStr(); - String host = strTags.get(CommonSpanTags.LOCAL_HOST); - String port = strTags.get(CommonSpanTags.LOCAL_PORT); - if (host != null && port != null) { - return host + ":" + port; + String tracerType = sofaTracerSpan.getSofaTracer().getTracerType(); + if (tracerType.equals(ComponentNameConstants.DUBBO_SERVER) + || tracerType.equals(ComponentNameConstants.DUBBO_CLIENT)) { + Map strTags = sofaTracerSpan.getTagsWithStr(); + String host = strTags.get(CommonSpanTags.LOCAL_HOST); + String port = strTags.get(CommonSpanTags.LOCAL_PORT); + if (host != null && port != null) { + return host + ":" + port; + } + } + if (sofaTracerSpan.getSpanReferences().size() >= 1) { + SofaTracerSpanContext parentSpanContext = preferredReference(sofaTracerSpan + .getSpanReferences()); + return parentSpanContext.getPeer(); } return null; } @@ -264,17 +291,6 @@ private int getComponentId(SofaTracerSpan sofaTracerSpan) { } - private boolean isHttpOrMvcSuccess(String resultCode) { - return resultCode.charAt(0) == '1' || resultCode.charAt(0) == '2' - || "302".equals(resultCode.trim()) || ("301".equals(resultCode.trim())); - } - - private boolean isWebHttpClientSuccess(String resultCode) { - return StringUtils.isNotBlank(resultCode) - && (isHttpOrMvcSuccess(resultCode) || SofaTracerConstant.RESULT_CODE_SUCCESS - .equals(resultCode)); - } - /** * from http://en.wikipedia.org/wiki/Fowler_Noll_Vo_hash * @@ -292,4 +308,18 @@ public static long FNV64HashCode(String data) { return hash; } + private SofaTracerSpanContext preferredReference(List references) { + SofaTracerSpanReferenceRelationship preferredReference = references.get(0); + for (SofaTracerSpanReferenceRelationship reference : references) { + // childOf takes precedence as a preferred parent + String referencedType = reference.getReferenceType(); + if (References.CHILD_OF.equals(referencedType) + && !References.CHILD_OF.equals(preferredReference.getReferenceType())) { + preferredReference = reference; + break; + } + } + return preferredReference.getSofaTracerSpanContext(); + } + } diff --git a/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/utils/ComponentName2ComponentId.java b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/utils/ComponentName2ComponentId.java index 52cae8b28..ad6164f62 100644 --- a/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/utils/ComponentName2ComponentId.java +++ b/sofa-tracer-plugins/sofa-tracer-skywalking-plugin/src/main/java/com/alipay/sofa/tracer/plugins/skywalking/utils/ComponentName2ComponentId.java @@ -27,13 +27,13 @@ public class ComponentName2ComponentId { public static final HashMap componentName2IDMap = new HashMap<>(); static { - //componentId in SkyWalking: https://github.com/apache/skywalking/blob/master/oap-server/server-bootstrap/src/main/resources/component-libraries.yml + //componentId in SkyWalking: https://github.com/apache/skywalking/blob/master/oap-server/server-starter/src/main/resources/component-libraries.yml componentName2IDMap.put(ComponentNameConstants.UNKNOWN, 0); componentName2IDMap.put(ComponentNameConstants.H2, 4); componentName2IDMap.put(ComponentNameConstants.MYSQL, 5); componentName2IDMap.put(ComponentNameConstants.ORACLE, 6); componentName2IDMap.put(ComponentNameConstants.REDIS, 7); - componentName2IDMap.put(ComponentNameConstants.MONGODB, 9); + componentName2IDMap.put(ComponentNameConstants.MONGODB, 42); componentName2IDMap.put(ComponentNameConstants.MEMCACHED, 20); componentName2IDMap.put(ComponentNameConstants.SOFA_RPC, 43); componentName2IDMap.put(ComponentNameConstants.DUBBO_CLIENT, 3); @@ -45,8 +45,8 @@ public class ComponentName2ComponentId { componentName2IDMap.put(ComponentNameConstants.FEIGN_CLIENT, 11); componentName2IDMap.put(ComponentNameConstants.KAFKAMQ_CONSUMER, 41); componentName2IDMap.put(ComponentNameConstants.KAFKAMQ_SEND, 40); - componentName2IDMap.put(ComponentNameConstants.ROCKETMQ_CONSUMER, 39); - componentName2IDMap.put(ComponentNameConstants.ROCKETMQ_SEND, 38); + componentName2IDMap.put(ComponentNameConstants.ROCKETMQ_CONSUMER, 25); + componentName2IDMap.put(ComponentNameConstants.ROCKETMQ_SEND, 25); componentName2IDMap.put(ComponentNameConstants.RABBITMQ_CONSUMER, 53); componentName2IDMap.put(ComponentNameConstants.RABBITMQ_SEND, 52); componentName2IDMap.put(ComponentNameConstants.MONGO_CLIENT, 42); diff --git a/sofa-tracer-plugins/sofa-tracer-spring-cloud-plugin/src/main/java/com/alipay/sofa/tracer/plugins/springcloud/instruments/feign/SofaTracerFeignClient.java b/sofa-tracer-plugins/sofa-tracer-spring-cloud-plugin/src/main/java/com/alipay/sofa/tracer/plugins/springcloud/instruments/feign/SofaTracerFeignClient.java index 8de5fa5f1..afdb27bb1 100644 --- a/sofa-tracer-plugins/sofa-tracer-spring-cloud-plugin/src/main/java/com/alipay/sofa/tracer/plugins/springcloud/instruments/feign/SofaTracerFeignClient.java +++ b/sofa-tracer-plugins/sofa-tracer-spring-cloud-plugin/src/main/java/com/alipay/sofa/tracer/plugins/springcloud/instruments/feign/SofaTracerFeignClient.java @@ -22,6 +22,7 @@ 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.utils.NetUtils; import com.alipay.common.tracer.core.utils.StringUtils; import com.alipay.sofa.tracer.plugins.springcloud.carriers.FeignRequestCarrier; import com.alipay.sofa.tracer.plugins.springcloud.tracers.FeignClientTracer; @@ -30,6 +31,7 @@ import feign.Response; import io.opentracing.tag.Tags; +import java.net.InetAddress; import java.net.MalformedURLException; import java.net.URL; import java.util.LinkedHashMap; @@ -103,7 +105,9 @@ private String[] parseRemoteHostAndPort(Request request) { } catch (MalformedURLException e) { SelfLog.error("cannot parse remote host and port. request:" + request.url(), e); } - hostWithPort[0] = requestUrl != null ? requestUrl.getHost() : ""; + InetAddress ipAddress = NetUtils.getIpAddress(requestUrl.getHost()); + hostWithPort[0] = requestUrl != null ? (ipAddress != null ? ipAddress.getHostAddress() + : requestUrl.getHost()) : ""; hostWithPort[1] = String.valueOf(requestUrl != null ? requestUrl.getPort() : -1); return hostWithPort; } @@ -157,6 +161,7 @@ private void appendRequestSpanTags(Request request, SofaTracerSpan sofaTracerSpa String[] hostWithPort = parseRemoteHostAndPort(request); sofaTracerSpan.setTag(CommonSpanTags.REMOTE_HOST, hostWithPort[0]); sofaTracerSpan.setTag(CommonSpanTags.REMOTE_PORT, hostWithPort[1]); + sofaTracerSpan.getSofaTracerSpanContext().setPeer(hostWithPort[0] + ":" + hostWithPort[1]); if (request.body() != null) { sofaTracerSpan.setTag(CommonSpanTags.REQ_SIZE, request.body().length); diff --git a/sofa-tracer-plugins/sofa-tracer-springmvc-plugin/src/main/java/com/alipay/sofa/tracer/plugins/springmvc/SpringMvcSofaTracerFilter.java b/sofa-tracer-plugins/sofa-tracer-springmvc-plugin/src/main/java/com/alipay/sofa/tracer/plugins/springmvc/SpringMvcSofaTracerFilter.java index a6d43e196..89c9382c1 100644 --- a/sofa-tracer-plugins/sofa-tracer-springmvc-plugin/src/main/java/com/alipay/sofa/tracer/plugins/springmvc/SpringMvcSofaTracerFilter.java +++ b/sofa-tracer-plugins/sofa-tracer-springmvc-plugin/src/main/java/com/alipay/sofa/tracer/plugins/springmvc/SpringMvcSofaTracerFilter.java @@ -74,7 +74,7 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo SofaTracerSpanContext spanContext = getSpanContextFromRequest(request); // sr springMvcSpan = springMvcTracer.serverReceive(spanContext); - + springMvcSpan.getSofaTracerSpanContext().setPeer(spanContext.getPeer()); if (StringUtils.isBlank(this.appName)) { this.appName = SofaTracerConfiguration .getProperty(SofaTracerConfiguration.TRACER_APPNAME_KEY); diff --git a/tracer-core/src/main/java/com/alipay/common/tracer/core/SofaTracer.java b/tracer-core/src/main/java/com/alipay/common/tracer/core/SofaTracer.java index 9404db3d8..f5cafe825 100644 --- a/tracer-core/src/main/java/com/alipay/common/tracer/core/SofaTracer.java +++ b/tracer-core/src/main/java/com/alipay/common/tracer/core/SofaTracer.java @@ -17,6 +17,7 @@ package com.alipay.common.tracer.core; import com.alipay.common.tracer.core.appender.self.SelfLog; +import com.alipay.common.tracer.core.configuration.SofaTracerConfiguration; import com.alipay.common.tracer.core.constants.ComponentNameConstants; import com.alipay.common.tracer.core.context.span.SofaTracerSpanContext; import com.alipay.common.tracer.core.generator.TraceIdGenerator; @@ -31,6 +32,7 @@ import com.alipay.common.tracer.core.span.SofaTracerSpan; import com.alipay.common.tracer.core.span.SofaTracerSpanReferenceRelationship; import com.alipay.common.tracer.core.utils.AssertUtils; +import com.alipay.common.tracer.core.utils.NetUtils; import com.alipay.common.tracer.core.utils.StringUtils; import com.alipay.sofa.common.code.LogCode2Description; import io.opentracing.References; @@ -86,6 +88,9 @@ public class SofaTracer implements Tracer { */ private final Sampler sampler; + private String appName = StringUtils.EMPTY_STRING; + private String instance = StringUtils.EMPTY_STRING; + protected SofaTracer(String tracerType, Reporter clientReporter, Reporter serverReporter, Sampler sampler, Map tracerTags) { this.tracerType = tracerType; @@ -306,7 +311,15 @@ public Span start() { long begin = this.startTime > 0 ? this.startTime : System.currentTimeMillis(); SofaTracerSpan sofaTracerSpan = new SofaTracerSpan(SofaTracer.this, begin, this.references, this.operationName, sofaTracerSpanContext, this.tags); - + if ((StringUtils.isBlank(SofaTracer.this.appName) || StringUtils + .isBlank(SofaTracer.this.instance))) { + SofaTracer.this.appName = SofaTracerConfiguration + .getProperty(SofaTracerConfiguration.TRACER_APPNAME_KEY); + SofaTracer.this.instance = SofaTracer.this.appName + "@" + + NetUtils.getLocalAddress().getHostAddress(); + } + sofaTracerSpanContext.setParams(SofaTracer.this.appName, SofaTracer.this.instance, + this.operationName); // calculate isSampled,but do not change parent's sampler behaviour boolean isSampled = calculateSampler(sofaTracerSpan); sofaTracerSpanContext.setSampled(isSampled); @@ -344,6 +357,8 @@ private SofaTracerSpanContext createChildContext() { SofaTracerSpanContext sofaTracerSpanContext = new SofaTracerSpanContext( preferredReference.getTraceId(), preferredReference.nextChildContextId(), preferredReference.getSpanId(), preferredReference.isSampled()); + sofaTracerSpanContext.setParentParams(preferredReference.getService(), + preferredReference.getServiceInstance(), preferredReference.getOperationName()); sofaTracerSpanContext.addBizBaggage(this.createChildBaggage(true)); sofaTracerSpanContext.addSysBaggage(this.createChildBaggage(false)); return sofaTracerSpanContext; diff --git a/tracer-core/src/main/java/com/alipay/common/tracer/core/context/span/SofaTracerSpanContext.java b/tracer-core/src/main/java/com/alipay/common/tracer/core/context/span/SofaTracerSpanContext.java index 7711e7c57..ecc9634e1 100644 --- a/tracer-core/src/main/java/com/alipay/common/tracer/core/context/span/SofaTracerSpanContext.java +++ b/tracer-core/src/main/java/com/alipay/common/tracer/core/context/span/SofaTracerSpanContext.java @@ -37,50 +37,65 @@ public class SofaTracerSpanContext implements SpanContext { //spanId separator - public static final String RPC_ID_SEPARATOR = "."; + public static final String RPC_ID_SEPARATOR = "."; //======================== The following is the key for serializing data ======================== - private static final String TRACE_ID_KET = "tcid"; + private static final String TRACE_ID_KET = "tcid"; - private static final String SPAN_ID_KET = "spid"; + private static final String SPAN_ID_KET = "spid"; - private static final String PARENT_SPAN_ID_KET = "pspid"; + private static final String PARENT_SPAN_ID_KET = "pspid"; - private static final String SAMPLE_KET = "sample"; + private static final String SAMPLE_KET = "sample"; + private static final String SERVICE_KET = "service"; + private static final String SERVICE_INSTANCE_KET = "instance"; + private static final String OPERATION_NAME = "opname"; + private static final String PARENT_SERVICE_KET = "pservice"; + private static final String PARENT_SERVICE_INSTANCE_KET = "pinstance"; + private static final String PARENT_OPERATION_NAME = "popname"; + private static final String PEER_KET = "peer"; /** * The serialization system transparently passes the prefix of the attribute key */ - private static final String SYS_BAGGAGE_PREFIX_KEY = "_sys_"; + private static final String SYS_BAGGAGE_PREFIX_KEY = "_sys_"; - private String traceId = StringUtils.EMPTY_STRING; + private String traceId = StringUtils.EMPTY_STRING; - private String spanId = StringUtils.EMPTY_STRING; + private String spanId = StringUtils.EMPTY_STRING; - private String parentId = StringUtils.EMPTY_STRING; + private String parentId = StringUtils.EMPTY_STRING; + + private String service = StringUtils.EMPTY_STRING; + private String serviceInstance = StringUtils.EMPTY_STRING; + private String operationName = StringUtils.EMPTY_STRING; + private String parentService = StringUtils.EMPTY_STRING; + private String parentServiceInstance = StringUtils.EMPTY_STRING; + private String parentOperationName = StringUtils.EMPTY_STRING; + private String peer = StringUtils.EMPTY_STRING; /** * Default will not be sampled */ - private boolean isSampled = false; + private boolean isSampled = false; /** * The system transparently transmits data, * mainly refers to the transparent transmission data of the system dimension. * Note that this field cannot be used for transparent transmission of business. */ - private final Map sysBaggage = new ConcurrentHashMap(); + private final Map sysBaggage = new ConcurrentHashMap(); /** * Transparent transmission of data, mainly refers to the transparent transmission data of the business */ - private final Map bizBaggage = new ConcurrentHashMap(); + private final Map bizBaggage = new ConcurrentHashMap(); /** * sub-context counter */ - private AtomicInteger childContextIndex = new AtomicInteger(0); + private AtomicInteger childContextIndex = new AtomicInteger(0); /** * clone a SofaTracerSpanContext instance @@ -92,6 +107,10 @@ public SofaTracerSpanContext cloneInstance() { spanContext.addSysBaggage(this.sysBaggage); spanContext.addBizBaggage(this.bizBaggage); spanContext.childContextIndex = this.childContextIndex; + spanContext.setParentParams(this.parentService, this.parentServiceInstance, + this.parentOperationName); + spanContext.setParams(this.service, this.serviceInstance, this.operationName); + spanContext.setPeer(this.peer); return spanContext; } @@ -117,6 +136,23 @@ public SofaTracerSpanContext(String traceId, String spanId, String parentId, boo this.isSampled = isSampled; } + public void setParams(String service, String serviceInstance, String operationName) { + this.service = service; + this.serviceInstance = serviceInstance; + this.operationName = operationName; + } + + public void setParentParams(String parentService, String parentServiceInstance, + String parentOperationName) { + this.parentService = parentService; + this.parentServiceInstance = parentServiceInstance; + this.parentOperationName = parentOperationName; + } + + public void setPeer(String peer) { + this.peer = peer; + } + public SofaTracerSpanContext addBizBaggage(Map bizBaggage) { if (bizBaggage != null && bizBaggage.size() > 0) { this.bizBaggage.putAll(bizBaggage); @@ -216,6 +252,20 @@ public String serializeSpanContext() { .append(StringUtils.AND); serializedValue.append(SAMPLE_KET).append(StringUtils.EQUAL).append(isSampled) .append(StringUtils.AND); + serializedValue.append(SERVICE_KET).append(StringUtils.EQUAL).append(service) + .append(StringUtils.AND); + serializedValue.append(SERVICE_INSTANCE_KET).append(StringUtils.EQUAL) + .append(serviceInstance).append(StringUtils.AND); + serializedValue.append(OPERATION_NAME).append(StringUtils.EQUAL).append(operationName) + .append(StringUtils.AND); + serializedValue.append(PARENT_SERVICE_KET).append(StringUtils.EQUAL).append(parentService) + .append(StringUtils.AND); + serializedValue.append(PARENT_SERVICE_INSTANCE_KET).append(StringUtils.EQUAL) + .append(parentServiceInstance).append(StringUtils.AND); + serializedValue.append(PARENT_OPERATION_NAME).append(StringUtils.EQUAL) + .append(parentOperationName).append(StringUtils.AND); + serializedValue.append(PEER_KET).append(StringUtils.EQUAL).append(peer) + .append(StringUtils.AND); //system bizBaggage if (this.sysBaggage.size() > 0) { serializedValue.append(StringUtils.mapToStringWithPrefix(this.sysBaggage, @@ -245,6 +295,14 @@ public static SofaTracerSpanContext deserializeFromString(String deserializeValu String parentId = StringUtils.EMPTY_STRING; //sampled default is false boolean sampled = false; + + String service = StringUtils.EMPTY_STRING; + String serviceInstance = StringUtils.EMPTY_STRING; + String operationName = StringUtils.EMPTY_STRING; + String parentService = StringUtils.EMPTY_STRING; + String parentServiceInstance = StringUtils.EMPTY_STRING; + String parentOperationName = StringUtils.EMPTY_STRING; + String peer = StringUtils.EMPTY_STRING; //sys bizBaggage Map sysBaggage = new HashMap(); //bizBaggage @@ -275,6 +333,34 @@ public static SofaTracerSpanContext deserializeFromString(String deserializeValu sampled = Boolean.parseBoolean(value); continue; } + if (SERVICE_KET.equals(key)) { + service = value; + continue; + } + if (SERVICE_INSTANCE_KET.equals(key)) { + serviceInstance = value; + continue; + } + if (OPERATION_NAME.equals(key)) { + operationName = value; + continue; + } + if (PARENT_SERVICE_KET.equals(key)) { + parentService = value; + continue; + } + if (PARENT_SERVICE_INSTANCE_KET.equals(key)) { + parentServiceInstance = value; + continue; + } + if (PARENT_OPERATION_NAME.equals(key)) { + parentOperationName = value; + continue; + } + if (PEER_KET.equals(key)) { + peer = value; + continue; + } int sysIndex = key.indexOf(SYS_BAGGAGE_PREFIX_KEY); if (sysIndex == 0) { //must have a prefix @@ -287,6 +373,10 @@ public static SofaTracerSpanContext deserializeFromString(String deserializeValu } SofaTracerSpanContext sofaTracerSpanContext = new SofaTracerSpanContext(traceId, spanId, parentId, sampled); + sofaTracerSpanContext.setParams(service, serviceInstance, operationName); + sofaTracerSpanContext.setParentParams(parentService, parentServiceInstance, + parentOperationName); + sofaTracerSpanContext.setPeer(peer); if (sysBaggage.size() > 0) { sofaTracerSpanContext.addSysBaggage(sysBaggage); } @@ -410,6 +500,34 @@ public String lastChildContextId() { return this.spanId + RPC_ID_SEPARATOR + childContextIndex.get(); } + public String getService() { + return this.service; + } + + public String getServiceInstance() { + return this.serviceInstance; + } + + public String getOperationName() { + return this.operationName; + } + + public String getParentService() { + return this.parentService; + } + + public String getParentServiceInstance() { + return this.parentServiceInstance; + } + + public String getParentOperationName() { + return this.parentOperationName; + } + + public String getPeer() { + return this.peer; + } + @Override public boolean equals(Object o) { if (this == o) { diff --git a/tracer-core/src/main/java/com/alipay/common/tracer/core/registry/AbstractTextB3Formatter.java b/tracer-core/src/main/java/com/alipay/common/tracer/core/registry/AbstractTextB3Formatter.java index 19a1755cb..2613d29f0 100644 --- a/tracer-core/src/main/java/com/alipay/common/tracer/core/registry/AbstractTextB3Formatter.java +++ b/tracer-core/src/main/java/com/alipay/common/tracer/core/registry/AbstractTextB3Formatter.java @@ -28,32 +28,40 @@ public abstract class AbstractTextB3Formatter implements RegistryExtractorInject /** * 128/64 bit traceId lower-hex string (required) */ - public static final String TRACE_ID_KEY_HEAD = "X-B3-TraceId"; + public static final String TRACE_ID_KEY_HEAD = "X-B3-TraceId"; /** * 64 bit spanId lower-hex string (required) */ - public static final String SPAN_ID_KEY_HEAD = "X-B3-SpanId"; + public static final String SPAN_ID_KEY_HEAD = "X-B3-SpanId"; /** * 64 bit parentSpanId lower-hex string (absent on root span) */ - public static final String PARENT_SPAN_ID_KEY_HEAD = "X-B3-ParentSpanId"; + public static final String PARENT_SPAN_ID_KEY_HEAD = "X-B3-ParentSpanId"; /** * "1" means report this span to the tracing system, "0" means do not. (absent means defer the * decision to the receiver of this header). */ - public static final String SAMPLED_KEY_HEAD = "X-B3-Sampled"; + public static final String SAMPLED_KEY_HEAD = "X-B3-Sampled"; /** * "1" implies sampled and is a request to override collection-tier sampling policy. */ - static final String FLAGS_KEY_HEAD = "X-B3-Flags"; + static final String FLAGS_KEY_HEAD = "X-B3-Flags"; + + private static final String SERVICE_KEY_HEAD = "X-B3-service"; + private static final String SERVICE_INSTANCE_KEY_HEAD = "X-B3-serviceInstance"; + private static final String OPERATION_NAME_KEY_HEAD = "X-B3-operationName"; + private static final String PARENT_SERVICE_KEY_HEAD = "X-B3-parentService"; + private static final String PARENT_SERVICE_INSTANCE_KEY_HEAD = "X-B3-parentServiceInstance"; + private static final String PARENT_OPERATION_NAME_KEY_HEAD = "X-B3-parentOperationName"; + private static final String PEER_KEY_HEAD = "X-B3-peer"; /** * Baggage items prefix */ - static final String BAGGAGE_KEY_PREFIX = "baggage-"; + static final String BAGGAGE_KEY_PREFIX = "baggage-"; /** * System Baggage items prefix */ - static final String BAGGAGE_SYS_KEY_PREFIX = "baggage-sys-"; + static final String BAGGAGE_SYS_KEY_PREFIX = "baggage-sys-"; @Override public SofaTracerSpanContext extract(TextMap carrier) { @@ -67,6 +75,14 @@ public SofaTracerSpanContext extract(TextMap carrier) { String parentId = null; boolean sampled = false; boolean isGetSampled = false; + String service = null; + String serviceInstance = null; + String operationName = null; + String parentService = null; + String parentServiceInstance = null; + String parentOperationName = null; + String peer = null; + //sysBaggage Map sysBaggage = new ConcurrentHashMap(); //bizBaggage @@ -98,6 +114,29 @@ public SofaTracerSpanContext extract(TextMap carrier) { } isGetSampled = true; } + if (service == null && SERVICE_KEY_HEAD.equalsIgnoreCase(key)) { + service = decodedValue(entry.getValue()); + } + if (serviceInstance == null && SERVICE_INSTANCE_KEY_HEAD.equalsIgnoreCase(key)) { + serviceInstance = decodedValue(entry.getValue()); + } + if (operationName == null && OPERATION_NAME_KEY_HEAD.equalsIgnoreCase(key)) { + operationName = decodedValue(entry.getValue()); + } + if (parentService == null && PARENT_SERVICE_KEY_HEAD.equalsIgnoreCase(key)) { + parentService = decodedValue(entry.getValue()); + } + if (parentServiceInstance == null + && PARENT_SERVICE_INSTANCE_KEY_HEAD.equalsIgnoreCase(key)) { + parentServiceInstance = decodedValue(entry.getValue()); + } + if (parentOperationName == null && PARENT_OPERATION_NAME_KEY_HEAD.equalsIgnoreCase(key)) { + parentOperationName = decodedValue(entry.getValue()); + } + if (peer == null && PEER_KEY_HEAD.equalsIgnoreCase(key)) { + peer = decodedValue(entry.getValue()); + } + if (key.indexOf(BAGGAGE_SYS_KEY_PREFIX) == 0) { String keyTmp = StringUtils.unescapeEqualAndPercent(key).substring( BAGGAGE_SYS_KEY_PREFIX.length()); @@ -125,8 +164,33 @@ public SofaTracerSpanContext extract(TextMap carrier) { if (parentId == null) { parentId = StringUtils.EMPTY_STRING; } + if (service == null) { + service = StringUtils.EMPTY_STRING; + } + if (serviceInstance == null) { + serviceInstance = StringUtils.EMPTY_STRING; + } + if (operationName == null) { + operationName = StringUtils.EMPTY_STRING; + } + if (parentService == null) { + parentService = StringUtils.EMPTY_STRING; + } + if (parentServiceInstance == null) { + parentServiceInstance = StringUtils.EMPTY_STRING; + } + if (parentOperationName == null) { + parentOperationName = StringUtils.EMPTY_STRING; + } + if (peer == null) { + peer = StringUtils.EMPTY_STRING; + } SofaTracerSpanContext sofaTracerSpanContext = new SofaTracerSpanContext(traceId, spanId, parentId, sampled); + sofaTracerSpanContext.setParams(service, serviceInstance, operationName); + sofaTracerSpanContext.setParentParams(parentService, parentServiceInstance, + parentOperationName); + sofaTracerSpanContext.setPeer(peer); if (sysBaggage.size() > 0) { sofaTracerSpanContext.addSysBaggage(sysBaggage); } @@ -148,6 +212,16 @@ public void inject(SofaTracerSpanContext spanContext, TextMap carrier) { carrier.put(PARENT_SPAN_ID_KEY_HEAD, encodedValue(spanContext.getParentId())); carrier.put(SPAN_ID_KEY_HEAD, encodedValue(spanContext.getSpanId())); carrier.put(SAMPLED_KEY_HEAD, encodedValue(String.valueOf(spanContext.isSampled()))); + + carrier.put(SERVICE_KEY_HEAD, encodedValue(spanContext.getService())); + carrier.put(SERVICE_INSTANCE_KEY_HEAD, encodedValue(spanContext.getServiceInstance())); + carrier.put(OPERATION_NAME_KEY_HEAD, encodedValue(spanContext.getOperationName())); + carrier.put(PARENT_SERVICE_KEY_HEAD, encodedValue(spanContext.getParentService())); + carrier.put(PARENT_SERVICE_INSTANCE_KEY_HEAD, + encodedValue(spanContext.getParentServiceInstance())); + carrier.put(PARENT_OPERATION_NAME_KEY_HEAD, + encodedValue(spanContext.getParentOperationName())); + carrier.put(PEER_KEY_HEAD, encodedValue(spanContext.getPeer())); //System Baggage items for (Map.Entry entry : spanContext.getSysBaggage().entrySet()) { String key = BAGGAGE_SYS_KEY_PREFIX + StringUtils.escapePercentEqualAnd(entry.getKey()); diff --git a/tracer-core/src/main/java/com/alipay/common/tracer/core/span/SofaTracerSpan.java b/tracer-core/src/main/java/com/alipay/common/tracer/core/span/SofaTracerSpan.java index 91d21313d..e6a5c02c6 100644 --- a/tracer-core/src/main/java/com/alipay/common/tracer/core/span/SofaTracerSpan.java +++ b/tracer-core/src/main/java/com/alipay/common/tracer/core/span/SofaTracerSpan.java @@ -25,6 +25,7 @@ import com.alipay.common.tracer.core.reporter.facade.Reporter; import com.alipay.common.tracer.core.tags.SpanTags; import com.alipay.common.tracer.core.utils.AssertUtils; +import com.alipay.common.tracer.core.utils.NetUtils; import com.alipay.common.tracer.core.utils.StringUtils; import com.alipay.sofa.common.code.LogCode2Description; import io.opentracing.Span; @@ -33,6 +34,7 @@ import java.io.PrintWriter; import java.io.StringWriter; +import java.net.InetAddress; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; diff --git a/tracer-core/src/main/java/com/alipay/common/tracer/core/utils/NetUtils.java b/tracer-core/src/main/java/com/alipay/common/tracer/core/utils/NetUtils.java index 094ed4073..fc924588e 100644 --- a/tracer-core/src/main/java/com/alipay/common/tracer/core/utils/NetUtils.java +++ b/tracer-core/src/main/java/com/alipay/common/tracer/core/utils/NetUtils.java @@ -16,11 +16,13 @@ */ package com.alipay.common.tracer.core.utils; +import com.alipay.common.tracer.core.appender.self.SelfLog; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.net.InetAddress; import java.net.NetworkInterface; +import java.net.UnknownHostException; import java.util.Enumeration; import java.util.regex.Pattern; @@ -108,6 +110,18 @@ public static String getLocalIpv4() { return address == null ? null : address.getHostAddress(); } + public static InetAddress getIpAddress(String hostName) { + InetAddress address = null; + try { + address = InetAddress.getByName(hostName); + } catch (UnknownHostException e) { + if (LOGGER.isWarnEnabled()) { + LOGGER.warn("cannot get the IP address of hostName:" + hostName, e); + } + } + return address; + } + /** * 遍历本地网卡,返回第一个合理的IP,保存到缓存中 * diff --git a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/listener/SofaTracerConfigurationListener.java b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/listener/SofaTracerConfigurationListener.java index c03a8e72f..10f186266 100644 --- a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/listener/SofaTracerConfigurationListener.java +++ b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/listener/SofaTracerConfigurationListener.java @@ -115,8 +115,8 @@ private boolean isSpringCloudBootstrapEnvironment(Environment environment) { if (!(environment instanceof ConfigurableEnvironment)) { return false; } else { - return !((ConfigurableEnvironment) environment).getPropertySources().contains( - "sofaBootstrap") + return ((ConfigurableEnvironment) environment).getPropertySources().contains( + "bootstrap") && isSpringCloud(); } }