From 133ae1f2e0ede21006b02ac4fb13f585371b88e4 Mon Sep 17 00:00:00 2001
From: GuoLei Song <35319286+glmapper@users.noreply.github.com>
Date: Fri, 24 Aug 2018 09:56:20 +0800
Subject: [PATCH] Upgrade zipkin server version to v2 (2.11.1) (#55)
* Upgrade zipkin server version to v2 (2.11.1)
* Retrieve comment; supplement upgrade zipkin version to readme; supplement sofarpc demo to readme;
* Add httpclient dependency
---
README.md | 1 +
.../tracer-sample-with-sofarpc/pom.xml | 2 +-
.../tracer-sample-with-zipkin/README.md | 18 ++--
.../tracer-sample-with-zipkin/pom.xml | 8 +-
tracer-sofa-boot-starter/pom.xml | 29 ++++---
.../ZipkinSofaTracerSpanRemoteReporter.java | 82 +++++++------------
.../ZipkinSofaTracerAutoConfiguration.java | 4 +-
.../sender/ZipkinRestTemplateSender.java | 50 +++--------
...ipkinSofaTracerSpanRemoteReporterTest.java | 3 +-
9 files changed, 77 insertions(+), 120 deletions(-)
diff --git a/README.md b/README.md
index 1fa07c41..203948c8 100644
--- a/README.md
+++ b/README.md
@@ -57,6 +57,7 @@ SOFATracer 的编译环境的要求为 JDK7 或者 JDK8,需要采用 [Apache M
* [SOFATracer 示例工程(基于 Spring MVC 示例落地日志)](./tracer-samples/tracer-sample-with-springmvc)
* [SOFATracer 示例工程(基于 Spring MVC 示例远程上报 Zipkin)](./tracer-samples/tracer-sample-with-zipkin)
+* [SOFATracer 示例工程(基于 SOFA RPC 示例落地日志)](./tracer-samples/tracer-sample-with-sofarpc)
* [SOFATracer 示例工程(基于日志编程接口 SLF4J 示例打印 traceId)](./tracer-samples/tracer-sample-with-slf4j)
## 六、文档
diff --git a/tracer-samples/tracer-sample-with-sofarpc/pom.xml b/tracer-samples/tracer-sample-with-sofarpc/pom.xml
index b56c4cd1..42f47256 100644
--- a/tracer-samples/tracer-sample-with-sofarpc/pom.xml
+++ b/tracer-samples/tracer-sample-with-sofarpc/pom.xml
@@ -5,7 +5,7 @@
com.alipay.sofa
sofa-tracer-samples
- 2.1.0
+ 2.2.0-SNAPSHOT
4.0.0
diff --git a/tracer-samples/tracer-sample-with-zipkin/README.md b/tracer-samples/tracer-sample-with-zipkin/README.md
index 676d4218..aa3e9fb1 100644
--- a/tracer-samples/tracer-sample-with-zipkin/README.md
+++ b/tracer-samples/tracer-sample-with-zipkin/README.md
@@ -59,15 +59,15 @@ logging.path=./logs
```xml
- io.zipkin.java
- zipkin
- 1.19.2
-
-
- io.zipkin.reporter
- zipkin-reporter
- 0.6.12
-
+ io.zipkin.zipkin2
+ zipkin
+ 2.11.1
+
+
+ io.zipkin.reporter2
+ zipkin-reporter
+ 2.7.7
+
```
## 启用 SOFATracer 汇报数据到 Zipkin
diff --git a/tracer-samples/tracer-sample-with-zipkin/pom.xml b/tracer-samples/tracer-sample-with-zipkin/pom.xml
index 1c2453d5..884b71c2 100644
--- a/tracer-samples/tracer-sample-with-zipkin/pom.xml
+++ b/tracer-samples/tracer-sample-with-zipkin/pom.xml
@@ -23,14 +23,14 @@
- io.zipkin.java
+ io.zipkin.zipkin2
zipkin
- 1.19.2
+ 2.11.1
- io.zipkin.reporter
+ io.zipkin.reporter2
zipkin-reporter
- 0.6.12
+ 2.7.7
org.springframework.boot
diff --git a/tracer-sofa-boot-starter/pom.xml b/tracer-sofa-boot-starter/pom.xml
index 2ae96fd6..06059c42 100644
--- a/tracer-sofa-boot-starter/pom.xml
+++ b/tracer-sofa-boot-starter/pom.xml
@@ -52,18 +52,31 @@
spring-boot-configuration-processor
true
+
+
+ org.apache.httpcomponents
+ httpclient
+ 4.5.3
+ provided
+
+
+ org.apache.httpcomponents
+ httpasyncclient
+ 4.1.3
+ provided
+
- io.zipkin.java
+ io.zipkin.zipkin2
zipkin
- 1.19.2
+ 2.11.1
true
- io.zipkin.reporter
+ io.zipkin.reporter2
zipkin-reporter
- 0.6.12
+ 2.7.7
true
@@ -82,13 +95,5 @@
fastjson
test
-
-
- io.zipkin.java
- zipkin-junit
- 1.19.2
- test
-
-
diff --git a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/zipkin/ZipkinSofaTracerSpanRemoteReporter.java b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/zipkin/ZipkinSofaTracerSpanRemoteReporter.java
index d5092199..db8e2aa6 100644
--- a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/zipkin/ZipkinSofaTracerSpanRemoteReporter.java
+++ b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/zipkin/ZipkinSofaTracerSpanRemoteReporter.java
@@ -26,21 +26,16 @@
import com.alipay.sofa.tracer.boot.zipkin.sender.ZipkinRestTemplateSender;
import org.springframework.util.Assert;
import org.springframework.web.client.RestTemplate;
-import zipkin.Annotation;
-import zipkin.BinaryAnnotation;
-import zipkin.Endpoint;
-import zipkin.Span;
-import zipkin.reporter.AsyncReporter;
-
+import zipkin2.Endpoint;
+import zipkin2.Span;
+import zipkin2.reporter.AsyncReporter;
import java.io.Closeable;
import java.io.Flushable;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
-import java.nio.charset.Charset;
import java.util.Map;
-import java.util.concurrent.TimeUnit;
/**
* ZipkinSofaTracerSpanRemoteReporter report {@link SofaTracerSpan} to Zipkin
@@ -50,8 +45,6 @@
*/
public class ZipkinSofaTracerSpanRemoteReporter implements SpanReportListener, Flushable, Closeable {
- private static final Charset UTF_8 = Charset.forName("UTF-8");
-
private static String processId = TracerUtils.getPID();
private final ZipkinRestTemplateSender sender;
@@ -63,11 +56,9 @@ public class ZipkinSofaTracerSpanRemoteReporter implements SpanReportListener, F
*/
private int ipAddressInt = -1;
- public ZipkinSofaTracerSpanRemoteReporter(RestTemplate restTemplate, String baseUrl,
- int flushInterval) {
+ public ZipkinSofaTracerSpanRemoteReporter(RestTemplate restTemplate, String baseUrl) {
this.sender = new ZipkinRestTemplateSender(restTemplate, baseUrl);
- this.delegate = AsyncReporter.builder(this.sender).queuedMaxSpans(1000)
- .messageTimeout(flushInterval, TimeUnit.SECONDS).build();
+ this.delegate = AsyncReporter.create(sender);
}
@Override
@@ -76,7 +67,7 @@ public void onSpanReport(SofaTracerSpan span) {
return;
}
//convert
- zipkin.Span zipkinSpan = convertToZipkinSpan(span);
+ Span zipkinSpan = convertToZipkinSpan(span);
this.delegate.report(zipkinSpan);
}
@@ -88,19 +79,22 @@ public void flush() throws IOException {
@Override
public void close() {
this.delegate.close();
- this.sender.close();
}
- private zipkin.Span convertToZipkinSpan(SofaTracerSpan sofaTracerSpan) {
- zipkin.Span.Builder zipkinSpanBuilder = Span.builder();
+ /**
+ * convent sofaTracerSpan model to zipKinSpan model
+ * @param sofaTracerSpan
+ * @return
+ */
+ private Span convertToZipkinSpan(SofaTracerSpan sofaTracerSpan) {
+ Span.Builder zipkinSpanBuilder = Span.newBuilder();
+ zipkinSpanBuilder.timestamp(sofaTracerSpan.getStartTime() * 1000);
+ //Annotations
Endpoint endpoint = getZipkinEndpoint(sofaTracerSpan.getOperationName());
this.addZipkinAnnotations(zipkinSpanBuilder, sofaTracerSpan, endpoint);
- this.addZipkinBinaryAnnotationsWithTags(zipkinSpanBuilder, sofaTracerSpan, endpoint);
- //baggage
+ this.addZipkinBinaryAnnotationsWithTags(zipkinSpanBuilder, sofaTracerSpan);
this.addZipkinBinaryAnnotationsWithBaggage(zipkinSpanBuilder, sofaTracerSpan);
- // Zipkin is in nanosecond :timestamp reference : zipkin.storage.QueryRequest.test()
- zipkinSpanBuilder.timestamp(sofaTracerSpan.getStartTime() * 1000);
- // Zipkin is in nanosecond
+ // Zipkin is in nanosecond cr-cs
zipkinSpanBuilder
.duration((sofaTracerSpan.getEndTime() - sofaTracerSpan.getStartTime()) * 1000);
//traceId
@@ -116,11 +110,10 @@ private zipkin.Span convertToZipkinSpan(SofaTracerSpan sofaTracerSpan) {
* off the pid in tail of the traceId, because it does not know the pid of the sender.
* resulting in over range when convert it to long type.
*/
- //zipkinSpanBuilder.traceId(traceIdToId(sofaTracerSpanContext.getTraceId()));
long[] traceIds = CommonUtils.hexToDualLong(sofaTracerSpanContext.getTraceId());
- zipkinSpanBuilder.traceIdHigh(traceIds[0]);
- zipkinSpanBuilder.traceId(traceIds[1]);
+ zipkinSpanBuilder.traceId(traceIds[0], traceIds[1]);
+ zipkinSpanBuilder.kind(Span.Kind.SERVER);
String parentSpanId = sofaTracerSpanContext.getParentId();
if (sofaTracerSpan.isServer() && parentSpanId != null
&& StringUtils.isNotBlank(parentSpanId)) {
@@ -159,8 +152,8 @@ private zipkin.Span convertToZipkinSpan(SofaTracerSpan sofaTracerSpan) {
}
private Endpoint getZipkinEndpoint(String operationName) {
+ InetAddress ipAddress = null;
if (this.ipAddressInt <= 0) {
- InetAddress ipAddress = null;
try {
ipAddress = InetAddress.getLocalHost();
this.ipAddressInt = ByteBuffer.wrap(ipAddress.getAddress()).getInt();
@@ -169,48 +162,39 @@ private Endpoint getZipkinEndpoint(String operationName) {
this.ipAddressInt = 256 * 256 * 256 * 127 + 1;
}
}
- return Endpoint.builder().serviceName(operationName).ipv4(this.ipAddressInt).build();
- }
-
- private void addZipkinBinaryAnnotation(String key, String value, Endpoint endpoint,
- zipkin.Span.Builder zipkinSpan) {
- BinaryAnnotation binaryAnn = BinaryAnnotation.builder().type(BinaryAnnotation.Type.STRING)
- .key(key).value(value.getBytes(UTF_8)).endpoint(endpoint).build();
- zipkinSpan.addBinaryAnnotation(binaryAnn);
+ return Endpoint.newBuilder().serviceName(operationName).ip(ipAddress).build();
}
/**
* Adds binary annotation from the Open Tracing Span
*/
- private void addZipkinBinaryAnnotationsWithTags(zipkin.Span.Builder zipkinSpan,
- SofaTracerSpan span, Endpoint endpoint) {
+ private void addZipkinBinaryAnnotationsWithTags(Span.Builder zipkinSpan, SofaTracerSpan span) {
for (Map.Entry e : span.getTagsWithStr().entrySet()) {
- addZipkinBinaryAnnotation(e.getKey(), e.getValue(), endpoint, zipkinSpan);
+ zipkinSpan.putTag(e.getKey(), e.getValue());
}
for (Map.Entry e : span.getTagsWithNumber().entrySet()) {
- addZipkinBinaryAnnotation(e.getKey(), e.getValue().toString(), endpoint, zipkinSpan);
+ zipkinSpan.putTag(e.getKey(), e.getValue().toString());
}
for (Map.Entry e : span.getTagsWithBool().entrySet()) {
- addZipkinBinaryAnnotation(e.getKey(), e.getValue().toString(), endpoint, zipkinSpan);
+ zipkinSpan.putTag(e.getKey(), e.getValue().toString());
}
}
- private void addZipkinBinaryAnnotationsWithBaggage(zipkin.Span.Builder zipkinSpan,
- SofaTracerSpan span) {
+ private void addZipkinBinaryAnnotationsWithBaggage(Span.Builder zipkinSpan, SofaTracerSpan span) {
SofaTracerSpanContext sofaTracerSpanContext = span.getSofaTracerSpanContext();
if (sofaTracerSpanContext != null) {
Map sysBaggage = sofaTracerSpanContext.getSysBaggage();
for (Map.Entry e : sysBaggage.entrySet()) {
- addZipkinBinaryAnnotation(e.getKey(), e.getValue(), null, zipkinSpan);
+ zipkinSpan.putTag(e.getKey(), e.getValue());
}
Map bizBaggage = sofaTracerSpanContext.getBizBaggage();
for (Map.Entry e : bizBaggage.entrySet()) {
- addZipkinBinaryAnnotation(e.getKey(), e.getValue(), null, zipkinSpan);
+ zipkinSpan.putTag(e.getKey(), e.getValue());
}
}
}
- private void addZipkinAnnotations(zipkin.Span.Builder zipkinSpan, SofaTracerSpan span,
+ private void addZipkinAnnotations(Span.Builder zipkinSpan, SofaTracerSpan span,
Endpoint endpoint) {
for (LogData logData : span.getLogs()) {
Map fields = logData.getFields();
@@ -218,12 +202,8 @@ private void addZipkinAnnotations(zipkin.Span.Builder zipkinSpan, SofaTracerSpan
continue;
}
for (Map.Entry entry : fields.entrySet()) {
- //ignore event key
- Annotation zipkinAnnotation = Annotation.builder()
- // Zipkin is in nanosecond
- .timestamp(logData.getTime() * 1000).value(entry.getValue().toString())
- .endpoint(endpoint).build();
- zipkinSpan.addAnnotation(zipkinAnnotation);
+ zipkinSpan.addAnnotation(logData.getTime() * 1000, entry.getValue().toString())
+ .localEndpoint(endpoint);
}
}
}
diff --git a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/zipkin/configuration/ZipkinSofaTracerAutoConfiguration.java b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/zipkin/configuration/ZipkinSofaTracerAutoConfiguration.java
index 12e80682..17566d58 100644
--- a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/zipkin/configuration/ZipkinSofaTracerAutoConfiguration.java
+++ b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/zipkin/configuration/ZipkinSofaTracerAutoConfiguration.java
@@ -37,7 +37,7 @@
@EnableConfigurationProperties({ ZipkinSofaTracerProperties.class,
ZipkinSofaTracerSamplerProperties.class })
@ConditionalOnProperty(value = "com.alipay.sofa.tracer.zipkin.enabled", matchIfMissing = true)
-@ConditionalOnClass({ zipkin.Span.class, zipkin.reporter.AsyncReporter.class })
+@ConditionalOnClass({ zipkin2.Span.class, zipkin2.reporter.AsyncReporter.class })
public class ZipkinSofaTracerAutoConfiguration {
@Bean
@@ -53,6 +53,6 @@ public ZipkinSofaTracerSpanRemoteReporter zpkinSofaTracerSpanReporter(ZipkinSofa
RestTemplate restTemplate = new RestTemplate();
zipkinSofaTracerRestTemplateCustomizer.customize(restTemplate);
return new ZipkinSofaTracerSpanRemoteReporter(restTemplate,
- zipkinSofaTracerProperties.getBaseUrl(), zipkinSofaTracerProperties.getFlushInterval());
+ zipkinSofaTracerProperties.getBaseUrl());
}
}
diff --git a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/zipkin/sender/ZipkinRestTemplateSender.java b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/zipkin/sender/ZipkinRestTemplateSender.java
index 2d55f697..4c3ed740 100644
--- a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/zipkin/sender/ZipkinRestTemplateSender.java
+++ b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/zipkin/sender/ZipkinRestTemplateSender.java
@@ -21,11 +21,10 @@
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.web.client.RestTemplate;
-import zipkin.reporter.BytesMessageEncoder;
-import zipkin.reporter.Callback;
-import zipkin.reporter.Encoding;
-import zipkin.reporter.Sender;
-
+import zipkin2.Call;
+import zipkin2.codec.Encoding;
+import zipkin2.reporter.BytesMessageEncoder;
+import zipkin2.reporter.Sender;
import java.net.URI;
import java.util.List;
@@ -35,20 +34,15 @@
* @author yangguanchao
* @since 2018/05/01
*/
-public class ZipkinRestTemplateSender implements Sender {
-
- private RestTemplate restTemplate;
+public class ZipkinRestTemplateSender extends Sender {
- private String url;
+ private RestTemplate restTemplate;
- /**
- * close is typically called from a different thread
- */
- private transient boolean closeCalled;
+ private String url;
public ZipkinRestTemplateSender(RestTemplate restTemplate, String baseUrl) {
this.restTemplate = restTemplate;
- this.url = baseUrl + (baseUrl.endsWith("/") ? "" : "/") + "api/v1/spans";
+ this.url = baseUrl + (baseUrl.endsWith("/") ? "" : "/") + "api/v2/spans";
}
@Override
@@ -68,44 +62,22 @@ public int messageSizeInBytes(List spans) {
}
@Override
- public void sendSpans(List encodedSpans, Callback callback) {
- if (this.closeCalled) {
- throw new IllegalStateException("Zipkin has closed!");
- }
+ public Call sendSpans(List encodedSpans) {
try {
byte[] message = BytesMessageEncoder.JSON.encode(encodedSpans);
post(message);
- callback.onComplete();
} catch (Throwable e) {
- callback.onError(e);
if (e instanceof Error) {
throw (Error) e;
}
}
- }
-
- /**
- * Sends an empty json message to the configured endpoint.
- */
- @Override
- public CheckResult check() {
- try {
- post(new byte[] { '[', ']' });
- return CheckResult.OK;
- } catch (Exception e) {
- return CheckResult.failed(e);
- }
- }
-
- @Override
- public void close() {
- this.closeCalled = true;
+ return null;
}
private void post(byte[] json) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
- RequestEntity requestEntity = new RequestEntity(json, httpHeaders,
+ RequestEntity requestEntity = new RequestEntity<>(json, httpHeaders,
HttpMethod.POST, URI.create(this.url));
this.restTemplate.exchange(requestEntity, String.class);
}
diff --git a/tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/zipkin/ZipkinSofaTracerSpanRemoteReporterTest.java b/tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/zipkin/ZipkinSofaTracerSpanRemoteReporterTest.java
index 4303efff..278456fb 100644
--- a/tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/zipkin/ZipkinSofaTracerSpanRemoteReporterTest.java
+++ b/tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/zipkin/ZipkinSofaTracerSpanRemoteReporterTest.java
@@ -59,9 +59,8 @@ public void before() throws Exception {
restTemplateCustomizer.customize(restTemplate);
//host http://zipkin-cloud-3.inc.host.net:9411
String baseUrl = "http://zipkin-cloud-3.inc.host.net:9411";
- int flushInterval = 1;
SpanReportListener spanReportListener = new ZipkinSofaTracerSpanRemoteReporter(
- restTemplate, baseUrl, flushInterval);
+ restTemplate, baseUrl);
SpanReportListenerHolder.addSpanReportListener(spanReportListener);
}