Search in sources :

Example 21 with SpanInScope

use of brave.Tracer.SpanInScope in project brave by openzipkin.

the class TracingMessageProducer method send.

/* @Override JMS 2.0 method: Intentionally no override to ensure JMS 1.1 works! */
@JMS2_0
public void send(Message message, CompletionListener completionListener) throws JMSException {
    Destination destination = destination(message);
    Span span = createAndStartProducerSpan(message, destination);
    // animal-sniffer mistakes this for AutoCloseable
    SpanInScope ws = tracer.withSpanInScope(span);
    Throwable error = null;
    try {
        delegate.send(message, TracingCompletionListener.create(completionListener, destination, span, current));
    } catch (Throwable t) {
        propagateIfFatal(t);
        error = t;
        throw t;
    } finally {
        if (error != null)
            span.error(error).finish();
        ws.close();
    }
}
Also used : Destination(javax.jms.Destination) SpanInScope(brave.Tracer.SpanInScope) Span(brave.Span)

Example 22 with SpanInScope

use of brave.Tracer.SpanInScope in project brave by openzipkin.

the class TracingMessageProducer method send.

@Override
public void send(Destination destination, Message message, int deliveryMode, int priority, long timeToLive) throws JMSException {
    Span span = createAndStartProducerSpan(message, destination);
    SpanInScope ws = tracer.withSpanInScope(span);
    Throwable error = null;
    try {
        delegate.send(destination, message, deliveryMode, priority, timeToLive);
    } catch (Throwable t) {
        propagateIfFatal(t);
        error = t;
        throw t;
    } finally {
        if (error != null)
            span.error(error);
        span.finish();
        ws.close();
    }
}
Also used : SpanInScope(brave.Tracer.SpanInScope) Span(brave.Span)

Example 23 with SpanInScope

use of brave.Tracer.SpanInScope in project cxf by apache.

the class AbstractBraveProvider method startTraceSpan.

protected TraceScopeHolder<TraceScope> startTraceSpan(final Map<String, List<String>> requestHeaders, URI uri, String method) {
    final Request request = HttpAdapterFactory.request(requestHeaders, uri, method);
    final HttpServerAdapter<Request, ?> adapter = HttpServerAdapterFactory.create(request);
    final HttpServerHandler<Request, ?> handler = HttpServerHandler.create(brave, adapter);
    Span span = handler.handleReceive(brave.tracing().propagation().extractor(adapter::requestHeader), request);
    // If the service resource is using asynchronous processing mode, the trace
    // scope will be closed in another thread and as such should be detached.
    SpanInScope scope = null;
    if (isAsyncResponse() && span != null) {
        // Do not modify the current context span
        propagateContinuationSpan(span);
    } else if (span != null) {
        scope = brave.tracing().tracer().withSpanInScope(span);
    }
    return new TraceScopeHolder<TraceScope>(new TraceScope(span, scope), scope == null);
}
Also used : SpanInScope(brave.Tracer.SpanInScope) Request(org.apache.cxf.tracing.brave.internal.HttpAdapterFactory.Request) Span(brave.Span)

Example 24 with SpanInScope

use of brave.Tracer.SpanInScope in project cxf by apache.

the class BraveTracingTest method testThatProvidedSpanIsNotDetachedWhenActiveUsingAsyncClient.

