Search in sources :

Example 6 with Kind

use of zipkin2.Span.Kind in project brave by openzipkin.

the class TracingRabbitListenerAdviceTest method reports_span_if_consume_fails_with_no_message.

@Test
public void reports_span_if_consume_fails_with_no_message() throws Throwable {
    Message message = MessageBuilder.withBody(new byte[] {}).build();
    onMessageConsumeFailed(message, new RuntimeException());
    assertThat(reportedSpans).extracting(Span::kind).containsExactly(CONSUMER, null);
    assertThat(reportedSpans).filteredOn(span -> span.kind() == null).extracting(Span::tags).extracting(tags -> tags.get("error")).contains("RuntimeException");
}
Also used : Tracing(brave.Tracing) MessageBuilder(org.springframework.amqp.core.MessageBuilder) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Span(zipkin2.Span) Test(org.junit.Test) MessageProperties(org.springframework.amqp.core.MessageProperties) Mockito.when(org.mockito.Mockito.when) ArrayList(java.util.ArrayList) List(java.util.List) Sampler(brave.sampler.Sampler) MethodInvocation(org.aopalliance.intercept.MethodInvocation) Message(org.springframework.amqp.core.Message) Assert.fail(org.junit.Assert.fail) CONSUMER(zipkin2.Span.Kind.CONSUMER) Before(org.junit.Before) Mockito.mock(org.mockito.Mockito.mock) Message(org.springframework.amqp.core.Message) Span(zipkin2.Span) Test(org.junit.Test)

Example 7 with Kind

use of zipkin2.Span.Kind in project brave by openzipkin.

the class V2SpanConverter method toSpan.

/**
 * Converts the input, parsing {@link Span#kind()} into RPC annotations.
 */
