elastic / elastic/apm-agent-java
Improve docs and add error log related to illegal activation of transaction
- Dominant language
- Java
- Stars
- 594
- Forks
- 338
- Avg merge
- 1d 13h
- Merged PRs (30d)
- 25
Description
Using multiple nested transactions, `ElasticApm.currentTransaction()` returns oldest transaction instead of newest created.
Activating a transaction **transaction.activate()** [pushes the transaction to the head](https://docs.oracle.com/javase/7/docs/api/java/util/Deque.html#push(E)) of the active stack:
ElasticApmTracer.java:
```
public void activate(TraceContextHolder holder) {
if (logger.isDebugEnabled()) {
logger.debug("Activating {} on thread {}", holder, Thread.currentThread().getId());
}
((Deque)this.activeStack.get()).push(holder);
}
```
But when retrieving it, the last element of the stack is checked:
ElasticApmTracer.java:
```
public Transaction currentTransaction() {
TraceContextHolder bottomOfStack = (TraceContextHolder)((Deque)this.activeStack.get()).peekLast();
if (bottomOfStack instanceof Transaction) {
return (Transaction)bottomOfStack;
....................
```
In the above method, **peekLast()** returns the oldest transaction instead of newly pushed one.
As multiple methods use [Deque::peek](https://docs.oracle.com/javase/7/docs/api/java/util/Queue.html#peek())(first element) to get active transaction, the logic inside **currentTransaction()** method should be change to iterate from first to last element:
```
@Nullable
public Transaction currentTransaction() {
- TraceContextHolder bottomOfStack = (TraceContextHolder)((Deque)this.activeStack.get()).peekLast();
+ TraceContextHolder bottomOfStack = (TraceContextHolder)((Deque)this.activeStack.get()).peek();
if (bottomOfStack instanceof Transaction) {
return (Transaction)bottomOfStack;
} else {
- Iterator it = ((Deque)this.activeStack.get()).descendingIterator();
+ Iterator it = ((Deque)this.activeStack.get()).iterator();
TraceContextHolder context;
do {
```
elastic-apm-agent-1.9.0
Contributor guide
Assessment
This issue has not been assessed yet.