Search in sources :

Example 46 with Tracer

use of io.opentracing.Tracer in project Payara by payara.

the class InvocationContext method saveTracingContext.

private void saveTracingContext() {
    ServiceLocator serviceLocator = Globals.getDefaultBaseServiceLocator();
    if (serviceLocator != null) {
        RequestTracingService requestTracing = serviceLocator.getService(RequestTracingService.class);
        OpenTracingService openTracing = serviceLocator.getService(OpenTracingService.class);
        // Check that there's actually a trace running
        if (requestTracing != null && requestTracing.isRequestTracingEnabled() && requestTracing.isTraceInProgress() && openTracing != null) {
            Tracer tracer = openTracing.getTracer(openTracing.getApplicationName(serviceLocator.getService(InvocationManager.class)));
            SpanContext spanContext = null;
            // Check if there's an active Span running
            Span activeSpan = tracer.activeSpan();
            if (activeSpan != null) {
                // The traceId is likely incorrect at this point as it initialises as a random UUID
                try {
                    ((RequestTraceSpan) activeSpan).setTraceId(requestTracing.getConversationID());
                } catch (ClassCastException cce) {
                    Logger.getLogger(InvocationContext.class).log(Level.FINE, "ClassCastException caught converting Span", cce);
                }
                spanContext = activeSpan.context();
            } else {
                // Create a new span context using the starting span as a parent - the request tracing service doesn't
                // know about unfinished spans so we can't get the actual parent with the current impl
                spanContext = new RequestTraceSpanContext(requestTracing.getConversationID(), requestTracing.getStartingTraceID());
            }
            // Check to see if we're using the mock tracer to prevent ClassCastExceptions
            try {
                tracer.inject(spanContext, Format.Builtin.TEXT_MAP, new MapToTextMap(spanContextMap = new HashMap()));
            } catch (ClassCastException cce) {
                Logger.getLogger(InvocationContext.class).log(Level.FINE, "ClassCastException caught injecting SpanContext", cce);
            }
        }
    }
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) RequestTraceSpanContext(fish.payara.notification.requesttracing.RequestTraceSpanContext) RequestTraceSpanContext(fish.payara.notification.requesttracing.RequestTraceSpanContext) SpanContext(io.opentracing.SpanContext) HashMap(java.util.HashMap) Tracer(io.opentracing.Tracer) RequestTraceSpan(fish.payara.notification.requesttracing.RequestTraceSpan) OpenTracingService(fish.payara.opentracing.OpenTracingService) MapToTextMap(fish.payara.opentracing.propagation.MapToTextMap) RequestTraceSpan(fish.payara.notification.requesttracing.RequestTraceSpan) Span(io.opentracing.Span) RequestTracingService(fish.payara.nucleus.requesttracing.RequestTracingService)

Example 47 with Tracer

use of io.opentracing.Tracer in project Payara by payara.

the class JaxrsClientRequestTracingFilter method filter.

@Override
public void filter(ClientRequestContext requestContext) throws IOException {
    final PayaraTracingServices payaraTracingServices = new PayaraTracingServices();
    final RequestTracingService requestTracing = payaraTracingServices.getRequestTracingService();
    if (requestTracing != null && requestTracing.isRequestTracingEnabled()) {
        // If there is a trace in progress, add the propagation headers with the relevant details
        if (requestTracing.isTraceInProgress()) {
            // Check that we aren't overwriting a header
            if (!requestContext.getHeaders().containsKey(PropagationHeaders.PROPAGATED_TRACE_ID)) {
                requestContext.getHeaders().add(PropagationHeaders.PROPAGATED_TRACE_ID, requestTracing.getConversationID());
            }
            // Check that we aren't overwriting a header
            if (!requestContext.getHeaders().containsKey(PropagationHeaders.PROPAGATED_PARENT_ID)) {
                requestContext.getHeaders().add(PropagationHeaders.PROPAGATED_PARENT_ID, requestTracing.getStartingTraceID());
            }
            // Check that we aren't overwriting a relationship type
            if (!requestContext.getHeaders().containsKey(PropagationHeaders.PROPAGATED_RELATIONSHIP_TYPE)) {
                if (requestContext.getMethod().equals("POST")) {
                    requestContext.getHeaders().add(PropagationHeaders.PROPAGATED_RELATIONSHIP_TYPE, RequestTraceSpan.SpanContextRelationshipType.FollowsFrom);
                } else {
                    requestContext.getHeaders().add(PropagationHeaders.PROPAGATED_RELATIONSHIP_TYPE, RequestTraceSpan.SpanContextRelationshipType.ChildOf);
                }
            }
        }
        // Check if we should trace this client call
        if (shouldTrace(requestContext)) {
            // Get or create the tracer instance for this application
            final Tracer tracer = payaraTracingServices.getActiveTracer();
            // Build a span with the required MicroProfile Opentracing tags
            SpanBuilder spanBuilder = tracer.buildSpan(requestContext.getMethod()).withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT).withTag(Tags.HTTP_METHOD.getKey(), requestContext.getMethod()).withTag(Tags.HTTP_URL.getKey(), requestContext.getUri().toURL().toString()).withTag(Tags.COMPONENT.getKey(), "jaxrs");
            // Get the propagated span context from the request if present
            // This is required to account for asynchronous client requests
            SpanContext parentSpanContext = (SpanContext) requestContext.getProperty(PropagationHeaders.OPENTRACING_PROPAGATED_SPANCONTEXT);
            if (parentSpanContext != null) {
                spanBuilder.asChildOf(parentSpanContext);
            } else {
                parentSpanContext = SpanPropagator.propagatedContext();
                if (parentSpanContext != null) {
                    spanBuilder.asChildOf(parentSpanContext);
                }
            }
            // If there is a propagated span context, set it as a parent of the new span
            parentSpanContext = tracer.extract(Format.Builtin.HTTP_HEADERS, new MultivaluedMapToTextMap(requestContext.getHeaders()));
            if (parentSpanContext != null) {
                spanBuilder.asChildOf(parentSpanContext);
            }
            // Start the span and mark it as active
            Span span = spanBuilder.start();
            Scope scope = tracer.activateSpan(span);
            requestContext.setProperty(Scope.class.getName(), scope);
            // Inject the active span context for propagation
            tracer.inject(span.context(), Format.Builtin.HTTP_HEADERS, new MultivaluedMapToTextMap(requestContext.getHeaders()));
        }
    }
}
Also used : SpanBuilder(io.opentracing.Tracer.SpanBuilder) SpanContext(io.opentracing.SpanContext) Scope(io.opentracing.Scope) Tracer(io.opentracing.Tracer) RequestTraceSpan(fish.payara.notification.requesttracing.RequestTraceSpan) Span(io.opentracing.Span) RequestTracingService(fish.payara.nucleus.requesttracing.RequestTracingService)

