Skip to content
This repository was archived by the owner on Jul 1, 2022. It is now read-only.

Commit 5050c73

Browse files
author
Ben Keith
committed
Implement Zipkin 2 JSON Sender
The existing ZipkinSender class only supported the Thrift v1 protocol, but the new Zipkin2Sender supports both v1 and v2 JSON. Otherwise, functionality is mostly the same. There is some conceptually duplicate code in the Sender classes due to the use of different classes between Zipkin 1/2 but it seemed overly complicated to adapt them to a common adapter interface. Signed-off-by: Ben Keith <bkeith@signalfx.com>
1 parent 09177f4 commit 5050c73

File tree

8 files changed

+947
-24
lines changed

8 files changed

+947
-24
lines changed

jaeger-zipkin/build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ description = 'Integration library for Zipkin'
33
dependencies {
44
compile group: 'io.zipkin.reporter', name: 'zipkin-reporter', version: '1.1.2'
55
compile group: 'io.zipkin.reporter', name: 'zipkin-sender-urlconnection', version: '1.1.2'
6+
7+
compile group: 'io.zipkin.reporter2', name: 'zipkin-reporter', version: '2.5.0'
8+
compile group: 'io.zipkin.reporter2', name: 'zipkin-sender-urlconnection', version: '2.5.0'
9+
610
compile project(path: ':jaeger-core', configuration: 'shadow')
711

812
testCompile group: 'junit', name: 'junit', version: junitVersion
9-
testCompile group: 'io.zipkin.java', name: 'zipkin-junit', version: '2.3.0'
13+
testCompile group: 'io.zipkin.java', name: 'zipkin-junit', version: '2.7.1'
1014
testCompile group: 'io.zipkin.brave', name: 'brave-http', version: braveHttpVersion
1115
testCompile group: 'com.tngtech.java', name: 'junit-dataprovider', version: junitDataProviderVersion
1216

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2018, The Jaeger Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package io.jaegertracing.senders.zipkin;
16+
17+
import io.jaegertracing.Span;
18+
import io.opentracing.tag.Tags;
19+
20+
/**
21+
* Logic that is common to both Thrift v1 and JSON v2 senders
22+
*/
23+
class ConverterUtil {
24+
static boolean isRpcServer(Span span) {
25+
return Tags.SPAN_KIND_SERVER.equals(span.getTags().get(Tags.SPAN_KIND.getKey()));
26+
}
27+
28+
static boolean isRpc(Span span) {
29+
return isRpcServer(span) || isRpcClient(span);
30+
31+
}
32+
33+
static boolean isRpcClient(Span span) {
34+
return Tags.SPAN_KIND_CLIENT.equals(span.getTags().get(Tags.SPAN_KIND.getKey()));
35+
}
36+
37+
38+
}

jaeger-zipkin/src/main/java/io/jaegertracing/senders/zipkin/ThriftSpanConverter.java

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ public static com.twitter.zipkin.thriftjava.Span convertSpan(Span span) {
5656
private static List<Annotation> buildAnnotations(Span span, Endpoint host) {
5757
List<Annotation> annotations = new ArrayList<Annotation>();
5858

59-
if (isRpc(span)) {
59+
if (ConverterUtil.isRpc(span)) {
6060
String startLabel = zipkincoreConstants.SERVER_RECV;
6161
String endLabel = zipkincoreConstants.SERVER_SEND;
62-
if (isRpcClient(span)) {
62+
if (ConverterUtil.isRpcClient(span)) {
6363
startLabel = zipkincoreConstants.CLIENT_SEND;
6464
endLabel = zipkincoreConstants.CLIENT_RECV;
6565
}
@@ -87,9 +87,9 @@ private static List<Annotation> buildAnnotations(Span span, Endpoint host) {
8787
private static List<BinaryAnnotation> buildBinaryAnnotations(Span span, Endpoint host) {
8888
List<BinaryAnnotation> binaryAnnotations = new ArrayList<BinaryAnnotation>();
8989
Map<String, Object> tags = span.getTags();
90-
boolean isRpc = isRpc(span);
91-
boolean isClient = isRpcClient(span);
92-
boolean firstSpanInProcess = span.getReferences().isEmpty() || isRpcServer(span);
90+
boolean isRpc = ConverterUtil.isRpc(span);
91+
boolean isClient = ConverterUtil.isRpcClient(span);
92+
boolean firstSpanInProcess = span.getReferences().isEmpty() || ConverterUtil.isRpcServer(span);
9393

9494
if (firstSpanInProcess) {
9595
Map<String, ?> processTags = span.getTracer().tags();
@@ -154,20 +154,6 @@ private static BinaryAnnotation buildBinaryAnnotation(String tagKey, Object tagV
154154
return banno;
155155
}
156156

157-
static boolean isRpcServer(Span span) {
158-
return Tags.SPAN_KIND_SERVER.equals(span.getTags().get(Tags.SPAN_KIND.getKey()));
159-
}
160-
161-
static boolean isRpc(Span span) {
162-
Object spanKindValue = span.getTags().get(Tags.SPAN_KIND.getKey());
163-
return Tags.SPAN_KIND_CLIENT.equals(spanKindValue) || Tags.SPAN_KIND_SERVER.equals(spanKindValue);
164-
165-
}
166-
167-
static boolean isRpcClient(Span span) {
168-
return Tags.SPAN_KIND_CLIENT.equals(span.getTags().get(Tags.SPAN_KIND.getKey()));
169-
}
170-
171157
/**
172158
* Extract peer Endpoint from tags
173159
*
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
/*
2+
* Copyright (c) 2018, The Jaeger Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package io.jaegertracing.senders.zipkin;
16+
17+
import com.google.gson.Gson;
18+
import io.jaegertracing.Constants;
19+
import io.jaegertracing.LogData;
20+
import io.jaegertracing.Span;
21+
import io.jaegertracing.SpanContext;
22+
import io.jaegertracing.Tracer;
23+
import io.opentracing.tag.Tags;
24+
import java.net.InetAddress;
25+
import java.net.UnknownHostException;
26+
import java.nio.ByteBuffer;
27+
import java.util.List;
28+
import java.util.Map;
29+
30+
/**
31+
* Converts a Jaeger span to a Zipkin2 span.
32+
*/
33+
public class V2SpanConverter {
34+
35+
private static final Gson gson = new Gson();
36+
37+
public static zipkin2.Span convertSpan(Span span) {
38+
Tracer tracer = span.getTracer();
39+
zipkin2.Endpoint host = zipkin2.Endpoint.newBuilder()
40+
.ip(convertIp(tracer.getIpv4()))
41+
.serviceName(tracer.getServiceName())
42+
.build();
43+
44+
zipkin2.Endpoint peerEndpoint = extractPeerEndpoint(span.getTags());
45+
46+
SpanContext context = span.context();
47+
zipkin2.Span.Builder builder = zipkin2.Span.newBuilder()
48+
.id(Long.toHexString(context.getSpanId()))
49+
.traceId(Long.toHexString(context.getTraceId()))
50+
.name(span.getOperationName())
51+
.parentId(Long.toHexString(context.getParentId()))
52+
.debug(context.isDebug())
53+
.localEndpoint(host)
54+
.remoteEndpoint(peerEndpoint)
55+
.kind(convertKind(span.getTags().get(Tags.SPAN_KIND.getKey())))
56+
.timestamp(span.getStart())
57+
.duration(span.getDuration());
58+
59+
buildAnnotations(span, builder);
60+
buildTags(span, builder);
61+
62+
return builder.build();
63+
}
64+
65+
private static zipkin2.Span.Kind convertKind(Object kind) {
66+
if (Tags.SPAN_KIND_SERVER.equals(kind)) {
67+
return zipkin2.Span.Kind.SERVER;
68+
} else if (Tags.SPAN_KIND_CLIENT.equals(kind)) {
69+
return zipkin2.Span.Kind.CLIENT;
70+
} else if (Tags.SPAN_KIND_CONSUMER.equals(kind)) {
71+
return zipkin2.Span.Kind.CONSUMER;
72+
} else if (Tags.SPAN_KIND_PRODUCER.equals(kind)) {
73+
return zipkin2.Span.Kind.PRODUCER;
74+
} else {
75+
return null;
76+
}
77+
}
78+
79+
private static void buildAnnotations(Span span, zipkin2.Span.Builder builder) {
80+
if (ConverterUtil.isRpc(span)) {
81+
String startLabel = zipkin.Constants.SERVER_RECV;
82+
String endLabel = zipkin.Constants.SERVER_SEND;
83+
if (ConverterUtil.isRpcClient(span)) {
84+
startLabel = zipkin.Constants.CLIENT_SEND;
85+
endLabel = zipkin.Constants.CLIENT_RECV;
86+
}
87+
88+
builder.addAnnotation(span.getStart(), startLabel);
89+
builder.addAnnotation(span.getStart() + span.getDuration(), endLabel);
90+
}
91+
92+
List<LogData> logs = span.getLogs();
93+
if (logs != null) {
94+
for (LogData logData : logs) {
95+
String logMessage = logData.getMessage();
96+
Map<String, ?> logFields = logData.getFields();
97+
if (logMessage != null) {
98+
builder.addAnnotation(logData.getTime(), logMessage);
99+
} else if (logFields != null) {
100+
builder.addAnnotation(logData.getTime(), gson.toJson(logFields));
101+
}
102+
}
103+
}
104+
}
105+
106+
private static void buildTags(Span span, zipkin2.Span.Builder builder) {
107+
Map<String, Object> tags = span.getTags();
108+
boolean isRpc = ConverterUtil.isRpc(span);
109+
boolean firstSpanInProcess = span.getReferences().isEmpty() || ConverterUtil.isRpcServer(span);
110+
111+
if (firstSpanInProcess) {
112+
Map<String, ?> processTags = span.getTracer().tags();
113+
// add tracer tags to first zipkin span in a process but remove "ip" tag as it is
114+
// taken care of separately.
115+
for (Map.Entry<String, ?> entry : processTags.entrySet()) {
116+
String tagKey = entry.getKey();
117+
if (!Constants.TRACER_IP_TAG_KEY.equals(tagKey)) {
118+
Object tagValue = entry.getValue();
119+
// add a tracer. prefix to process tags for zipkin
120+
builder.putTag("tracer." + tagKey, tagValue.toString());
121+
}
122+
}
123+
}
124+
125+
if (!isRpc) {
126+
String componentName;
127+
Object componentTag = tags.get(Tags.COMPONENT.getKey());
128+
if (componentTag instanceof String) {
129+
componentName = componentTag.toString();
130+
} else {
131+
// spans always have associated tracers, and service names
132+
componentName = span.getTracer().getServiceName();
133+
}
134+
135+
builder.putTag(zipkin.Constants.LOCAL_COMPONENT, componentName);
136+
}
137+
138+
if (tags != null) {
139+
for (Map.Entry<String, Object> entry : tags.entrySet()) {
140+
String tagKey = entry.getKey();
141+
// Every value is converted to string because zipkin search doesn't
142+
// work well with ints, and bytes.
143+
Object tagValue = entry.getValue();
144+
builder.putTag(tagKey, tagValue.toString());
145+
}
146+
}
147+
}
148+
149+
private static InetAddress convertIp(int ip) {
150+
byte[] bytes = ByteBuffer.allocate(4).putInt(ip).array();
151+
try {
152+
return InetAddress.getByAddress(bytes);
153+
} catch (UnknownHostException e) {
154+
return null;
155+
}
156+
}
157+
158+
/**
159+
* Extract peer Endpoint from tags
160+
*
161+
* @param tags tags
162+
* @return null or peer endpoint
163+
*/
164+
public static zipkin2.Endpoint extractPeerEndpoint(Map<String, Object> tags) {
165+
Object peerIpv4 = tags.get(Tags.PEER_HOST_IPV4.getKey());
166+
Object peerPort = tags.get(Tags.PEER_PORT.getKey());
167+
Object peerService = tags.get(Tags.PEER_SERVICE.getKey());
168+
169+
if (peerIpv4 == null && peerPort == null && peerService == null) {
170+
return null;
171+
}
172+
173+
zipkin2.Endpoint.Builder builder = zipkin2.Endpoint.newBuilder();
174+
175+
if (peerIpv4 instanceof String) {
176+
builder.ip((String) peerIpv4);
177+
}
178+
if (peerPort instanceof Number) {
179+
builder.port(((Number) peerPort).intValue());
180+
}
181+
if (peerService instanceof String) {
182+
builder.serviceName((String) peerService);
183+
}
184+
185+
return builder.build();
186+
}
187+
}

0 commit comments

Comments
 (0)