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