Search in sources :

Example 1 with V1Annotation

use of zipkin2.v1.V1Annotation in project zipkin by openzipkin.

the class V1SpanConverter method processBinaryAnnotations.

void processBinaryAnnotations(V1Span source) {
    zipkin2.Endpoint ca = null, sa = null, ma = null;
    for (int i = 0, length = source.binaryAnnotations.size(); i < length; i++) {
        V1BinaryAnnotation b = source.binaryAnnotations.get(i);
        // endpoint. Hence, we leniently parse.
        if ("ca".equals(b.key)) {
            ca = b.endpoint;
            continue;
        } else if ("sa".equals(b.key)) {
            sa = b.endpoint;
            continue;
        } else if ("ma".equals(b.key)) {
            ma = b.endpoint;
            continue;
        }
        Span.Builder currentSpan = forEndpoint(source, b.endpoint);
        // don't add marker "lc" tags
        if ("lc".equals(b.key) && b.stringValue.isEmpty())
            continue;
        currentSpan.putTag(b.key, b.stringValue);
    }
    boolean noCoreAnnotations = cs == null && cr == null && ss == null && sr == null;
    // special-case when we are missing core annotations, but we have both address annotations
    if (noCoreAnnotations && (ca != null || sa != null)) {
        if (ca != null && sa != null) {
            forEndpoint(source, ca).remoteEndpoint(sa);
        } else if (sa != null) {
            // "sa" is a default for a remote address, don't make it a client span
            forEndpoint(source, null).remoteEndpoint(sa);
        } else {
            // ca != null: treat it like a server
            forEndpoint(source, null).kind(Kind.SERVER).remoteEndpoint(ca);
        }
        return;
    }
    V1Annotation server = sr != null ? sr : ss;
    if (ca != null && server != null && !ca.equals(server.endpoint)) {
        // the same service name as "sa". Removing the service name prevents creating loopback links.
        if (hasSameServiceName(ca, server.endpoint)) {
            ca = ca.toBuilder().serviceName(null).build();
        }
        forEndpoint(source, server.endpoint).remoteEndpoint(ca);
    }
    if (sa != null) {
        // client span
        if (cs != null) {
            forEndpoint(source, cs.endpoint).remoteEndpoint(sa);
        } else if (cr != null) {
            forEndpoint(source, cr.endpoint).remoteEndpoint(sa);
        }
    }
    if (ma != null) {
        // a messaging span. This will ensure both sides have the address of the broker.
        if (ms != null)
            forEndpoint(source, ms.endpoint).remoteEndpoint(ma);
        if (mr != null)
            forEndpoint(source, mr.endpoint).remoteEndpoint(ma);
    }
}
Also used : Endpoint(zipkin2.Endpoint) Span(zipkin2.Span) Endpoint(zipkin2.Endpoint)

Example 2 with V1Annotation

use of zipkin2.v1.V1Annotation in project zipkin by openzipkin.

the class V1SpanConverter method processAnnotations.

