From 397d5099b90d798bc677d435d4fcbb5afa55b842 Mon Sep 17 00:00:00 2001 From: zzuzh Date: Thu, 8 Jul 2021 17:19:26 +0800 Subject: [PATCH 1/9] jaeger-http --- pom.xml | 1 + .../sofa-tracer-jaeger-plugin/pom.xml | 89 +++++++++++++++++++ ...aegerSofaTracerRestTemplateCustomizer.java | 20 +++++ .../JaegerSofaTracerAutoConfiguration.java | 53 +++++++++++ .../JaegerSofaTracerProperties.java | 60 +++++++++++++ .../main/resources/META-INF/spring.factories | 1 + 6 files changed, 224 insertions(+) create mode 100644 sofa-tracer-plugins/sofa-tracer-jaeger-plugin/pom.xml create mode 100644 sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerRestTemplateCustomizer.java create mode 100644 tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/configuration/JaegerSofaTracerAutoConfiguration.java create mode 100644 tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/properties/JaegerSofaTracerProperties.java diff --git a/pom.xml b/pom.xml index 9764545bf..556acb16c 100644 --- a/pom.xml +++ b/pom.xml @@ -40,6 +40,7 @@ tracer-test/logback-test tracer-test/log4j2-test tracer-test/log4j-test + sofa-tracer-plugins/sofa-tracer-jaeger-plugin diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/pom.xml b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/pom.xml new file mode 100644 index 000000000..3d922c569 --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/pom.xml @@ -0,0 +1,89 @@ + + + + tracer-all-parent + com.alipay.sofa + 3.1.1 + ../../pom.xml + + 4.0.0 + + sofa-tracer-jaeger-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 + + + + \ No newline at end of file diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerRestTemplateCustomizer.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerRestTemplateCustomizer.java new file mode 100644 index 000000000..322eaed3b --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerRestTemplateCustomizer.java @@ -0,0 +1,20 @@ +/* + * 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.jaeger; + +public class JaegerSofaTracerRestTemplateCustomizer { +} diff --git a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/configuration/JaegerSofaTracerAutoConfiguration.java b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/configuration/JaegerSofaTracerAutoConfiguration.java new file mode 100644 index 000000000..e0250955e --- /dev/null +++ b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/configuration/JaegerSofaTracerAutoConfiguration.java @@ -0,0 +1,53 @@ +/* + * 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.jaeger.configuration; + +import com.alipay.sofa.tracer.boot.jaeger.properties.JaegerSofaTracerProperties; +import com.alipay.sofa.tracer.boot.zipkin.properties.ZipkinSofaTracerProperties; +import com.alipay.sofa.tracer.plugins.zipkin.ZipkinSofaTracerRestTemplateCustomizer; +import com.alipay.sofa.tracer.plugins.zipkin.ZipkinSofaTracerSpanRemoteReporter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +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; + +@Configuration +@EnableConfigurationProperties(JaegerSofaTracerProperties.class) +@ConditionalOnProperty(value = "com.alipay.sofa.tracer.jaeger.enabled", matchIfMissing = false) +@ConditionalOnClass({ zipkin2.Span.class, zipkin2.reporter.AsyncReporter.class, RestTemplate.class }) +public class JaegerSofaTracerAutoConfiguration { + @Autowired + private JaegerSofaTracerProperties jaegerProperties; + + @Bean + @ConditionalOnMissingBean + public ZipkinSofaTracerRestTemplateCustomizer zipkinSofaTracerRestTemplateCustomizer() { + return new ZipkinSofaTracerRestTemplateCustomizer(jaegerProperties.isGzipped()); + } + + @Bean + @ConditionalOnMissingBean + public ZipkinSofaTracerSpanRemoteReporter zipkinSofaTracerSpanReporter(ZipkinSofaTracerRestTemplateCustomizer zipkinSofaTracerRestTemplateCustomizer) { + RestTemplate restTemplate = new RestTemplate(); + zipkinSofaTracerRestTemplateCustomizer.customize(restTemplate); + return new ZipkinSofaTracerSpanRemoteReporter(restTemplate, jaegerProperties.getBaseUrl()); + } +} diff --git a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/properties/JaegerSofaTracerProperties.java b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/properties/JaegerSofaTracerProperties.java new file mode 100644 index 000000000..bd7970157 --- /dev/null +++ b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/properties/JaegerSofaTracerProperties.java @@ -0,0 +1,60 @@ +/* + * 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.jaeger.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("com.alipay.sofa.tracer.jaeger") +public class JaegerSofaTracerProperties { + /** + * URL of the zipkin query server instance. + */ + private String baseUrl = "http://localhost:9411/"; + /** + * zipkin reporter is disabled by default + */ + private boolean enabled = false; + /** + * When enabled, spans are gzipped before sent to the zipkin server + */ + private boolean gzipped = false; + + 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 boolean isGzipped() { + return gzipped; + } + + public void setGzipped(boolean gzipped) { + this.gzipped = gzipped; + } + +} 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..f9d9835d5 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.jaeger.configuration.JaegerSofaTracerAutoConfiguration,\ 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 c51656ab5e192294d8da974ed23030b7cb423484 Mon Sep 17 00:00:00 2001 From: zzuzh Date: Thu, 15 Jul 2021 11:44:47 +0800 Subject: [PATCH 2/9] jaegerSpan --- pom.xml | 9 +- .../sofa-tracer-jaeger-plugin/pom.xml | 27 +++ .../JaegerSofaTracerSpanRemoteReporter.java | 57 ++++++ .../jaeger/adapter/JaegerSpanAdapter.java | 181 ++++++++++++++++++ .../tracer/plugins/jaeger/adapter/Utils.java | 38 ++++ .../initialize/JaegerReportRegisterBean.java | 55 ++++++ .../JaegerProperties.java} | 8 +- ...aegerSofaTracerSpanRemoteReporterTest.java | 74 +++++++ .../java/adapter/JaegerSpanAdapterTest.java | 76 ++++++++ .../JaegerReportRegisterBeanTest.java | 41 ++++ .../src/test/resources/sofa.tracer.properties | 4 + .../src/test/resources/spring-bean.xml | 15 ++ tracer-sofa-boot-starter/pom.xml | 4 + ...aegerAgentSofaTracerAutoConfiguration.java | 48 +++++ .../JaegerAgentSofaTracerProperties.java | 60 ++++++ .../main/resources/META-INF/spring.factories | 1 + 16 files changed, 695 insertions(+), 3 deletions(-) create mode 100644 sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerSpanRemoteReporter.java create mode 100644 sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java create mode 100644 sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/Utils.java create mode 100644 sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/initialize/JaegerReportRegisterBean.java rename sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/{JaegerSofaTracerRestTemplateCustomizer.java => properties/JaegerProperties.java} (59%) create mode 100644 sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/JaegerSofaTracerSpanRemoteReporterTest.java create mode 100644 sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/adapter/JaegerSpanAdapterTest.java create mode 100644 sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/initialize/JaegerReportRegisterBeanTest.java create mode 100644 sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/resources/sofa.tracer.properties create mode 100644 sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/resources/spring-bean.xml create mode 100644 tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/configuration/JaegerAgentSofaTracerAutoConfiguration.java create mode 100644 tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/properties/JaegerAgentSofaTracerProperties.java diff --git a/pom.xml b/pom.xml index 556acb16c..27319ff21 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-jaeger-plugin sofa-tracer-plugins/sofa-tracer-dubbo-plugin sofa-tracer-plugins/sofa-tracer-spring-cloud-plugin sofa-tracer-plugins/sofa-tracer-resttmplate-plugin @@ -40,7 +41,7 @@ tracer-test/logback-test tracer-test/log4j2-test tracer-test/log4j-test - sofa-tracer-plugins/sofa-tracer-jaeger-plugin + @@ -52,6 +53,7 @@ ${user.dir} 1.6.7 1.5 + true @@ -131,6 +133,11 @@ sofa-tracer-zipkin-plugin ${project.version} + + com.alipay.sofa + sofa-tracer-jaeger-plugin + ${project.version} + com.alipay.sofa sofa-tracer-spring-cloud-plugin diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/pom.xml b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/pom.xml index 3d922c569..ff5be8a8a 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/pom.xml +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/pom.xml @@ -37,6 +37,33 @@ org.springframework spring-context + + + io.jaegertracing + jaeger-client + 1.6.0 + + + + io.opentracing + opentracing-api + 0.33.0 + + + io.opentracing + opentracing-noop + 0.33.0 + + + io.opentracing + opentracing-mock + 0.33.0 + + + io.opentracing + opentracing-util + 0.33.0 + org.jmockit diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerSpanRemoteReporter.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerSpanRemoteReporter.java new file mode 100644 index 000000000..7436eef29 --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerSpanRemoteReporter.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.jaeger; + +import com.alipay.common.tracer.core.listener.SpanReportListener; +import com.alipay.common.tracer.core.span.SofaTracerSpan; +import com.alipay.sofa.tracer.plugins.jaeger.adapter.JaegerSpanAdapter; +import io.jaegertracing.internal.JaegerSpan; +import io.jaegertracing.internal.reporters.RemoteReporter; +import io.jaegertracing.thrift.internal.senders.UdpSender; +import org.apache.thrift.transport.TTransportException; +import java.io.Closeable; +import java.io.Flushable; +import java.io.IOException; + +public class JaegerSofaTracerSpanRemoteReporter implements SpanReportListener, Closeable { + private JaegerSpanAdapter adapter = new JaegerSpanAdapter(); + private UdpSender jaegerUdpSender; + private RemoteReporter reporter; + + public JaegerSofaTracerSpanRemoteReporter(String host, int port, int maxPacketSize) + throws TTransportException { + jaegerUdpSender = new UdpSender(host, port, maxPacketSize); + //还可以设置udp的很多参数 + RemoteReporter.Builder builder = new RemoteReporter.Builder().withSender(jaegerUdpSender); + reporter = builder.build(); + } + + @Override + public void onSpanReport(SofaTracerSpan sofaTracerSpan) { + if (sofaTracerSpan == null || !sofaTracerSpan.getSofaTracerSpanContext().isSampled()) { + return; + } + JaegerSpan jaegerSpan = adapter.convertToJaegerSpan(sofaTracerSpan, reporter); + reporter.report(jaegerSpan); + } + + @Override + public void close() throws IOException { + this.reporter.close(); + } + +} diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java new file mode 100644 index 000000000..42697f580 --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java @@ -0,0 +1,181 @@ +/* + * 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.jaeger.adapter; + +import com.alipay.common.tracer.core.SofaTracer; +import com.alipay.common.tracer.core.context.span.SofaTracerSpanContext; +import com.alipay.common.tracer.core.span.CommonSpanTags; +import com.alipay.common.tracer.core.span.SofaTracerSpan; +import com.alipay.common.tracer.core.span.SofaTracerSpanReferenceRelationship; +import io.jaegertracing.internal.*; +import io.jaegertracing.internal.reporters.RemoteReporter; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +public class JaegerSpanAdapter { + + public JaegerSpan convertToJaegerSpan(SofaTracerSpan sofaTracerSpan, RemoteReporter reporter) { + final boolean computeDurationViaNanoTicks = false; + final long startTimeNanoTicks = 0L; + SofaTracerSpanContext context = sofaTracerSpan.getSofaTracerSpanContext(); + String operationName = sofaTracerSpan.getOperationName(); + long startTimeMicroseconds = sofaTracerSpan.getStartTime() * 1000; + + //创建JaegerSpan需要传入的Map tags + Map tags = new LinkedHashMap<>(); + tags.putAll(sofaTracerSpan.getTagsWithStr()); + tags.putAll(sofaTracerSpan.getTagsWithBool()); + tags.putAll(sofaTracerSpan.getTagsWithNumber()); + + // JaegerTracer + JaegerTracer jaegerTracer = getJaegerTracer(sofaTracerSpan, reporter); + + //jaegerSpanContext + JaegerSpanContext jaegerSpanContext = getJaegerSpanContext(context); + + List references = getJaegerReference(sofaTracerSpan); + + //create JaegerSpan + JaegerSpan jaegerSpan = new JaegerObjectFactory().createSpan(jaegerTracer, operationName, + jaegerSpanContext, startTimeMicroseconds, startTimeNanoTicks, + computeDurationViaNanoTicks, tags, references); + + //设置durationMicroseconds + jaegerSpan.finish(sofaTracerSpan.getEndTime() * 1000); + + //设置logs + jaegerSpan = setLogData(sofaTracerSpan, jaegerSpan); + return jaegerSpan; + } + + /** + * 创建JaegerTracer对象 + * @param sofaTracerSpan + * @param reporter + * @return JaegerTracer + */ + private JaegerTracer getJaegerTracer(SofaTracerSpan sofaTracerSpan, RemoteReporter reporter) { + String serviceName = sofaTracerSpan.getTagsWithStr().get(CommonSpanTags.LOCAL_APP); + serviceName = JaegerTracer.Builder.checkValidServiceName(serviceName); + JaegerTracer.Builder jaegerBuilder = new JaegerTracer.Builder(serviceName) + .withReporter(reporter); + //add tags + jaegerBuilder = addJaegerTracerTags(jaegerBuilder, sofaTracerSpan); + //把sofa的traceid转换成jaeger的traceid,如果单独使用一个long无法转换 + jaegerBuilder.withTraceId128Bit(); + return jaegerBuilder.build(); + } + + /** + * 转换SpanContext + * @param sofaTracerSpanContext + * @return JaegerSpanContext + */ + private JaegerSpanContext getJaegerSpanContext(SofaTracerSpanContext sofaTracerSpanContext) { + //sofatracer中traceId分成两部分 + String sofaTraceId = sofaTracerSpanContext.getTraceId(); + long traceIdHigh; + long traceIdLow; + //这种方式取了13位时间戳中的数字,使用时间戳后7位作为开头可能导致最后生成的hex串长度不够16,这是会在前面加0,结果是traceid不一致 + if (sofaTraceId.length() > 14) { + traceIdHigh = Utils.hexToLong(sofaTraceId.substring(0, 14)); + traceIdLow = Utils.hexToLong(sofaTraceId.substring(14)); + } else { + traceIdHigh = 0L; + traceIdLow = Utils.hexToLong(sofaTraceId.substring(0)); + } + long spanId = FNV64HashCode(sofaTracerSpanContext.getSpanId()); + long parentId = FNV64HashCode(sofaTracerSpanContext.getParentId()); + //如果设置成1会发送两个span为什么?? + byte flag = 0; + return new JaegerSpanContext(traceIdHigh, traceIdLow, spanId, parentId, flag); + } + + /** + * 转换tags + * @param builder + * @param span + * @return JaegerTracer.Builder + */ + + private JaegerTracer.Builder addJaegerTracerTags(JaegerTracer.Builder builder, + SofaTracerSpan span) { + + for (Map.Entry e : span.getTagsWithStr().entrySet()) { + builder.withTag(e.getKey(), e.getValue()); + } + for (Map.Entry e : span.getTagsWithNumber().entrySet()) { + builder.withTag(e.getKey(), e.getValue()); + } + for (Map.Entry e : span.getTagsWithBool().entrySet()) { + builder.withTag(e.getKey(), e.getValue()); + } + + return builder; + } + + /** + * 转化logdata,zipkin里面使用annotation来表示 + */ + private JaegerSpan setLogData(SofaTracerSpan span, JaegerSpan jaegerSpan) { + List sofaLogDatas = span.getLogs(); + + for (com.alipay.common.tracer.core.span.LogData sofalogData : sofaLogDatas) { + jaegerSpan.log(sofalogData.getTime() * 1000, sofalogData.getFields()); + } + return jaegerSpan; + } + + /** + * 把字符串转换成long + * @param data + * @return long + */ + private static long FNV64HashCode(String data) { + long hash = 0xcbf29ce484222325L; + for (int i = 0; i < data.length(); ++i) { + char c = data.charAt(i); + hash ^= c; + hash *= 0x100000001b3L; + } + return hash; + } + + /** + * 转换Reference + * @param sofaTracerSpan + * @return List + */ + private List getJaegerReference(SofaTracerSpan sofaTracerSpan) { + List jaegerReferences = new ArrayList<>(); + List sofaReferences = sofaTracerSpan + .getSpanReferences(); + for (SofaTracerSpanReferenceRelationship sofaReference : sofaReferences) { + // type 都是使用的opentracing中的常量,但是spancontext这两者需要互相转换 + JaegerSpanContext jaegerSpanContext = getJaegerSpanContext(sofaReference + .getSofaTracerSpanContext()); + Reference jaegerReference = new Reference(jaegerSpanContext, + sofaReference.getReferenceType()); + jaegerReferences.add(jaegerReference); + } + return jaegerReferences; + } + +} diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/Utils.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/Utils.java new file mode 100644 index 000000000..81b8d5d9e --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/Utils.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.jaeger.adapter; + +import org.springframework.util.Assert; + +public class Utils { + /** + * convert hexSting to long + * @param hexString + * @return + */ + public static long hexToLong(String hexString) { + Assert.hasText(hexString, "Can't convert empty hex string to long"); + int length = hexString.length(); + if (length < 1) { + throw new IllegalArgumentException("length must be more than zero : " + hexString); + } + if (length <= 16) { + return Long.parseLong(hexString, 16); + } + throw new IllegalArgumentException("length must less than 16 :" + hexString); + } +} diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/initialize/JaegerReportRegisterBean.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/initialize/JaegerReportRegisterBean.java new file mode 100644 index 000000000..b51fc8e12 --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/initialize/JaegerReportRegisterBean.java @@ -0,0 +1,55 @@ +/* + * 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.jaeger.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.jaeger.JaegerSofaTracerSpanRemoteReporter; +import com.alipay.sofa.tracer.plugins.jaeger.properties.JaegerProperties; +import org.apache.thrift.transport.TTransportException; +import org.springframework.beans.factory.InitializingBean; + +import java.util.ArrayList; +import java.util.List; + +public class JaegerReportRegisterBean implements InitializingBean { + @Override + public void afterPropertiesSet() throws TTransportException { + + boolean enabled = false; + String enabledStr = SofaTracerConfiguration + .getProperty(JaegerProperties.JAEGER_AGENT_IS_ENABLED_KEY); + if (StringUtils.isNotBlank(enabledStr) && "true".equalsIgnoreCase(enabledStr)) { + enabled = true; + } + if (!enabled) { + return; + } + String host = SofaTracerConfiguration.getProperty(JaegerProperties.JAEGER_AGENT_HOST_KEY); + int port = Integer.parseInt(SofaTracerConfiguration + .getProperty(JaegerProperties.JAEGER_AGENT_PORT_KEY)); + int maxPacketSize = Integer.parseInt(SofaTracerConfiguration + .getProperty(JaegerProperties.JAEGER_AGENT_MAX_PACKET_SIZE_KEY)); + SpanReportListener spanReportListener = new JaegerSofaTracerSpanRemoteReporter(host, port, + maxPacketSize); + List spanReportListenerList = new ArrayList(); + spanReportListenerList.add(spanReportListener); + SpanReportListenerHolder.addSpanReportListeners(spanReportListenerList); + } +} diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerRestTemplateCustomizer.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/properties/JaegerProperties.java similarity index 59% rename from sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerRestTemplateCustomizer.java rename to sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/properties/JaegerProperties.java index 322eaed3b..d3f3f7a9f 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerRestTemplateCustomizer.java +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/properties/JaegerProperties.java @@ -14,7 +14,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alipay.sofa.tracer.plugins.jaeger; +package com.alipay.sofa.tracer.plugins.jaeger.properties; -public class JaegerSofaTracerRestTemplateCustomizer { +public class JaegerProperties { + public static final String JAEGER_AGENT_IS_ENABLED_KEY = "com.alipay.sofa.tracer.jaeger.agent.enabled"; + public static final String JAEGER_AGENT_HOST_KEY = "com.alipay.sofa.tracer.jaeger.agent.host"; + public static final String JAEGER_AGENT_PORT_KEY = "com.alipay.sofa.tracer.jaeger.agent.port"; + public static final String JAEGER_AGENT_MAX_PACKET_SIZE_KEY = "com.alipay.sofa.tracer.jaeger.agent.maxPacketSize"; } diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/JaegerSofaTracerSpanRemoteReporterTest.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/JaegerSofaTracerSpanRemoteReporterTest.java new file mode 100644 index 000000000..fcb9ee941 --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/JaegerSofaTracerSpanRemoteReporterTest.java @@ -0,0 +1,74 @@ +/* + * 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.span.CommonSpanTags; +import com.alipay.common.tracer.core.span.LogData; +import com.alipay.common.tracer.core.span.SofaTracerSpan; +import com.alipay.sofa.tracer.plugins.jaeger.adapter.JaegerSpanAdapter; +import io.jaegertracing.internal.JaegerSpan; +import io.jaegertracing.internal.exceptions.SenderException; +import io.jaegertracing.internal.reporters.RemoteReporter; +import io.jaegertracing.spi.Sender; +import io.jaegertracing.thrift.internal.senders.UdpSender; +import org.apache.thrift.transport.TTransportException; +import org.junit.Before; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +public class JaegerSofaTracerSpanRemoteReporterTest { + private JaegerSpanAdapter jaegerSpanAdapter = new JaegerSpanAdapter(); + + private final String tracerType = "SofaTracerSpanTest"; + + private SofaTracer sofaTracer; + + private SofaTracerSpan sofaTracerSpan; + + private JaegerSpan jaegerSpan; + + @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("tagsNumberkey", 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 testUdpSender() throws TTransportException, SenderException { + + Sender sender = new UdpSender("127.0.0.1", 6831, 0); + RemoteReporter.Builder builder = new RemoteReporter.Builder().withSender(sender); + RemoteReporter reporter = builder.build(); + jaegerSpan = jaegerSpanAdapter.convertToJaegerSpan(sofaTracerSpan, reporter); + reporter.report(jaegerSpan); + } + +} diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/adapter/JaegerSpanAdapterTest.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/adapter/JaegerSpanAdapterTest.java new file mode 100644 index 000000000..9c45cf151 --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/adapter/JaegerSpanAdapterTest.java @@ -0,0 +1,76 @@ +/* + * 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 adapter; + +import com.alipay.common.tracer.core.SofaTracer; +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.jaeger.adapter.JaegerSpanAdapter; +import io.jaegertracing.internal.JaegerSpan; +import io.jaegertracing.internal.reporters.RemoteReporter; +import io.jaegertracing.spi.Sender; +import io.jaegertracing.thrift.internal.senders.UdpSender; +import org.apache.thrift.transport.TTransportException; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +public class JaegerSpanAdapterTest { + private JaegerSpanAdapter jaegerSpanAdapter = new JaegerSpanAdapter(); + + private final String tracerType = "SofaTracerSpanTest"; + + 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("tagsNumberkey", 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 testConvertToJaegerSpan() throws TTransportException { + Sender sender = new UdpSender("127.0.0.1", 6831, 0); + RemoteReporter.Builder builder = new RemoteReporter.Builder().withSender(sender); + RemoteReporter reporter = builder.build(); + JaegerSpan span = jaegerSpanAdapter.convertToJaegerSpan(sofaTracerSpan, reporter); + Assert.assertTrue(span != null); + Assert.assertTrue(span.getOperationName().equalsIgnoreCase( + sofaTracerSpan.getOperationName())); + Assert.assertTrue(span.getTags().size() == 4); + + } +} diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/initialize/JaegerReportRegisterBeanTest.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/initialize/JaegerReportRegisterBeanTest.java new file mode 100644 index 000000000..b9ceff4dc --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/initialize/JaegerReportRegisterBeanTest.java @@ -0,0 +1,41 @@ +/* + * 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 initialize; + +import com.alipay.common.tracer.core.listener.SpanReportListenerHolder; +import com.alipay.sofa.tracer.plugins.jaeger.initialize.JaegerReportRegisterBean; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class JaegerReportRegisterBeanTest { + private ClassPathXmlApplicationContext applicationContext; + + @Before + public void init() { + applicationContext = new ClassPathXmlApplicationContext("spring-bean.xml"); + } + + @Test + public void testAfterPropertiesSet() { + Object jaegerReportRegisterBean = applicationContext.getBean("jaegerReportRegisterBean"); + Assert.assertTrue(jaegerReportRegisterBean instanceof JaegerReportRegisterBean); + Assert.assertTrue(SpanReportListenerHolder.getSpanReportListenersHolder().size() > 0); + + } +} diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/resources/sofa.tracer.properties b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/resources/sofa.tracer.properties new file mode 100644 index 000000000..20e62a1b3 --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/resources/sofa.tracer.properties @@ -0,0 +1,4 @@ +com.alipay.sofa.tracer.jaeger.agent.enabled=true +com.alipay.sofa.tracer.jaeger.agent.host=127.0.0.1 +com.alipay.sofa.tracer.jaeger.agent.port=6831 +com.alipay.sofa.tracer.jaeger.agent.maxPacketSize=0 \ No newline at end of file diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/resources/spring-bean.xml b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/resources/spring-bean.xml new file mode 100644 index 000000000..7e2530d6b --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/resources/spring-bean.xml @@ -0,0 +1,15 @@ + + + + + + diff --git a/tracer-sofa-boot-starter/pom.xml b/tracer-sofa-boot-starter/pom.xml index 4af66ef6e..e02f48649 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-jaeger-plugin + com.alipay.sofa sofa-tracer-flexible-plugin diff --git a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/configuration/JaegerAgentSofaTracerAutoConfiguration.java b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/configuration/JaegerAgentSofaTracerAutoConfiguration.java new file mode 100644 index 000000000..4ecd404f0 --- /dev/null +++ b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/configuration/JaegerAgentSofaTracerAutoConfiguration.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.jaeger.configuration; + +import com.alipay.sofa.tracer.boot.jaeger.properties.JaegerAgentSofaTracerProperties; +import com.alipay.sofa.tracer.plugins.jaeger.JaegerSofaTracerSpanRemoteReporter; +import io.jaegertracing.internal.JaegerSpan; +import io.jaegertracing.internal.JaegerSpanContext; +import io.jaegertracing.internal.JaegerTracer; +import org.apache.thrift.transport.TTransportException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +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; + +@Configuration +@EnableConfigurationProperties(JaegerAgentSofaTracerProperties.class) +@ConditionalOnProperty(value = "com.alipay.sofa.tracer.jaeger.agent.enabled", matchIfMissing = false) +//@ConditionalOnClass({ JaegerSpan.class, JaegerTracer.class, JaegerSpanContext.class }) +public class JaegerAgentSofaTracerAutoConfiguration { + @Autowired + private JaegerAgentSofaTracerProperties jaegerAgentProperties; + + @Bean + @ConditionalOnMissingBean + public JaegerSofaTracerSpanRemoteReporter JaegerAgentSofaTracerSpanReporter() + throws TTransportException { + return new JaegerSofaTracerSpanRemoteReporter(jaegerAgentProperties.getHost(), + jaegerAgentProperties.getPort(), jaegerAgentProperties.getMaxPacketSize()); + } +} diff --git a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/properties/JaegerAgentSofaTracerProperties.java b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/properties/JaegerAgentSofaTracerProperties.java new file mode 100644 index 000000000..f7a108922 --- /dev/null +++ b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/properties/JaegerAgentSofaTracerProperties.java @@ -0,0 +1,60 @@ +/* + * 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.jaeger.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("com.alipay.sofa.tracer.jaeger.agent") +public class JaegerAgentSofaTracerProperties { + + private String host = "127.0.0.1"; + private boolean enabled = false; + private int port = 6831; + private int maxPacketSize = 0; + + public String getHost() { + return this.host; + } + + public boolean isEnabled() { + return this.enabled; + } + + public void setHost(String host) { + this.host = host; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public int getPort() { + return this.port; + } + + public void setPort(int port) { + this.port = port; + } + + public int getMaxPacketSize() { + return this.maxPacketSize; + } + + public void setMaxPacketSize(int maxPacketSize) { + this.maxPacketSize = maxPacketSize; + } +} 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 f9d9835d5..abfc8c3ff 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 @@ -3,6 +3,7 @@ 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.jaeger.configuration.JaegerSofaTracerAutoConfiguration,\ +com.alipay.sofa.tracer.boot.jaeger.configuration.JaegerAgentSofaTracerAutoConfiguration,\ 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 8b8a1e5836c880ab0c0182aa5d12e19d7418af08 Mon Sep 17 00:00:00 2001 From: zzuzh Date: Fri, 16 Jul 2021 13:02:09 +0800 Subject: [PATCH 3/9] fix_traceid --- .../jaeger/adapter/JaegerSpanAdapter.java | 20 ++++++++----------- .../tracer/plugins/jaeger/adapter/Utils.java | 2 +- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java index 42697f580..cf2210ff8 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java @@ -89,22 +89,18 @@ private JaegerTracer getJaegerTracer(SofaTracerSpan sofaTracerSpan, RemoteReport * @return JaegerSpanContext */ private JaegerSpanContext getJaegerSpanContext(SofaTracerSpanContext sofaTracerSpanContext) { - //sofatracer中traceId分成两部分 + String sofaTraceId = sofaTracerSpanContext.getTraceId(); - long traceIdHigh; - long traceIdLow; - //这种方式取了13位时间戳中的数字,使用时间戳后7位作为开头可能导致最后生成的hex串长度不够16,这是会在前面加0,结果是traceid不一致 - if (sofaTraceId.length() > 14) { - traceIdHigh = Utils.hexToLong(sofaTraceId.substring(0, 14)); - traceIdLow = Utils.hexToLong(sofaTraceId.substring(14)); - } else { - traceIdHigh = 0L; - traceIdLow = Utils.hexToLong(sofaTraceId.substring(0)); - } + //长度不够32位高位补0 + sofaTraceId = "00000000000000000000000000000000".substring(sofaTraceId.length()) + + sofaTraceId; + long traceIdHigh = Utils.hexToLong(sofaTraceId.substring(0, 16)); + long traceIdLow = Utils.hexToLong(sofaTraceId.substring(16)); + long spanId = FNV64HashCode(sofaTracerSpanContext.getSpanId()); long parentId = FNV64HashCode(sofaTracerSpanContext.getParentId()); //如果设置成1会发送两个span为什么?? - byte flag = 0; + byte flag = 1; return new JaegerSpanContext(traceIdHigh, traceIdLow, spanId, parentId, flag); } diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/Utils.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/Utils.java index 81b8d5d9e..e3ec8138a 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/Utils.java +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/Utils.java @@ -31,7 +31,7 @@ public static long hexToLong(String hexString) { throw new IllegalArgumentException("length must be more than zero : " + hexString); } if (length <= 16) { - return Long.parseLong(hexString, 16); + return Long.parseUnsignedLong(hexString, 16); } throw new IllegalArgumentException("length must less than 16 :" + hexString); } From 4d01ab1656b243e22dcffb6036e1584cb04aee11 Mon Sep 17 00:00:00 2001 From: zzuzh Date: Sun, 18 Jul 2021 00:02:59 +0800 Subject: [PATCH 4/9] fix_dumplicate_span --- .../JaegerSofaTracerSpanRemoteReporter.java | 5 ++-- .../jaeger/adapter/JaegerSpanAdapter.java | 27 ++++++++++++++++--- ...aegerSofaTracerSpanRemoteReporterTest.java | 5 ++-- .../src/test/resources/sofa.tracer.properties | 2 +- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerSpanRemoteReporter.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerSpanRemoteReporter.java index 7436eef29..cc895e5f1 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerSpanRemoteReporter.java +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerSpanRemoteReporter.java @@ -32,8 +32,8 @@ public class JaegerSofaTracerSpanRemoteReporter implements SpanReportListener, C private UdpSender jaegerUdpSender; private RemoteReporter reporter; - public JaegerSofaTracerSpanRemoteReporter(String host, int port, int maxPacketSize) - throws TTransportException { + public JaegerSofaTracerSpanRemoteReporter(String host, int port, int maxPacketSize) throws TTransportException { + //默认使用的compact jaegerUdpSender = new UdpSender(host, port, maxPacketSize); //还可以设置udp的很多参数 RemoteReporter.Builder builder = new RemoteReporter.Builder().withSender(jaegerUdpSender); @@ -46,7 +46,6 @@ public void onSpanReport(SofaTracerSpan sofaTracerSpan) { return; } JaegerSpan jaegerSpan = adapter.convertToJaegerSpan(sofaTracerSpan, reporter); - reporter.report(jaegerSpan); } @Override diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java index cf2210ff8..74ec3781e 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java @@ -16,7 +16,6 @@ */ package com.alipay.sofa.tracer.plugins.jaeger.adapter; -import com.alipay.common.tracer.core.SofaTracer; import com.alipay.common.tracer.core.context.span.SofaTracerSpanContext; import com.alipay.common.tracer.core.span.CommonSpanTags; import com.alipay.common.tracer.core.span.SofaTracerSpan; @@ -62,6 +61,10 @@ public JaegerSpan convertToJaegerSpan(SofaTracerSpan sofaTracerSpan, RemoteRepor //设置logs jaegerSpan = setLogData(sofaTracerSpan, jaegerSpan); + + //convert baggage + jaegerSpan = convertBaggage(sofaTracerSpan, jaegerSpan); + return jaegerSpan; } @@ -99,8 +102,8 @@ private JaegerSpanContext getJaegerSpanContext(SofaTracerSpanContext sofaTracerS long spanId = FNV64HashCode(sofaTracerSpanContext.getSpanId()); long parentId = FNV64HashCode(sofaTracerSpanContext.getParentId()); - //如果设置成1会发送两个span为什么?? - byte flag = 1; + //设置为1表示 sampled == true + byte flag = sofaTracerSpanContext.isSampled() ? (byte) 1 : (byte) 0; return new JaegerSpanContext(traceIdHigh, traceIdLow, spanId, parentId, flag); } @@ -127,6 +130,24 @@ private JaegerTracer.Builder addJaegerTracerTags(JaegerTracer.Builder builder, return builder; } + /** + * 将sofaTracer 中的 business baggage和 system baggage转换为jaeger中的 baggage + * @param sofaTracerSpan + * @param jaegerSpan + * @return JaegerSpan + */ + + private JaegerSpan convertBaggage(SofaTracerSpan sofaTracerSpan, JaegerSpan jaegerSpan) { + + Iterable> baggages = sofaTracerSpan.getSofaTracerSpanContext() + .baggageItems(); + for (Map.Entry baggage : baggages) { + jaegerSpan.setBaggageItem(baggage.getKey(), baggage.getValue()); + } + return jaegerSpan; + + } + /** * 转化logdata,zipkin里面使用annotation来表示 */ diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/JaegerSofaTracerSpanRemoteReporterTest.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/JaegerSofaTracerSpanRemoteReporterTest.java index fcb9ee941..227463375 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/JaegerSofaTracerSpanRemoteReporterTest.java +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/JaegerSofaTracerSpanRemoteReporterTest.java @@ -50,6 +50,7 @@ public void init() throws InterruptedException { sofaTracerSpan.setTag("tagsStrkey", "tagsStrVal"); sofaTracerSpan.setTag("tagsBooleankey", true); sofaTracerSpan.setTag("tagsNumberkey", 2018); + sofaTracerSpan.setTag("span.kind", "client"); sofaTracerSpan.setBaggageItem("baggageKey", "baggageVal"); sofaTracerSpan.setTag(CommonSpanTags.LOCAL_APP, "SofaTracerSpanTest"); Map logMap = new HashMap(); @@ -62,13 +63,13 @@ public void init() throws InterruptedException { } @Test - public void testUdpSender() throws TTransportException, SenderException { + public void testUdpSender() throws TTransportException { Sender sender = new UdpSender("127.0.0.1", 6831, 0); RemoteReporter.Builder builder = new RemoteReporter.Builder().withSender(sender); RemoteReporter reporter = builder.build(); jaegerSpan = jaegerSpanAdapter.convertToJaegerSpan(sofaTracerSpan, reporter); - reporter.report(jaegerSpan); + // reporter.report(jaegerSpan); } } diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/resources/sofa.tracer.properties b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/resources/sofa.tracer.properties index 20e62a1b3..e634a0027 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/resources/sofa.tracer.properties +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/resources/sofa.tracer.properties @@ -1,4 +1,4 @@ com.alipay.sofa.tracer.jaeger.agent.enabled=true com.alipay.sofa.tracer.jaeger.agent.host=127.0.0.1 com.alipay.sofa.tracer.jaeger.agent.port=6831 -com.alipay.sofa.tracer.jaeger.agent.maxPacketSize=0 \ No newline at end of file +com.alipay.sofa.tracer.jaeger.agent.maxPacketSize=0 From 6fa6ca7b8e951912aad14dd5dc142a5d7440054a Mon Sep 17 00:00:00 2001 From: zzuzh Date: Sun, 18 Jul 2021 19:22:53 +0800 Subject: [PATCH 5/9] fix_process_tags --- .../JaegerSofaTracerSpanRemoteReporter.java | 33 +++++++++++++++---- .../jaeger/adapter/JaegerSpanAdapter.java | 27 +++++++-------- .../jaeger/properties/JaegerProperties.java | 11 ++++--- ...aegerSofaTracerSpanRemoteReporterTest.java | 26 +++++++++++---- .../java/adapter/JaegerSpanAdapterTest.java | 2 +- .../src/test/resources/sofa.tracer.properties | 16 ++++++--- tracer-sofa-boot-starter/pom.xml | 7 ++++ ...aegerAgentSofaTracerAutoConfiguration.java | 5 ++- .../JaegerSofaTracerAutoConfiguration.java | 4 +++ .../JaegerAgentSofaTracerProperties.java | 16 +++++++-- .../JaegerSofaTracerProperties.java | 6 ++-- 11 files changed, 110 insertions(+), 43 deletions(-) diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerSpanRemoteReporter.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerSpanRemoteReporter.java index cc895e5f1..2d00fce35 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerSpanRemoteReporter.java +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerSpanRemoteReporter.java @@ -16,15 +16,15 @@ */ package com.alipay.sofa.tracer.plugins.jaeger; +import com.alipay.common.tracer.core.configuration.SofaTracerConfiguration; import com.alipay.common.tracer.core.listener.SpanReportListener; import com.alipay.common.tracer.core.span.SofaTracerSpan; import com.alipay.sofa.tracer.plugins.jaeger.adapter.JaegerSpanAdapter; -import io.jaegertracing.internal.JaegerSpan; +import com.alipay.sofa.tracer.plugins.jaeger.properties.JaegerProperties; import io.jaegertracing.internal.reporters.RemoteReporter; import io.jaegertracing.thrift.internal.senders.UdpSender; import org.apache.thrift.transport.TTransportException; import java.io.Closeable; -import java.io.Flushable; import java.io.IOException; public class JaegerSofaTracerSpanRemoteReporter implements SpanReportListener, Closeable { @@ -32,12 +32,26 @@ public class JaegerSofaTracerSpanRemoteReporter implements SpanReportListener, C private UdpSender jaegerUdpSender; private RemoteReporter reporter; - public JaegerSofaTracerSpanRemoteReporter(String host, int port, int maxPacketSize) throws TTransportException { + public JaegerSofaTracerSpanRemoteReporter(String host, int port, int maxPacketSize) + throws TTransportException { //默认使用的compact jaegerUdpSender = new UdpSender(host, port, maxPacketSize); - //还可以设置udp的很多参数 - RemoteReporter.Builder builder = new RemoteReporter.Builder().withSender(jaegerUdpSender); - reporter = builder.build(); + + //使用配置文件 sofa.tracer.properties来配置jaeger 中的command queue + //向command queue中写入FlushCommand的间隔时间 + Integer flushInterval = SofaTracerConfiguration.getIntegerDefaultIfNull( + JaegerProperties.JAEGER_AGENT_FLUSH_INTERVAL_MS_KEY, 1000); + //command queue的大小,过大浪费空间,过小会导致span丢失 + Integer maxQueueSize = SofaTracerConfiguration.getIntegerDefaultIfNull( + JaegerProperties.JAEGER_AGENT_MAX_QUEUE_SIZE_KEY, 100); + //写入CloseCommand的超时时间 + Integer closeEnqueueTimeout = SofaTracerConfiguration.getIntegerDefaultIfNull( + JaegerProperties.JAEGER_AGENT_CLOSE_ENQUEUE_TIMEOUT_MILLIS_KEY, 1000); + + reporter = new RemoteReporter.Builder().withSender(jaegerUdpSender) + .withFlushInterval(flushInterval).withMaxQueueSize(maxQueueSize) + .withCloseEnqueueTimeout(closeEnqueueTimeout).build(); + } @Override @@ -45,7 +59,8 @@ public void onSpanReport(SofaTracerSpan sofaTracerSpan) { if (sofaTracerSpan == null || !sofaTracerSpan.getSofaTracerSpanContext().isSampled()) { return; } - JaegerSpan jaegerSpan = adapter.convertToJaegerSpan(sofaTracerSpan, reporter); + //转换过程中创建的SofaTracer中包含的reporter会自动上传转换好的jaegerSpan + adapter.convertAndReport(sofaTracerSpan, reporter); } @Override @@ -53,4 +68,8 @@ public void close() throws IOException { this.reporter.close(); } + public RemoteReporter getReporter() { + return this.reporter; + } + } diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java index 74ec3781e..f9c27d45b 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java @@ -28,9 +28,13 @@ import java.util.List; import java.util.Map; +/** + * convert sofaTracerSpan to JaegerSpan and use UdpSender to send jaegerSpan to the jaeger agent + */ + public class JaegerSpanAdapter { - public JaegerSpan convertToJaegerSpan(SofaTracerSpan sofaTracerSpan, RemoteReporter reporter) { + public JaegerSpan convertAndReport(SofaTracerSpan sofaTracerSpan, RemoteReporter reporter) { final boolean computeDurationViaNanoTicks = false; final long startTimeNanoTicks = 0L; SofaTracerSpanContext context = sofaTracerSpan.getSofaTracerSpanContext(); @@ -56,10 +60,10 @@ public JaegerSpan convertToJaegerSpan(SofaTracerSpan sofaTracerSpan, RemoteRepor jaegerSpanContext, startTimeMicroseconds, startTimeNanoTicks, computeDurationViaNanoTicks, tags, references); - //设置durationMicroseconds + //set durationMicroseconds jaegerSpan.finish(sofaTracerSpan.getEndTime() * 1000); - //设置logs + //set logs jaegerSpan = setLogData(sofaTracerSpan, jaegerSpan); //convert baggage @@ -69,7 +73,7 @@ public JaegerSpan convertToJaegerSpan(SofaTracerSpan sofaTracerSpan, RemoteRepor } /** - * 创建JaegerTracer对象 + * create JaegerTracer Object * @param sofaTracerSpan * @param reporter * @return JaegerTracer @@ -108,7 +112,7 @@ private JaegerSpanContext getJaegerSpanContext(SofaTracerSpanContext sofaTracerS } /** - * 转换tags + * 设置UI中process的tag * @param builder * @param span * @return JaegerTracer.Builder @@ -117,16 +121,9 @@ private JaegerSpanContext getJaegerSpanContext(SofaTracerSpanContext sofaTracerS private JaegerTracer.Builder addJaegerTracerTags(JaegerTracer.Builder builder, SofaTracerSpan span) { - for (Map.Entry e : span.getTagsWithStr().entrySet()) { - builder.withTag(e.getKey(), e.getValue()); - } - for (Map.Entry e : span.getTagsWithNumber().entrySet()) { - builder.withTag(e.getKey(), e.getValue()); + if (span.getTagsWithStr().containsKey("ip")) { + builder.withTag("ip", span.getTagsWithStr().get("ip")); } - for (Map.Entry e : span.getTagsWithBool().entrySet()) { - builder.withTag(e.getKey(), e.getValue()); - } - return builder; } @@ -149,7 +146,7 @@ private JaegerSpan convertBaggage(SofaTracerSpan sofaTracerSpan, JaegerSpan jaeg } /** - * 转化logdata,zipkin里面使用annotation来表示 + * 转化logdata */ private JaegerSpan setLogData(SofaTracerSpan span, JaegerSpan jaegerSpan) { List sofaLogDatas = span.getLogs(); diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/properties/JaegerProperties.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/properties/JaegerProperties.java index d3f3f7a9f..f997dbc3e 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/properties/JaegerProperties.java +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/properties/JaegerProperties.java @@ -17,8 +17,11 @@ package com.alipay.sofa.tracer.plugins.jaeger.properties; public class JaegerProperties { - public static final String JAEGER_AGENT_IS_ENABLED_KEY = "com.alipay.sofa.tracer.jaeger.agent.enabled"; - public static final String JAEGER_AGENT_HOST_KEY = "com.alipay.sofa.tracer.jaeger.agent.host"; - public static final String JAEGER_AGENT_PORT_KEY = "com.alipay.sofa.tracer.jaeger.agent.port"; - public static final String JAEGER_AGENT_MAX_PACKET_SIZE_KEY = "com.alipay.sofa.tracer.jaeger.agent.maxPacketSize"; + public static final String JAEGER_AGENT_IS_ENABLED_KEY = "com.alipay.sofa.tracer.jaeger.agent.enabled"; + public static final String JAEGER_AGENT_HOST_KEY = "com.alipay.sofa.tracer.jaeger.agent.host"; + public static final String JAEGER_AGENT_PORT_KEY = "com.alipay.sofa.tracer.jaeger.agent.port"; + public static final String JAEGER_AGENT_MAX_PACKET_SIZE_KEY = "com.alipay.sofa.tracer.jaeger.agent.maxPacketSize"; + public static final String JAEGER_AGENT_FLUSH_INTERVAL_MS_KEY = "com.alipay.sofa.tracer.jaeger.agent.flushInterval"; + public static final String JAEGER_AGENT_MAX_QUEUE_SIZE_KEY = "com.alipay.sofa.tracer.jaeger.agent.maxQueueSize"; + public static final String JAEGER_AGENT_CLOSE_ENQUEUE_TIMEOUT_MILLIS_KEY = "com.alipay.sofa.tracer.jaeger.agent.closeEnqueueTimeout"; } diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/JaegerSofaTracerSpanRemoteReporterTest.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/JaegerSofaTracerSpanRemoteReporterTest.java index 227463375..120c0cb3a 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/JaegerSofaTracerSpanRemoteReporterTest.java +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/JaegerSofaTracerSpanRemoteReporterTest.java @@ -15,16 +15,19 @@ * limitations under the License. */ import com.alipay.common.tracer.core.SofaTracer; +import com.alipay.common.tracer.core.configuration.SofaTracerConfiguration; 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.jaeger.JaegerSofaTracerSpanRemoteReporter; import com.alipay.sofa.tracer.plugins.jaeger.adapter.JaegerSpanAdapter; +import com.alipay.sofa.tracer.plugins.jaeger.properties.JaegerProperties; import io.jaegertracing.internal.JaegerSpan; -import io.jaegertracing.internal.exceptions.SenderException; import io.jaegertracing.internal.reporters.RemoteReporter; import io.jaegertracing.spi.Sender; import io.jaegertracing.thrift.internal.senders.UdpSender; import org.apache.thrift.transport.TTransportException; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -63,13 +66,24 @@ public void init() throws InterruptedException { } @Test - public void testUdpSender() throws TTransportException { + public void testSpanReport() throws TTransportException { Sender sender = new UdpSender("127.0.0.1", 6831, 0); - RemoteReporter.Builder builder = new RemoteReporter.Builder().withSender(sender); - RemoteReporter reporter = builder.build(); - jaegerSpan = jaegerSpanAdapter.convertToJaegerSpan(sofaTracerSpan, reporter); - // reporter.report(jaegerSpan); + RemoteReporter reporter = new RemoteReporter.Builder().withSender(sender).build(); + jaegerSpan = jaegerSpanAdapter.convertAndReport(sofaTracerSpan, reporter); + + } + + @Test + public void testCommandQueueSetting() throws TTransportException { + JaegerSofaTracerSpanRemoteReporter reporter = new JaegerSofaTracerSpanRemoteReporter( + "127.0.0.1", 6831, 0); + Assert.assertTrue(SofaTracerConfiguration.getIntegerDefaultIfNull( + JaegerProperties.JAEGER_AGENT_FLUSH_INTERVAL_MS_KEY, 1000) == 200); + Assert.assertTrue(SofaTracerConfiguration.getIntegerDefaultIfNull( + JaegerProperties.JAEGER_AGENT_MAX_QUEUE_SIZE_KEY, 100) == 200); + Assert.assertTrue(SofaTracerConfiguration.getIntegerDefaultIfNull( + JaegerProperties.JAEGER_AGENT_CLOSE_ENQUEUE_TIMEOUT_MILLIS_KEY, 1000) == 2000); } } diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/adapter/JaegerSpanAdapterTest.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/adapter/JaegerSpanAdapterTest.java index 9c45cf151..3bbc46883 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/adapter/JaegerSpanAdapterTest.java +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/adapter/JaegerSpanAdapterTest.java @@ -66,7 +66,7 @@ public void testConvertToJaegerSpan() throws TTransportException { Sender sender = new UdpSender("127.0.0.1", 6831, 0); RemoteReporter.Builder builder = new RemoteReporter.Builder().withSender(sender); RemoteReporter reporter = builder.build(); - JaegerSpan span = jaegerSpanAdapter.convertToJaegerSpan(sofaTracerSpan, reporter); + JaegerSpan span = jaegerSpanAdapter.convertAndReport(sofaTracerSpan, reporter); Assert.assertTrue(span != null); Assert.assertTrue(span.getOperationName().equalsIgnoreCase( sofaTracerSpan.getOperationName())); diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/resources/sofa.tracer.properties b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/resources/sofa.tracer.properties index e634a0027..606b9d4d7 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/resources/sofa.tracer.properties +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/resources/sofa.tracer.properties @@ -1,4 +1,12 @@ -com.alipay.sofa.tracer.jaeger.agent.enabled=true -com.alipay.sofa.tracer.jaeger.agent.host=127.0.0.1 -com.alipay.sofa.tracer.jaeger.agent.port=6831 -com.alipay.sofa.tracer.jaeger.agent.maxPacketSize=0 +#com.alipay.sofa.tracer.jaeger.agent.enabled=true +#com.alipay.sofa.tracer.jaeger.agent.host=127.0.0.1 +#com.alipay.sofa.tracer.jaeger.agent.port=6831 +#com.alipay.sofa.tracer.jaeger.agent.maxPacketSize=0 + +com.alipay.sofa.tracer.zipkin.enabled=true +com.alipay.sofa.tracer.zipkin.baseUrl=http://localhost:9411 +com.alipay.sofa.tracer.zipkin.gzipped=true + +com.alipay.sofa.tracer.jaeger.agent.flushInterval=200 +com.alipay.sofa.tracer.jaeger.agent.maxQueueSize=200 +com.alipay.sofa.tracer.jaeger.agent.closeEnqueueTimeout=2000 diff --git a/tracer-sofa-boot-starter/pom.xml b/tracer-sofa-boot-starter/pom.xml index e02f48649..4d7641717 100644 --- a/tracer-sofa-boot-starter/pom.xml +++ b/tracer-sofa-boot-starter/pom.xml @@ -183,6 +183,13 @@ 2.7.15 true + + + io.jaegertracing + jaeger-client + 1.6.0 + true + org.springframework.data diff --git a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/configuration/JaegerAgentSofaTracerAutoConfiguration.java b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/configuration/JaegerAgentSofaTracerAutoConfiguration.java index 4ecd404f0..81763fafa 100644 --- a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/configuration/JaegerAgentSofaTracerAutoConfiguration.java +++ b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/configuration/JaegerAgentSofaTracerAutoConfiguration.java @@ -30,10 +30,13 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +/** + * report jaeger.thrift data to the jaeger agent + */ @Configuration @EnableConfigurationProperties(JaegerAgentSofaTracerProperties.class) @ConditionalOnProperty(value = "com.alipay.sofa.tracer.jaeger.agent.enabled", matchIfMissing = false) -//@ConditionalOnClass({ JaegerSpan.class, JaegerTracer.class, JaegerSpanContext.class }) +@ConditionalOnClass({ JaegerSpan.class, JaegerTracer.class, JaegerSpanContext.class }) public class JaegerAgentSofaTracerAutoConfiguration { @Autowired private JaegerAgentSofaTracerProperties jaegerAgentProperties; diff --git a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/configuration/JaegerSofaTracerAutoConfiguration.java b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/configuration/JaegerSofaTracerAutoConfiguration.java index e0250955e..6cd19cc2f 100644 --- a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/configuration/JaegerSofaTracerAutoConfiguration.java +++ b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/configuration/JaegerSofaTracerAutoConfiguration.java @@ -29,6 +29,10 @@ import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; +/** + * report Zipkin v2 JSON format span to the jaeger collector + */ + @Configuration @EnableConfigurationProperties(JaegerSofaTracerProperties.class) @ConditionalOnProperty(value = "com.alipay.sofa.tracer.jaeger.enabled", matchIfMissing = false) diff --git a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/properties/JaegerAgentSofaTracerProperties.java b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/properties/JaegerAgentSofaTracerProperties.java index f7a108922..73f719037 100644 --- a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/properties/JaegerAgentSofaTracerProperties.java +++ b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/properties/JaegerAgentSofaTracerProperties.java @@ -20,11 +20,23 @@ @ConfigurationProperties("com.alipay.sofa.tracer.jaeger.agent") public class JaegerAgentSofaTracerProperties { - + /** + * the address of agent + */ private String host = "127.0.0.1"; + /** + * whether report span to jaeger agent + */ private boolean enabled = false; + /** + * jaeger agent port to accept jaeger.thrift + */ private int port = 6831; - private int maxPacketSize = 0; + /** + * the max byte of the packet + * In UDP over IPv4, the limit is 65,507 bytes + */ + private int maxPacketSize = 65000; public String getHost() { return this.host; diff --git a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/properties/JaegerSofaTracerProperties.java b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/properties/JaegerSofaTracerProperties.java index bd7970157..ebb0a7d82 100644 --- a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/properties/JaegerSofaTracerProperties.java +++ b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/properties/JaegerSofaTracerProperties.java @@ -21,15 +21,15 @@ @ConfigurationProperties("com.alipay.sofa.tracer.jaeger") public class JaegerSofaTracerProperties { /** - * URL of the zipkin query server instance. + * URL of the jaeger collector. */ private String baseUrl = "http://localhost:9411/"; /** - * zipkin reporter is disabled by default + * jaeger reporter is disabled by default */ private boolean enabled = false; /** - * When enabled, spans are gzipped before sent to the zipkin server + * When enabled, spans are gzipped before sent to the jaeger server */ private boolean gzipped = false; From b698acfc7463eb02d8b443ee36376eb7bd7b8b43 Mon Sep 17 00:00:00 2001 From: zzuzh Date: Sun, 18 Jul 2021 21:41:00 +0800 Subject: [PATCH 6/9] fix_dumplicate_jaegerTracer --- .../JaegerSofaTracerSpanRemoteReporter.java | 28 ++- .../jaeger/adapter/JaegerSpanAdapter.java | 109 +++++------- .../tracer/plugins/jaeger/adapter/Utils.java | 38 ---- .../initialize/JaegerReportRegisterBean.java | 3 +- .../jaeger/properties/JaegerProperties.java | 1 + .../tracer/plugins/jaeger/utils/NetUtils.java | 166 ++++++++++++++++++ ...aegerSofaTracerSpanRemoteReporterTest.java | 10 +- .../java/adapter/JaegerSpanAdapterTest.java | 11 +- .../src/test/resources/sofa.tracer.properties | 14 +- ...aegerAgentSofaTracerAutoConfiguration.java | 9 +- 10 files changed, 257 insertions(+), 132 deletions(-) delete mode 100644 sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/Utils.java create mode 100644 sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/utils/NetUtils.java diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerSpanRemoteReporter.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerSpanRemoteReporter.java index 2d00fce35..904e83f37 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerSpanRemoteReporter.java +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerSpanRemoteReporter.java @@ -21,6 +21,8 @@ import com.alipay.common.tracer.core.span.SofaTracerSpan; import com.alipay.sofa.tracer.plugins.jaeger.adapter.JaegerSpanAdapter; import com.alipay.sofa.tracer.plugins.jaeger.properties.JaegerProperties; +import com.alipay.sofa.tracer.plugins.jaeger.utils.NetUtils; +import io.jaegertracing.internal.JaegerTracer; import io.jaegertracing.internal.reporters.RemoteReporter; import io.jaegertracing.thrift.internal.senders.UdpSender; import org.apache.thrift.transport.TTransportException; @@ -31,20 +33,21 @@ public class JaegerSofaTracerSpanRemoteReporter implements SpanReportListener, C private JaegerSpanAdapter adapter = new JaegerSpanAdapter(); private UdpSender jaegerUdpSender; private RemoteReporter reporter; + private JaegerTracer jaegerTracer; - public JaegerSofaTracerSpanRemoteReporter(String host, int port, int maxPacketSize) - throws TTransportException { - //默认使用的compact + public JaegerSofaTracerSpanRemoteReporter(String host, int port, int maxPacketSize, + String serviceName) throws TTransportException { + //user compact thrift protocol by default jaegerUdpSender = new UdpSender(host, port, maxPacketSize); - //使用配置文件 sofa.tracer.properties来配置jaeger 中的command queue - //向command queue中写入FlushCommand的间隔时间 + //Use the configuration file named sofa.tracer.properties to configure the command queue in jaeger + //The interval of writing FlushCommand to the command queue Integer flushInterval = SofaTracerConfiguration.getIntegerDefaultIfNull( JaegerProperties.JAEGER_AGENT_FLUSH_INTERVAL_MS_KEY, 1000); - //command queue的大小,过大浪费空间,过小会导致span丢失 + // size of the command queue is too large will waste space, and too small will cause the span to be lost Integer maxQueueSize = SofaTracerConfiguration.getIntegerDefaultIfNull( JaegerProperties.JAEGER_AGENT_MAX_QUEUE_SIZE_KEY, 100); - //写入CloseCommand的超时时间 + //Timeout for writing CloseCommand Integer closeEnqueueTimeout = SofaTracerConfiguration.getIntegerDefaultIfNull( JaegerProperties.JAEGER_AGENT_CLOSE_ENQUEUE_TIMEOUT_MILLIS_KEY, 1000); @@ -52,6 +55,9 @@ public JaegerSofaTracerSpanRemoteReporter(String host, int port, int maxPacketSi .withFlushInterval(flushInterval).withMaxQueueSize(maxQueueSize) .withCloseEnqueueTimeout(closeEnqueueTimeout).build(); + jaegerTracer = new JaegerTracer.Builder(serviceName).withReporter(reporter) + .withTraceId128Bit().withTag("ip", NetUtils.getLocalIpv4()).build(); + } @Override @@ -59,8 +65,8 @@ public void onSpanReport(SofaTracerSpan sofaTracerSpan) { if (sofaTracerSpan == null || !sofaTracerSpan.getSofaTracerSpanContext().isSampled()) { return; } - //转换过程中创建的SofaTracer中包含的reporter会自动上传转换好的jaegerSpan - adapter.convertAndReport(sofaTracerSpan, reporter); + // the sender in jaegerTracer will send the input sofaTracerSpan according to the setting + adapter.convertAndReport(sofaTracerSpan, jaegerTracer); } @Override @@ -72,4 +78,8 @@ public RemoteReporter getReporter() { return this.reporter; } + public JaegerTracer getJaegerTracer() { + return this.jaegerTracer; + } + } diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java index f9c27d45b..daae4a4ea 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java @@ -17,11 +17,10 @@ package com.alipay.sofa.tracer.plugins.jaeger.adapter; import com.alipay.common.tracer.core.context.span.SofaTracerSpanContext; -import com.alipay.common.tracer.core.span.CommonSpanTags; import com.alipay.common.tracer.core.span.SofaTracerSpan; import com.alipay.common.tracer.core.span.SofaTracerSpanReferenceRelationship; import io.jaegertracing.internal.*; -import io.jaegertracing.internal.reporters.RemoteReporter; +import org.springframework.util.Assert; import java.util.ArrayList; import java.util.LinkedHashMap; @@ -34,22 +33,19 @@ public class JaegerSpanAdapter { - public JaegerSpan convertAndReport(SofaTracerSpan sofaTracerSpan, RemoteReporter reporter) { + public JaegerSpan convertAndReport(SofaTracerSpan sofaTracerSpan, JaegerTracer jaegerTracer) { final boolean computeDurationViaNanoTicks = false; final long startTimeNanoTicks = 0L; SofaTracerSpanContext context = sofaTracerSpan.getSofaTracerSpanContext(); String operationName = sofaTracerSpan.getOperationName(); long startTimeMicroseconds = sofaTracerSpan.getStartTime() * 1000; - //创建JaegerSpan需要传入的Map tags + //construct tags in JaegerSpan Map tags = new LinkedHashMap<>(); tags.putAll(sofaTracerSpan.getTagsWithStr()); tags.putAll(sofaTracerSpan.getTagsWithBool()); tags.putAll(sofaTracerSpan.getTagsWithNumber()); - // JaegerTracer - JaegerTracer jaegerTracer = getJaegerTracer(sofaTracerSpan, reporter); - //jaegerSpanContext JaegerSpanContext jaegerSpanContext = getJaegerSpanContext(context); @@ -73,62 +69,28 @@ public JaegerSpan convertAndReport(SofaTracerSpan sofaTracerSpan, RemoteReporter } /** - * create JaegerTracer Object - * @param sofaTracerSpan - * @param reporter - * @return JaegerTracer - */ - private JaegerTracer getJaegerTracer(SofaTracerSpan sofaTracerSpan, RemoteReporter reporter) { - String serviceName = sofaTracerSpan.getTagsWithStr().get(CommonSpanTags.LOCAL_APP); - serviceName = JaegerTracer.Builder.checkValidServiceName(serviceName); - JaegerTracer.Builder jaegerBuilder = new JaegerTracer.Builder(serviceName) - .withReporter(reporter); - //add tags - jaegerBuilder = addJaegerTracerTags(jaegerBuilder, sofaTracerSpan); - //把sofa的traceid转换成jaeger的traceid,如果单独使用一个long无法转换 - jaegerBuilder.withTraceId128Bit(); - return jaegerBuilder.build(); - } - - /** - * 转换SpanContext + * convert SpanContext * @param sofaTracerSpanContext * @return JaegerSpanContext */ private JaegerSpanContext getJaegerSpanContext(SofaTracerSpanContext sofaTracerSpanContext) { String sofaTraceId = sofaTracerSpanContext.getTraceId(); - //长度不够32位高位补0 + //when length is less than 32 add 0 to the font sofaTraceId = "00000000000000000000000000000000".substring(sofaTraceId.length()) + sofaTraceId; - long traceIdHigh = Utils.hexToLong(sofaTraceId.substring(0, 16)); - long traceIdLow = Utils.hexToLong(sofaTraceId.substring(16)); + long traceIdHigh = hexToLong(sofaTraceId.substring(0, 16)); + long traceIdLow = hexToLong(sofaTraceId.substring(16)); long spanId = FNV64HashCode(sofaTracerSpanContext.getSpanId()); long parentId = FNV64HashCode(sofaTracerSpanContext.getParentId()); - //设置为1表示 sampled == true + //when flag == 1 it means sampled byte flag = sofaTracerSpanContext.isSampled() ? (byte) 1 : (byte) 0; return new JaegerSpanContext(traceIdHigh, traceIdLow, spanId, parentId, flag); } /** - * 设置UI中process的tag - * @param builder - * @param span - * @return JaegerTracer.Builder - */ - - private JaegerTracer.Builder addJaegerTracerTags(JaegerTracer.Builder builder, - SofaTracerSpan span) { - - if (span.getTagsWithStr().containsKey("ip")) { - builder.withTag("ip", span.getTagsWithStr().get("ip")); - } - return builder; - } - - /** - * 将sofaTracer 中的 business baggage和 system baggage转换为jaeger中的 baggage + * convert businessBaggage and systemBaggage in sofaTracer to the baggage in jaeger * @param sofaTracerSpan * @param jaegerSpan * @return JaegerSpan @@ -146,7 +108,7 @@ private JaegerSpan convertBaggage(SofaTracerSpan sofaTracerSpan, JaegerSpan jaeg } /** - * 转化logdata + * convert logdata */ private JaegerSpan setLogData(SofaTracerSpan span, JaegerSpan jaegerSpan) { List sofaLogDatas = span.getLogs(); @@ -158,22 +120,7 @@ private JaegerSpan setLogData(SofaTracerSpan span, JaegerSpan jaegerSpan) { } /** - * 把字符串转换成long - * @param data - * @return long - */ - private static long FNV64HashCode(String data) { - long hash = 0xcbf29ce484222325L; - for (int i = 0; i < data.length(); ++i) { - char c = data.charAt(i); - hash ^= c; - hash *= 0x100000001b3L; - } - return hash; - } - - /** - * 转换Reference + * convert Reference * @param sofaTracerSpan * @return List */ @@ -182,7 +129,7 @@ private List getJaegerReference(SofaTracerSpan sofaTracerSpan) { List sofaReferences = sofaTracerSpan .getSpanReferences(); for (SofaTracerSpanReferenceRelationship sofaReference : sofaReferences) { - // type 都是使用的opentracing中的常量,但是spancontext这两者需要互相转换 + // Type are constants in opentracing , but spanContext needs to convert JaegerSpanContext jaegerSpanContext = getJaegerSpanContext(sofaReference .getSofaTracerSpanContext()); Reference jaegerReference = new Reference(jaegerSpanContext, @@ -192,4 +139,36 @@ private List getJaegerReference(SofaTracerSpan sofaTracerSpan) { return jaegerReferences; } + /** + * convert hexSting to long + * @param hexString + * @return converted long number + */ + public long hexToLong(String hexString) { + Assert.hasText(hexString, "Can't convert empty hex string to long"); + int length = hexString.length(); + if (length < 1) { + throw new IllegalArgumentException("length must be more than zero : " + hexString); + } + if (length <= 16) { + return Long.parseUnsignedLong(hexString, 16); + } + throw new IllegalArgumentException("length must less than 16 :" + hexString); + } + + /** + * convert string to long + * @param data origin string + * @return long converted long + */ + private static long FNV64HashCode(String data) { + 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-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/Utils.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/Utils.java deleted file mode 100644 index e3ec8138a..000000000 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/Utils.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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.jaeger.adapter; - -import org.springframework.util.Assert; - -public class Utils { - /** - * convert hexSting to long - * @param hexString - * @return - */ - public static long hexToLong(String hexString) { - Assert.hasText(hexString, "Can't convert empty hex string to long"); - int length = hexString.length(); - if (length < 1) { - throw new IllegalArgumentException("length must be more than zero : " + hexString); - } - if (length <= 16) { - return Long.parseUnsignedLong(hexString, 16); - } - throw new IllegalArgumentException("length must less than 16 :" + hexString); - } -} diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/initialize/JaegerReportRegisterBean.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/initialize/JaegerReportRegisterBean.java index b51fc8e12..37a9963a0 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/initialize/JaegerReportRegisterBean.java +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/initialize/JaegerReportRegisterBean.java @@ -47,7 +47,8 @@ public void afterPropertiesSet() throws TTransportException { int maxPacketSize = Integer.parseInt(SofaTracerConfiguration .getProperty(JaegerProperties.JAEGER_AGENT_MAX_PACKET_SIZE_KEY)); SpanReportListener spanReportListener = new JaegerSofaTracerSpanRemoteReporter(host, port, - maxPacketSize); + maxPacketSize, + SofaTracerConfiguration.getProperty(JaegerProperties.JAEGER_AGENT_SERVICE_NAME_KEY)); List spanReportListenerList = new ArrayList(); spanReportListenerList.add(spanReportListener); SpanReportListenerHolder.addSpanReportListeners(spanReportListenerList); diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/properties/JaegerProperties.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/properties/JaegerProperties.java index f997dbc3e..d3fdfe31d 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/properties/JaegerProperties.java +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/properties/JaegerProperties.java @@ -24,4 +24,5 @@ public class JaegerProperties { public static final String JAEGER_AGENT_FLUSH_INTERVAL_MS_KEY = "com.alipay.sofa.tracer.jaeger.agent.flushInterval"; public static final String JAEGER_AGENT_MAX_QUEUE_SIZE_KEY = "com.alipay.sofa.tracer.jaeger.agent.maxQueueSize"; public static final String JAEGER_AGENT_CLOSE_ENQUEUE_TIMEOUT_MILLIS_KEY = "com.alipay.sofa.tracer.jaeger.agent.closeEnqueueTimeout"; + public static final String JAEGER_AGENT_SERVICE_NAME_KEY = "spring.application.name"; } diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/utils/NetUtils.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/utils/NetUtils.java new file mode 100644 index 000000000..dee853e84 --- /dev/null +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/utils/NetUtils.java @@ -0,0 +1,166 @@ +/* + * 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.jaeger.utils; + +import com.alipay.common.tracer.core.utils.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.util.Enumeration; +import java.util.regex.Pattern; + +/** + * 网络操作工具类 + * + * @author GengZhang + */ +public class NetUtils { + /** + * slf4j Logger for this class + */ + private final static Logger LOGGER = LoggerFactory.getLogger(NetUtils.class); + + /** + * 任意地址 + */ + public static final String ANYHOST = "0.0.0.0"; + /** + * 本机地址正则 + */ + private static final Pattern LOCAL_IP_PATTERN = Pattern.compile("127(\\.\\d{1,3}){3}$"); + + /** + * IPv4地址 + */ + public static final Pattern IPV4_PATTERN = Pattern + .compile("^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$"); + + private NetUtils() { + } + + /** + * 是否本地地址 127.x.x.x 或者 localhost + * + * @param host 地址 + * @return 是否本地地址 + */ + public static boolean isLocalHost(String host) { + return StringUtils.isNotBlank(host) + && (LOCAL_IP_PATTERN.matcher(host).matches() || "localhost".equalsIgnoreCase(host)); + } + + /** + * 是否默认地址 0.0.0.0 + * + * @param host 地址 + * @return 是否默认地址 + */ + public static boolean isAnyHost(String host) { + return ANYHOST.equals(host); + } + + /** + * 是否IPv4地址 0.0.0.0 + * + * @param host 地址 + * @return 是否默认地址 + */ + public static boolean isIPv4Host(String host) { + return StringUtils.isNotBlank(host) && IPV4_PATTERN.matcher(host).matches(); + } + + /** + * 是否合法地址(非本地,非默认的IPv4地址) + * + * @param address InetAddress + * @return 是否合法 + */ + private static boolean isValidAddress(InetAddress address) { + if (address == null || address.isLoopbackAddress()) { + return false; + } + String name = address.getHostAddress(); + return (name != null && !isAnyHost(name) && !isLocalHost(name) && isIPv4Host(name)); + } + + /** + * 得到本机IPv4地址 + * + * @return ip地址 + */ + public static String getLocalIpv4() { + InetAddress address = getLocalAddress(); + return address == null ? null : address.getHostAddress(); + } + + /** + * 遍历本地网卡,返回第一个合理的IP,保存到缓存中 + * + * @return 本地网卡IP + */ + public static InetAddress getLocalAddress() { + InetAddress localAddress = null; + try { + localAddress = InetAddress.getLocalHost(); + if (isValidAddress(localAddress)) { + return localAddress; + } + } catch (Throwable e) { + if (LOGGER.isWarnEnabled()) { + LOGGER.warn("Error when retrieving ip address: " + e.getMessage(), e); + } + } + try { + Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); + if (interfaces != null) { + while (interfaces.hasMoreElements()) { + try { + NetworkInterface network = interfaces.nextElement(); + Enumeration addresses = network.getInetAddresses(); + while (addresses.hasMoreElements()) { + try { + InetAddress address = addresses.nextElement(); + if (isValidAddress(address)) { + return address; + } + } catch (Throwable e) { + if (LOGGER.isWarnEnabled()) { + LOGGER.warn( + "Error when retrieving ip address: " + e.getMessage(), e); + } + } + } + } catch (Throwable e) { + if (LOGGER.isWarnEnabled()) { + LOGGER.warn("Error when retrieving ip address: " + e.getMessage(), e); + } + } + } + } + } catch (Throwable e) { + if (LOGGER.isWarnEnabled()) { + LOGGER.warn("Error when retrieving ip address: " + e.getMessage(), e); + } + } + if (LOGGER.isErrorEnabled()) { + LOGGER.error("Can't get valid host, will use 127.0.0.1 instead."); + } + return localAddress; + } +} diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/JaegerSofaTracerSpanRemoteReporterTest.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/JaegerSofaTracerSpanRemoteReporterTest.java index 120c0cb3a..a04073cea 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/JaegerSofaTracerSpanRemoteReporterTest.java +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/JaegerSofaTracerSpanRemoteReporterTest.java @@ -23,6 +23,7 @@ import com.alipay.sofa.tracer.plugins.jaeger.adapter.JaegerSpanAdapter; import com.alipay.sofa.tracer.plugins.jaeger.properties.JaegerProperties; import io.jaegertracing.internal.JaegerSpan; +import io.jaegertracing.internal.JaegerTracer; import io.jaegertracing.internal.reporters.RemoteReporter; import io.jaegertracing.spi.Sender; import io.jaegertracing.thrift.internal.senders.UdpSender; @@ -68,16 +69,17 @@ public void init() throws InterruptedException { @Test public void testSpanReport() throws TTransportException { - Sender sender = new UdpSender("127.0.0.1", 6831, 0); - RemoteReporter reporter = new RemoteReporter.Builder().withSender(sender).build(); - jaegerSpan = jaegerSpanAdapter.convertAndReport(sofaTracerSpan, reporter); + JaegerSofaTracerSpanRemoteReporter remoteReporter = new JaegerSofaTracerSpanRemoteReporter( + "127.0.0.1", 6831, 65000, "testService"); + JaegerTracer jaegerTracer = remoteReporter.getJaegerTracer(); + jaegerSpan = jaegerSpanAdapter.convertAndReport(sofaTracerSpan, jaegerTracer); } @Test public void testCommandQueueSetting() throws TTransportException { JaegerSofaTracerSpanRemoteReporter reporter = new JaegerSofaTracerSpanRemoteReporter( - "127.0.0.1", 6831, 0); + "127.0.0.1", 6831, 0, "testService"); Assert.assertTrue(SofaTracerConfiguration.getIntegerDefaultIfNull( JaegerProperties.JAEGER_AGENT_FLUSH_INTERVAL_MS_KEY, 1000) == 200); Assert.assertTrue(SofaTracerConfiguration.getIntegerDefaultIfNull( diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/adapter/JaegerSpanAdapterTest.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/adapter/JaegerSpanAdapterTest.java index 3bbc46883..427a256de 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/adapter/JaegerSpanAdapterTest.java +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/adapter/JaegerSpanAdapterTest.java @@ -20,8 +20,10 @@ 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.jaeger.JaegerSofaTracerSpanRemoteReporter; import com.alipay.sofa.tracer.plugins.jaeger.adapter.JaegerSpanAdapter; import io.jaegertracing.internal.JaegerSpan; +import io.jaegertracing.internal.JaegerTracer; import io.jaegertracing.internal.reporters.RemoteReporter; import io.jaegertracing.spi.Sender; import io.jaegertracing.thrift.internal.senders.UdpSender; @@ -63,10 +65,11 @@ public void init() throws InterruptedException { @Test public void testConvertToJaegerSpan() throws TTransportException { - Sender sender = new UdpSender("127.0.0.1", 6831, 0); - RemoteReporter.Builder builder = new RemoteReporter.Builder().withSender(sender); - RemoteReporter reporter = builder.build(); - JaegerSpan span = jaegerSpanAdapter.convertAndReport(sofaTracerSpan, reporter); + + JaegerSofaTracerSpanRemoteReporter remoteReporter = new JaegerSofaTracerSpanRemoteReporter( + "127.0.0.1", 6831, 65000, "testService"); + JaegerTracer jaegerTracer = remoteReporter.getJaegerTracer(); + JaegerSpan span = jaegerSpanAdapter.convertAndReport(sofaTracerSpan, jaegerTracer); Assert.assertTrue(span != null); Assert.assertTrue(span.getOperationName().equalsIgnoreCase( sofaTracerSpan.getOperationName())); diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/resources/sofa.tracer.properties b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/resources/sofa.tracer.properties index 606b9d4d7..88005ca9e 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/resources/sofa.tracer.properties +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/resources/sofa.tracer.properties @@ -1,12 +1,8 @@ -#com.alipay.sofa.tracer.jaeger.agent.enabled=true -#com.alipay.sofa.tracer.jaeger.agent.host=127.0.0.1 -#com.alipay.sofa.tracer.jaeger.agent.port=6831 -#com.alipay.sofa.tracer.jaeger.agent.maxPacketSize=0 - -com.alipay.sofa.tracer.zipkin.enabled=true -com.alipay.sofa.tracer.zipkin.baseUrl=http://localhost:9411 -com.alipay.sofa.tracer.zipkin.gzipped=true - +com.alipay.sofa.tracer.jaeger.agent.enabled=true +com.alipay.sofa.tracer.jaeger.agent.host=127.0.0.1 +com.alipay.sofa.tracer.jaeger.agent.port=6831 +com.alipay.sofa.tracer.jaeger.agent.maxPacketSize=65000 com.alipay.sofa.tracer.jaeger.agent.flushInterval=200 com.alipay.sofa.tracer.jaeger.agent.maxQueueSize=200 com.alipay.sofa.tracer.jaeger.agent.closeEnqueueTimeout=2000 +spring.application.name=initializeTestService \ No newline at end of file diff --git a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/configuration/JaegerAgentSofaTracerAutoConfiguration.java b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/configuration/JaegerAgentSofaTracerAutoConfiguration.java index 81763fafa..46d1d4eaf 100644 --- a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/configuration/JaegerAgentSofaTracerAutoConfiguration.java +++ b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/configuration/JaegerAgentSofaTracerAutoConfiguration.java @@ -23,6 +23,7 @@ import io.jaegertracing.internal.JaegerTracer; import org.apache.thrift.transport.TTransportException; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -41,11 +42,15 @@ public class JaegerAgentSofaTracerAutoConfiguration { @Autowired private JaegerAgentSofaTracerProperties jaegerAgentProperties; + @Value("${spring.application.name}") + private String serviceName; + @Bean @ConditionalOnMissingBean - public JaegerSofaTracerSpanRemoteReporter JaegerAgentSofaTracerSpanReporter() + public JaegerSofaTracerSpanRemoteReporter jaegerAgentSofaTracerSpanReporter() throws TTransportException { return new JaegerSofaTracerSpanRemoteReporter(jaegerAgentProperties.getHost(), - jaegerAgentProperties.getPort(), jaegerAgentProperties.getMaxPacketSize()); + jaegerAgentProperties.getPort(), jaegerAgentProperties.getMaxPacketSize(), serviceName); } + } From 9202609a609cd50d2f2599fa27c336885ed8dc77 Mon Sep 17 00:00:00 2001 From: zzuzh Date: Mon, 2 Aug 2021 14:11:01 +0800 Subject: [PATCH 7/9] fix_format --- pom.xml | 1 - .../plugins/jaeger/JaegerSofaTracerSpanRemoteReporter.java | 3 ++- .../sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 27319ff21..74b463f4b 100644 --- a/pom.xml +++ b/pom.xml @@ -53,7 +53,6 @@ ${user.dir} 1.6.7 1.5 - true diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerSpanRemoteReporter.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerSpanRemoteReporter.java index 904e83f37..44ab1aa17 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerSpanRemoteReporter.java +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerSpanRemoteReporter.java @@ -71,7 +71,8 @@ public void onSpanReport(SofaTracerSpan sofaTracerSpan) { @Override public void close() throws IOException { - this.reporter.close(); + //close jaegerTracer and it will close reporter internal + this.jaegerTracer.close(); } public RemoteReporter getReporter() { diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java index daae4a4ea..4059e739e 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java @@ -56,7 +56,7 @@ public JaegerSpan convertAndReport(SofaTracerSpan sofaTracerSpan, JaegerTracer j jaegerSpanContext, startTimeMicroseconds, startTimeNanoTicks, computeDurationViaNanoTicks, tags, references); - //set durationMicroseconds + //set durationMicroseconds, if the span is sampled will add AppendCommand to CommandQueue jaegerSpan.finish(sofaTracerSpan.getEndTime() * 1000); //set logs From 4bf6bafbd2aba764e44a4c3205d2b2c858ce386d Mon Sep 17 00:00:00 2001 From: zzuzh Date: Tue, 17 Aug 2021 21:07:04 +0800 Subject: [PATCH 8/9] =?UTF-8?q?http=E4=B8=8A=E4=BC=A0=E9=83=A8=E5=88=86?= =?UTF-8?q?=E6=94=B9=E6=88=90=E4=BD=BF=E7=94=A8HttpSender=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0Jaeger.thrift=E6=A0=BC=E5=BC=8F=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 18 +- .../sofa-tracer-jaeger-plugin/pom.xml | 14 +- .../JaegerSofaTracerSpanRemoteReporter.java | 60 ++++--- .../jaeger/adapter/JaegerSpanAdapter.java | 62 +++++-- .../initialize/JaegerReportRegisterBean.java | 61 +++++-- .../jaeger/properties/JaegerProperties.java | 23 ++- ...aegerSofaTracerSpanRemoteReporterTest.java | 33 ++-- .../java/adapter/JaegerSpanAdapterTest.java | 11 +- .../JaegerReportRegisterBeanTest.java | 4 + .../src/test/resources/sofa.tracer.properties | 11 +- .../plugins/zipkin/adapter/NetUtils.java | 166 ------------------ .../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 | 5 +- .../common/tracer/core}/utils/NetUtils.java | 3 +- tracer-sofa-boot-starter/pom.xml | 4 + ...aegerAgentSofaTracerAutoConfiguration.java | 56 ------ .../JaegerSofaTracerAutoConfiguration.java | 39 ++-- .../JaegerAgentSofaTracerProperties.java | 72 -------- .../JaegerSofaTracerProperties.java | 68 +++++-- .../main/resources/META-INF/spring.factories | 1 - 25 files changed, 314 insertions(+), 416 deletions(-) delete mode 100644 sofa-tracer-plugins/sofa-tracer-zipkin-plugin/src/main/java/com/alipay/sofa/tracer/plugins/zipkin/adapter/NetUtils.java rename {sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger => tracer-core/src/main/java/com/alipay/common/tracer/core}/utils/NetUtils.java (98%) delete mode 100644 tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/configuration/JaegerAgentSofaTracerAutoConfiguration.java delete mode 100644 tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/properties/JaegerAgentSofaTracerProperties.java diff --git a/pom.xml b/pom.xml index 74b463f4b..c0d1af217 100644 --- a/pom.xml +++ b/pom.xml @@ -42,6 +42,7 @@ tracer-test/log4j2-test tracer-test/log4j-test + @@ -240,6 +241,16 @@ 1.3.1 + + org.projectlombok + lombok + 1.16.10 + + + com.alibaba + fastjson + 1.2.51 + javax.servlet @@ -247,6 +258,7 @@ 3.1.0 provided + org.apache.httpcomponents @@ -301,11 +313,7 @@ ${jmh.version} test - - com.alibaba - fastjson - 1.2.51 - + diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/pom.xml b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/pom.xml index ff5be8a8a..770ac5c84 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/pom.xml +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/pom.xml @@ -37,11 +37,22 @@ org.springframework spring-context - + io.jaegertracing jaeger-client 1.6.0 + + + com.squareup.okhttp3 + okhttp + + + + + com.squareup.okhttp3 + okhttp + 3.12.1 @@ -64,6 +75,7 @@ opentracing-util 0.33.0 + org.jmockit diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerSpanRemoteReporter.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerSpanRemoteReporter.java index 44ab1aa17..ebb8faf98 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerSpanRemoteReporter.java +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/JaegerSofaTracerSpanRemoteReporter.java @@ -16,48 +16,64 @@ */ package com.alipay.sofa.tracer.plugins.jaeger; -import com.alipay.common.tracer.core.configuration.SofaTracerConfiguration; import com.alipay.common.tracer.core.listener.SpanReportListener; import com.alipay.common.tracer.core.span.SofaTracerSpan; import com.alipay.sofa.tracer.plugins.jaeger.adapter.JaegerSpanAdapter; -import com.alipay.sofa.tracer.plugins.jaeger.properties.JaegerProperties; -import com.alipay.sofa.tracer.plugins.jaeger.utils.NetUtils; +import com.alipay.common.tracer.core.utils.NetUtils; import io.jaegertracing.internal.JaegerTracer; import io.jaegertracing.internal.reporters.RemoteReporter; +import io.jaegertracing.thrift.internal.senders.HttpSender; +import io.jaegertracing.thrift.internal.senders.ThriftSender; import io.jaegertracing.thrift.internal.senders.UdpSender; +import okhttp3.OkHttpClient; import org.apache.thrift.transport.TTransportException; import java.io.Closeable; import java.io.IOException; +import java.util.concurrent.TimeUnit; +/** + * JaegerSofaTracerSpanRemoteReporter + * @author: zhaochen + */ public class JaegerSofaTracerSpanRemoteReporter implements SpanReportListener, Closeable { - private JaegerSpanAdapter adapter = new JaegerSpanAdapter(); - private UdpSender jaegerUdpSender; + private JaegerSpanAdapter adapter = new JaegerSpanAdapter(); + private ThriftSender sender; private RemoteReporter reporter; private JaegerTracer jaegerTracer; + private static final int OKHTTP_MAX_REQUESTS = 64; + private static final Long OKHTTP_READ_TIMEOUT_SECONDS = 60L; public JaegerSofaTracerSpanRemoteReporter(String host, int port, int maxPacketSize, - String serviceName) throws TTransportException { - //user compact thrift protocol by default - jaegerUdpSender = new UdpSender(host, port, maxPacketSize); + String serviceName, int flushInterval, + int maxQueueSize, int closeEnqueueTimeout) + throws TTransportException { + //use UdpSender to send to the jaeger Agent + sender = new UdpSender(host, port, maxPacketSize); + buildTracer(serviceName, flushInterval, maxQueueSize, closeEnqueueTimeout); + } - //Use the configuration file named sofa.tracer.properties to configure the command queue in jaeger - //The interval of writing FlushCommand to the command queue - Integer flushInterval = SofaTracerConfiguration.getIntegerDefaultIfNull( - JaegerProperties.JAEGER_AGENT_FLUSH_INTERVAL_MS_KEY, 1000); - // size of the command queue is too large will waste space, and too small will cause the span to be lost - Integer maxQueueSize = SofaTracerConfiguration.getIntegerDefaultIfNull( - JaegerProperties.JAEGER_AGENT_MAX_QUEUE_SIZE_KEY, 100); - //Timeout for writing CloseCommand - Integer closeEnqueueTimeout = SofaTracerConfiguration.getIntegerDefaultIfNull( - JaegerProperties.JAEGER_AGENT_CLOSE_ENQUEUE_TIMEOUT_MILLIS_KEY, 1000); + public JaegerSofaTracerSpanRemoteReporter(String baseUrl, int maxPacketSizeBytes, + String serviceName, int flushInterval, + int maxQueueSize, int closeEnqueueTimeout) + throws TTransportException { + String url = baseUrl + (baseUrl.endsWith("/") ? "" : "/") + "api/traces"; + OkHttpClient client = new OkHttpClient.Builder().readTimeout(OKHTTP_READ_TIMEOUT_SECONDS, + TimeUnit.SECONDS).build(); + client.dispatcher().setMaxRequests(OKHTTP_MAX_REQUESTS); + client.dispatcher().setMaxRequestsPerHost(OKHTTP_MAX_REQUESTS); + //use Http sender to send to Jaeger collector directly + sender = new HttpSender.Builder(url).withMaxPacketSize(maxPacketSizeBytes) + .withClient(client).build(); + buildTracer(serviceName, flushInterval, maxQueueSize, closeEnqueueTimeout); + } - reporter = new RemoteReporter.Builder().withSender(jaegerUdpSender) - .withFlushInterval(flushInterval).withMaxQueueSize(maxQueueSize) - .withCloseEnqueueTimeout(closeEnqueueTimeout).build(); + private void buildTracer(String serviceName, int flushInterval, int maxQueueSize, + int closeEnqueueTimeout) { + reporter = new RemoteReporter.Builder().withSender(sender).withFlushInterval(flushInterval) + .withMaxQueueSize(maxQueueSize).withCloseEnqueueTimeout(closeEnqueueTimeout).build(); jaegerTracer = new JaegerTracer.Builder(serviceName).withReporter(reporter) .withTraceId128Bit().withTag("ip", NetUtils.getLocalIpv4()).build(); - } @Override diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java index 4059e739e..6415cf045 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java @@ -16,10 +16,19 @@ */ package com.alipay.sofa.tracer.plugins.jaeger.adapter; +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 io.jaegertracing.internal.*; +import com.alipay.common.tracer.core.utils.StringUtils; +import io.jaegertracing.internal.JaegerSpan; +import io.jaegertracing.internal.JaegerTracer; +import io.jaegertracing.internal.JaegerSpanContext; +import io.jaegertracing.internal.Reference; +import io.jaegertracing.internal.JaegerObjectFactory; +import io.opentracing.tag.Tags; import org.springframework.util.Assert; import java.util.ArrayList; @@ -28,7 +37,9 @@ import java.util.Map; /** - * convert sofaTracerSpan to JaegerSpan and use UdpSender to send jaegerSpan to the jaeger agent + * JaegerSpanAdapter + * sofaTracerSpan to JaegerSpan and use UdpSender to send jaegerSpan to the jaeger agent + * @author zhaochen */ public class JaegerSpanAdapter { @@ -42,7 +53,16 @@ public JaegerSpan convertAndReport(SofaTracerSpan sofaTracerSpan, JaegerTracer j //construct tags in JaegerSpan Map tags = new LinkedHashMap<>(); - tags.putAll(sofaTracerSpan.getTagsWithStr()); + Map strTags = sofaTracerSpan.getTagsWithStr(); + //in sofaTracer error contains the error message while in jaeger error represents whether it's error + if (strTags.containsKey(Tags.ERROR.getKey())) { + strTags.put("error.message", strTags.get(Tags.ERROR.getKey())); + } + String resultCode = strTags.get(CommonSpanTags.RESULT_CODE); + if (StringUtils.isNotBlank(resultCode) && !isWebHttpClientSuccess(resultCode)) { + sofaTracerSpan.getTagsWithBool().put(Tags.ERROR.getKey(), true); + } + tags.putAll(strTags); tags.putAll(sofaTracerSpan.getTagsWithBool()); tags.putAll(sofaTracerSpan.getTagsWithNumber()); @@ -56,15 +76,15 @@ public JaegerSpan convertAndReport(SofaTracerSpan sofaTracerSpan, JaegerTracer j jaegerSpanContext, startTimeMicroseconds, startTimeNanoTicks, computeDurationViaNanoTicks, tags, references); - //set durationMicroseconds, if the span is sampled will add AppendCommand to CommandQueue - jaegerSpan.finish(sofaTracerSpan.getEndTime() * 1000); - //set logs jaegerSpan = setLogData(sofaTracerSpan, jaegerSpan); //convert baggage jaegerSpan = convertBaggage(sofaTracerSpan, jaegerSpan); + //set durationMicroseconds, if the span is sampled will add AppendCommand to CommandQueue + jaegerSpan.finish(sofaTracerSpan.getEndTime() * 1000); + return jaegerSpan; } @@ -74,11 +94,8 @@ public JaegerSpan convertAndReport(SofaTracerSpan sofaTracerSpan, JaegerTracer j * @return JaegerSpanContext */ private JaegerSpanContext getJaegerSpanContext(SofaTracerSpanContext sofaTracerSpanContext) { - - String sofaTraceId = sofaTracerSpanContext.getTraceId(); //when length is less than 32 add 0 to the font - sofaTraceId = "00000000000000000000000000000000".substring(sofaTraceId.length()) - + sofaTraceId; + String sofaTraceId = padLeft(sofaTracerSpanContext.getTraceId(), 32); long traceIdHigh = hexToLong(sofaTraceId.substring(0, 16)); long traceIdLow = hexToLong(sofaTraceId.substring(16)); @@ -111,9 +128,9 @@ private JaegerSpan convertBaggage(SofaTracerSpan sofaTracerSpan, JaegerSpan jaeg * convert logdata */ private JaegerSpan setLogData(SofaTracerSpan span, JaegerSpan jaegerSpan) { - List sofaLogDatas = span.getLogs(); + List sofaLogDatas = span.getLogs(); - for (com.alipay.common.tracer.core.span.LogData sofalogData : sofaLogDatas) { + for (LogData sofalogData : sofaLogDatas) { jaegerSpan.log(sofalogData.getTime() * 1000, sofalogData.getFields()); } return jaegerSpan; @@ -171,4 +188,25 @@ private static long FNV64HashCode(String data) { return hash; } + 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)); + } + + private String padLeft(String id, int desiredLength) { + StringBuilder builder = new StringBuilder(desiredLength); + int offset = desiredLength - id.length(); + + for (int i = 0; i < offset; i++) + builder.append('0'); + builder.append(id); + return builder.toString(); + } + } diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/initialize/JaegerReportRegisterBean.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/initialize/JaegerReportRegisterBean.java index 37a9963a0..6f73c8fe6 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/initialize/JaegerReportRegisterBean.java +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/initialize/JaegerReportRegisterBean.java @@ -28,29 +28,64 @@ import java.util.ArrayList; import java.util.List; +/** + * JaegerReportRegisterBean + * @author: zhaochen + */ public class JaegerReportRegisterBean implements InitializingBean { @Override public void afterPropertiesSet() throws TTransportException { - boolean enabled = false; + //Use the configuration file named sofa.tracer.properties to configure the command queue in jaeger + //The interval of writing FlushCommand to the command queue + int flushInterval = SofaTracerConfiguration.getIntegerDefaultIfNull( + JaegerProperties.JAEGER_FLUSH_INTERVAL_MS_KEY, 1000); + // size of the command queue is too large will waste space, and too small will cause the span to be lost + int maxQueueSize = SofaTracerConfiguration.getIntegerDefaultIfNull( + JaegerProperties.JAEGER_MAX_QUEUE_SIZE_KEY, 10000); + //Timeout for writing CloseCommand + int closeEnqueueTimeout = SofaTracerConfiguration.getIntegerDefaultIfNull( + JaegerProperties.JAEGER_CLOSE_ENQUEUE_TIMEOUT_MILLIS_KEY, 1000); + String serviceName = SofaTracerConfiguration + .getProperty(JaegerProperties.JAEGER_SERVICE_NAME_KEY); + String enabledStr = SofaTracerConfiguration - .getProperty(JaegerProperties.JAEGER_AGENT_IS_ENABLED_KEY); + .getProperty(JaegerProperties.JAEGER_IS_ENABLED_KEY); + String receiver = SofaTracerConfiguration.getProperty(JaegerProperties.JAEGER_RECEIVER_KEY, + "collector"); + boolean enabled = false; if (StringUtils.isNotBlank(enabledStr) && "true".equalsIgnoreCase(enabledStr)) { enabled = true; } if (!enabled) { return; } - String host = SofaTracerConfiguration.getProperty(JaegerProperties.JAEGER_AGENT_HOST_KEY); - int port = Integer.parseInt(SofaTracerConfiguration - .getProperty(JaegerProperties.JAEGER_AGENT_PORT_KEY)); - int maxPacketSize = Integer.parseInt(SofaTracerConfiguration - .getProperty(JaegerProperties.JAEGER_AGENT_MAX_PACKET_SIZE_KEY)); - SpanReportListener spanReportListener = new JaegerSofaTracerSpanRemoteReporter(host, port, - maxPacketSize, - SofaTracerConfiguration.getProperty(JaegerProperties.JAEGER_AGENT_SERVICE_NAME_KEY)); - List spanReportListenerList = new ArrayList(); - spanReportListenerList.add(spanReportListener); - SpanReportListenerHolder.addSpanReportListeners(spanReportListenerList); + // receiver is collector + if ("collector".equals(receiver)) { + String baseUrl = SofaTracerConfiguration.getProperty( + JaegerProperties.JAEGER_COLLECTOR_BASE_URL_KEY, "http://localhost:14268/"); + Integer maxPacketSize = SofaTracerConfiguration.getIntegerDefaultIfNull( + JaegerProperties.JAEGER_COLLECTOR_MAX_PACKET_SIZE_KEY, 2 * 1024 * 1024); + SpanReportListener spanReportListener = new JaegerSofaTracerSpanRemoteReporter(baseUrl, + maxPacketSize, serviceName, flushInterval, maxQueueSize, closeEnqueueTimeout); + List spanReportListenerList = new ArrayList(); + spanReportListenerList.add(spanReportListener); + SpanReportListenerHolder.addSpanReportListeners(spanReportListenerList); + } + // receiver is agent + if ("agent".equals(receiver)) { + String host = SofaTracerConfiguration.getProperty( + JaegerProperties.JAEGER_AGENT_HOST_KEY, "127.0.0.1"); + int port = SofaTracerConfiguration.getIntegerDefaultIfNull( + JaegerProperties.JAEGER_AGENT_PORT_KEY, 6381); + int maxPacketSize = SofaTracerConfiguration.getIntegerDefaultIfNull( + JaegerProperties.JAEGER_AGENT_MAX_PACKET_SIZE_KEY, 65000); + SpanReportListener spanReportListener = new JaegerSofaTracerSpanRemoteReporter(host, + port, maxPacketSize, serviceName, flushInterval, maxQueueSize, closeEnqueueTimeout); + List spanReportListenerList = new ArrayList(); + spanReportListenerList.add(spanReportListener); + SpanReportListenerHolder.addSpanReportListeners(spanReportListenerList); + } + } } diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/properties/JaegerProperties.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/properties/JaegerProperties.java index d3fdfe31d..5cde15805 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/properties/JaegerProperties.java +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/properties/JaegerProperties.java @@ -16,13 +16,20 @@ */ package com.alipay.sofa.tracer.plugins.jaeger.properties; +/** + * JaegerProperties + * @author: zhaochen + */ public class JaegerProperties { - public static final String JAEGER_AGENT_IS_ENABLED_KEY = "com.alipay.sofa.tracer.jaeger.agent.enabled"; - public static final String JAEGER_AGENT_HOST_KEY = "com.alipay.sofa.tracer.jaeger.agent.host"; - public static final String JAEGER_AGENT_PORT_KEY = "com.alipay.sofa.tracer.jaeger.agent.port"; - public static final String JAEGER_AGENT_MAX_PACKET_SIZE_KEY = "com.alipay.sofa.tracer.jaeger.agent.maxPacketSize"; - public static final String JAEGER_AGENT_FLUSH_INTERVAL_MS_KEY = "com.alipay.sofa.tracer.jaeger.agent.flushInterval"; - public static final String JAEGER_AGENT_MAX_QUEUE_SIZE_KEY = "com.alipay.sofa.tracer.jaeger.agent.maxQueueSize"; - public static final String JAEGER_AGENT_CLOSE_ENQUEUE_TIMEOUT_MILLIS_KEY = "com.alipay.sofa.tracer.jaeger.agent.closeEnqueueTimeout"; - public static final String JAEGER_AGENT_SERVICE_NAME_KEY = "spring.application.name"; + public static final String JAEGER_IS_ENABLED_KEY = "com.alipay.sofa.tracer.jaeger.enabled"; + public static final String JAEGER_COLLECTOR_BASE_URL_KEY = "com.alipay.sofa.tracer.jaeger.collector.baseUrl"; + public static final String JAEGER_COLLECTOR_MAX_PACKET_SIZE_KEY = "com.alipay.sofa.tracer.jaeger.collector.maxPacketSizeBytes"; + public static final String JAEGER_RECEIVER_KEY = "com.alipay.sofa.tracer.jaeger.receiver"; + public static final String JAEGER_AGENT_HOST_KEY = "com.alipay.sofa.tracer.jaeger.agent.host"; + public static final String JAEGER_AGENT_PORT_KEY = "com.alipay.sofa.tracer.jaeger.agent.port"; + public static final String JAEGER_AGENT_MAX_PACKET_SIZE_KEY = "com.alipay.sofa.tracer.jaeger.agent.maxPacketSizeBytes"; + public static final String JAEGER_FLUSH_INTERVAL_MS_KEY = "com.alipay.sofa.tracer.jaeger.flushInterval"; + public static final String JAEGER_MAX_QUEUE_SIZE_KEY = "com.alipay.sofa.tracer.jaeger.maxQueueSize"; + public static final String JAEGER_CLOSE_ENQUEUE_TIMEOUT_MILLIS_KEY = "com.alipay.sofa.tracer.jaeger.closeEnqueueTimeout"; + public static final String JAEGER_SERVICE_NAME_KEY = "spring.application.name"; } diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/JaegerSofaTracerSpanRemoteReporterTest.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/JaegerSofaTracerSpanRemoteReporterTest.java index a04073cea..9311ee94d 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/JaegerSofaTracerSpanRemoteReporterTest.java +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/JaegerSofaTracerSpanRemoteReporterTest.java @@ -22,11 +22,7 @@ import com.alipay.sofa.tracer.plugins.jaeger.JaegerSofaTracerSpanRemoteReporter; import com.alipay.sofa.tracer.plugins.jaeger.adapter.JaegerSpanAdapter; import com.alipay.sofa.tracer.plugins.jaeger.properties.JaegerProperties; -import io.jaegertracing.internal.JaegerSpan; import io.jaegertracing.internal.JaegerTracer; -import io.jaegertracing.internal.reporters.RemoteReporter; -import io.jaegertracing.spi.Sender; -import io.jaegertracing.thrift.internal.senders.UdpSender; import org.apache.thrift.transport.TTransportException; import org.junit.Assert; import org.junit.Before; @@ -35,6 +31,10 @@ import java.util.HashMap; import java.util.Map; +/** + * JaegerSofaTracerSpanRemoteReporterTest + * @author: zhaochen + */ public class JaegerSofaTracerSpanRemoteReporterTest { private JaegerSpanAdapter jaegerSpanAdapter = new JaegerSpanAdapter(); @@ -44,8 +44,6 @@ public class JaegerSofaTracerSpanRemoteReporterTest { private SofaTracerSpan sofaTracerSpan; - private JaegerSpan jaegerSpan; - @Before public void init() throws InterruptedException { sofaTracer = new SofaTracer.Builder(tracerType).withTag("tracer", "SofaTraceZipkinTest") @@ -67,25 +65,34 @@ public void init() throws InterruptedException { } @Test - public void testSpanReport() throws TTransportException { + public void testAgentSpanReport() throws TTransportException { JaegerSofaTracerSpanRemoteReporter remoteReporter = new JaegerSofaTracerSpanRemoteReporter( - "127.0.0.1", 6831, 65000, "testService"); + "127.0.0.1", 6831, 65000, "testService", 1000, 10000, 1000); JaegerTracer jaegerTracer = remoteReporter.getJaegerTracer(); - jaegerSpan = jaegerSpanAdapter.convertAndReport(sofaTracerSpan, jaegerTracer); + jaegerSpanAdapter.convertAndReport(sofaTracerSpan, jaegerTracer); + + } + @Test + public void testCollectorSpanReport() throws TTransportException { + + JaegerSofaTracerSpanRemoteReporter remoteReporter = new JaegerSofaTracerSpanRemoteReporter( + "http://localhost:14268/", 2 * 1024 * 1024, "testService", 1000, 10000, 1000); + JaegerTracer jaegerTracer = remoteReporter.getJaegerTracer(); + jaegerSpanAdapter.convertAndReport(sofaTracerSpan, jaegerTracer); } @Test public void testCommandQueueSetting() throws TTransportException { JaegerSofaTracerSpanRemoteReporter reporter = new JaegerSofaTracerSpanRemoteReporter( - "127.0.0.1", 6831, 0, "testService"); + "127.0.0.1", 6831, 0, "testService", 1000, 10000, 1000); Assert.assertTrue(SofaTracerConfiguration.getIntegerDefaultIfNull( - JaegerProperties.JAEGER_AGENT_FLUSH_INTERVAL_MS_KEY, 1000) == 200); + JaegerProperties.JAEGER_FLUSH_INTERVAL_MS_KEY, 1000) == 200); Assert.assertTrue(SofaTracerConfiguration.getIntegerDefaultIfNull( - JaegerProperties.JAEGER_AGENT_MAX_QUEUE_SIZE_KEY, 100) == 200); + JaegerProperties.JAEGER_MAX_QUEUE_SIZE_KEY, 10000) == 200); Assert.assertTrue(SofaTracerConfiguration.getIntegerDefaultIfNull( - JaegerProperties.JAEGER_AGENT_CLOSE_ENQUEUE_TIMEOUT_MILLIS_KEY, 1000) == 2000); + JaegerProperties.JAEGER_CLOSE_ENQUEUE_TIMEOUT_MILLIS_KEY, 1000) == 2000); } } diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/adapter/JaegerSpanAdapterTest.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/adapter/JaegerSpanAdapterTest.java index 427a256de..5944d1b90 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/adapter/JaegerSpanAdapterTest.java +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/adapter/JaegerSpanAdapterTest.java @@ -24,9 +24,6 @@ import com.alipay.sofa.tracer.plugins.jaeger.adapter.JaegerSpanAdapter; import io.jaegertracing.internal.JaegerSpan; import io.jaegertracing.internal.JaegerTracer; -import io.jaegertracing.internal.reporters.RemoteReporter; -import io.jaegertracing.spi.Sender; -import io.jaegertracing.thrift.internal.senders.UdpSender; import org.apache.thrift.transport.TTransportException; import org.junit.Assert; import org.junit.Before; @@ -35,6 +32,10 @@ import java.util.HashMap; import java.util.Map; +/** + * JaegerSpanAdapterTest + * @author: zhaochen + */ public class JaegerSpanAdapterTest { private JaegerSpanAdapter jaegerSpanAdapter = new JaegerSpanAdapter(); @@ -64,10 +65,10 @@ public void init() throws InterruptedException { } @Test - public void testConvertToJaegerSpan() throws TTransportException { + public void testConvertAndReport() throws TTransportException { JaegerSofaTracerSpanRemoteReporter remoteReporter = new JaegerSofaTracerSpanRemoteReporter( - "127.0.0.1", 6831, 65000, "testService"); + "127.0.0.1", 6831, 65000, "testService", 1000, 10000, 1000); JaegerTracer jaegerTracer = remoteReporter.getJaegerTracer(); JaegerSpan span = jaegerSpanAdapter.convertAndReport(sofaTracerSpan, jaegerTracer); Assert.assertTrue(span != null); diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/initialize/JaegerReportRegisterBeanTest.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/initialize/JaegerReportRegisterBeanTest.java index b9ceff4dc..de45e1cb1 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/initialize/JaegerReportRegisterBeanTest.java +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/java/initialize/JaegerReportRegisterBeanTest.java @@ -23,6 +23,10 @@ import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; +/** + * JaegerReportRegisterBeanTest + * @author: zhaochen + */ public class JaegerReportRegisterBeanTest { private ClassPathXmlApplicationContext applicationContext; diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/resources/sofa.tracer.properties b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/resources/sofa.tracer.properties index 88005ca9e..83268d292 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/resources/sofa.tracer.properties +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/test/resources/sofa.tracer.properties @@ -1,8 +1,9 @@ -com.alipay.sofa.tracer.jaeger.agent.enabled=true +com.alipay.sofa.tracer.jaeger.enabled=true +com.alipay.sofa.tracer.jaeger.receiver = collector com.alipay.sofa.tracer.jaeger.agent.host=127.0.0.1 com.alipay.sofa.tracer.jaeger.agent.port=6831 -com.alipay.sofa.tracer.jaeger.agent.maxPacketSize=65000 -com.alipay.sofa.tracer.jaeger.agent.flushInterval=200 -com.alipay.sofa.tracer.jaeger.agent.maxQueueSize=200 -com.alipay.sofa.tracer.jaeger.agent.closeEnqueueTimeout=2000 +com.alipay.sofa.tracer.jaeger.agent.maxPacketSizeBytes=65000 +com.alipay.sofa.tracer.jaeger.flushInterval=200 +com.alipay.sofa.tracer.jaeger.maxQueueSize=200 +com.alipay.sofa.tracer.jaeger.closeEnqueueTimeout=2000 spring.application.name=initializeTestService \ No newline at end of file diff --git a/sofa-tracer-plugins/sofa-tracer-zipkin-plugin/src/main/java/com/alipay/sofa/tracer/plugins/zipkin/adapter/NetUtils.java b/sofa-tracer-plugins/sofa-tracer-zipkin-plugin/src/main/java/com/alipay/sofa/tracer/plugins/zipkin/adapter/NetUtils.java deleted file mode 100644 index 3d533bc41..000000000 --- a/sofa-tracer-plugins/sofa-tracer-zipkin-plugin/src/main/java/com/alipay/sofa/tracer/plugins/zipkin/adapter/NetUtils.java +++ /dev/null @@ -1,166 +0,0 @@ -/* - * 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.zipkin.adapter; - -import com.alipay.common.tracer.core.utils.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.net.InetAddress; -import java.net.NetworkInterface; -import java.util.Enumeration; -import java.util.regex.Pattern; - -/** - * 网络操作工具类 - * - * @author GengZhang - */ -class NetUtils { - /** - * slf4j Logger for this class - */ - private final static Logger LOGGER = LoggerFactory.getLogger(NetUtils.class); - - /** - * 任意地址 - */ - public static final String ANYHOST = "0.0.0.0"; - /** - * 本机地址正则 - */ - private static final Pattern LOCAL_IP_PATTERN = Pattern.compile("127(\\.\\d{1,3}){3}$"); - - /** - * IPv4地址 - */ - public static final Pattern IPV4_PATTERN = Pattern - .compile("^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$"); - - private NetUtils() { - } - - /** - * 是否本地地址 127.x.x.x 或者 localhost - * - * @param host 地址 - * @return 是否本地地址 - */ - public static boolean isLocalHost(String host) { - return StringUtils.isNotBlank(host) - && (LOCAL_IP_PATTERN.matcher(host).matches() || "localhost".equalsIgnoreCase(host)); - } - - /** - * 是否默认地址 0.0.0.0 - * - * @param host 地址 - * @return 是否默认地址 - */ - public static boolean isAnyHost(String host) { - return ANYHOST.equals(host); - } - - /** - * 是否IPv4地址 0.0.0.0 - * - * @param host 地址 - * @return 是否默认地址 - */ - public static boolean isIPv4Host(String host) { - return StringUtils.isNotBlank(host) && IPV4_PATTERN.matcher(host).matches(); - } - - /** - * 是否合法地址(非本地,非默认的IPv4地址) - * - * @param address InetAddress - * @return 是否合法 - */ - private static boolean isValidAddress(InetAddress address) { - if (address == null || address.isLoopbackAddress()) { - return false; - } - String name = address.getHostAddress(); - return (name != null && !isAnyHost(name) && !isLocalHost(name) && isIPv4Host(name)); - } - - /** - * 得到本机IPv4地址 - * - * @return ip地址 - */ - public static String getLocalIpv4() { - InetAddress address = getLocalAddress(); - return address == null ? null : address.getHostAddress(); - } - - /** - * 遍历本地网卡,返回第一个合理的IP,保存到缓存中 - * - * @return 本地网卡IP - */ - public static InetAddress getLocalAddress() { - InetAddress localAddress = null; - try { - localAddress = InetAddress.getLocalHost(); - if (isValidAddress(localAddress)) { - return localAddress; - } - } catch (Throwable e) { - if (LOGGER.isWarnEnabled()) { - LOGGER.warn("Error when retrieving ip address: " + e.getMessage(), e); - } - } - try { - Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); - if (interfaces != null) { - while (interfaces.hasMoreElements()) { - try { - NetworkInterface network = interfaces.nextElement(); - Enumeration addresses = network.getInetAddresses(); - while (addresses.hasMoreElements()) { - try { - InetAddress address = addresses.nextElement(); - if (isValidAddress(address)) { - return address; - } - } catch (Throwable e) { - if (LOGGER.isWarnEnabled()) { - LOGGER.warn( - "Error when retrieving ip address: " + e.getMessage(), e); - } - } - } - } catch (Throwable e) { - if (LOGGER.isWarnEnabled()) { - LOGGER.warn("Error when retrieving ip address: " + e.getMessage(), e); - } - } - } - } - } catch (Throwable e) { - if (LOGGER.isWarnEnabled()) { - LOGGER.warn("Error when retrieving ip address: " + e.getMessage(), e); - } - } - if (LOGGER.isErrorEnabled()) { - LOGGER.error("Can't get valid host, will use 127.0.0.1 instead."); - } - return localAddress; - } -} 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..1a623522b 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,14 +101,14 @@ 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 * @return a new spam */ public SofaTracerSpan clientSend(String operationName) { - SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext(); + SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();//把traceContext中的父span弹出 SofaTracerSpan serverSpan = sofaTraceContext.pop(); SofaTracerSpan clientSpan = null; try { @@ -196,6 +196,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-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/utils/NetUtils.java b/tracer-core/src/main/java/com/alipay/common/tracer/core/utils/NetUtils.java similarity index 98% rename from sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/utils/NetUtils.java rename to tracer-core/src/main/java/com/alipay/common/tracer/core/utils/NetUtils.java index dee853e84..094ed4073 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/utils/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.jaeger.utils; +package com.alipay.common.tracer.core.utils; -import com.alipay.common.tracer.core.utils.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/tracer-sofa-boot-starter/pom.xml b/tracer-sofa-boot-starter/pom.xml index 4d7641717..065a925e1 100644 --- a/tracer-sofa-boot-starter/pom.xml +++ b/tracer-sofa-boot-starter/pom.xml @@ -291,5 +291,9 @@ 1.3.1 test + + org.projectlombok + lombok + diff --git a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/configuration/JaegerAgentSofaTracerAutoConfiguration.java b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/configuration/JaegerAgentSofaTracerAutoConfiguration.java deleted file mode 100644 index 46d1d4eaf..000000000 --- a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/configuration/JaegerAgentSofaTracerAutoConfiguration.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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.jaeger.configuration; - -import com.alipay.sofa.tracer.boot.jaeger.properties.JaegerAgentSofaTracerProperties; -import com.alipay.sofa.tracer.plugins.jaeger.JaegerSofaTracerSpanRemoteReporter; -import io.jaegertracing.internal.JaegerSpan; -import io.jaegertracing.internal.JaegerSpanContext; -import io.jaegertracing.internal.JaegerTracer; -import org.apache.thrift.transport.TTransportException; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -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; - -/** - * report jaeger.thrift data to the jaeger agent - */ -@Configuration -@EnableConfigurationProperties(JaegerAgentSofaTracerProperties.class) -@ConditionalOnProperty(value = "com.alipay.sofa.tracer.jaeger.agent.enabled", matchIfMissing = false) -@ConditionalOnClass({ JaegerSpan.class, JaegerTracer.class, JaegerSpanContext.class }) -public class JaegerAgentSofaTracerAutoConfiguration { - @Autowired - private JaegerAgentSofaTracerProperties jaegerAgentProperties; - - @Value("${spring.application.name}") - private String serviceName; - - @Bean - @ConditionalOnMissingBean - public JaegerSofaTracerSpanRemoteReporter jaegerAgentSofaTracerSpanReporter() - throws TTransportException { - return new JaegerSofaTracerSpanRemoteReporter(jaegerAgentProperties.getHost(), - jaegerAgentProperties.getPort(), jaegerAgentProperties.getMaxPacketSize(), serviceName); - } - -} diff --git a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/configuration/JaegerSofaTracerAutoConfiguration.java b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/configuration/JaegerSofaTracerAutoConfiguration.java index 6cd19cc2f..87dd023af 100644 --- a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/configuration/JaegerSofaTracerAutoConfiguration.java +++ b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/configuration/JaegerSofaTracerAutoConfiguration.java @@ -17,41 +17,54 @@ package com.alipay.sofa.tracer.boot.jaeger.configuration; import com.alipay.sofa.tracer.boot.jaeger.properties.JaegerSofaTracerProperties; -import com.alipay.sofa.tracer.boot.zipkin.properties.ZipkinSofaTracerProperties; -import com.alipay.sofa.tracer.plugins.zipkin.ZipkinSofaTracerRestTemplateCustomizer; -import com.alipay.sofa.tracer.plugins.zipkin.ZipkinSofaTracerSpanRemoteReporter; +import com.alipay.sofa.tracer.plugins.jaeger.JaegerSofaTracerSpanRemoteReporter; +import io.jaegertracing.internal.JaegerSpan; +import io.jaegertracing.internal.JaegerSpanContext; +import io.jaegertracing.internal.JaegerTracer; +import org.apache.thrift.transport.TTransportException; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 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; /** - * report Zipkin v2 JSON format span to the jaeger collector + * JaegerSofaTracerAutoConfiguration + * @author: zhaochen */ - @Configuration @EnableConfigurationProperties(JaegerSofaTracerProperties.class) @ConditionalOnProperty(value = "com.alipay.sofa.tracer.jaeger.enabled", matchIfMissing = false) -@ConditionalOnClass({ zipkin2.Span.class, zipkin2.reporter.AsyncReporter.class, RestTemplate.class }) +@ConditionalOnClass({ JaegerSpan.class, JaegerTracer.class, JaegerSpanContext.class }) public class JaegerSofaTracerAutoConfiguration { @Autowired private JaegerSofaTracerProperties jaegerProperties; + @Value("${spring.application.name}") + private String serviceName; + @Bean @ConditionalOnMissingBean - public ZipkinSofaTracerRestTemplateCustomizer zipkinSofaTracerRestTemplateCustomizer() { - return new ZipkinSofaTracerRestTemplateCustomizer(jaegerProperties.isGzipped()); + @ConditionalOnProperty(value = "com.alipay.sofa.tracer.jaeger.receiver", havingValue = "collector", matchIfMissing = true) + public JaegerSofaTracerSpanRemoteReporter jaegerSofaTracerSpanRemoteReporter() + throws TTransportException { + return new JaegerSofaTracerSpanRemoteReporter(jaegerProperties.getCollectorBaseUrl(), + jaegerProperties.getCollectorMaxPacketSizeBytes(), serviceName, + jaegerProperties.getFlushIntervalMill(), jaegerProperties.getMaxQueueSize(), + jaegerProperties.getCloseEnqueueTimeoutMill()); } @Bean @ConditionalOnMissingBean - public ZipkinSofaTracerSpanRemoteReporter zipkinSofaTracerSpanReporter(ZipkinSofaTracerRestTemplateCustomizer zipkinSofaTracerRestTemplateCustomizer) { - RestTemplate restTemplate = new RestTemplate(); - zipkinSofaTracerRestTemplateCustomizer.customize(restTemplate); - return new ZipkinSofaTracerSpanRemoteReporter(restTemplate, jaegerProperties.getBaseUrl()); + @ConditionalOnProperty(value = "com.alipay.sofa.tracer.jaeger.receiver", havingValue = "agent", matchIfMissing = false) + public JaegerSofaTracerSpanRemoteReporter jaegerAgentSofaTracerSpanReporter() + throws TTransportException { + return new JaegerSofaTracerSpanRemoteReporter(jaegerProperties.getAgentHost(), + jaegerProperties.getAgentPort(), jaegerProperties.getAgentMaxPacketSizeBytes(), + serviceName, jaegerProperties.getFlushIntervalMill(), + jaegerProperties.getMaxQueueSize(), jaegerProperties.getCloseEnqueueTimeoutMill()); } } diff --git a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/properties/JaegerAgentSofaTracerProperties.java b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/properties/JaegerAgentSofaTracerProperties.java deleted file mode 100644 index 73f719037..000000000 --- a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/properties/JaegerAgentSofaTracerProperties.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * 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.jaeger.properties; - -import org.springframework.boot.context.properties.ConfigurationProperties; - -@ConfigurationProperties("com.alipay.sofa.tracer.jaeger.agent") -public class JaegerAgentSofaTracerProperties { - /** - * the address of agent - */ - private String host = "127.0.0.1"; - /** - * whether report span to jaeger agent - */ - private boolean enabled = false; - /** - * jaeger agent port to accept jaeger.thrift - */ - private int port = 6831; - /** - * the max byte of the packet - * In UDP over IPv4, the limit is 65,507 bytes - */ - private int maxPacketSize = 65000; - - public String getHost() { - return this.host; - } - - public boolean isEnabled() { - return this.enabled; - } - - public void setHost(String host) { - this.host = host; - } - - public void setEnabled(boolean enabled) { - this.enabled = enabled; - } - - public int getPort() { - return this.port; - } - - public void setPort(int port) { - this.port = port; - } - - public int getMaxPacketSize() { - return this.maxPacketSize; - } - - public void setMaxPacketSize(int maxPacketSize) { - this.maxPacketSize = maxPacketSize; - } -} diff --git a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/properties/JaegerSofaTracerProperties.java b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/properties/JaegerSofaTracerProperties.java index ebb0a7d82..a9b328f8d 100644 --- a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/properties/JaegerSofaTracerProperties.java +++ b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/jaeger/properties/JaegerSofaTracerProperties.java @@ -16,45 +16,77 @@ */ package com.alipay.sofa.tracer.boot.jaeger.properties; +import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; +/** + * JaegerSofaTracerProperties + * @author: zhaochen + */ +@Data @ConfigurationProperties("com.alipay.sofa.tracer.jaeger") public class JaegerSofaTracerProperties { + + private Agent agent = new Agent(); + private Collector collector = new Collector(); + + private boolean enabled = false; + /** + * receiver of span, it can be collector or agent + */ + private String receiver = "collector"; + /** - * URL of the jaeger collector. + *The interval of writing FlushCommand to the command queue */ - private String baseUrl = "http://localhost:9411/"; + private int flushIntervalMill = 1000; /** - * jaeger reporter is disabled by default + * size of the command queue is too large will waste space, and too small will cause the span to be lost */ - private boolean enabled = false; + private Integer maxQueueSize = 10000; /** - * When enabled, spans are gzipped before sent to the jaeger server + * Timeout for writing CloseCommand */ - private boolean gzipped = false; + private Integer closeEnqueueTimeoutMill = 1000; + + public String getCollectorBaseUrl() { + return this.collector.getBaseUrl(); + } - public String getBaseUrl() { - return this.baseUrl; + public int getCollectorMaxPacketSizeBytes() { + return this.collector.getMaxPacketSizeBytes(); } - public boolean isEnabled() { - return this.enabled; + public String getAgentHost() { + return this.agent.getHost(); } - public void setBaseUrl(String baseUrl) { - this.baseUrl = baseUrl; + public int getAgentPort() { + return this.agent.getPort(); } - public void setEnabled(boolean enabled) { - this.enabled = enabled; + public int getAgentMaxPacketSizeBytes() { + return this.agent.getMaxPacketSizeBytes(); } - public boolean isGzipped() { - return gzipped; + @Data + private class Agent { + private String host = "127.0.0.1"; + private int port = 6831; + /** + * the max byte of the packet + * In UDP over IPv4, the limit is 65,507 bytes + */ + private int maxPacketSizeBytes = 65000; } - public void setGzipped(boolean gzipped) { - this.gzipped = gzipped; + @Data + private class Collector { + private String baseUrl = "http://localhost:14268/"; + /** + * the max packet size in default it is 2MB + */ + private int maxPacketSizeBytes = 2 * 1024 * 1024; } } 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 abfc8c3ff..f9d9835d5 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 @@ -3,7 +3,6 @@ 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.jaeger.configuration.JaegerSofaTracerAutoConfiguration,\ -com.alipay.sofa.tracer.boot.jaeger.configuration.JaegerAgentSofaTracerAutoConfiguration,\ 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 38f8f4290c47e84faac6781c6eab204a5397c344 Mon Sep 17 00:00:00 2001 From: zzuzh Date: Thu, 23 Sep 2021 20:11:45 +0800 Subject: [PATCH 9/9] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=88=A4=E6=96=ADerror?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugins/jaeger/adapter/JaegerSpanAdapter.java | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java index 6415cf045..6546d0dad 100644 --- a/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java +++ b/sofa-tracer-plugins/sofa-tracer-jaeger-plugin/src/main/java/com/alipay/sofa/tracer/plugins/jaeger/adapter/JaegerSpanAdapter.java @@ -57,9 +57,6 @@ public JaegerSpan convertAndReport(SofaTracerSpan sofaTracerSpan, JaegerTracer j //in sofaTracer error contains the error message while in jaeger error represents whether it's error if (strTags.containsKey(Tags.ERROR.getKey())) { strTags.put("error.message", strTags.get(Tags.ERROR.getKey())); - } - String resultCode = strTags.get(CommonSpanTags.RESULT_CODE); - if (StringUtils.isNotBlank(resultCode) && !isWebHttpClientSuccess(resultCode)) { sofaTracerSpan.getTagsWithBool().put(Tags.ERROR.getKey(), true); } tags.putAll(strTags); @@ -188,17 +185,6 @@ private static long FNV64HashCode(String data) { return hash; } - 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)); - } - private String padLeft(String id, int desiredLength) { StringBuilder builder = new StringBuilder(desiredLength); int offset = desiredLength - id.length();