use of io.opencensus.trace.export.SpanData in project instrumentation-java by census-instrumentation.
the class CensusSpringAspectTest method handlesException.
@Test
public void handlesException() {
// When
Sample sample = (Sample) context.getBean("sample");
try {
sample.boom();
} catch (Exception ignored) {
// ok
}
// Then
List<SpanData> spanList = handler.waitForExport(1);
assertThat(spanList).isNotNull();
assertThat(spanList.size()).isEqualTo(1);
SpanData spanData = spanList.get(0);
assertThat(spanData.getName()).isEqualTo("boom");
assertThat(spanData.getStatus()).isEqualTo(Status.UNKNOWN);
SpanData.TimedEvents<Annotation> annotations = spanData.getAnnotations();
assertThat(annotations).isNotNull();
List<SpanData.TimedEvent<Annotation>> events = annotations.getEvents();
assertThat(events.size()).isEqualTo(1);
assertThat(events.get(0).getEvent().getDescription()).isEqualTo("error");
}
use of io.opencensus.trace.export.SpanData in project instrumentation-java by census-instrumentation.
the class TraceWebAsyncClientAutoConfigurationTest method should_close_span_upon_failure_callback.
@Test(timeout = 10000)
@Order(2)
public void should_close_span_upon_failure_callback() {
boolean exceptionOccured = false;
final ListenableFuture<ResponseEntity<String>> future;
try {
future = asyncRestTemplate.getForEntity("http://localhost:" + port() + "/fail", String.class);
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(100);
} catch (Exception e) {
System.out.println("exception " + e);
}
future.cancel(true);
}
}).start();
future.get(500, TimeUnit.MILLISECONDS);
} catch (CancellationException e) {
assertThat(e).isInstanceOf(CancellationException.class);
exceptionOccured = true;
} catch (Exception e) {
Assert.fail("unexpected exception:" + e);
}
assertThat(exceptionOccured).isTrue();
List<SpanData> spans = handler.waitForExport(1);
System.out.println("Spans " + spans.toString());
SpanData span = spans.get(0);
assertThat(span.getName()).isEqualTo("/fail");
assertThat(span.getStatus().isOk()).isFalse();
assertThat(span.getAttributes().getAttributeMap().get(HttpTraceAttributeConstants.HTTP_METHOD)).isEqualTo(AttributeValue.stringAttributeValue("GET"));
assertThat(span.getAttributes().getAttributeMap().get(HttpTraceAttributeConstants.HTTP_HOST)).isEqualTo(AttributeValue.stringAttributeValue("localhost"));
assertThat(span.getAttributes().getAttributeMap().get(HttpTraceAttributeConstants.HTTP_PATH)).isEqualTo(AttributeValue.stringAttributeValue("/fail"));
assertThat(span.getAttributes().getAttributeMap().get(HttpTraceAttributeConstants.HTTP_STATUS_CODE)).isEqualTo(AttributeValue.longAttributeValue(0));
assertThat(span.getKind()).isEqualTo(CLIENT);
}
use of io.opencensus.trace.export.SpanData in project instrumentation-java by census-instrumentation.
the class TracezZPageHandler method emitHtmlBody.
private void emitHtmlBody(Map<String, String> queryMap, PrintWriter out) throws UnsupportedEncodingException {
if (runningSpanStore == null || sampledSpanStore == null) {
out.write("OpenCensus implementation not available.");
return;
}
Formatter formatter = new Formatter(out, Locale.US);
emitSummaryTable(out, formatter);
String spanName = queryMap.get(HEADER_SPAN_NAME);
if (spanName != null) {
tracer.getCurrentSpan().addAnnotation("Render spans.", ImmutableMap.<String, AttributeValue>builder().put("SpanName", AttributeValue.stringAttributeValue(spanName)).build());
String typeStr = queryMap.get(HEADER_SAMPLES_TYPE);
if (typeStr != null) {
List<SpanData> spans = null;
RequestType type = RequestType.fromString(typeStr);
if (type == RequestType.UNKNOWN) {
return;
}
if (type == RequestType.RUNNING) {
// Display running.
spans = new ArrayList<>(runningSpanStore.getRunningSpans(RunningSpanStore.Filter.create(spanName, 0)));
// Sort active spans incremental.
Collections.sort(spans, new SpanDataComparator(/* incremental= */
true));
} else {
String subtypeStr = queryMap.get(HEADER_SAMPLES_SUB_TYPE);
if (subtypeStr != null) {
int subtype = Integer.parseInt(subtypeStr);
if (type == RequestType.FAILED) {
if (subtype < 0 || subtype >= CanonicalCode.values().length) {
return;
}
// Display errors. subtype 0 means all.
CanonicalCode code = subtype == 0 ? null : CanonicalCode.values()[subtype];
spans = new ArrayList<>(sampledSpanStore.getErrorSampledSpans(ErrorFilter.create(spanName, code, 0)));
} else {
if (subtype < 0 || subtype >= LatencyBucketBoundaries.values().length) {
return;
}
// Display latency.
LatencyBucketBoundaries latencyBucketBoundaries = LatencyBucketBoundaries.values()[subtype];
spans = new ArrayList<>(sampledSpanStore.getLatencySampledSpans(LatencyFilter.create(spanName, latencyBucketBoundaries.getLatencyLowerNs(), latencyBucketBoundaries.getLatencyUpperNs(), 0)));
// Sort sampled spans decremental.
Collections.sort(spans, new SpanDataComparator(/* incremental= */
false));
}
}
}
emitSpanNameAndCountPages(formatter, spanName, spans == null ? 0 : spans.size(), type);
if (spans != null) {
emitSpans(out, formatter, spans);
emitLegend(out);
}
}
}
}
use of io.opencensus.trace.export.SpanData in project instrumentation-java by census-instrumentation.
the class ZipkinExporterHandlerTest method generateSpan_NoKindAndRemoteParent.
@Test
public void generateSpan_NoKindAndRemoteParent() {
SpanData data = SpanData.create(SpanContext.create(TraceId.fromLowerBase16(TRACE_ID), SpanId.fromLowerBase16(SPAN_ID), TraceOptions.builder().setIsSampled(true).build()), // TODO SpanId.fromLowerBase16
SpanId.fromLowerBase16(PARENT_SPAN_ID), true, /* hasRemoteParent */
"Recv.helloworld.Greeter.SayHello", /* name */
null, /* kind */
Timestamp.create(1505855794, 194009601), /* startTimestamp */
Attributes.create(attributes, 0), TimedEvents.create(annotations, 0), TimedEvents.create(messageEvents, 0), Links.create(Collections.<Link>emptyList(), 0), null, /* childSpanCount */
Status.OK, Timestamp.create(1505855799, 465726528));
assertThat(ZipkinExporterHandler.generateSpan(data, localEndpoint)).isEqualTo(Span.newBuilder().traceId(TRACE_ID).parentId(PARENT_SPAN_ID).id(SPAN_ID).kind(Span.Kind.SERVER).name(data.getName()).timestamp(1505855794000000L + 194009601L / 1000).duration((1505855799000000L + 465726528L / 1000) - (1505855794000000L + 194009601L / 1000)).localEndpoint(localEndpoint).addAnnotation(1505855799000000L + 433901068L / 1000, "RECEIVED").addAnnotation(1505855799000000L + 459486280L / 1000, "SENT").putTag(ZipkinExporterHandler.STATUS_CODE, "OK").build());
}
use of io.opencensus.trace.export.SpanData in project instrumentation-java by census-instrumentation.
the class SpanExporterImplTest method exportMoreSpansThanTheMaximumLimit.
@Test
public void exportMoreSpansThanTheMaximumLimit() {
final int bufferSize = 4;
final int maxReferencedSpans = bufferSize * 4;
SpanExporterImpl spanExporter = SpanExporterImpl.create(bufferSize, Duration.create(1, 0));
StartEndHandler startEndHandler = new StartEndHandlerImpl(spanExporter, runningSpanStore, sampledSpanStore, new SimpleEventQueue());
BlockingExporter blockingExporter = new BlockingExporter();
spanExporter.registerHandler("test.service", serviceHandler);
spanExporter.registerHandler("test.blocking", blockingExporter);
List<SpanData> spansToExport = new ArrayList<>(maxReferencedSpans);
for (int i = 0; i < maxReferencedSpans; i++) {
spansToExport.add(createSampledEndedSpan(startEndHandler, "span_1_" + i).toSpanData());
}
assertThat(spanExporter.getReferencedSpans()).isEqualTo(maxReferencedSpans);
// Now we should start dropping.
for (int i = 0; i < 7; i++) {
createSampledEndedSpan(startEndHandler, "span_2_" + i);
assertThat(spanExporter.getDroppedSpans()).isEqualTo(i + 1);
}
assertThat(spanExporter.getReferencedSpans()).isEqualTo(maxReferencedSpans);
// Release the blocking exporter
blockingExporter.unblock();
List<SpanData> exported = serviceHandler.waitForExport(maxReferencedSpans);
assertThat(exported).isNotNull();
assertThat(exported).containsExactlyElementsIn(spansToExport);
exported.clear();
spansToExport.clear();
// We cannot compare with maxReferencedSpans here because the worker thread may get
// unscheduled immediately after exporting, but before updating the pushed spans, if that is
// the case at most bufferSize spans will miss.
assertThat(spanExporter.getPushedSpans()).isAtLeast((long) maxReferencedSpans - bufferSize);
for (int i = 0; i < 7; i++) {
spansToExport.add(createSampledEndedSpan(startEndHandler, "span_3_" + i).toSpanData());
// No more dropped spans.
assertThat(spanExporter.getDroppedSpans()).isEqualTo(7);
}
exported = serviceHandler.waitForExport(7);
assertThat(exported).isNotNull();
assertThat(exported).containsExactlyElementsIn(spansToExport);
}
Aggregations