public static zipkin.Span toSpan(Span in) {
    String traceId = in.traceId();
    zipkin.Span.Builder result = zipkin.Span.builder().traceId(HexCodec.lowerHexToUnsignedLong(traceId)).parentId(in.parentId() != null ? HexCodec.lowerHexToUnsignedLong(in.parentId()) : null).id(HexCodec.lowerHexToUnsignedLong(in.id())).debug(in.debug()).name(// avoid a NPE
    in.name() != null ? in.name() : "");
    if (traceId.length() == 32) {
        result.traceIdHigh(HexCodec.lowerHexToUnsignedLong(traceId, 0));
    }
    long startTs = in.timestamp() == null ? 0L : in.timestamp();
    Long endTs = in.duration() == null ? 0L : in.timestamp() + in.duration();
    if (startTs != 0L) {
        result.timestamp(startTs);
        result.duration(in.duration());
    }
    zipkin.Endpoint local = in.localEndpoint() != null ? toEndpoint(in.localEndpoint()) : null;
    zipkin.Endpoint remote = in.remoteEndpoint() != null ? toEndpoint(in.remoteEndpoint()) : null;
    Kind kind = in.kind();
    Annotation cs = null, sr = null, ss = null, cr = null, ms = null, mr = null, ws = null, wr = null;
    String remoteEndpointType = null;
    boolean wroteEndpoint = false;
    for (int i = 0, length = in.annotations().size(); i < length; i++) {
        zipkin2.Annotation input = in.annotations().get(i);
        Annotation a = Annotation.create(input.timestamp(), input.value(), local);
        if (a.value.length() == 2) {
            if (a.value.equals(Constants.CLIENT_SEND)) {
                kind = Kind.CLIENT;
                cs = a;
                remoteEndpointType = SERVER_ADDR;
            } else if (a.value.equals(Constants.SERVER_RECV)) {
                kind = Kind.SERVER;
                sr = a;
                remoteEndpointType = CLIENT_ADDR;
            } else if (a.value.equals(Constants.SERVER_SEND)) {
                kind = Kind.SERVER;
                ss = a;
            } else if (a.value.equals(Constants.CLIENT_RECV)) {
                kind = Kind.CLIENT;
                cr = a;
            } else if (a.value.equals(Constants.MESSAGE_SEND)) {
                kind = Kind.PRODUCER;
                ms = a;
            } else if (a.value.equals(Constants.MESSAGE_RECV)) {
                kind = Kind.CONSUMER;
                mr = a;
            } else if (a.value.equals(Constants.WIRE_SEND)) {
                ws = a;
            } else if (a.value.equals(Constants.WIRE_RECV)) {
                wr = a;
            } else {
                wroteEndpoint = true;
                result.addAnnotation(a);
            }
        } else {
            wroteEndpoint = true;
            result.addAnnotation(a);
        }
    }
    if (kind != null) {
        switch(kind) {
            case CLIENT:
                remoteEndpointType = Constants.SERVER_ADDR;
                if (startTs != 0L)
                    cs = Annotation.create(startTs, Constants.CLIENT_SEND, local);
                if (endTs != 0L)
                    cr = Annotation.create(endTs, Constants.CLIENT_RECV, local);
                break;
            case SERVER:
                remoteEndpointType = Constants.CLIENT_ADDR;
                if (startTs != 0L)
                    sr = Annotation.create(startTs, Constants.SERVER_RECV, local);
                if (endTs != 0L)
                    ss = Annotation.create(endTs, Constants.SERVER_SEND, local);
                break;
            case PRODUCER:
                remoteEndpointType = Constants.MESSAGE_ADDR;
                if (startTs != 0L)
                    ms = Annotation.create(startTs, Constants.MESSAGE_SEND, local);
                if (endTs != 0L)
                    ws = Annotation.create(endTs, Constants.WIRE_SEND, local);
                break;
            case CONSUMER:
                remoteEndpointType = Constants.MESSAGE_ADDR;
                if (startTs != 0L && endTs != 0L) {
                    wr = Annotation.create(startTs, Constants.WIRE_RECV, local);
                    mr = Annotation.create(endTs, Constants.MESSAGE_RECV, local);
                } else if (startTs != 0L) {
                    mr = Annotation.create(startTs, Constants.MESSAGE_RECV, local);
                }
                break;
            default:
                throw new AssertionError("update kind mapping");
        }
    }
    for (Map.Entry<String, String> tag : in.tags().entrySet()) {
        wroteEndpoint = true;
        result.addBinaryAnnotation(BinaryAnnotation.create(tag.getKey(), tag.getValue(), local));
    }
    if (cs != null || sr != null || ss != null || cr != null || ws != null || wr != null || ms != null || mr != null) {
        if (cs != null)
            result.addAnnotation(cs);
        if (sr != null)
            result.addAnnotation(sr);
        if (ss != null)
            result.addAnnotation(ss);
        if (cr != null)
            result.addAnnotation(cr);
        if (ws != null)
            result.addAnnotation(ws);
        if (wr != null)
            result.addAnnotation(wr);
        if (ms != null)
            result.addAnnotation(ms);
        if (mr != null)
            result.addAnnotation(mr);
        wroteEndpoint = true;
    } else if (local != null && remote != null) {
        // special-case when we are missing core annotations, but we have both address annotations
        result.addBinaryAnnotation(BinaryAnnotation.address(CLIENT_ADDR, local));
        wroteEndpoint = true;
        remoteEndpointType = SERVER_ADDR;
    }
    if (remoteEndpointType != null && remote != null) {
        result.addBinaryAnnotation(BinaryAnnotation.address(remoteEndpointType, remote));
    }
    // don't report server-side timestamp on shared or incomplete spans
    if (Boolean.TRUE.equals(in.shared()) && sr != null) {
        result.timestamp(null).duration(null);
    }
    if (local != null && !wroteEndpoint) {
        // create a dummy annotation
        result.addBinaryAnnotation(BinaryAnnotation.create(LOCAL_COMPONENT, "", local));
    }
    return result.build();
}
Also used : Span(zipkin2.Span) Annotation(zipkin.Annotation) BinaryAnnotation(zipkin.BinaryAnnotation) Endpoint(zipkin2.Endpoint) Kind(zipkin2.Span.Kind) Map(java.util.Map)