void processAnnotations(V1Span source) {
    for (int i = 0, length = source.annotations.size(); i < length; i++) {
        V1Annotation a = source.annotations.get(i);
        Span.Builder currentSpan = forEndpoint(source, a.endpoint);
        // core annotations require an endpoint. Don't give special treatment when that's missing
        if (a.value.length() == 2 && a.endpoint != null) {
            if (a.value.equals("cs")) {
                currentSpan.kind(Kind.CLIENT);
                cs = a;
            } else if (a.value.equals("sr")) {
                currentSpan.kind(Kind.SERVER);
                sr = a;
            } else if (a.value.equals("ss")) {
                currentSpan.kind(Kind.SERVER);
                ss = a;
            } else if (a.value.equals("cr")) {
                currentSpan.kind(Kind.CLIENT);
                cr = a;
            } else if (a.value.equals("ms")) {
                currentSpan.kind(Kind.PRODUCER);
                ms = a;
            } else if (a.value.equals("mr")) {
                currentSpan.kind(Kind.CONSUMER);
                mr = a;
            } else if (a.value.equals("ws")) {
                ws = a;
            } else if (a.value.equals("wr")) {
                wr = a;
            } else {
                currentSpan.addAnnotation(a.timestamp, a.value);
            }
        } else {
            currentSpan.addAnnotation(a.timestamp, a.value);
        }
    }
    // When bridging between event and span model, you can end up missing a start annotation
    if (cs == null && endTimestampReflectsSpanDuration(cr, source)) {
        cs = V1Annotation.create(source.timestamp, "cs", cr.endpoint);
    }
    if (sr == null && endTimestampReflectsSpanDuration(ss, source)) {
        sr = V1Annotation.create(source.timestamp, "sr", ss.endpoint);
    }
    if (cs != null && sr != null) {
        // in a shared span, the client side owns span duration by annotations or explicit timestamp
        maybeTimestampDuration(source, cs, cr);
        // special-case loopback: We need to make sure on loopback there are two span2s
        Span.Builder client = forEndpoint(source, cs.endpoint);
        Span.Builder server;
        if (hasSameServiceName(cs.endpoint, sr.endpoint)) {
            client.kind(Kind.CLIENT);
            // fork a new span for the server side
            server = newSpanBuilder(source, sr.endpoint).kind(Kind.SERVER);
        } else {
            server = forEndpoint(source, sr.endpoint);
        }
        // the server side is smaller than that, we have to read annotations to find out
        server.shared(true).timestamp(sr.timestamp);
        if (ss != null)
            server.duration(ss.timestamp - sr.timestamp);
        // one-way has no duration
        if (cr == null && source.duration == 0)
            client.duration(null);
    } else if (cs != null && cr != null) {
        maybeTimestampDuration(source, cs, cr);
    } else if (sr != null && ss != null) {
        maybeTimestampDuration(source, sr, ss);
    } else {
        // otherwise, the span is incomplete. revert special-casing
        handleIncompleteRpc(source);
    }
    // implied shared. When we only see the server-side, carry this signal over.
    if (cs == null && sr != null && // case could be due to the client-side of that RPC.
    (source.timestamp == 0 || (ss != null && source.duration == 0))) {
        forEndpoint(source, sr.endpoint).shared(true);
    }
    // ms and mr are not supposed to be in the same span, but in case they are..
    if (ms != null && mr != null) {
        // special-case loopback: We need to make sure on loopback there are two span2s
        Span.Builder producer = forEndpoint(source, ms.endpoint);
        Span.Builder consumer;
        if (hasSameServiceName(ms.endpoint, mr.endpoint)) {
            producer.kind(Kind.PRODUCER);
            // fork a new span for the consumer side
            consumer = newSpanBuilder(source, mr.endpoint).kind(Kind.CONSUMER);
        } else {
            consumer = forEndpoint(source, mr.endpoint);
        }
        consumer.shared(true);
        if (wr != null) {
            consumer.timestamp(wr.timestamp).duration(mr.timestamp - wr.timestamp);
        } else {
            consumer.timestamp(mr.timestamp);
        }
        producer.timestamp(ms.timestamp).duration(ws != null ? ws.timestamp - ms.timestamp : null);
    } else if (ms != null) {
        maybeTimestampDuration(source, ms, ws);
    } else if (mr != null) {
        if (wr != null) {
            maybeTimestampDuration(source, wr, mr);
        } else {
            maybeTimestampDuration(source, mr, null);
        }
    } else {
        if (ws != null)
            forEndpoint(source, ws.endpoint).addAnnotation(ws.timestamp, ws.value);
        if (wr != null)
            forEndpoint(source, wr.endpoint).addAnnotation(wr.timestamp, wr.value);
    }
}
Also used : Span(zipkin2.Span) Endpoint(zipkin2.Endpoint)

Example 3 with V1Annotation

use of zipkin2.v1.V1Annotation in project zipkin by openzipkin.

the class V1SpanWriter method write.

