Search in sources :

Example 1 with RequestTraceSpanContext

use of fish.payara.notification.requesttracing.RequestTraceSpanContext in project Payara by payara.

the class Tracer method extract.

@Override
public <C> SpanContext extract(Format<C> format, C carrier) {
    boolean traceIDRecieved = false;
    if (carrier instanceof TextMap) {
        TextMap map = (TextMap) carrier;
        Map<String, String> baggageItems = new HashMap<>();
        Iterator<Map.Entry<String, String>> allEntries = map.iterator();
        UUID traceId = null;
        UUID spanId = null;
        if (format.equals(Format.Builtin.HTTP_HEADERS)) {
            while (allEntries.hasNext()) {
                Entry<String, String> entry = allEntries.next();
                switch(entry.getKey()) {
                    case TRACEID_KEY:
                        traceId = UUID.fromString(decodeURLString(entry.getValue()));
                        traceIDRecieved = true;
                        break;
                    case SPANID_KEY:
                        spanId = UUID.fromString(decodeURLString(entry.getValue()));
                        break;
                    default:
                        baggageItems.put(decodeURLString(entry.getKey()), decodeURLString(entry.getValue()));
                        break;
                }
            }
        } else if (format.equals(Format.Builtin.TEXT_MAP)) {
            while (allEntries.hasNext()) {
                Entry<String, String> entry = allEntries.next();
                switch(entry.getKey()) {
                    case TRACEID_KEY:
                        traceId = UUID.fromString(entry.getValue());
                        traceIDRecieved = true;
                        break;
                    case SPANID_KEY:
                        spanId = UUID.fromString(entry.getValue());
                        break;
                    default:
                        baggageItems.put(entry.getKey(), entry.getValue());
                        break;
                }
            }
        } else {
            throw new InvalidCarrierFormatException(format, carrier);
        }
        if (traceIDRecieved) {
            if (spanId == null) {
                throw new IllegalArgumentException("No SpanId recieved");
            }
            return new RequestTraceSpanContext(traceId, spanId, baggageItems);
        } else {
            // Did not recieve a SpanContext
            return null;
        }
    } else if (carrier instanceof ByteBuffer) {
        ByteBuffer buffer = (ByteBuffer) carrier;
        if (format.equals(Format.Builtin.BINARY)) {
            try {
                ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer.array());
                ObjectInputStream objectStream = new ObjectInputStream(inputStream);
                return (SpanContext) objectStream.readObject();
            } catch (IOException | ClassNotFoundException | ClassCastException ex) {
                Logger.getLogger(Tracer.class.getName()).log(Level.FINER, null, ex);
                // Serialised state is invalid
                throw new IllegalArgumentException(ex);
            }
        } else {
            throw new InvalidCarrierFormatException(format, carrier);
        }
    }
    throw new InvalidCarrierFormatException(format, carrier);
}
Also used : HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) RequestTraceSpanContext(fish.payara.notification.requesttracing.RequestTraceSpanContext) Entry(java.util.Map.Entry) ByteArrayInputStream(java.io.ByteArrayInputStream) TextMap(io.opentracing.propagation.TextMap) UUID(java.util.UUID) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with RequestTraceSpanContext

use of fish.payara.notification.requesttracing.RequestTraceSpanContext in project Payara by payara.

the class Tracer method inject.

@Override
public <C> void inject(SpanContext spanContext, Format<C> format, C carrier) {
    RequestTraceSpanContext payaraSpanContext = (RequestTraceSpanContext) spanContext;
    Iterable<Map.Entry<String, String>> baggageItems = payaraSpanContext.baggageItems();
    if (carrier instanceof TextMap) {
        TextMap map = (TextMap) carrier;
        if (format.equals(Format.Builtin.HTTP_HEADERS)) {
            for (Map.Entry<String, String> baggage : baggageItems) {
                map.put(encodeURLString(baggage.getKey()), encodeURLString(baggage.getValue()));
            }
            map.put(TRACEID_KEY, encodeURLString(payaraSpanContext.getTraceId().toString()));
            map.put(SPANID_KEY, encodeURLString(payaraSpanContext.getSpanId().toString()));
        } else if (format.equals(Format.Builtin.TEXT_MAP)) {
            for (Map.Entry<String, String> baggage : baggageItems) {
                map.put(baggage.getKey(), baggage.getValue());
            }
            map.put(TRACEID_KEY, payaraSpanContext.getTraceId().toString());
            map.put(SPANID_KEY, payaraSpanContext.getSpanId().toString());
        } else {
            throw new InvalidCarrierFormatException(format, carrier);
        }
    } else if (carrier instanceof ByteBuffer) {
        ByteBuffer buffer = (ByteBuffer) carrier;
        if (format.equals(Format.Builtin.BINARY)) {
            ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
            try {
                ObjectOutputStream objectWriter = new ObjectOutputStream(bytesOut);
                objectWriter.writeObject(spanContext);
                objectWriter.flush();
                buffer.put(bytesOut.toByteArray());
            } catch (IOException ex) {
                Logger.getLogger(Tracer.class.getName()).log(Level.WARNING, null, ex);
                throw new UncheckedIOException(ex);
            }
        } else {
            throw new InvalidCarrierFormatException(format, carrier);
        }
    } else {
        throw new InvalidCarrierFormatException(format, carrier);
    }
}
Also used : UncheckedIOException(java.io.UncheckedIOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ObjectOutputStream(java.io.ObjectOutputStream) ByteBuffer(java.nio.ByteBuffer) RequestTraceSpanContext(fish.payara.notification.requesttracing.RequestTraceSpanContext) Entry(java.util.Map.Entry) TextMap(io.opentracing.propagation.TextMap) HashMap(java.util.HashMap) Map(java.util.Map) TextMap(io.opentracing.propagation.TextMap)

Example 3 with RequestTraceSpanContext

use of fish.payara.notification.requesttracing.RequestTraceSpanContext 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 4 with RequestTraceSpanContext

use of fish.payara.notification.requesttracing.RequestTraceSpanContext 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)4 HashMap (java.util.HashMap)3 MapToTextMap (fish.payara.opentracing.propagation.MapToTextMap)2 SpanContext (io.opentracing.SpanContext)2 Tracer (io.opentracing.Tracer)2 TextMap (io.opentracing.propagation.TextMap)2 ByteBuffer (java.nio.ByteBuffer)2 Entry (java.util.Map.Entry)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 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 UncheckedIOException (java.io.UncheckedIOException)1 Map (java.util.Map)1