Search in sources :

Example 6 with SpanContext

use of io.opencensus.trace.SpanContext in project instrumentation-java by census-instrumentation.

the class StackdriverV2ExporterHandler method generateSpan.

@VisibleForTesting
Span generateSpan(SpanData spanData, Map<String, AttributeValue> resourceLabels, Map<String, AttributeValue> fixedAttributes) {
    SpanContext context = spanData.getContext();
    final String spanIdHex = context.getSpanId().toLowerBase16();
    SpanName spanName = SpanName.newBuilder().setProject(projectId).setTrace(context.getTraceId().toLowerBase16()).setSpan(spanIdHex).build();
    Span.Builder spanBuilder = Span.newBuilder().setName(spanName.toString()).setSpanId(spanIdHex).setDisplayName(toTruncatableStringProto(toDisplayName(spanData.getName(), spanData.getKind()))).setStartTime(toTimestampProto(spanData.getStartTimestamp())).setAttributes(toAttributesProto(spanData.getAttributes(), resourceLabels, fixedAttributes)).setTimeEvents(toTimeEventsProto(spanData.getAnnotations(), spanData.getMessageEvents()));
    io.opencensus.trace.Status status = spanData.getStatus();
    if (status != null) {
        spanBuilder.setStatus(toStatusProto(status));
    }
    Timestamp end = spanData.getEndTimestamp();
    if (end != null) {
        spanBuilder.setEndTime(toTimestampProto(end));
    }
    spanBuilder.setLinks(toLinksProto(spanData.getLinks()));
    Integer childSpanCount = spanData.getChildSpanCount();
    if (childSpanCount != null) {
        spanBuilder.setChildSpanCount(Int32Value.newBuilder().setValue(childSpanCount).build());
    }
    if (spanData.getParentSpanId() != null && spanData.getParentSpanId().isValid()) {
        spanBuilder.setParentSpanId(spanData.getParentSpanId().toLowerBase16());
    }
    /*@Nullable*/
    Boolean hasRemoteParent = spanData.getHasRemoteParent();
    if (hasRemoteParent != null) {
        spanBuilder.setSameProcessAsParentSpan(BoolValue.of(!hasRemoteParent));
    }
    return spanBuilder.build();
}
Also used : SpanName(com.google.devtools.cloudtrace.v2.SpanName) SpanContext(io.opencensus.trace.SpanContext) TruncatableString(com.google.devtools.cloudtrace.v2.TruncatableString) Span(com.google.devtools.cloudtrace.v2.Span) Timestamp(io.opencensus.common.Timestamp) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 7 with SpanContext

use of io.opencensus.trace.SpanContext in project instrumentation-java by census-instrumentation.

the class DatadogExporterHandler method convertToJson.

String convertToJson(Collection<SpanData> spanDataList) {
    final ArrayList<DatadogSpan> datadogSpans = new ArrayList<>();
    for (SpanData sd : spanDataList) {
        SpanContext sc = sd.getContext();
        final long startTime = timestampToNanos(sd.getStartTimestamp());
        final Timestamp endTimestamp = Optional.ofNullable(sd.getEndTimestamp()).orElseGet(() -> Tracing.getClock().now());
        final long endTime = timestampToNanos(endTimestamp);
        final long duration = endTime - startTime;
        final Long parentId = Optional.ofNullable(sd.getParentSpanId()).map(DatadogExporterHandler::convertSpanId).orElse(null);
        final Map<String, AttributeValue> attributes = sd.getAttributes().getAttributeMap();
        final Map<String, String> meta = attributes.isEmpty() ? new HashMap<>() : attributesToMeta(attributes);
        final String resource = meta.getOrDefault("resource", "UNKNOWN");
        final DatadogSpan span = new DatadogSpan(sc.getTraceId().getLowerLong(), convertSpanId(sc.getSpanId()), sd.getName(), resource, this.service, this.type, startTime, duration, parentId, errorCode(sd.getStatus()), meta);
        datadogSpans.add(span);
    }
    final Collection<List<DatadogSpan>> traces = datadogSpans.stream().collect(Collectors.groupingBy(DatadogSpan::getTraceId, Collectors.toList())).values();
    return gson.toJson(traces);
}
Also used : AttributeValue(io.opencensus.trace.AttributeValue) SpanContext(io.opencensus.trace.SpanContext) SpanData(io.opencensus.trace.export.SpanData) ArrayList(java.util.ArrayList) Timestamp(io.opencensus.common.Timestamp) ArrayList(java.util.ArrayList) List(java.util.List)

