Search in sources :

Example 11 with Annotation

use of org.exquery.xquery3.Annotation in project zipkin by openzipkin.

the class JacksonSpanDecoder method parseSpan.

static Span parseSpan(JsonParser jsonParser) throws IOException {
    if (!jsonParser.isExpectedStartObjectToken()) {
        throw new IOException("Not a valid JSON object, start token: " + jsonParser.currentToken());
    }
    Span.Builder result = Span.newBuilder();
    while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
        String fieldName = jsonParser.currentName();
        JsonToken value = jsonParser.nextToken();
        if (value == JsonToken.VALUE_NULL) {
            continue;
        }
        switch(fieldName) {
            case "traceId":
                result.traceId(jsonParser.getValueAsString());
                break;
            case "parentId":
                result.parentId(jsonParser.getValueAsString());
                break;
            case "id":
                result.id(jsonParser.getValueAsString());
                break;
            case "kind":
                result.kind(Span.Kind.valueOf(jsonParser.getValueAsString()));
                break;
            case "name":
                result.name(jsonParser.getValueAsString());
                break;
            case "timestamp":
                result.timestamp(jsonParser.getValueAsLong());
                break;
            case "duration":
                result.duration(jsonParser.getValueAsLong());
                break;
            case "localEndpoint":
                result.localEndpoint(parseEndpoint(jsonParser));
                break;
            case "remoteEndpoint":
                result.remoteEndpoint(parseEndpoint(jsonParser));
                break;
            case "annotations":
                if (!jsonParser.isExpectedStartArrayToken()) {
                    throw new IOException("Invalid span, expecting annotations array start, got: " + value);
                }
                while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                    Annotation a = parseAnnotation(jsonParser);
                    result.addAnnotation(a.timestamp(), a.value());
                }
                break;
            case "tags":
                if (value != JsonToken.START_OBJECT) {
                    throw new IOException("Invalid span, expecting tags object, got: " + value);
                }
                while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                    result.putTag(jsonParser.currentName(), jsonParser.nextTextValue());
                }
                break;
            case "debug":
                result.debug(jsonParser.getBooleanValue());
                break;
            case "shared":
                result.shared(jsonParser.getBooleanValue());
                break;
            default:
                jsonParser.skipChildren();
        }
    }
    return result.build();
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) Span(zipkin2.Span) Annotation(zipkin2.Annotation)

Example 12 with Annotation

use of org.exquery.xquery3.Annotation in project zipkin by openzipkin.

the class V2SpanWriter method write.

@Override
public void write(Span value, WriteBuffer b) {
    b.writeAscii("{\"traceId\":\"");
    b.writeAscii(value.traceId());
    b.writeByte('"');
    if (value.parentId() != null) {
        b.writeAscii(",\"parentId\":\"");
        b.writeAscii(value.parentId());
        b.writeByte('"');
    }
    b.writeAscii(",\"id\":\"");
    b.writeAscii(value.id());
    b.writeByte('"');
    if (value.kind() != null) {
        b.writeAscii(",\"kind\":\"");
        b.writeAscii(value.kind().toString());
        b.writeByte('"');
    }
    if (value.name() != null) {
        b.writeAscii(",\"name\":\"");
        b.writeUtf8(jsonEscape(value.name()));
        b.writeByte('"');
    }
    if (value.timestampAsLong() != 0L) {
        b.writeAscii(",\"timestamp\":");
        b.writeAscii(value.timestampAsLong());
    }
    if (value.durationAsLong() != 0L) {
        b.writeAscii(",\"duration\":");
        b.writeAscii(value.durationAsLong());
    }
    if (value.localEndpoint() != null) {
        b.writeAscii(",\"localEndpoint\":");
        writeEndpoint(value.localEndpoint(), b, false);
    }
    if (value.remoteEndpoint() != null) {
        b.writeAscii(",\"remoteEndpoint\":");
        writeEndpoint(value.remoteEndpoint(), b, false);
    }
    if (!value.annotations().isEmpty()) {
        b.writeAscii(",\"annotations\":");
        b.writeByte('[');
        for (int i = 0, length = value.annotations().size(); i < length; ) {
            Annotation a = value.annotations().get(i++);
            writeAnnotation(a.timestamp(), a.value(), null, b);
            if (i < length)
                b.writeByte(',');
        }
        b.writeByte(']');
    }
    if (!value.tags().isEmpty()) {
        b.writeAscii(",\"tags\":{");
        Iterator<Map.Entry<String, String>> i = value.tags().entrySet().iterator();
        while (i.hasNext()) {
            Map.Entry<String, String> entry = i.next();
            b.writeByte('"');
            b.writeUtf8(jsonEscape(entry.getKey()));
            b.writeAscii("\":\"");
            b.writeUtf8(jsonEscape(entry.getValue()));
            b.writeByte('"');
            if (i.hasNext())
                b.writeByte(',');
        }
        b.writeByte('}');
    }
    if (Boolean.TRUE.equals(value.debug())) {
        b.writeAscii(",\"debug\":true");
    }
    if (Boolean.TRUE.equals(value.shared())) {
        b.writeAscii(",\"shared\":true");
    }
    b.writeByte('}');
}
Also used : Map(java.util.Map) Endpoint(zipkin2.Endpoint) Annotation(zipkin2.Annotation)

