use of fish.payara.nucleus.requesttracing.RequestTracingService in project Payara by payara.
the class Span method finish.
@Override
public void finish(long finishMicros) {
// Convert the timestamp to that required by the Request Tracing Service
long finishMillis = convertTimestampMicrosToTimestampMillis(finishMicros);
// Pass through to the Request Tracing Service
RequestTracingService requestTracingService = getRequestTracingService();
if (requestTracingService != null) {
requestTracingService.traceSpan(this, finishMillis);
}
}
use of fish.payara.nucleus.requesttracing.RequestTracingService 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 fish.payara.nucleus.requesttracing.RequestTracingService 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 fish.payara.nucleus.requesttracing.RequestTracingService in project Payara by payara.
the class RequestTracingCdiInterceptor method traceCdiCall.
@AroundInvoke
public Object traceCdiCall(InvocationContext ctx) throws Exception {
RequestTracingService requestTracing = Globals.getDefaultHabitat().getService(RequestTracingService.class);
Object proceed = null;
if (requestTracing != null && requestTracing.isRequestTracingEnabled()) {
RequestTraceSpan span = new RequestTraceSpan("executeCdiMethod");
span.addSpanTag("TargetClass", ctx.getTarget().getClass().getName());
span.addSpanTag("MethodName", ctx.getMethod().getName());
proceed = ctx.proceed();
requestTracing.traceSpan(span);
} else {
proceed = ctx.proceed();
}
return proceed;
}
use of fish.payara.nucleus.requesttracing.RequestTracingService in project Payara by payara.
the class Span method getRequestTracingService.
/**
* Helper method that gets the Request Tracing Service if this instance does not have it.
*/
private RequestTracingService getRequestTracingService() {
RequestTracingService requestTracingService = null;
ServiceLocator serviceLocator = Globals.getDefaultBaseServiceLocator();
if (serviceLocator != null) {
ServiceHandle<RequestTracingService> serviceHandle = serviceLocator.getServiceHandle(RequestTracingService.class);
if (serviceHandle != null && serviceHandle.isActive()) {
requestTracingService = serviceHandle.getService();
}
}
return requestTracingService;
}
Aggregations