Example 8 with Kind

use of zipkin2.Span.Kind in project spring-cloud-gcp by spring-cloud.

the class StackdriverTraceReporterTests method testSingleClientSpan.

@Test
public void testSingleClientSpan() {
    Span parent = Span.newBuilder().traceId("123").id("9999").name("http:call").timestamp(beginTime).duration(endTime - beginTime).kind(Span.Kind.CLIENT).build();
    this.reporter.report(parent);
    Assert.assertEquals(1, this.test.traceSpans.size());
    TraceSpan traceSpan = this.test.traceSpans.get(0);
    Assert.assertEquals("http:call", traceSpan.getName());
    // Client span chould use CS and CR time, not Span begin or end time.
    Assert.assertEquals(this.spanTranslator.createTimestamp(beginTime), traceSpan.getStartTime());
    Assert.assertEquals(this.spanTranslator.createTimestamp(endTime), traceSpan.getEndTime());
    Assert.assertEquals(TraceSpan.SpanKind.RPC_CLIENT, traceSpan.getKind());
}
Also used : TraceSpan(com.google.devtools.cloudtrace.v1.TraceSpan) Span(zipkin2.Span) TraceSpan(com.google.devtools.cloudtrace.v1.TraceSpan) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 9 with Kind

use of zipkin2.Span.Kind in project spring-cloud-sleuth by spring-cloud.

the class WebClientDiscoveryExceptionTests method shouldCloseSpanUponException.

// issue #240
private void shouldCloseSpanUponException(ResponseEntityProvider provider) throws IOException, InterruptedException {
    Span span = this.tracer.nextSpan().name("new trace");
    try (Tracer.SpanInScope ws = this.tracer.withSpanInScope(span.start())) {
        provider.get(this);
        Assertions.fail("should throw an exception");
    } catch (RuntimeException e) {
    } finally {
        span.finish();
    }
    // hystrix commands should finish at this point
    Thread.sleep(200);
    List<zipkin2.Span> spans = this.reporter.getSpans();
    then(spans).hasSize(2);
    then(spans.stream().filter(span1 -> span1.kind() == zipkin2.Span.Kind.CLIENT).findFirst().get().tags()).containsKey("error");
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) EnableFeignClients(org.springframework.cloud.openfeign.EnableFeignClients) RunWith(org.junit.runner.RunWith) Span(brave.Span) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) EnableDiscoveryClient(org.springframework.cloud.client.discovery.EnableDiscoveryClient) Sampler(brave.sampler.Sampler) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) RibbonClient(org.springframework.cloud.netflix.ribbon.RibbonClient) Map(java.util.Map) ArrayListSpanReporter(org.springframework.cloud.sleuth.util.ArrayListSpanReporter) Assertions(org.assertj.core.api.Assertions) Reporter(zipkin2.reporter.Reporter) RANDOM_PORT(org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT) RestTemplate(org.springframework.web.client.RestTemplate) Before(org.junit.Before) EurekaClientAutoConfiguration(org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration) Tracing(brave.Tracing) Tracer(brave.Tracer) EnableAutoConfiguration(org.springframework.boot.autoconfigure.EnableAutoConfiguration) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) IOException(java.io.IOException) Test(org.junit.Test) TestPropertySource(org.springframework.test.context.TestPropertySource) BDDAssertions.then(org.assertj.core.api.BDDAssertions.then) Configuration(org.springframework.context.annotation.Configuration) LoadBalanced(org.springframework.cloud.client.loadbalancer.LoadBalanced) List(java.util.List) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) TraceWebServletAutoConfiguration(org.springframework.cloud.sleuth.instrument.web.TraceWebServletAutoConfiguration) ResponseEntity(org.springframework.http.ResponseEntity) Bean(org.springframework.context.annotation.Bean) FeignClient(org.springframework.cloud.openfeign.FeignClient) Tracer(brave.Tracer) Span(brave.Span)

