Search in sources :

Example 1 with SpanBuilder

use of io.opentracing.Tracer.SpanBuilder 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 2 with SpanBuilder

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

the class RequestHandler method beforeRequest.

public void beforeRequest(Object request, Context context) {
    logger.info("before send {}", request);
    // we cannot use active span because we don't know in which thread it is executed
    // and we cannot therefore activate span. thread can come from common thread pool.
    SpanBuilder spanBuilder = tracer.buildSpan(OPERATION_NAME).ignoreActiveSpan().withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT);
    if (parentContext != null) {
        spanBuilder.asChildOf(parentContext);
    }
    context.put("span", spanBuilder.startManual());
}
Also used : SpanBuilder(io.opentracing.Tracer.SpanBuilder)

Example 3 with SpanBuilder

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

the class JaxwsContainerRequestTracingFilter method filterRequest.

// Before method invocation
@Override
public void filterRequest(Packet pipeRequest, MonitorContext monitorContext) {
    // If request tracing is enabled, and there's a trace in progress (which there should be!)
    if (isTraceInProgress()) {
        // Get the Traced annotation from the target method if CDI is initialised
        Traced tracedAnnotation = getTraceAnnotation(monitorContext);
        // pattern and a traced annotation set to true (via annotation or config override)
        if (shouldTrace(pipeRequest) && shouldTrace(monitorContext, tracedAnnotation)) {
            // Get the application's tracer instance
            Tracer tracer = getTracer();
            HttpServletRequest httpRequest = (HttpServletRequest) pipeRequest.get(SERVLET_REQUEST);
            // Create a Span and instrument it with details about the request
            SpanBuilder spanBuilder = tracer.buildSpan(determineOperationName(pipeRequest, monitorContext, tracedAnnotation)).withTag(SPAN_KIND.getKey(), SPAN_KIND_SERVER).withTag(HTTP_METHOD.getKey(), httpRequest.getMethod()).withTag(HTTP_URL.getKey(), httpRequest.getRequestURL().toString()).withTag(COMPONENT.getKey(), "jaxws");
            SpanContext spanContext = null;
            try {
                // Extract the context from the tracer if there is one
                spanContext = tracer.extract(HTTP_HEADERS, new MultivaluedMapToTextMap(getHeaders(httpRequest)));
            } catch (IllegalArgumentException e) {
                logger.log(WARNING, e.getMessage());
            }
            // If there was a context injected into the tracer, add it as a parent of the new span
            if (spanContext != null) {
                spanBuilder.asChildOf(spanContext);
            }
            // Start the span and continue on to the targeted method
            Scope scope = tracer.activateSpan(spanBuilder.start());
            httpRequest.setAttribute(Scope.class.getName(), scope);
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) SpanBuilder(io.opentracing.Tracer.SpanBuilder) SpanContext(io.opentracing.SpanContext) Traced(org.eclipse.microprofile.opentracing.Traced) Scope(io.opentracing.Scope) Tracer(io.opentracing.Tracer)

Example 4 with SpanBuilder

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

the class OpenTracingRequestEventListener method onIncomingRequest.

private void onIncomingRequest(final RequestEvent event, final String operationName) {
    LOG.fine(() -> "onIncomingRequest(event=" + event.getType() + ", operationName=" + operationName + ")");
    final ContainerRequest requestContext = event.getContainerRequest();
    final Tracer tracer = openTracing.getTracer(this.applicationName);
    // Create a Span and instrument it with details about the request
    final SpanBuilder spanBuilder = // 
    tracer.buildSpan(operationName).withTag(Tags.SPAN_KIND.getKey(), // 
    Tags.SPAN_KIND_SERVER).withTag(Tags.HTTP_METHOD.getKey(), // 
    requestContext.getMethod()).withTag(Tags.HTTP_URL.getKey(), // 
    toString(requestContext.getUriInfo())).withTag(Tags.COMPONENT.getKey(), "jaxrs");
    // If there was a context injected into the tracer, add it as a parent of the new span
    final SpanContext spanContext = findSpanContext(requestContext, tracer);
    if (spanContext != null) {
        spanBuilder.asChildOf(spanContext);
    }
    // Start the span and continue on to the targeted method
    Scope scope = tracer.activateSpan(spanBuilder.start());
    // tracer.activeSpan();
    requestContext.setProperty(Scope.class.getName(), scope);
    LOG.fine(() -> "Request tracing enabled for request=" + requestContext.getRequest() + " to application=" + this.applicationName + " on uri=" + toString(requestContext.getUriInfo()));
}
Also used : SpanBuilder(io.opentracing.Tracer.SpanBuilder) SpanContext(io.opentracing.SpanContext) Scope(io.opentracing.Scope) Tracer(io.opentracing.Tracer) ContainerRequest(org.glassfish.jersey.server.ContainerRequest)

Example 5 with SpanBuilder

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

the class ContextSetupProviderImpl method startConcurrentContextSpan.

private void startConcurrentContextSpan(ComponentInvocation invocation, InvocationContext handle) {
    Tracer tracer = openTracing.getTracer(openTracing.getApplicationName(Globals.getDefaultBaseServiceLocator().getService(InvocationManager.class)));
    // Start a trace in the request tracing system
    SpanBuilder builder = tracer.buildSpan("executeConcurrentContext");
    // Check for propagated span
    if (handle.getSpanContextMap() != null) {
        @SuppressWarnings("unchecked") SpanContext spanContext = tracer.extract(Format.Builtin.TEXT_MAP, new MapToTextMap(handle.getSpanContextMap()));
        if (spanContext != null) {
            builder.asChildOf(spanContext);
            // Check for the presence of a propagated parent operation name
            try {
                String operationName = ((RequestTraceSpanContext) spanContext).getBaggageItems().get("operation.name");
                if (operationName != null) {
                    builder.withTag("Parent Operation Name", operationName);
                }
            } catch (ClassCastException cce) {
                logger.log(Level.FINE, "ClassCastException caught converting Span Context", cce);
            }
        }
    }
    if (invocation != null) {
        builder = builder.withTag("App Name", invocation.getAppName()).withTag("Component ID", invocation.getComponentId()).withTag("Module Name", invocation.getModuleName());
        Object instance = invocation.getInstance();
        if (instance != null) {
            builder.withTag("Class Name", instance.getClass().getName());
        }
    }
    builder.withTag("Thread Name", Thread.currentThread().getName());
    tracer.activateSpan(builder.start());
}
Also used : SpanBuilder(io.opentracing.Tracer.SpanBuilder) RequestTraceSpanContext(fish.payara.notification.requesttracing.RequestTraceSpanContext) SpanContext(io.opentracing.SpanContext) Tracer(io.opentracing.Tracer) MapToTextMap(fish.payara.opentracing.propagation.MapToTextMap)

Aggregations

SpanBuilder (io.opentracing.Tracer.SpanBuilder)9 SpanContext (io.opentracing.SpanContext)6 Tracer (io.opentracing.Tracer)5 Scope (io.opentracing.Scope)3 Span (io.opentracing.Span)3 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 RequestTraceSpan (fish.payara.notification.requesttracing.RequestTraceSpan)1 RequestTraceSpanContext (fish.payara.notification.requesttracing.RequestTraceSpanContext)1 RequestTracingService (fish.payara.nucleus.requesttracing.RequestTracingService)1 MapToTextMap (fish.payara.opentracing.propagation.MapToTextMap)1 Sample (io.micrometer.core.instrument.Timer.Sample)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 TextMapExtractAdapter (io.opentracing.propagation.TextMapExtractAdapter)1 Context (io.vertx.core.Context)1 Future (io.vertx.core.Future)1 Handler (io.vertx.core.Handler)1 Vertx (io.vertx.core.Vertx)1 Buffer (io.vertx.core.buffer.Buffer)1 HttpServerRequest (io.vertx.core.http.HttpServerRequest)1 HttpServerResponse (io.vertx.core.http.HttpServerResponse)1