@Test
public void testThatProvidedSpanIsNotDetachedWhenActiveUsingAsyncClient() throws Exception {
    final WebClient client = createWebClient("/bookstore/books", new BraveClientProvider(brave));
    final Span span = brave.tracer().nextSpan().name("test span").start();
    try (SpanInScope scope = brave.tracer().withSpanInScope(span)) {
        final Future<Response> f = client.async().get();
        final Response r = f.get(1, TimeUnit.SECONDS);
        assertEquals(Status.OK.getStatusCode(), r.getStatus());
        assertThat(brave.tracer().currentSpan().context().spanId(), equalTo(span.context().spanId()));
        assertThat(TestSpanReporter.getAllSpans().size(), equalTo(3));
        assertThat(TestSpanReporter.getAllSpans().get(0).name(), equalTo("get books"));
        assertThat(TestSpanReporter.getAllSpans().get(1).name(), equalTo("get /bookstore/books"));
        assertThat(TestSpanReporter.getAllSpans().get(2).name(), equalTo("get " + client.getCurrentURI()));
    } finally {
        span.finish();
    }
    // Await till flush happens, usually a second is enough
    await().atMost(Duration.ofSeconds(1L)).until(() -> TestSpanReporter.getAllSpans().size() == 4);
    assertThat(TestSpanReporter.getAllSpans().size(), equalTo(4));
    assertThat(TestSpanReporter.getAllSpans().get(3).name(), equalTo("test span"));
}
Also used : Response(javax.ws.rs.core.Response) SpanInScope(brave.Tracer.SpanInScope) BraveClientProvider(org.apache.cxf.tracing.brave.jaxrs.BraveClientProvider) WebClient(org.apache.cxf.jaxrs.client.WebClient) Span(brave.Span) HasSpan.hasSpan(org.apache.cxf.systest.jaxrs.tracing.brave.HasSpan.hasSpan) Test(org.junit.Test)

Example 25 with SpanInScope

use of brave.Tracer.SpanInScope in project cxf by apache.

the class BraveTracingTest method testThatProvidedSpanIsNotClosedWhenActive.

@Test
public void testThatProvidedSpanIsNotClosedWhenActive() throws MalformedURLException {
    final WebClient client = createWebClient("/bookstore/books", new BraveClientProvider(brave));
    final Span span = brave.tracer().nextSpan().name("test span").start();
    try (SpanInScope scope = brave.tracer().withSpanInScope(span)) {
        final Response r = client.get();
        assertEquals(Status.OK.getStatusCode(), r.getStatus());
        assertThat(TestSpanReporter.getAllSpans().size(), equalTo(3));
        assertThat(TestSpanReporter.getAllSpans().get(0).name(), equalTo("get books"));
        assertThat(TestSpanReporter.getAllSpans().get(0).parentId(), not(nullValue()));
        assertThat(TestSpanReporter.getAllSpans().get(1).name(), equalTo("get /bookstore/books"));
        assertThat(TestSpanReporter.getAllSpans().get(2).name(), equalTo("get " + client.getCurrentURI()));
    } finally {
        span.finish();
    }
    // Await till flush happens, usually a second is enough
    await().atMost(Duration.ofSeconds(1L)).until(() -> TestSpanReporter.getAllSpans().size() == 4);
    assertThat(TestSpanReporter.getAllSpans().size(), equalTo(4));
    assertThat(TestSpanReporter.getAllSpans().get(3).name(), equalTo("test span"));
}
Also used : Response(javax.ws.rs.core.Response) SpanInScope(brave.Tracer.SpanInScope) BraveClientProvider(org.apache.cxf.tracing.brave.jaxrs.BraveClientProvider) WebClient(org.apache.cxf.jaxrs.client.WebClient) Span(brave.Span) HasSpan.hasSpan(org.apache.cxf.systest.jaxrs.tracing.brave.HasSpan.hasSpan) Test(org.junit.Test)

Aggregations

SpanInScope (brave.Tracer.SpanInScope)55 Span (brave.Span)34 Test (org.junit.Test)23 MutableSpan (brave.handler.MutableSpan)13 MockResponse (okhttp3.mockwebserver.MockResponse)4 Tracer (brave.Tracer)3 TopicPublisher (javax.jms.TopicPublisher)3 TraceContextOrSamplingFlags (brave.propagation.TraceContextOrSamplingFlags)2 RequestContext (com.netflix.zuul.context.RequestContext)2 Map (java.util.Map)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CyclicBarrier (java.util.concurrent.CyclicBarrier)2 Destination (javax.jms.Destination)2 Response (javax.ws.rs.core.Response)2 WebClient (org.apache.cxf.jaxrs.client.WebClient)2 HasSpan.hasSpan (org.apache.cxf.systest.jaxrs.tracing.brave.HasSpan.hasSpan)2 Request (org.apache.cxf.tracing.brave.internal.HttpAdapterFactory.Request)2 BraveClientProvider (org.apache.cxf.tracing.brave.jaxrs.BraveClientProvider)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)2 Tracing (brave.Tracing)1