Example 10 with Kind

use of zipkin2.Span.Kind in project spring-cloud-sleuth by spring-cloud.

the class FeignRetriesTests method testRetriedWhenRequestEventuallyIsSent.

@Test
public void testRetriedWhenRequestEventuallyIsSent() throws Exception {
    String url = "http://localhost:" + server.getPort();
    final AtomicInteger atomicInteger = new AtomicInteger();
    // Client to simulate a retry scenario
    final Client client = (request, options) -> {
        // we simulate an exception only for the first request
        if (atomicInteger.get() == 1) {
            throw new IOException();
        } else {
            // with the second retry (first retry) we send back good result
            return Response.builder().status(200).reason("OK").headers(new HashMap<>()).body("OK", Charset.defaultCharset()).build();
        }
    };
    TestInterface api = Feign.builder().client(new TracingFeignClient(this.httpTracing, new Client() {

        @Override
        public Response execute(Request request, Request.Options options) throws IOException {
            atomicInteger.incrementAndGet();
            return client.execute(request, options);
        }
    })).target(TestInterface.class, url);
    then(api.decodedPost()).isEqualTo("OK");
    // request interception should take place only twice (1st request & 2nd retry)
    then(atomicInteger.get()).isEqualTo(2);
    then(this.reporter.getSpans().get(0).tags()).containsEntry("error", "IOException");
    then(this.reporter.getSpans().get(1).kind().ordinal()).isEqualTo(Span.Kind.CLIENT.ordinal());
}
Also used : Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) Span(zipkin2.Span) HashMap(java.util.HashMap) Client(feign.Client) ErrorParser(org.springframework.cloud.sleuth.ErrorParser) CurrentTraceContext(brave.propagation.CurrentTraceContext) ExceptionMessageErrorParser(org.springframework.cloud.sleuth.ExceptionMessageErrorParser) Charset(java.nio.charset.Charset) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) ArrayListSpanReporter(org.springframework.cloud.sleuth.util.ArrayListSpanReporter) MockWebServer(okhttp3.mockwebserver.MockWebServer) FeignException(feign.FeignException) Before(org.junit.Before) Tracing(brave.Tracing) HttpTracing(brave.http.HttpTracing) Response(feign.Response) SleuthHttpParserAccessor(org.springframework.cloud.sleuth.instrument.web.SleuthHttpParserAccessor) RequestLine(feign.RequestLine) IOException(java.io.IOException) Test(org.junit.Test) BDDAssertions.then(org.assertj.core.api.BDDAssertions.then) Feign(feign.Feign) BDDMockito(org.mockito.BDDMockito) Rule(org.junit.Rule) BeanFactory(org.springframework.beans.factory.BeanFactory) Request(feign.Request) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Assertions.failBecauseExceptionWasNotThrown(org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown) Response(feign.Response) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashMap(java.util.HashMap) Request(feign.Request) IOException(java.io.IOException) Client(feign.Client) Test(org.junit.Test)

Aggregations

Span (zipkin2.Span)82 Test (org.junit.Test)54 Endpoint (zipkin2.Endpoint)25 Test (org.junit.jupiter.api.Test)17 V1Span (zipkin2.v1.V1Span)10 TraceSpan (com.google.devtools.cloudtrace.v1.TraceSpan)8 IOException (java.io.IOException)6 Map (java.util.Map)5 TestObjects.newClientSpan (zipkin2.TestObjects.newClientSpan)5 Tracer (brave.Tracer)4 Tracing (brave.Tracing)4 Trace (com.google.devtools.cloudtrace.v1.Trace)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 Before (org.junit.Before)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 Span (brave.Span)3 Sampler (brave.sampler.Sampler)3 Annotation (zipkin2.Annotation)3