Skip to content

Commit

Permalink
Fix upgrade zipkin2 (#60)
Browse files Browse the repository at this point in the history
* adapter SOFARpc span model to zipkin span model
  • Loading branch information
glmapper authored and guanchao-yang committed Aug 29, 2018
1 parent ee6c0e5 commit 8d02193
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public SofaTracerSpanContext getSpanContextFromRequest(HttpServletRequest reques
SofaTracer tracer = springMvcTracer.getSofaTracer();
SofaTracerSpanContext spanContext = (SofaTracerSpanContext) tracer.extract(
ExtendFormat.Builtin.B3_HTTP_HEADERS, new SpringMvcHeadersCarrier(headers));
spanContext.setSpanId(spanContext.nextChildContextId());
return spanContext;
}

Expand Down
2 changes: 1 addition & 1 deletion tracer-sofa-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<dependency>
<groupId>io.zipkin.zipkin2</groupId>
<artifactId>zipkin</artifactId>
<version>2.11.1</version>
<version>2.11.3</version>
<optional>true</optional>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
*/
package com.alipay.sofa.tracer.boot.zipkin;

import com.alipay.common.tracer.core.SofaTracer;
import com.alipay.common.tracer.core.context.span.SofaTracerSpanContext;
import com.alipay.common.tracer.core.listener.SpanReportListener;
import com.alipay.common.tracer.core.span.LogData;
import com.alipay.common.tracer.core.span.SofaTracerSpan;
import com.alipay.common.tracer.core.utils.CommonUtils;
import com.alipay.common.tracer.core.utils.StringUtils;
import com.alipay.common.tracer.core.utils.TracerUtils;
import com.alipay.sofa.tracer.boot.zipkin.sender.ZipkinRestTemplateSender;
Expand All @@ -45,16 +45,18 @@
*/
public class ZipkinSofaTracerSpanRemoteReporter implements SpanReportListener, Flushable, Closeable {

private static String processId = TracerUtils.getPID();
private static String processId = TracerUtils.getPID();

private final ZipkinRestTemplateSender sender;

private final AsyncReporter<Span> delegate;

private static final String SOFARPC_TRACER_TYPE = "RPC_TRACER";

/***
* cache and performance improve
*/
private int ipAddressInt = -1;
private int ipAddressInt = -1;

public ZipkinSofaTracerSpanRemoteReporter(RestTemplate restTemplate, String baseUrl) {
this.sender = new ZipkinRestTemplateSender(restTemplate, baseUrl);
Expand Down Expand Up @@ -89,17 +91,16 @@ public void close() {
private Span convertToZipkinSpan(SofaTracerSpan sofaTracerSpan) {
Span.Builder zipkinSpanBuilder = Span.newBuilder();
zipkinSpanBuilder.timestamp(sofaTracerSpan.getStartTime() * 1000);
// Zipkin is in nanosecond cr-cs
zipkinSpanBuilder
.duration((sofaTracerSpan.getEndTime() - sofaTracerSpan.getStartTime()) * 1000);
//Annotations
Endpoint endpoint = getZipkinEndpoint(sofaTracerSpan.getOperationName());
this.addZipkinAnnotations(zipkinSpanBuilder, sofaTracerSpan, endpoint);
this.addZipkinBinaryAnnotationsWithTags(zipkinSpanBuilder, sofaTracerSpan);
this.addZipkinBinaryAnnotationsWithBaggage(zipkinSpanBuilder, sofaTracerSpan);
// Zipkin is in nanosecond cr-cs
zipkinSpanBuilder
.duration((sofaTracerSpan.getEndTime() - sofaTracerSpan.getStartTime()) * 1000);
//traceId
SofaTracerSpanContext sofaTracerSpanContext = sofaTracerSpan.getSofaTracerSpanContext();

/**
* Changes:
* 1.From using zipkin span's traceId alone, to using both traceid and traceIdHigh
Expand All @@ -110,37 +111,36 @@ private 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.
*/
long[] traceIds = CommonUtils.hexToDualLong(sofaTracerSpanContext.getTraceId());

zipkinSpanBuilder.traceId(traceIds[0], traceIds[1]);
zipkinSpanBuilder.kind(Span.Kind.SERVER);
// v2 span model will padLeft automatic
zipkinSpanBuilder.traceId(sofaTracerSpanContext.getTraceId());
String parentSpanId = sofaTracerSpanContext.getParentId();
if (sofaTracerSpan.isServer() && parentSpanId != null
&& StringUtils.isNotBlank(parentSpanId)) {
if (CommonUtils.isHexString(parentSpanId)) {
zipkinSpanBuilder.parentId(CommonUtils.hexToLong(parentSpanId));
} else {
zipkinSpanBuilder.parentId(spanIdToLong(parentSpanId));
}
// v2 span model Unsets the {@link Span#parentId()} if the input is 0.
zipkinSpanBuilder.parentId(spanIdToLong(parentSpanId));
} else if (sofaTracerSpan.getParentSofaTracerSpan() != null) {
SofaTracerSpanContext parentSofaTracerSpanContext = sofaTracerSpan
.getParentSofaTracerSpan().getSofaTracerSpanContext();
if (parentSofaTracerSpanContext != null) {
parentSpanId = parentSofaTracerSpanContext.getSpanId();
if (parentSpanId != null && StringUtils.isNotBlank(parentSpanId)) {
if (CommonUtils.isHexString(parentSpanId)) {
zipkinSpanBuilder.parentId(CommonUtils.hexToLong(parentSpanId));
} else {
zipkinSpanBuilder.parentId(spanIdToLong(parentSpanId));
}
zipkinSpanBuilder.parentId(spanIdToLong(parentSpanId));
}
}

}

//spanId
String spanId = sofaTracerSpanContext.getSpanId();
zipkinSpanBuilder.id(spanIdToLong(spanId));
//kind
SofaTracer sofaTracer = sofaTracerSpan.getSofaTracer();
// adapter SOFARpc span model
if (SOFARPC_TRACER_TYPE.equals(sofaTracer.getTracerType())) {
zipkinSpanBuilder.kind(Span.Kind.CLIENT);
} else {
zipkinSpanBuilder.kind(sofaTracerSpan.isClient() ? Span.Kind.CLIENT : Span.Kind.SERVER);
}
//name
String operationName = sofaTracerSpan.getOperationName();
if (StringUtils.isNotBlank(operationName)) {
Expand Down

0 comments on commit 8d02193

Please sign in to comment.