@Override
public void write(V1Span value, WriteBuffer b) {
    b.writeAscii("{\"traceId\":\"");
    if (value.traceIdHigh() != 0L)
        b.writeLongHex(value.traceIdHigh());
    b.writeLongHex(value.traceId());
    b.writeByte('"');
    if (value.parentId() != 0L) {
        b.writeAscii(",\"parentId\":\"");
        b.writeLongHex(value.parentId());
        b.writeByte('"');
    }
    b.writeAscii(",\"id\":\"");
    b.writeLongHex(value.id());
    b.writeByte('"');
    b.writeAscii(",\"name\":\"");
    if (value.name() != null)
        b.writeUtf8(jsonEscape(value.name()));
    b.writeByte('"');
    if (value.timestamp() != 0L) {
        b.writeAscii(",\"timestamp\":");
        b.writeAscii(value.timestamp());
    }
    if (value.duration() != 0L) {
        b.writeAscii(",\"duration\":");
        b.writeAscii(value.duration());
    }
    int annotationCount = value.annotations().size();
    Endpoint lastEndpoint = null;
    byte[] lastEndpointBytes = null;
    if (annotationCount > 0) {
        b.writeAscii(",\"annotations\":[");
        for (int i = 0; i < annotationCount; ) {
            V1Annotation a = value.annotations().get(i++);
            Endpoint endpoint = a.endpoint();
            byte[] endpointBytes;
            if (endpoint == null) {
                endpointBytes = null;
            } else if (endpoint.equals(lastEndpoint)) {
                endpointBytes = lastEndpointBytes;
            } else {
                lastEndpoint = endpoint;
                endpointBytes = lastEndpointBytes = legacyEndpointBytes(endpoint);
            }
            writeAnnotation(a.timestamp(), a.value(), endpointBytes, b);
            if (i < annotationCount)
                b.writeByte(',');
        }
        b.writeByte(']');
    }
    int binaryAnnotationCount = value.binaryAnnotations().size();
    if (binaryAnnotationCount > 0) {
        b.writeAscii(",\"binaryAnnotations\":[");
        for (int i = 0; i < binaryAnnotationCount; ) {
            V1BinaryAnnotation a = value.binaryAnnotations().get(i++);
            Endpoint endpoint = a.endpoint();
            byte[] endpointBytes;
            if (endpoint == null) {
                endpointBytes = null;
            } else if (endpoint.equals(lastEndpoint)) {
                endpointBytes = lastEndpointBytes;
            } else {
                lastEndpoint = endpoint;
                endpointBytes = lastEndpointBytes = legacyEndpointBytes(endpoint);
            }
            if (a.stringValue() != null) {
                writeBinaryAnnotation(a.key(), a.stringValue(), endpointBytes, b);
            } else {
                b.writeAscii("{\"key\":\"");
                b.writeAscii(a.key());
                b.writeAscii("\",\"value\":true,\"endpoint\":");
                b.write(endpointBytes);
                b.writeByte('}');
            }
            if (i < binaryAnnotationCount)
                b.writeByte(',');
        }
        b.writeByte(']');
    }
    if (Boolean.TRUE.equals(value.debug())) {
        b.writeAscii(",\"debug\":true");
    }
    b.writeByte('}');
}
Also used : Endpoint(zipkin2.Endpoint) V1BinaryAnnotation(zipkin2.v1.V1BinaryAnnotation) Endpoint(zipkin2.Endpoint) V1Annotation(zipkin2.v1.V1Annotation)

Example 4 with V1Annotation

use of zipkin2.v1.V1Annotation in project zipkin by openzipkin.

the class V1SpanWriter method sizeInBytes.

