Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
The User of this Mac authored and The User of this Mac committed Jul 8, 2020
2 parents 07385ef + 0c202e6 commit 161c858
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,10 @@ public void onNotification(PipelineMessageNotification notification) {
transactionUtils.startTransactionIfNone(notification);
break;

case PipelineMessageNotification.PROCESS_COMPLETE:
case PipelineMessageNotification.PROCESS_END:
transactionUtils.endTransactionIfNeeded(notification);
break;

case PipelineMessageNotification.PROCESS_COMPLETE:
// Ignored, as it is skipped when flow exception is thrown
break;
}
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package co.elastic.apm.mule.utils;

import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.log4j.MDC;
import org.mule.api.MessagingException;
import org.mule.api.MuleEvent;
import org.mule.api.MuleMessage;
Expand All @@ -21,6 +22,9 @@
*
*/
public class TransactionUtils {

private static final String MDC_TRANSACTION_ID = "transaction.id";
private static final String MDC_TRACE_ID = "trace.id";

@Autowired
private SpanStore txMap;
Expand Down Expand Up @@ -56,6 +60,8 @@ public void startTransactionIfNone(PipelineMessageNotification notification) {

txMap.storeTransactionOrSpan(messageId, notification, transaction);

if (isLogCorrelationEnabled())
addMdcHeaders(transaction);
}

/**
Expand All @@ -82,9 +88,27 @@ public void endTransactionIfNeeded(PipelineMessageNotification notification) {
transaction.captureException(exceptionThrown);

transaction.end(notification.getTimestamp() * 1_000);

if (isLogCorrelationEnabled())
removeMdcHeaders();
}
}

private boolean isLogCorrelationEnabled() {
return "true".equals(System.getProperty("elastic.apm.enable_log_correlation"))
|| "true".equals(System.getenv("ELASTIC_APM_ENABLE_LOG_CORRELATION"));
}

private void addMdcHeaders(Transaction transaction) {
MDC.put(MDC_TRACE_ID, transaction.getTraceId());
MDC.put(MDC_TRANSACTION_ID, transaction.getId());
}

private void removeMdcHeaders() {
MDC.remove(MDC_TRACE_ID);
MDC.remove(MDC_TRANSACTION_ID);
}

private MuleMessage getMuleMessage(PipelineMessageNotification notification) {
return ((MuleEvent) notification.getSource()).getMessage();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,10 @@ public void onNotification(PipelineMessageNotification notification) {
transactionUtils.startTransactionIfNone(notification);
break;

case PipelineMessageNotification.PROCESS_COMPLETE:
case PipelineMessageNotification.PROCESS_END:
transactionUtils.endTransactionIfNeeded(notification);
break;

case PipelineMessageNotification.PROCESS_COMPLETE:
// Ignored, as it is skipped when flow exception is thrown
break;
}
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package co.elastic.apm.mule.utils;

import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.log4j.MDC;
import org.mule.api.MessagingException;
import org.mule.api.MuleEvent;
import org.mule.api.MuleMessage;
Expand All @@ -22,6 +23,9 @@
*/
public class TransactionUtils {

private static final String MDC_TRANSACTION_ID = "transaction.id";
private static final String MDC_TRACE_ID = "trace.id";

@Autowired
private SpanStore txMap;

Expand Down Expand Up @@ -56,6 +60,9 @@ public void startTransactionIfNone(PipelineMessageNotification notification) {

txMap.storeTransactionOrSpan(messageId, notification, transaction);

if (isLogCorrelationEnabled())
addMdcHeaders(transaction);

}

/**
Expand All @@ -82,9 +89,27 @@ public void endTransactionIfNeeded(PipelineMessageNotification notification) {
transaction.captureException(exceptionThrown);

transaction.end(notification.getTimestamp() * 1_000);

if (isLogCorrelationEnabled())
removeMdcHeaders();
}
}

private boolean isLogCorrelationEnabled() {
return "true".equals(System.getProperty("elastic.apm.enable_log_correlation"))
|| "true".equals(System.getenv("ELASTIC_APM_ENABLE_LOG_CORRELATION"));
}

private void addMdcHeaders(Transaction transaction) {
MDC.put(MDC_TRACE_ID, transaction.getTraceId());
MDC.put(MDC_TRANSACTION_ID, transaction.getId());
}

private void removeMdcHeaders() {
MDC.remove(MDC_TRACE_ID);
MDC.remove(MDC_TRANSACTION_ID);
}

private MuleMessage getMuleMessage(PipelineMessageNotification notification) {
return ((MuleEvent) notification.getSource()).getMessage();
}
Expand Down

0 comments on commit 161c858

Please sign in to comment.