Example 8 with SpanContext

use of io.opencensus.trace.SpanContext in project instrumentation-java by census-instrumentation.

the class JsonConversionUtils method convertToJson.

/**
 * Converts a collection of {@link SpanData} to a Collection of json string.
 *
 * @param appName the name of app to include in traces.
 * @param spanDataList Collection of {@code SpanData} to be converted to json.
 * @return Collection of {@code SpanData} converted to JSON to be indexed.
 */
static List<String> convertToJson(String appName, Collection<SpanData> spanDataList) {
    List<String> spanJson = new ArrayList<String>();
    if (spanDataList == null) {
        return spanJson;
    }
    StringBuilder sb = new StringBuilder();
    for (final SpanData span : spanDataList) {
        final SpanContext spanContext = span.getContext();
        final SpanId parentSpanId = span.getParentSpanId();
        final Timestamp startTimestamp = span.getStartTimestamp();
        final Timestamp endTimestamp = span.getEndTimestamp();
        final Status status = span.getStatus();
        if (endTimestamp == null) {
            continue;
        }
        sb.append('{');
        sb.append("\"appName\":\"").append(appName).append("\",");
        sb.append("\"spanId\":\"").append(encodeSpanId(spanContext.getSpanId())).append("\",");
        sb.append("\"traceId\":\"").append(encodeTraceId(spanContext.getTraceId())).append("\",");
        if (parentSpanId != null) {
            sb.append("\"parentId\":\"").append(encodeSpanId(parentSpanId)).append("\",");
        }
        sb.append("\"timestamp\":").append(toMillis(startTimestamp)).append(',');
        sb.append("\"duration\":").append(toMillis(startTimestamp, endTimestamp)).append(',');
        sb.append("\"name\":\"").append(toSpanName(span)).append("\",");
        sb.append("\"kind\":\"").append(toSpanKind(span)).append("\",");
        sb.append("\"dateStarted\":\"").append(formatDate(startTimestamp)).append("\",");
        sb.append("\"dateEnded\":\"").append(formatDate(endTimestamp)).append('"');
        if (status == null) {
            sb.append(",\"status\":").append("\"ok\"");
        } else if (!status.isOk()) {
            sb.append(",\"error\":").append("true");
        }
        Map<String, AttributeValue> attributeMap = span.getAttributes().getAttributeMap();
        if (attributeMap.size() > 0) {
            StringBuilder builder = new StringBuilder();
            builder.append('{');
            for (Entry<String, AttributeValue> entry : attributeMap.entrySet()) {
                if (builder.length() > 1) {
                    builder.append(',');
                }
                builder.append("\"").append(entry.getKey()).append("\":\"").append(attributeValueToString(entry.getValue())).append("\"");
            }
            builder.append('}');
            sb.append(",\"data\":").append(builder);
        }
        sb.append('}');
        spanJson.add(sb.toString());
    }
    return spanJson;
}
Also used : Status(io.opencensus.trace.Status) AttributeValue(io.opencensus.trace.AttributeValue) SpanContext(io.opencensus.trace.SpanContext) SpanData(io.opencensus.trace.export.SpanData) ArrayList(java.util.ArrayList) Timestamp(io.opencensus.common.Timestamp) SpanId(io.opencensus.trace.SpanId)

Example 9 with SpanContext

use of io.opencensus.trace.SpanContext in project instrumentation-java by census-instrumentation.

the class JaegerExporterHandler method spanDataToJaegerThriftSpan.

private Span spanDataToJaegerThriftSpan(final SpanData spanData) {
    final long startTimeInMicros = timestampToMicros(spanData.getStartTimestamp());
    final long endTimeInMicros = timestampToMicros(spanData.getEndTimestamp());
    final SpanContext context = spanData.getContext();
    copyToBuffer(context.getTraceId());
    List<Tag> tags = attributesToTags(spanData.getAttributes().getAttributeMap(), spanKindToTag(spanData.getKind()));
    addStatusTags(tags, spanData.getStatus());
    return new io.jaegertracing.thriftjava.Span(traceIdLow(), traceIdHigh(), spanIdToLong(context.getSpanId()), spanIdToLong(spanData.getParentSpanId()), spanData.getName(), optionsToFlags(context.getTraceOptions()), startTimeInMicros, endTimeInMicros - startTimeInMicros).setReferences(linksToReferences(spanData.getLinks().getLinks())).setTags(tags).setLogs(timedEventsToLogs(spanData.getAnnotations().getEvents(), spanData.getMessageEvents().getEvents()));
}
Also used : SpanContext(io.opencensus.trace.SpanContext) Span(io.jaegertracing.thriftjava.Span) Tag(io.jaegertracing.thriftjava.Tag)

