Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Upgrade zipkin server version to v2 (2.11.1) (#55) #17

Merged
merged 1 commit into from
Aug 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

## 六、文档
Expand Down
2 changes: 1 addition & 1 deletion tracer-samples/tracer-sample-with-sofarpc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-tracer-samples</artifactId>
<version>2.1.0</version>
<version>2.2.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
18 changes: 9 additions & 9 deletions tracer-samples/tracer-sample-with-zipkin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ logging.path=./logs

```xml
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin</artifactId>
<version>1.19.2</version>
</dependency>
<dependency>
<groupId>io.zipkin.reporter</groupId>
<artifactId>zipkin-reporter</artifactId>
<version>0.6.12</version>
</dependency>
<groupId>io.zipkin.zipkin2</groupId>
<artifactId>zipkin</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-reporter</artifactId>
<version>2.7.7</version>
</dependency>
```

## 启用 SOFATracer 汇报数据到 Zipkin
Expand Down
8 changes: 4 additions & 4 deletions tracer-samples/tracer-sample-with-zipkin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
</dependency>
<!-- zipkin client -->
<dependency>
<groupId>io.zipkin.java</groupId>
<groupId>io.zipkin.zipkin2</groupId>
<artifactId>zipkin</artifactId>
<version>1.19.2</version>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>io.zipkin.reporter</groupId>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-reporter</artifactId>
<version>0.6.12</version>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
29 changes: 17 additions & 12 deletions tracer-sofa-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,31 @@
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--httpclient-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<version>4.1.3</version>
<scope>provided</scope>
</dependency>

<!-- zipkin optional : add manually -->
<dependency>
<groupId>io.zipkin.java</groupId>
<groupId>io.zipkin.zipkin2</groupId>
<artifactId>zipkin</artifactId>
<version>1.19.2</version>
<version>2.11.1</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.zipkin.reporter</groupId>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-reporter</artifactId>
<version>0.6.12</version>
<version>2.7.7</version>
<optional>true</optional>
</dependency>
<!-- test -->
Expand All @@ -82,13 +95,5 @@
<artifactId>fastjson</artifactId>
<scope>test</scope>
</dependency>
<!-- zipkin -->
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-junit</artifactId>
<version>1.19.2</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -76,7 +67,7 @@ public void onSpanReport(SofaTracerSpan span) {
return;
}
//convert
zipkin.Span zipkinSpan = convertToZipkinSpan(span);
Span zipkinSpan = convertToZipkinSpan(span);
this.delegate.report(zipkinSpan);
}

Expand All @@ -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
Expand All @@ -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)) {
Expand Down Expand Up @@ -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();
Expand All @@ -169,61 +162,48 @@ 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<String, String> e : span.getTagsWithStr().entrySet()) {
addZipkinBinaryAnnotation(e.getKey(), e.getValue(), endpoint, zipkinSpan);
zipkinSpan.putTag(e.getKey(), e.getValue());
}
for (Map.Entry<String, Number> e : span.getTagsWithNumber().entrySet()) {
addZipkinBinaryAnnotation(e.getKey(), e.getValue().toString(), endpoint, zipkinSpan);
zipkinSpan.putTag(e.getKey(), e.getValue().toString());
}
for (Map.Entry<String, Boolean> 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<String, String> sysBaggage = sofaTracerSpanContext.getSysBaggage();
for (Map.Entry<String, String> e : sysBaggage.entrySet()) {
addZipkinBinaryAnnotation(e.getKey(), e.getValue(), null, zipkinSpan);
zipkinSpan.putTag(e.getKey(), e.getValue());
}
Map<String, String> bizBaggage = sofaTracerSpanContext.getBizBaggage();
for (Map.Entry<String, String> 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<String, ?> fields = logData.getFields();
if (fields == null || fields.size() <= 0) {
continue;
}
for (Map.Entry<String, ?> 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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}
}
Loading