Example 13 with Annotation

use of org.exquery.xquery3.Annotation in project zipkin by openzipkin.

the class QueryRequest method test.

/**
 * Tests the supplied trace against the current request.
 *
 * <p>This is used when the backend cannot fully refine a trace query.
 */
public boolean test(List<Span> spans) {
    // v2 returns raw spans in any order, get the root's timestamp or the first timestamp
    long timestamp = 0L;
    for (Span span : spans) {
        if (span.timestampAsLong() == 0L)
            continue;
        if (span.parentId() == null) {
            timestamp = span.timestampAsLong();
            break;
        }
        if (timestamp == 0L || timestamp > span.timestampAsLong()) {
            timestamp = span.timestampAsLong();
        }
    }
    if (timestamp == 0L || timestamp < (endTs() - lookback()) * 1000 || timestamp > endTs() * 1000) {
        return false;
    }
    boolean testedDuration = minDuration() == null && maxDuration() == null;
    String serviceNameToMatch = serviceName();
    String remoteServiceNameToMatch = remoteServiceName();
    String spanNameToMatch = spanName();
    Map<String, String> annotationQueryRemaining = new LinkedHashMap<String, String>(annotationQuery());
    for (Span span : spans) {
        String localServiceName = span.localServiceName();
        // service name, when present, constrains other queries.
        if (serviceName() == null || serviceName().equals(localServiceName)) {
            serviceNameToMatch = null;
            for (Annotation a : span.annotations()) {
                if ("".equals(annotationQueryRemaining.get(a.value()))) {
                    annotationQueryRemaining.remove(a.value());
                }
            }
            for (Map.Entry<String, String> t : span.tags().entrySet()) {
                String value = annotationQueryRemaining.get(t.getKey());
                if (value == null)
                    continue;
                if (value.isEmpty() || value.equals(t.getValue())) {
                    annotationQueryRemaining.remove(t.getKey());
                }
            }
            if (remoteServiceNameToMatch != null && remoteServiceNameToMatch.equals(span.remoteServiceName())) {
                remoteServiceNameToMatch = null;
            }
            if (spanNameToMatch != null && spanNameToMatch.equals(span.name())) {
                spanNameToMatch = null;
            }
            if (!testedDuration) {
                if (minDuration() != null && maxDuration() != null) {
                    testedDuration = span.durationAsLong() >= minDuration() && span.durationAsLong() <= maxDuration();
                } else if (minDuration() != null) {
                    testedDuration = span.durationAsLong() >= minDuration();
                }
            }
        }
    }
    return (serviceName() == null || serviceNameToMatch == null) && remoteServiceNameToMatch == null && spanNameToMatch == null && annotationQueryRemaining.isEmpty() && testedDuration;
}
Also used : Span(zipkin2.Span) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Annotation(zipkin2.Annotation) LinkedHashMap(java.util.LinkedHashMap)

Example 14 with Annotation

use of org.exquery.xquery3.Annotation in project zipkin by openzipkin.

the class JsonSerializers method parseSpan.

