|
| 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