Search in sources :

Example 1 with MapToTextMap

use of fish.payara.opentracing.propagation.MapToTextMap 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);
            }
        }
    }
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) RequestTraceSpanContext(fish.payara.notification.requesttracing.RequestTraceSpanContext) RequestTraceSpanContext(fish.payara.notification.requesttracing.RequestTraceSpanContext) SpanContext(io.opentracing.SpanContext) HashMap(java.util.HashMap) Tracer(io.opentracing.Tracer) RequestTraceSpan(fish.payara.notification.requesttracing.RequestTraceSpan) OpenTracingService(fish.payara.opentracing.OpenTracingService) MapToTextMap(fish.payara.opentracing.propagation.MapToTextMap) RequestTraceSpan(fish.payara.notification.requesttracing.RequestTraceSpan) Span(io.opentracing.Span) RequestTracingService(fish.payara.nucleus.requesttracing.RequestTracingService)

Example 2 with MapToTextMap

use of fish.payara.opentracing.propagation.MapToTextMap 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

RequestTraceSpanContext (fish.payara.notification.requesttracing.RequestTraceSpanContext)2 MapToTextMap (fish.payara.opentracing.propagation.MapToTextMap)2 SpanContext (io.opentracing.SpanContext)2 Tracer (io.opentracing.Tracer)2 RequestTraceSpan (fish.payara.notification.requesttracing.RequestTraceSpan)1 RequestTracingService (fish.payara.nucleus.requesttracing.RequestTracingService)1 OpenTracingService (fish.payara.opentracing.OpenTracingService)1 Span (io.opentracing.Span)1 SpanBuilder (io.opentracing.Tracer.SpanBuilder)1 HashMap (java.util.HashMap)1 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)1