@Override
public int sizeInBytes(V1Span value) {
    // {"traceId":"xxxxxxxxxxxxxxxx"
    int sizeInBytes = 29;
    if (value.traceIdHigh() != 0L)
        sizeInBytes += 16;
    if (value.parentId() != 0L) {
        // ,"parentId":"0123456789abcdef"
        sizeInBytes += 30;
    }
    // ,"id":"0123456789abcdef"
    sizeInBytes += 24;
    // ,"name":""
    sizeInBytes += 10;
    if (value.name() != null) {
        sizeInBytes += jsonEscapedSizeInBytes(value.name());
    }
    if (value.timestamp() != 0L) {
        // ,"timestamp":
        sizeInBytes += 13;
        sizeInBytes += asciiSizeInBytes(value.timestamp());
    }
    if (value.duration() != 0L) {
        // ,"duration":
        sizeInBytes += 12;
        sizeInBytes += asciiSizeInBytes(value.duration());
    }
    int annotationCount = value.annotations().size();
    Endpoint lastEndpoint = null;
    int lastEndpointSize = 0;
    if (annotationCount > 0) {
        // ,"annotations":[]
        sizeInBytes += 17;
        // comma to join elements
        if (annotationCount > 1)
            sizeInBytes += annotationCount - 1;
        for (int i = 0; i < annotationCount; i++) {
            V1Annotation a = value.annotations().get(i);
            Endpoint endpoint = a.endpoint();
            int endpointSize;
            if (endpoint == null) {
                endpointSize = 0;
            } else if (endpoint.equals(lastEndpoint)) {
                endpointSize = lastEndpointSize;
            } else {
                lastEndpoint = endpoint;
                endpointSize = lastEndpointSize = endpointSizeInBytes(endpoint, true);
            }
            sizeInBytes += V2SpanWriter.annotationSizeInBytes(a.timestamp(), a.value(), endpointSize);
        }
    }
    int binaryAnnotationCount = value.binaryAnnotations().size();
    if (binaryAnnotationCount > 0) {
        // ,"binaryAnnotations":[]
        sizeInBytes += 23;
        // commas
        if (binaryAnnotationCount > 1)
            sizeInBytes += binaryAnnotationCount - 1;
        for (int i = 0; i < binaryAnnotationCount; ) {
            V1BinaryAnnotation a = value.binaryAnnotations().get(i++);
            Endpoint endpoint = a.endpoint();
            int endpointSize;
            if (endpoint == null) {
                endpointSize = 0;
            } else if (endpoint.equals(lastEndpoint)) {
                endpointSize = lastEndpointSize;
            } else {
                lastEndpoint = endpoint;
                endpointSize = lastEndpointSize = endpointSizeInBytes(endpoint, true);
            }
            if (a.stringValue() != null) {
                sizeInBytes += binaryAnnotationSizeInBytes(a.key(), a.stringValue(), endpointSize);
            } else {
                // {"key":"NN","value":true,"endpoint":}
                sizeInBytes += 37;
                sizeInBytes += endpointSize;
            }
        }
    }
    // ,"debug":true
    if (Boolean.TRUE.equals(value.debug()))
        sizeInBytes += 13;
    // }
    return ++sizeInBytes;
}
Also used : Endpoint(zipkin2.Endpoint) V1BinaryAnnotation(zipkin2.v1.V1BinaryAnnotation) Endpoint(zipkin2.Endpoint) V1Annotation(zipkin2.v1.V1Annotation)

Example 5 with V1Annotation

use of zipkin2.v1.V1Annotation in project zipkin by openzipkin.

the class V1ThriftSpanWriter method writeAnnotations.

static void writeAnnotations(WriteBuffer buffer, V1Span v1Span, byte[] endpointBytes) {
    int annotationCount = v1Span.annotations().size();
    ThriftCodec.writeListBegin(buffer, annotationCount);
    for (int i = 0; i < annotationCount; i++) {
        V1Annotation a = v1Span.annotations().get(i);
        ThriftAnnotationWriter.write(a.timestamp(), a.value(), endpointBytes, buffer);
    }
}
Also used : Endpoint(zipkin2.Endpoint) V1Annotation(zipkin2.v1.V1Annotation)

Aggregations

Endpoint (zipkin2.Endpoint)5 V1Annotation (zipkin2.v1.V1Annotation)3 Span (zipkin2.Span)2 V1BinaryAnnotation (zipkin2.v1.V1BinaryAnnotation)2