Only iterate stack if not empty
@@ -272,11 +272,12 @@ public class ElasticApmTracer {
@Nullable
public Transaction currentTransaction() {
- final TraceContextHolder<?> bottomOfStack = activeStack.get().peekLast();
+ Deque<TraceContextHolder<?>> stack = activeStack.get();
+ final TraceContextHolder<?> bottomOfStack = stack.peekLast();
if (bottomOfStack instanceof Transaction) {
return (Transaction) bottomOfStack;
- } else {
- for (Iterator<TraceContextHolder<?>> it = activeStack.get().descendingIterator(); it.hasNext(); ) {
+ } else if (bottomOfStack != null) {
+ for (Iterator<TraceContextHolder<?>> it = stack.descendingIterator(); it.hasNext(); ) {
TraceContextHolder<?> context = it.next();
if (context instanceof Transaction) {
return (Transaction) context;
