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();
}
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);
}
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;
}
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()));
}
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();
}
Aggregations