use of io.opencensus.trace.export.SpanData in project instrumentation-java by census-instrumentation.
the class StackdriverV2ExporterHandlerProtoTest method exportChildSpanCount.
@Test
public void exportChildSpanCount() {
SpanData spanData = SpanData.create(spanContext, parentSpanId, /* hasRemoteParent= */
true, SPAN_NAME, Kind.SERVER, startTimestamp, attributes, annotations, messageEvents, links, CHILD_SPAN_COUNT, status, endTimestamp);
assertThat(handler.generateSpan(spanData, EMPTY_RESOURCE_LABELS, Collections.<String, AttributeValue>emptyMap()).getChildSpanCount().getValue()).isEqualTo(CHILD_SPAN_COUNT);
}
use of io.opencensus.trace.export.SpanData 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.export.SpanData in project instrumentation-java by census-instrumentation.
the class DatadogExporterHandlerTest method testJsonConversion.
@Test
public void testJsonConversion() {
SpanData data = SpanData.create(SpanContext.create(TraceId.fromLowerBase16(TRACE_ID), SpanId.fromLowerBase16(SPAN_ID), TraceOptions.builder().setIsSampled(true).build(), Tracestate.builder().build()), SpanId.fromLowerBase16(PARENT_SPAN_ID), /* hasRemoteParent= */
true, "SpanName", /* kind= */
null, /* startTimestamp= */
Timestamp.create(1505855794, 194009601), SpanData.Attributes.create(attributes, 0), SpanData.TimedEvents.create(annotations, 0), SpanData.TimedEvents.create(messageEvents, 0), SpanData.Links.create(Collections.emptyList(), 0), /* childSpanCount= */
null, Status.OK, /* endTimestamp= */
Timestamp.create(1505855799, 465726528));
final String expected = "[[" + "{" + "\"trace_id\":3298601478987650031," + "\"span_id\":7151185124527981047," + "\"name\":\"SpanName\"," + "\"resource\":\"/foo\"," + "\"service\":\"service\"," + "\"type\":\"web\"," + "\"start\":1505855794194009601," + "\"duration\":5271716927," + "\"parent_id\":8429705776517054011," + "\"error\":0," + "\"meta\":{" + "\"resource\":\"/foo\"," + "\"http.url\":\"http://localhost/foo\"" + "}" + "}" + "]]";
assertThat(handler.convertToJson(Collections.singletonList(data))).isEqualTo(expected);
}
use of io.opencensus.trace.export.SpanData 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.export.SpanData in project instrumentation-java by census-instrumentation.
the class JsonConversionUtilsTest method setUp.
@Before
public void setUp() {
SpanData spanData = SpanData.create(SpanContext.create(TraceId.fromLowerBase16(SAMPLE_TRACE_ID), SpanId.fromLowerBase16(SAMPLE_SPAN_ID), SAMPLE_TRACE_OPTION, SAMPLE_TRACE_STATE), SpanId.fromLowerBase16(SAMPLE_PARENT_SPAN_ID), true, "SpanName", null, Timestamp.create(155196336, 194009601), Attributes.create(attributes, 0), TimedEvents.create(annotations, 0), TimedEvents.create(messageEvents, 0), Links.create(Collections.<Link>emptyList(), 0), null, Status.OK, Timestamp.create(155296336, 465726528));
spanDataList = new ArrayList<SpanData>();
spanDataList.add(spanData);
}
Aggregations