static Span parseSpan(JsonParser parser) throws IOException {
    if (!parser.isExpectedStartObjectToken()) {
        throw new IllegalArgumentException("Not a valid JSON object, start token: " + parser.currentToken());
    }
    Span.Builder result = Span.newBuilder();
    JsonToken value;
    while ((value = parser.nextValue()) != JsonToken.END_OBJECT) {
        if (value == null) {
            throw new IOException("End of input while parsing object.");
        }
        if (value == JsonToken.VALUE_NULL) {
            continue;
        }
        switch(parser.currentName()) {
            case "traceId":
                result.traceId(parser.getText());
                break;
            case "parentId":
                result.parentId(parser.getText());
                break;
            case "id":
                result.id(parser.getText());
                break;
            case "kind":
                result.kind(Span.Kind.valueOf(parser.getText()));
                break;
            case "name":
                result.name(parser.getText());
                break;
            case "timestamp":
                result.timestamp(parser.getLongValue());
                break;
            case "duration":
                result.duration(parser.getLongValue());
                break;
            case "localEndpoint":
                result.localEndpoint(parseEndpoint(parser));
                break;
            case "remoteEndpoint":
                result.remoteEndpoint(parseEndpoint(parser));
                break;
            case "annotations":
                if (value != JsonToken.START_ARRAY) {
                    throw new IOException("Invalid span, expecting annotations array start, got: " + value);
                }
                while (parser.nextToken() != JsonToken.END_ARRAY) {
                    Annotation a = parseAnnotation(parser);
                    result.addAnnotation(a.timestamp(), a.value());
                }
                break;
            case "tags":
                if (value != JsonToken.START_OBJECT) {
                    throw new IOException("Invalid span, expecting tags object, got: " + value);
                }
                while (parser.nextValue() != JsonToken.END_OBJECT) {
                    result.putTag(parser.currentName(), parser.getValueAsString());
                }
                break;
            case "debug":
                result.debug(parser.getBooleanValue());
                break;
            case "shared":
                result.shared(parser.getBooleanValue());
                break;
            default:
        }
    }
    return result.build();
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) Span(zipkin2.Span) Annotation(zipkin2.Annotation)

Example 15 with Annotation

use of org.exquery.xquery3.Annotation in project zipkin-gcp by openzipkin.

the class LabelExtractor method extract.

/**
 * Extracts the Stackdriver span labels that are equivalent to the Zipkin Span annotations.
 *
 * @param zipkinSpan The Zipkin Span
 * @return A map of the Stackdriver span labels equivalent to the Zipkin annotations.
 */
Map<String, String> extract(Span zipkinSpan) {
    Map<String, String> result = new LinkedHashMap<>();
    for (Map.Entry<String, String> tag : zipkinSpan.tags().entrySet()) {
        result.put(getLabelName(tag.getKey()), tag.getValue());
    }
    // trace might not show the final destination.
    if (zipkinSpan.localEndpoint() != null && zipkinSpan.kind() == Span.Kind.SERVER) {
        if (zipkinSpan.localEndpoint().ipv4() != null) {
            result.put(getLabelName("endpoint.ipv4"), zipkinSpan.localEndpoint().ipv4());
        }
        if (zipkinSpan.localEndpoint().ipv6() != null) {
            result.put(getLabelName("endpoint.ipv6"), zipkinSpan.localEndpoint().ipv6());
        }
    }
    for (Annotation annotation : zipkinSpan.annotations()) {
        result.put(getLabelName(annotation.value()), formatTimestamp(annotation.timestamp()));
    }
    if (zipkinSpan.localEndpoint() != null && !zipkinSpan.localEndpoint().serviceName().isEmpty()) {
        result.put(kComponentLabelKey, zipkinSpan.localEndpoint().serviceName());
    }
    if (zipkinSpan.parentId() == null) {
        String agentName = System.getProperty("stackdriver.trace.zipkin.agent", "zipkin-java");
        result.put(kAgentLabelKey, agentName);
    }
    return result;
}
Also used : LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Annotation(zipkin2.Annotation) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

Annotation (zipkin2.Annotation)14 Map (java.util.Map)8 Test (org.junit.Test)5 Endpoint (zipkin2.Endpoint)5 ArrayList (java.util.ArrayList)4 Span (zipkin2.Span)4 LinkedHashMap (java.util.LinkedHashMap)3 TreeMap (java.util.TreeMap)3 QName (javax.xml.namespace.QName)3 Annotation (org.sbolstandard.core2.Annotation)3 JsonToken (com.fasterxml.jackson.core.JsonToken)2 JaegerSpan (io.jaegertracing.internal.JaegerSpan)2 IOException (java.io.IOException)2 URI (java.net.URI)2 List (java.util.List)2 Tracing (brave.Tracing)1 Platform (brave.internal.Platform)1 TraceContext (brave.propagation.TraceContext)1 TimeEvents (com.google.devtools.cloudtrace.v2.Span.TimeEvents)1 Timestamp (com.google.protobuf.Timestamp)1