Search in sources :

Example 31 with SpanData

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");
}
Also used : SpanData(io.opencensus.trace.export.SpanData) Annotation(io.opencensus.trace.Annotation) Test(org.junit.Test)

Example 32 with SpanData

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);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) SpanData(io.opencensus.trace.export.SpanData) CancellationException(java.util.concurrent.CancellationException) CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException) Order(org.springframework.core.annotation.Order) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 33 with SpanData

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);
            }
        }
    }
}
Also used : SpanData(io.opencensus.trace.export.SpanData) Formatter(java.util.Formatter) CanonicalCode(io.opencensus.trace.Status.CanonicalCode) LatencyBucketBoundaries(io.opencensus.trace.export.SampledSpanStore.LatencyBucketBoundaries)

Example 34 with SpanData

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());
}
Also used : SpanData(io.opencensus.trace.export.SpanData) Link(io.opencensus.trace.Link) Test(org.junit.Test)

Example 35 with SpanData

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);
}
Also used : StartEndHandler(io.opencensus.implcore.trace.RecordEventsSpanImpl.StartEndHandler) SpanData(io.opencensus.trace.export.SpanData) StartEndHandlerImpl(io.opencensus.implcore.trace.StartEndHandlerImpl) ArrayList(java.util.ArrayList) SimpleEventQueue(io.opencensus.implcore.internal.SimpleEventQueue) Test(org.junit.Test)

Aggregations

SpanData (io.opencensus.trace.export.SpanData)86 Test (org.junit.Test)74 RecordEventsSpanImpl (io.opencensus.implcore.trace.RecordEventsSpanImpl)18 Link (io.opencensus.trace.Link)17 AttributeValue (io.opencensus.trace.AttributeValue)13 Collectors (java.util.stream.Collectors)13 Scope (org.apache.ignite.spi.tracing.Scope)13 TracingConfigurationCoordinates (org.apache.ignite.spi.tracing.TracingConfigurationCoordinates)13 TracingConfigurationParameters (org.apache.ignite.spi.tracing.TracingConfigurationParameters)13 TracingSpi (org.apache.ignite.spi.tracing.TracingSpi)13 IgniteEx (org.apache.ignite.internal.IgniteEx)12 SAMPLING_RATE_ALWAYS (org.apache.ignite.spi.tracing.TracingConfigurationParameters.SAMPLING_RATE_ALWAYS)12 OpenCensusTracingSpi (org.apache.ignite.spi.tracing.opencensus.OpenCensusTracingSpi)12 SpanId (io.opencensus.trace.SpanId)11 Collections (java.util.Collections)9 SpanType (org.apache.ignite.internal.processors.tracing.SpanType)9 TX (org.apache.ignite.spi.tracing.Scope.TX)9 ArrayList (java.util.ArrayList)8 Arrays (java.util.Arrays)8 Set (java.util.Set)8