use of io.opencensus.trace.AttributeValue in project instrumentation-java by census-instrumentation.
the class SpanDataTest method spanDataEquals.
@Test
public void spanDataEquals() {
SpanData allSpanData1 = SpanData.create(spanContext, parentSpanId, false, SPAN_NAME, Kind.CLIENT, startTimestamp, attributes, annotations, messageEvents, links, CHILD_SPAN_COUNT, status, endTimestamp);
SpanData allSpanData2 = SpanData.create(spanContext, parentSpanId, false, SPAN_NAME, Kind.CLIENT, startTimestamp, attributes, annotations, messageEvents, links, CHILD_SPAN_COUNT, status, endTimestamp);
SpanData emptySpanData = SpanData.create(spanContext, parentSpanId, false, SPAN_NAME, null, startTimestamp, Attributes.create(Collections.<String, AttributeValue>emptyMap(), 0), TimedEvents.create(Collections.<SpanData.TimedEvent<Annotation>>emptyList(), 0), TimedEvents.create(Collections.<SpanData.TimedEvent<MessageEvent>>emptyList(), 0), Links.create(Collections.<Link>emptyList(), 0), 0, status, endTimestamp);
new EqualsTester().addEqualityGroup(allSpanData1, allSpanData2).addEqualityGroup(emptySpanData).testEquals();
}
use of io.opencensus.trace.AttributeValue in project instrumentation-java by census-instrumentation.
the class RecordEventsSpanImplTest method droppingAndAddingAttributes.
@Test
public void droppingAndAddingAttributes() {
final int maxNumberOfAttributes = 8;
TraceParams traceParams = TraceParams.DEFAULT.toBuilder().setMaxNumberOfAttributes(maxNumberOfAttributes).build();
RecordEventsSpanImpl span = RecordEventsSpanImpl.startSpan(spanContext, SPAN_NAME, null, parentSpanId, false, traceParams, startEndHandler, timestampConverter, testClock);
for (int i = 0; i < 2 * maxNumberOfAttributes; i++) {
Map<String, AttributeValue> attributes = new HashMap<String, AttributeValue>();
attributes.put("MyStringAttributeKey" + i, AttributeValue.longAttributeValue(i));
span.putAttributes(attributes);
}
SpanData spanData = span.toSpanData();
assertThat(spanData.getAttributes().getDroppedAttributesCount()).isEqualTo(maxNumberOfAttributes);
assertThat(spanData.getAttributes().getAttributeMap().size()).isEqualTo(maxNumberOfAttributes);
for (int i = 0; i < maxNumberOfAttributes; i++) {
assertThat(spanData.getAttributes().getAttributeMap().get("MyStringAttributeKey" + (i + maxNumberOfAttributes))).isEqualTo(AttributeValue.longAttributeValue(i + maxNumberOfAttributes));
}
for (int i = 0; i < maxNumberOfAttributes / 2; i++) {
Map<String, AttributeValue> attributes = new HashMap<String, AttributeValue>();
attributes.put("MyStringAttributeKey" + i, AttributeValue.longAttributeValue(i));
span.putAttributes(attributes);
}
spanData = span.toSpanData();
assertThat(spanData.getAttributes().getDroppedAttributesCount()).isEqualTo(maxNumberOfAttributes * 3 / 2);
assertThat(spanData.getAttributes().getAttributeMap().size()).isEqualTo(maxNumberOfAttributes);
// Test that we still have in the attributes map the latest maxNumberOfAttributes / 2 entries.
for (int i = 0; i < maxNumberOfAttributes / 2; i++) {
assertThat(spanData.getAttributes().getAttributeMap().get("MyStringAttributeKey" + (i + maxNumberOfAttributes * 3 / 2))).isEqualTo(AttributeValue.longAttributeValue(i + maxNumberOfAttributes * 3 / 2));
}
// Test that we have the newest re-added initial entries.
for (int i = 0; i < maxNumberOfAttributes / 2; i++) {
assertThat(spanData.getAttributes().getAttributeMap().get("MyStringAttributeKey" + i)).isEqualTo(AttributeValue.longAttributeValue(i));
}
}
use of io.opencensus.trace.AttributeValue in project instrumentation-java by census-instrumentation.
the class RecordEventsSpanImplTest method droppingAttributes.
@Test
public void droppingAttributes() {
final int maxNumberOfAttributes = 8;
TraceParams traceParams = TraceParams.DEFAULT.toBuilder().setMaxNumberOfAttributes(maxNumberOfAttributes).build();
RecordEventsSpanImpl span = RecordEventsSpanImpl.startSpan(spanContext, SPAN_NAME, null, parentSpanId, false, traceParams, startEndHandler, timestampConverter, testClock);
for (int i = 0; i < 2 * maxNumberOfAttributes; i++) {
Map<String, AttributeValue> attributes = new HashMap<String, AttributeValue>();
attributes.put("MyStringAttributeKey" + i, AttributeValue.longAttributeValue(i));
span.putAttributes(attributes);
}
SpanData spanData = span.toSpanData();
assertThat(spanData.getAttributes().getDroppedAttributesCount()).isEqualTo(maxNumberOfAttributes);
assertThat(spanData.getAttributes().getAttributeMap().size()).isEqualTo(maxNumberOfAttributes);
for (int i = 0; i < maxNumberOfAttributes; i++) {
assertThat(spanData.getAttributes().getAttributeMap().get("MyStringAttributeKey" + (i + maxNumberOfAttributes))).isEqualTo(AttributeValue.longAttributeValue(i + maxNumberOfAttributes));
}
span.end();
spanData = span.toSpanData();
assertThat(spanData.getAttributes().getDroppedAttributesCount()).isEqualTo(maxNumberOfAttributes);
assertThat(spanData.getAttributes().getAttributeMap().size()).isEqualTo(maxNumberOfAttributes);
for (int i = 0; i < maxNumberOfAttributes; i++) {
assertThat(spanData.getAttributes().getAttributeMap().get("MyStringAttributeKey" + (i + maxNumberOfAttributes))).isEqualTo(AttributeValue.longAttributeValue(i + maxNumberOfAttributes));
}
}
use of io.opencensus.trace.AttributeValue 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.AttributeValue 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;
}
Aggregations