Example 48 with Tracer

use of io.opentracing.Tracer in project opentracing-java by opentracing.

the class GlobalTracerTest method testGet_SingletonReference.

@Test
public void testGet_SingletonReference() {
    Tracer tracer = GlobalTracer.get();
    assertThat(tracer, is(instanceOf(GlobalTracer.class)));
    assertThat(tracer, is(sameInstance(GlobalTracer.get())));
}
Also used : Tracer(io.opentracing.Tracer) Test(org.junit.Test)

Example 49 with Tracer

use of io.opentracing.Tracer in project opentracing-java by opentracing.

the class GlobalTracerTest method testDelegation_extract.

@Test
public void testDelegation_extract() {
    Tracer mockTracer = mock(Tracer.class);
    Format<Object> mockFormat = mock(Format.class);
    Object mockCarrier = mock(Object.class);
    GlobalTracer.register(mockTracer);
    GlobalTracer.get().extract(mockFormat, mockCarrier);
    verify(mockTracer).extract(eq(mockFormat), eq(mockCarrier));
    verifyNoMoreInteractions(mockTracer, mockFormat, mockCarrier);
}
Also used : Tracer(io.opentracing.Tracer) Test(org.junit.Test)

Example 50 with Tracer

use of io.opentracing.Tracer in project opentracing-java by opentracing.

the class GlobalTracerTest method concurrencyTest.

@Test
public void concurrencyTest() throws InterruptedException, ExecutionException {
    final int threadCount = 10;
    ExecutorService threadpool = Executors.newFixedThreadPool(2 * threadCount);
    try {
        // Try to do ten register() calls and ten buildSpan() calls concurrently.
        List<Callable<Void>> registerCalls = new ArrayList<Callable<Void>>();
        List<Callable<Tracer.SpanBuilder>> buildSpanCalls = new ArrayList<Callable<Tracer.SpanBuilder>>();
        for (int i = 0; i < threadCount; i++) {
            registerCalls.add(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    GlobalTracer.register(mock(Tracer.class));
                    return null;
                }
            });
            buildSpanCalls.add(new Callable<Tracer.SpanBuilder>() {

                @Override
                public Tracer.SpanBuilder call() throws Exception {
                    return GlobalTracer.get().buildSpan("my-operation");
                }
            });
        }
        // Schedule the threads.
        List<Future<Void>> registerResults = threadpool.invokeAll(registerCalls);
        List<Future<Tracer.SpanBuilder>> buildSpanResults = threadpool.invokeAll(buildSpanCalls);
        // there may only be one registration success.
        boolean registered = false;
        int exceptions = 0;
        for (Future<Void> result : registerResults) {
            try {
                // void, but should throw exception 9 times
                result.get();
                assertThat("previous registration", registered, is(false));
                registered = true;
            } catch (ExecutionException expected) {
                exceptions++;
            }
        }
        assertThat("Tracer registration", registered, is(true));
        assertThat("Registration exceptions", exceptions, is(threadCount - 1));
        for (Future<Tracer.SpanBuilder> result : buildSpanResults) {
            // each spanbuilder must be either null (from registered mock tracer) or noop
            assertThat("SpanBuilder", result.get(), anyOf(nullValue(), instanceOf(NoopSpanBuilder.class)));
        }
    } finally {
        threadpool.shutdown();
    }
}
Also used : NoopSpanBuilder(io.opentracing.noop.NoopSpanBuilder) Tracer(io.opentracing.Tracer) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) ExecutionException(java.util.concurrent.ExecutionException) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

Tracer (io.opentracing.Tracer)104 Span (io.opentracing.Span)49 SpanContext (io.opentracing.SpanContext)30 Map (java.util.Map)21 Vertx (io.vertx.core.Vertx)19 HashMap (java.util.HashMap)19 Test (org.junit.Test)19 BeforeEach (org.junit.jupiter.api.BeforeEach)19 Test (org.junit.jupiter.api.Test)19 Future (io.vertx.core.Future)18 Buffer (io.vertx.core.buffer.Buffer)16 HttpURLConnection (java.net.HttpURLConnection)14 EventBus (io.vertx.core.eventbus.EventBus)13 JsonObject (io.vertx.core.json.JsonObject)12 Objects (java.util.Objects)12 Logger (org.slf4j.Logger)11 LoggerFactory (org.slf4j.LoggerFactory)11 Tags (io.opentracing.tag.Tags)9 Scope (io.opentracing.Scope)8 GlobalTracer (io.opentracing.util.GlobalTracer)8