Example 10 with SpanContext

use of io.opencensus.trace.SpanContext in project instrumentation-java by census-instrumentation.

the class TraceProtoUtils method toSpanProto.

/**
 * Converts {@link SpanData} to {@link Span} proto.
 *
 * @param spanData the {@code SpanData}.
 * @return proto representation of {@code Span}.
 */
static Span toSpanProto(SpanData spanData) {
    SpanContext spanContext = spanData.getContext();
    TraceId traceId = spanContext.getTraceId();
    SpanId spanId = spanContext.getSpanId();
    Span.Builder spanBuilder = Span.newBuilder().setTraceId(toByteString(traceId.getBytes())).setSpanId(toByteString(spanId.getBytes())).setTracestate(toTracestateProto(spanContext.getTracestate())).setName(toTruncatableStringProto(spanData.getName())).setStartTime(toTimestampProto(spanData.getStartTimestamp())).setAttributes(toAttributesProto(spanData.getAttributes())).setTimeEvents(toTimeEventsProto(spanData.getAnnotations(), spanData.getMessageEvents())).setLinks(toLinksProto(spanData.getLinks()));
    Kind kind = spanData.getKind();
    if (kind != null) {
        spanBuilder.setKind(toSpanKindProto(kind));
    }
    io.opencensus.trace.Status status = spanData.getStatus();
    if (status != null) {
        spanBuilder.setStatus(toStatusProto(status));
    }
    Timestamp end = spanData.getEndTimestamp();
    if (end != null) {
        spanBuilder.setEndTime(toTimestampProto(end));
    }
    Integer childSpanCount = spanData.getChildSpanCount();
    if (childSpanCount != null) {
        spanBuilder.setChildSpanCount(UInt32Value.newBuilder().setValue(childSpanCount).build());
    }
    Boolean hasRemoteParent = spanData.getHasRemoteParent();
    if (hasRemoteParent != null) {
        spanBuilder.setSameProcessAsParentSpan(BoolValue.of(!hasRemoteParent));
    }
    SpanId parentSpanId = spanData.getParentSpanId();
    if (parentSpanId != null && parentSpanId.isValid()) {
        spanBuilder.setParentSpanId(toByteString(parentSpanId.getBytes()));
    }
    return spanBuilder.build();
}
Also used : SpanContext(io.opencensus.trace.SpanContext) SpanKind(io.opencensus.proto.trace.v1.Span.SpanKind) Kind(io.opencensus.trace.Span.Kind) TraceId(io.opencensus.trace.TraceId) Span(io.opencensus.proto.trace.v1.Span) Timestamp(io.opencensus.common.Timestamp) SpanId(io.opencensus.trace.SpanId)

Aggregations

SpanContext (io.opencensus.trace.SpanContext)21 Test (org.junit.Test)8 Timestamp (io.opencensus.common.Timestamp)6 AttributeValue (io.opencensus.trace.AttributeValue)4 Status (io.opencensus.trace.Status)4 SpanData (io.opencensus.trace.export.SpanData)4 Span (io.opencensus.trace.Span)3 SpanId (io.opencensus.trace.SpanId)3 ArrayList (java.util.ArrayList)3 CloudTraceContext (com.google.apphosting.api.CloudTraceContext)2 Context (io.grpc.Context)2 Annotation (io.opencensus.trace.Annotation)2 SpanBuilder (io.opencensus.trace.SpanBuilder)2 Map (java.util.Map)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Span (com.google.devtools.cloudtrace.v2.Span)1 SpanName (com.google.devtools.cloudtrace.v2.SpanName)1 TruncatableString (com.google.devtools.cloudtrace.v2.TruncatableString)1 Metadata (io.grpc.Metadata)1 ServerStreamTracer (io.grpc.ServerStreamTracer)1