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());
}
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());
}
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());
}
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);
}
}
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());
}
}
}
}
}
Aggregations