use of io.opencensus.trace.export.SpanData in project instrumentation-java by census-instrumentation.
the class UrlInstrumentationIT method getContent_fails.
@Test(timeout = 60000)
public void getContent_fails() throws MalformedURLException {
URL url = new URL("file:///nonexistent");
try {
url.getContent();
fail();
} catch (IOException e) {
SpanData span = testHandler.waitForExport(1).get(0);
assertThat(span.getName()).isEqualTo("java.net.URL#getContent");
assertThat(span.getStatus().isOk()).isFalse();
}
}
use of io.opencensus.trace.export.SpanData in project instrumentation-java by census-instrumentation.
the class RecordEventsSpanImplTest method droppingLinks.
@Test
public void droppingLinks() {
final int maxNumberOfLinks = 8;
TraceParams traceParams = TraceParams.DEFAULT.toBuilder().setMaxNumberOfLinks(maxNumberOfLinks).build();
RecordEventsSpanImpl span = RecordEventsSpanImpl.startSpan(spanContext, SPAN_NAME, null, parentSpanId, false, traceParams, startEndHandler, timestampConverter, testClock);
Link link = Link.fromSpanContext(spanContext, Link.Type.CHILD_LINKED_SPAN);
for (int i = 0; i < 2 * maxNumberOfLinks; i++) {
span.addLink(link);
}
SpanData spanData = span.toSpanData();
assertThat(spanData.getLinks().getDroppedLinksCount()).isEqualTo(maxNumberOfLinks);
assertThat(spanData.getLinks().getLinks().size()).isEqualTo(maxNumberOfLinks);
for (int i = 0; i < maxNumberOfLinks; i++) {
assertThat(spanData.getLinks().getLinks().get(i)).isEqualTo(link);
}
span.end();
spanData = span.toSpanData();
assertThat(spanData.getLinks().getDroppedLinksCount()).isEqualTo(maxNumberOfLinks);
assertThat(spanData.getLinks().getLinks().size()).isEqualTo(maxNumberOfLinks);
for (int i = 0; i < maxNumberOfLinks; i++) {
assertThat(spanData.getLinks().getLinks().get(i)).isEqualTo(link);
}
}
use of io.opencensus.trace.export.SpanData 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.export.SpanData in project instrumentation-java by census-instrumentation.
the class RecordEventsSpanImplTest method toSpanData_EndedSpan.
@Test
public void toSpanData_EndedSpan() {
RecordEventsSpanImpl span = RecordEventsSpanImpl.startSpan(spanContext, SPAN_NAME, null, parentSpanId, false, TraceParams.DEFAULT, startEndHandler, timestampConverter, testClock);
Mockito.verify(startEndHandler, Mockito.times(1)).onStart(span);
span.putAttribute("MySingleStringAttributeKey", AttributeValue.stringAttributeValue("MySingleStringAttributeValue"));
span.putAttributes(attributes);
testClock.advanceTime(Duration.create(0, 100));
span.addAnnotation(Annotation.fromDescription(ANNOTATION_DESCRIPTION));
testClock.advanceTime(Duration.create(0, 100));
span.addAnnotation(ANNOTATION_DESCRIPTION, attributes);
testClock.advanceTime(Duration.create(0, 100));
NetworkEvent networkEvent = NetworkEvent.builder(NetworkEvent.Type.RECV, 1).setUncompressedMessageSize(3).build();
span.addNetworkEvent(networkEvent);
Link link = Link.fromSpanContext(spanContext, Link.Type.CHILD_LINKED_SPAN);
span.addLink(link);
testClock.advanceTime(Duration.create(0, 100));
span.end(EndSpanOptions.builder().setStatus(Status.CANCELLED).build());
Mockito.verify(startEndHandler, Mockito.times(1)).onEnd(span);
SpanData spanData = span.toSpanData();
assertThat(spanData.getContext()).isEqualTo(spanContext);
assertThat(spanData.getName()).isEqualTo(SPAN_NAME);
assertThat(spanData.getParentSpanId()).isEqualTo(parentSpanId);
assertThat(spanData.getHasRemoteParent()).isFalse();
assertThat(spanData.getAttributes().getDroppedAttributesCount()).isEqualTo(0);
assertThat(spanData.getAttributes().getAttributeMap()).isEqualTo(expectedAttributes);
assertThat(spanData.getAnnotations().getDroppedEventsCount()).isEqualTo(0);
assertThat(spanData.getAnnotations().getEvents().size()).isEqualTo(2);
assertThat(spanData.getAnnotations().getEvents().get(0).getTimestamp()).isEqualTo(timestamp.addNanos(100));
assertThat(spanData.getAnnotations().getEvents().get(0).getEvent()).isEqualTo(Annotation.fromDescription(ANNOTATION_DESCRIPTION));
assertThat(spanData.getAnnotations().getEvents().get(1).getTimestamp()).isEqualTo(timestamp.addNanos(200));
assertThat(spanData.getAnnotations().getEvents().get(1).getEvent()).isEqualTo(Annotation.fromDescriptionAndAttributes(ANNOTATION_DESCRIPTION, attributes));
assertThat(spanData.getNetworkEvents().getDroppedEventsCount()).isEqualTo(0);
assertThat(spanData.getNetworkEvents().getEvents().size()).isEqualTo(1);
assertThat(spanData.getNetworkEvents().getEvents().get(0).getTimestamp()).isEqualTo(timestamp.addNanos(300));
assertThat(spanData.getNetworkEvents().getEvents().get(0).getEvent()).isEqualTo(networkEvent);
assertThat(spanData.getLinks().getDroppedLinksCount()).isEqualTo(0);
assertThat(spanData.getLinks().getLinks().size()).isEqualTo(1);
assertThat(spanData.getLinks().getLinks().get(0)).isEqualTo(link);
assertThat(spanData.getStartTimestamp()).isEqualTo(timestamp);
assertThat(spanData.getStatus()).isEqualTo(Status.CANCELLED);
assertThat(spanData.getEndTimestamp()).isEqualTo(timestamp.addNanos(400));
}
use of io.opencensus.trace.export.SpanData in project instrumentation-java by census-instrumentation.
the class RecordEventsSpanImplTest method noEventsRecordedAfterEnd.
@Test
public void noEventsRecordedAfterEnd() {
RecordEventsSpanImpl span = RecordEventsSpanImpl.startSpan(spanContext, SPAN_NAME, null, parentSpanId, false, TraceParams.DEFAULT, startEndHandler, timestampConverter, testClock);
span.end();
// Check that adding trace events after Span#end() does not throw any exception and are not
// recorded.
span.putAttributes(attributes);
span.putAttribute("MySingleStringAttributeKey", AttributeValue.stringAttributeValue("MySingleStringAttributeValue"));
span.addAnnotation(Annotation.fromDescription(ANNOTATION_DESCRIPTION));
span.addAnnotation(ANNOTATION_DESCRIPTION, attributes);
span.addNetworkEvent(NetworkEvent.builder(NetworkEvent.Type.RECV, 1).setUncompressedMessageSize(3).build());
span.addLink(Link.fromSpanContext(spanContext, Link.Type.CHILD_LINKED_SPAN));
SpanData spanData = span.toSpanData();
assertThat(spanData.getStartTimestamp()).isEqualTo(timestamp);
assertThat(spanData.getAttributes().getAttributeMap()).isEmpty();
assertThat(spanData.getAnnotations().getEvents()).isEmpty();
assertThat(spanData.getNetworkEvents().getEvents()).isEmpty();
assertThat(spanData.getLinks().getLinks()).isEmpty();
assertThat(spanData.getStatus()).isEqualTo(Status.OK);
assertThat(spanData.getEndTimestamp()).isEqualTo(timestamp);
}
Aggregations