Skip to content

Commit

Permalink
Merge pull request #4853 from Pandrex247/FISH-389
Browse files Browse the repository at this point in the history
FISH-389 Add Transaction ID as Span Baggage Item
  • Loading branch information
jGauravGupta authored Sep 5, 2020
2 parents ee97386 + 9bf3dc4 commit b6f0e41
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 26 deletions.
13 changes: 12 additions & 1 deletion appserver/ejb/ejb-container/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
holder.
-->
<!--"Portions Copyright [2016-2019] [Payara Foundation and/or its affiliates]" -->
<!--"Portions Copyright [2016-2020] [Payara Foundation and/or its affiliates]" -->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -247,5 +247,16 @@
<artifactId>payara-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>fish.payara.server.internal.payara-modules</groupId>
<artifactId>opentracing-adapter</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@

import fish.payara.cluster.DistributedLockType;
import fish.payara.notification.requesttracing.RequestTraceSpanLog;
import fish.payara.nucleus.requesttracing.RequestTracingService;

import java.io.Serializable;
import java.lang.reflect.Constructor;
Expand Down Expand Up @@ -122,6 +121,10 @@
import javax.transaction.Transaction;
import javax.transaction.UserTransaction;

import fish.payara.nucleus.requesttracing.RequestTracingService;
import fish.payara.opentracing.OpenTracingService;
import io.opentracing.Span;
import io.opentracing.Tracer;
import org.glassfish.api.invocation.ComponentInvocation;
import org.glassfish.api.invocation.InvocationManager;
import org.glassfish.api.naming.GlassfishNamingManager;
Expand Down Expand Up @@ -235,7 +238,8 @@ public enum ContainerType {

protected ContainerType containerType;

private final RequestTracingService requestTracing;
private final RequestTracingService requestTracingService;
private final OpenTracingService openTracingService;

// constants for EJB(Local)Home/EJB(Local)Object methods,
// used in authorizeRemoteMethod and authorizeLocalMethod
Expand Down Expand Up @@ -570,7 +574,8 @@ protected BaseContainer(ContainerType type, EjbDescriptor ejbDesc, ClassLoader l
{
this.containerType = type;
this.securityManager = sm;
this.requestTracing = Globals.getDefaultHabitat().getService(RequestTracingService.class);
this.requestTracingService = Globals.getDefaultHabitat().getService(RequestTracingService.class);
this.openTracingService = Globals.getDefaultHabitat().getService(OpenTracingService.class);

try {
this.loader = loader;
Expand Down Expand Up @@ -4159,11 +4164,8 @@ final void onEjbMethodStart(String method_sig) {
callFlowInfo.getModuleName(),
callFlowInfo.getComponentName(),
method_sig);
if (requestTracing.isRequestTracingEnabled()) {
RequestTraceSpanLog spanLog = constructEjbMethodSpanLog(callFlowInfo, true);
requestTracing.addSpanLog(spanLog);
}
//callFlowAgent.ejbMethodStart(callFlowInfo);

addEjbMethodTraceLog(callFlowInfo, true);
}

final void onEjbMethodEnd(String method_sig, Throwable th) {
Expand All @@ -4173,10 +4175,29 @@ final void onEjbMethodEnd(String method_sig, Throwable th) {
callFlowInfo.getComponentName(),
th,
method_sig);
//callFlowAgent.ejbMethodEnd(callFlowInfo);
if (requestTracing.isRequestTracingEnabled()) {
RequestTraceSpanLog spanLog = constructEjbMethodSpanLog(callFlowInfo, false);
requestTracing.addSpanLog(spanLog);
addEjbMethodTraceLog(callFlowInfo, false);
}

private void addEjbMethodTraceLog(CallFlowInfo info, boolean callEnter) {
if (openTracingService.isEnabled()) {
Tracer tracer = openTracingService.getTracer(openTracingService.getApplicationName(invocationManager));
RequestTraceSpanLog spanLog = constructEjbMethodSpanLog(info, callEnter);

if (tracer != null) {
Span span = tracer.activeSpan();

if (span != null) {
span.log(spanLog.getTimeMillis(), spanLog.getLogEntries());
} else {
// Traces started in the pre-OpenTracing style won't have an active span, so just attempt to add as
// is to thread local trace if there is one
requestTracingService.addSpanLog(spanLog);
}
} else {
// If we couldn't get a tracer here, it's because we couldn't get a name from the invocation manager.
// In such a case, just try to add the span log to the currently active thread local request trace
requestTracingService.addSpanLog(spanLog);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void executeRemoteEjbMethodIT() {
span.setBaggageItem("Wibbles", "Wobbles");
String baggageItems = ejb.annotatedMethod();
Assert.assertTrue("Baggage items didn't match, received: " + baggageItems,
baggageItems.equals("\nWibbles : Wobbles\n"));
baggageItems.contains("\nWibbles : Wobbles\n"));

span.setBaggageItem("Nibbles", "Nobbles");
baggageItems = ejb.nonAnnotatedMethod();
Expand All @@ -102,5 +102,28 @@ public void executeRemoteEjbMethodIT() {
}
}

@Test
public void transactionIdAddedAsBaggageIT() {
Properties contextProperties = new Properties();
contextProperties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
contextProperties.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
contextProperties.setProperty("org.omg.CORBA.ORBInitialPort", "3700");

try {
Context context = new InitialContext(contextProperties);
EjbRemote ejb = (EjbRemote) context.lookup("java:global/remote-ejb-tracing-server/Ejb");

Tracer tracer = GlobalTracer.get();

try (Scope scope = tracer.buildSpan("ExecuteEjb").startActive(true)) {
String baggageItems = ejb.annotatedMethod();
Assert.assertTrue("Baggage items didn't contain transaction ID, received: " + baggageItems,
baggageItems.contains("TX-ID"));
}
} catch (NamingException ne) {
Assert.fail("Failed performing lookup:\n" + ne.getMessage());
}
}


}
8 changes: 7 additions & 1 deletion appserver/transaction/jta/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,14 @@
</dependency>
<dependency>
<groupId>fish.payara.server.internal.payara-modules</groupId>
<artifactId>requesttracing-core</artifactId>
<artifactId>opentracing-adapter</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* holder.
*/

// Portions Copyright [2016-2019] [Payara Foundation and/or its affiliates.]
// Portions Copyright [2016-2020] [Payara Foundation and/or its affiliates.]

package com.sun.enterprise.transaction;

Expand All @@ -59,6 +59,9 @@
import com.sun.logging.LogDomains;
import fish.payara.notification.requesttracing.RequestTraceSpanLog;
import fish.payara.nucleus.requesttracing.RequestTracingService;
import fish.payara.opentracing.OpenTracingService;
import io.opentracing.Span;
import io.opentracing.Tracer;
import org.glassfish.api.admin.ServerEnvironment;
import org.glassfish.api.invocation.ComponentInvocation;
import org.glassfish.api.invocation.InvocationException;
Expand Down Expand Up @@ -112,6 +115,8 @@ public class JavaEETransactionManagerSimplified

@Inject protected InvocationManager invMgr;

@Inject private Provider<OpenTracingService> openTracingServiceProvider;

@Inject private Provider<RequestTracingService> requestTracing;

private JavaEETransactionManagerDelegate delegate;
Expand Down Expand Up @@ -198,6 +203,16 @@ public void postConstruct() {
initProperties();
}

private OpenTracingService getOpenTracing() {
OpenTracingService openTracingService = openTracingServiceProvider.get();
if (openTracingService == null) {
_logger.log(Level.INFO, "Error retrieving OpenTracing "
+ "service during initialisation of "
+ "JavaEETransactionManagerSimplified - NullPointerException");
}
return openTracingService;
}

private RequestTracingService getRequestTracing() {
RequestTracingService requestTracingService = requestTracing.get();
if (requestTracingService == null) {
Expand Down Expand Up @@ -662,11 +677,12 @@ private JavaEETransactionImpl initJavaEETransaction(int timeout) {

setCurrentTransaction(tx);

if (requestTracing != null && getRequestTracing().isRequestTracingEnabled()) {
RequestTraceSpanLog spanLog = constructJTABeginSpanLog(tx);
getRequestTracing().addSpanLog(spanLog);
if (openTracingServiceProvider != null) {
OpenTracingService openTracingService = getOpenTracing();
if (openTracingService != null && openTracingService.isEnabled()) {
addJtaEventTraceLog(constructJTABeginSpanLog(tx), tx, openTracingService);
}
}

return tx;
}

Expand Down Expand Up @@ -954,9 +970,11 @@ public void commit() throws RollbackException,
}
}

if (requestTracing != null && tx != null && getRequestTracing().isRequestTracingEnabled()) {
RequestTraceSpanLog spanLog = constructJTAEndSpanLog(tx);
getRequestTracing().addSpanLog(spanLog);
if (openTracingServiceProvider != null) {
OpenTracingService openTracingService = getOpenTracing();
if (openTracingService != null && openTracingService.isEnabled()) {
addJtaEventTraceLog(constructJTAEndSpanLog(tx), tx, openTracingService);
}
}
} finally {
setCurrentTransaction(null); // clear current thread's tx
Expand Down Expand Up @@ -992,9 +1010,11 @@ public void rollback() throws IllegalStateException, SecurityException,
}
}

if (requestTracing != null && tx != null && getRequestTracing().isRequestTracingEnabled()) {
RequestTraceSpanLog spanLog = constructJTAEndSpanLog(tx);
getRequestTracing().addSpanLog(spanLog);
if (openTracingServiceProvider != null) {
OpenTracingService openTracingService = getOpenTracing();
if (openTracingService != null && openTracingService.isEnabled()) {
addJtaEventTraceLog(constructJTAEndSpanLog(tx), tx, openTracingService);
}
}
} finally {
setCurrentTransaction(null); // clear current thread's tx
Expand Down Expand Up @@ -1716,6 +1736,32 @@ public JavaEETransaction createImportedTransaction(TransactionInternal jtsTx)
return tx;
}

private void addJtaEventTraceLog(RequestTraceSpanLog spanLog, JavaEETransaction tx,
OpenTracingService openTracingService) {
Tracer tracer = openTracingService.getTracer(openTracingService.getApplicationName(invMgr));

if (tracer != null) {
Span span = tracer.activeSpan();

if (span != null) {
span.log(spanLog.getTimeMillis(), spanLog.getLogEntries());

// Add transaction ID as baggage item
if (tx != null) {
if (tx.getClass().equals(JavaEETransactionImpl.class)) {
span.setBaggageItem("TX-ID", ((JavaEETransactionImpl) tx).getTransactionId());
} else {
span.setBaggageItem("TransactionInfo", tx.toString());
}
}
}
} else {
// If we couldn't get a tracer here, it's because we couldn't get a name from the invocation manager.
// In such a case, just try to add the span log to the currently active thread local request trace
getRequestTracing().addSpanLog(spanLog);
}
}

private RequestTraceSpanLog constructJTABeginSpanLog(JavaEETransactionImpl transaction) {
RequestTraceSpanLog spanLog = new RequestTraceSpanLog("jtaContextBeginEvent");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ public void event(Event<?> event) {
* @return The Tracer instance for the given application
*/
public synchronized Tracer getTracer(String applicationName) {
if (applicationName == null) {
return null;
}

// Get the tracer if there is one
Tracer tracer = tracers.get(applicationName);

Expand Down

0 comments on commit b6f0e41

Please sign in to comment.