This repository has been archived by the owner on Jul 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 231
Implement Zipkin 2 JSON Sender #399
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
jaeger-zipkin/src/main/java/io/jaegertracing/zipkin/ConverterUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (c) 2018, The Jaeger Authors | ||
* | ||
* Licensed 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 io.jaegertracing.zipkin; | ||
|
||
import io.jaegertracing.Span; | ||
import io.opentracing.tag.Tags; | ||
|
||
/** | ||
* Logic that is common to both Thrift v1 and JSON v2 senders | ||
*/ | ||
public class ConverterUtil { | ||
public static boolean isRpcServer(Span span) { | ||
return Tags.SPAN_KIND_SERVER.equals(span.getTags().get(Tags.SPAN_KIND.getKey())); | ||
} | ||
|
||
public static boolean isRpc(Span span) { | ||
return isRpcServer(span) || isRpcClient(span); | ||
} | ||
|
||
public static boolean isRpcClient(Span span) { | ||
return Tags.SPAN_KIND_CLIENT.equals(span.getTags().get(Tags.SPAN_KIND.getKey())); | ||
} | ||
} |
164 changes: 164 additions & 0 deletions
164
jaeger-zipkin/src/main/java/io/jaegertracing/zipkin/V2SpanConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/* | ||
* Copyright (c) 2018, The Jaeger Authors | ||
* | ||
* Licensed 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 io.jaegertracing.zipkin; | ||
|
||
import com.google.gson.Gson; | ||
import io.jaegertracing.Constants; | ||
import io.jaegertracing.LogData; | ||
import io.jaegertracing.Span; | ||
import io.jaegertracing.SpanContext; | ||
import io.jaegertracing.Tracer; | ||
import io.opentracing.tag.Tags; | ||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
import java.nio.ByteBuffer; | ||
import java.util.List; | ||
import java.util.Map; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** | ||
* Converts a Jaeger span to a Zipkin2 span. | ||
*/ | ||
@Slf4j | ||
public class V2SpanConverter { | ||
|
||
private static final Gson gson = new Gson(); | ||
|
||
public static zipkin2.Span convertSpan(Span span) { | ||
Tracer tracer = span.getTracer(); | ||
zipkin2.Endpoint host = zipkin2.Endpoint.newBuilder() | ||
.ip(convertIp(tracer.getIpv4())) | ||
.serviceName(tracer.getServiceName()) | ||
.build(); | ||
|
||
zipkin2.Endpoint peerEndpoint = extractPeerEndpoint(span.getTags()); | ||
|
||
SpanContext context = span.context(); | ||
zipkin2.Span.Builder builder = zipkin2.Span.newBuilder() | ||
.id(Long.toHexString(context.getSpanId())) | ||
.traceId(Long.toHexString(context.getTraceId())) | ||
.name(span.getOperationName()) | ||
.parentId(Long.toHexString(context.getParentId())) | ||
.debug(context.isDebug()) | ||
.localEndpoint(host) | ||
.remoteEndpoint(peerEndpoint) | ||
.kind(convertKind(span.getTags().get(Tags.SPAN_KIND.getKey()))) | ||
.timestamp(span.getStart()) | ||
.duration(span.getDuration()); | ||
|
||
buildAnnotations(span, builder); | ||
buildTags(span, builder); | ||
|
||
return builder.build(); | ||
} | ||
|
||
private static zipkin2.Span.Kind convertKind(Object kind) { | ||
if (Tags.SPAN_KIND_SERVER.equals(kind)) { | ||
return zipkin2.Span.Kind.SERVER; | ||
} else if (Tags.SPAN_KIND_CLIENT.equals(kind)) { | ||
return zipkin2.Span.Kind.CLIENT; | ||
} else if (Tags.SPAN_KIND_CONSUMER.equals(kind)) { | ||
return zipkin2.Span.Kind.CONSUMER; | ||
} else if (Tags.SPAN_KIND_PRODUCER.equals(kind)) { | ||
return zipkin2.Span.Kind.PRODUCER; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
private static void buildAnnotations(Span span, zipkin2.Span.Builder builder) { | ||
List<LogData> logs = span.getLogs(); | ||
if (logs != null) { | ||
for (LogData logData : logs) { | ||
String logMessage = logData.getMessage(); | ||
Map<String, ?> logFields = logData.getFields(); | ||
if (logMessage != null) { | ||
builder.addAnnotation(logData.getTime(), logMessage); | ||
} else if (logFields != null) { | ||
builder.addAnnotation(logData.getTime(), gson.toJson(logFields)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static void buildTags(Span span, zipkin2.Span.Builder builder) { | ||
Map<String, Object> tags = span.getTags(); | ||
boolean firstSpanInProcess = span.getReferences().isEmpty() || ConverterUtil.isRpcServer(span); | ||
|
||
if (firstSpanInProcess) { | ||
Map<String, ?> processTags = span.getTracer().tags(); | ||
// add tracer tags to first zipkin span in a process but remove "ip" tag as it is | ||
// taken care of separately. | ||
for (Map.Entry<String, ?> entry : processTags.entrySet()) { | ||
String tagKey = entry.getKey(); | ||
if (!Constants.TRACER_IP_TAG_KEY.equals(tagKey)) { | ||
Object tagValue = entry.getValue(); | ||
// add a tracer. prefix to process tags for zipkin | ||
builder.putTag("tracer." + tagKey, tagValue.toString()); | ||
} | ||
} | ||
} | ||
|
||
if (tags != null) { | ||
for (Map.Entry<String, Object> entry : tags.entrySet()) { | ||
String tagKey = entry.getKey(); | ||
// Every value is converted to string because zipkin search doesn't | ||
// work well with ints, and bytes. | ||
Object tagValue = entry.getValue(); | ||
builder.putTag(tagKey, tagValue.toString()); | ||
} | ||
} | ||
} | ||
|
||
private static InetAddress convertIp(int ip) { | ||
byte[] bytes = ByteBuffer.allocate(4).putInt(ip).array(); | ||
try { | ||
return InetAddress.getByAddress(bytes); | ||
} catch (UnknownHostException e) { | ||
log.error("Jaeger span IP " + ip + " could not be converted", e); | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Extract peer Endpoint from tags | ||
* | ||
* @param tags tags | ||
* @return null or peer endpoint | ||
*/ | ||
public static zipkin2.Endpoint extractPeerEndpoint(Map<String, Object> tags) { | ||
Object peerIpv4 = tags.get(Tags.PEER_HOST_IPV4.getKey()); | ||
Object peerPort = tags.get(Tags.PEER_PORT.getKey()); | ||
Object peerService = tags.get(Tags.PEER_SERVICE.getKey()); | ||
|
||
if (peerIpv4 == null && peerPort == null && peerService == null) { | ||
return null; | ||
} | ||
|
||
zipkin2.Endpoint.Builder builder = zipkin2.Endpoint.newBuilder(); | ||
|
||
if (peerIpv4 instanceof String) { | ||
builder.ip((String) peerIpv4); | ||
} | ||
if (peerPort instanceof Number) { | ||
builder.port(((Number) peerPort).intValue()); | ||
} | ||
if (peerService instanceof String) { | ||
builder.serviceName((String) peerService); | ||
} | ||
|
||
return builder.build(); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
jaeger-zipkin/src/main/java/io/jaegertracing/zipkin/reporters/ZipkinV2Reporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright (c) 2018, The Jaeger Authors | ||
* | ||
* Licensed 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 io.jaegertracing.zipkin.reporters; | ||
|
||
import io.jaegertracing.reporters.Reporter; | ||
import io.jaegertracing.zipkin.V2SpanConverter; | ||
|
||
/** | ||
* Wrapper around a zipkin v2 AsyncReporter that reports spans using the newer v2 Span class | ||
*/ | ||
public class ZipkinV2Reporter implements Reporter { | ||
public final zipkin2.reporter.AsyncReporter<zipkin2.Span> reporter; | ||
|
||
public ZipkinV2Reporter(zipkin2.reporter.AsyncReporter<zipkin2.Span> reporter) { | ||
this.reporter = reporter; | ||
} | ||
|
||
@Override | ||
public void report(io.jaegertracing.Span span) { | ||
reporter.report(V2SpanConverter.convertSpan(span)); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
reporter.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this an expected case? If not, a log statement should be produced, like, "Could not identify the span kind during conversion. Skipping."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC internal spans has a null kind.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes they have kind set to
null