Search in sources :

Example 56 with Scope

use of io.opentracing.Scope in project jaeger-client-java by jaegertracing.

the class PropagationTest method testCustomScopeManager.

@Test
public void testCustomScopeManager() {
    Scope scope = mock(Scope.class);
    Tracer tracer = new Tracer.Builder("test", new InMemoryReporter(), new ConstSampler(true)).withScopeManager(new ScopeManager() {

        @Override
        public Scope activate(io.opentracing.Span span, boolean finishSpanOnClose) {
            return scope;
        }

        @Override
        public Scope active() {
            return scope;
        }
    }).build();
    assertEquals(scope, tracer.scopeManager().active());
}
Also used : InMemoryReporter(com.uber.jaeger.reporters.InMemoryReporter) ScopeManager(io.opentracing.ScopeManager) Scope(io.opentracing.Scope) ConstSampler(com.uber.jaeger.samplers.ConstSampler) Test(org.junit.Test)

Example 57 with Scope

use of io.opentracing.Scope in project jaeger-client-java by jaegertracing.

the class PropagationTest method testActiveSpanNotAutoFinishOnClose.

@Test
public void testActiveSpanNotAutoFinishOnClose() {
    InMemoryReporter reporter = new InMemoryReporter();
    Tracer tracer = new Tracer.Builder("test", reporter, new ConstSampler(true)).build();
    Scope scope = tracer.buildSpan("parent").startActive(false);
    Span span = (Span) scope.span();
    scope.close();
    assertTrue(reporter.getSpans().isEmpty());
    span.finish();
    assertEquals(1, reporter.getSpans().size());
}
Also used : InMemoryReporter(com.uber.jaeger.reporters.InMemoryReporter) Scope(io.opentracing.Scope) ConstSampler(com.uber.jaeger.samplers.ConstSampler) Test(org.junit.Test)

Example 58 with Scope

use of io.opentracing.Scope in project jaeger-client-java by jaegertracing.

the class PropagationTest method testActiveSpanAutoReference.

@Test
public void testActiveSpanAutoReference() {
    InMemoryReporter reporter = new InMemoryReporter();
    Tracer tracer = new Tracer.Builder("test", reporter, new ConstSampler(true)).build();
    try (Scope parent = tracer.buildSpan("parent").startActive(true)) {
        tracer.buildSpan("child").startActive(true).close();
    }
    assertEquals(2, reporter.getSpans().size());
    Span childSpan = reporter.getSpans().get(0);
    Span parentSpan = reporter.getSpans().get(1);
    assertEquals("child", childSpan.getOperationName());
    assertEquals(1, childSpan.getReferences().size());
    assertEquals("parent", parentSpan.getOperationName());
    assertTrue(parentSpan.getReferences().isEmpty());
    assertEquals(References.CHILD_OF, childSpan.getReferences().get(0).getType());
    assertEquals(parentSpan.context(), childSpan.getReferences().get(0).getSpanContext());
    assertEquals(parentSpan.context().getTraceId(), childSpan.context().getTraceId());
    assertEquals(parentSpan.context().getSpanId(), childSpan.context().getParentId());
}
Also used : InMemoryReporter(com.uber.jaeger.reporters.InMemoryReporter) Scope(io.opentracing.Scope) ConstSampler(com.uber.jaeger.samplers.ConstSampler) Test(org.junit.Test)

Example 59 with Scope

use of io.opentracing.Scope in project jaeger-client-java by jaegertracing.

the class TracingRequestInterceptor method process.

@Override
public void process(HttpRequest httpRequest, HttpContext httpContext) throws HttpException, IOException {
    try {
        spanCreationInterceptor.process(httpRequest, httpContext);
        Scope currentScope = tracer.scopeManager().active();
        if (currentScope != null) {
            onSpanStarted(currentScope.span(), httpRequest, httpContext);
        } else {
            log.warn("Current scope is null; possibly failed to start client tracing span.");
        }
        spanInjectionInterceptor.process(httpRequest, httpContext);
    } catch (Exception e) {
        log.error("Could not start client tracing span.", e);
    }
}
Also used : Scope(io.opentracing.Scope) HttpException(org.apache.http.HttpException) IOException(java.io.IOException)

Example 60 with Scope

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

the class JaxrsClientRequestTracingFilter method filter.

// After method invocation
@Override
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
    final PayaraTracingServices payaraTracingServices = new PayaraTracingServices();
    final RequestTracingService requestTracing = payaraTracingServices.getRequestTracingService();
    // If request tracing is enabled, and there's a trace actually in progress, add info about method
    if (requestTracing != null && requestTracing.isRequestTracingEnabled() && shouldTrace(requestContext)) {
        // Get the active span from the application's tracer instance
        Span activeSpan = payaraTracingServices.getActiveTracer().scopeManager().activeSpan();
        if (activeSpan == null) {
            // This should really only occur when enabling request tracing due to the nature of the
            // service not being enabled when making the request to enable it, and then
            // obviously being enabled on the return. Any other entrance into here is likely a bug
            // caused by a tracing context not being propagated.
            Logger.getLogger(JaxrsClientRequestTracingFilter.class.getName()).log(Level.FINE, "activeScope in opentracing request tracing filter was null for {0}", responseContext);
            return;
        }
        try {
            // Get the response status and add it to the active span
            StatusType statusInfo = responseContext.getStatusInfo();
            activeSpan.setTag(Tags.HTTP_STATUS.getKey(), statusInfo.getStatusCode());
            // If the response status is an error, add error info to the active span
            if (statusInfo.getFamily() == Family.CLIENT_ERROR || statusInfo.getFamily() == Family.SERVER_ERROR) {
                activeSpan.setTag(Tags.ERROR.getKey(), true);
                activeSpan.log(Collections.singletonMap("event", "error"));
                activeSpan.log(Collections.singletonMap("error.object", statusInfo.getFamily()));
            }
        } finally {
            activeSpan.finish();
            Object scopeObj = requestContext.getProperty(Scope.class.getName());
            if (scopeObj != null && scopeObj instanceof Scope) {
                try (Scope scope = (Scope) scopeObj) {
                    requestContext.removeProperty(Scope.class.getName());
                }
            }
        }
    }
}
Also used : Scope(io.opentracing.Scope) StatusType(javax.ws.rs.core.Response.StatusType) RequestTraceSpan(fish.payara.notification.requesttracing.RequestTraceSpan) Span(io.opentracing.Span) RequestTracingService(fish.payara.nucleus.requesttracing.RequestTracingService)

Aggregations

Scope (io.opentracing.Scope)80 Test (org.junit.Test)52 Span (io.opentracing.Span)46 MockSpan (io.opentracing.mock.MockSpan)10 Tracer (io.opentracing.Tracer)7 Response (javax.ws.rs.core.Response)6 InMemoryReporter (com.uber.jaeger.reporters.InMemoryReporter)5 ConstSampler (com.uber.jaeger.samplers.ConstSampler)5 ScopeManager (io.opentracing.ScopeManager)5 SpanInScope (brave.Tracer.SpanInScope)4 SpanContext (io.opentracing.SpanContext)4 HashMap (java.util.HashMap)4 SpanBuilder (io.opentracing.Tracer.SpanBuilder)3 IOException (java.io.IOException)3 Traced (org.eclipse.microprofile.opentracing.Traced)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 BraveSpan (brave.opentracing.BraveSpan)2 RequestTraceSpan (fish.payara.notification.requesttracing.RequestTraceSpan)2 RequestTracingService (fish.payara.nucleus.requesttracing.RequestTracingService)2 Downstream (io.jaegertracing.crossdock.api.Downstream)2