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