Skip to content

Commit

Permalink
Fix invalid currentTransaction() logic (elastic#3294)
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasKunz authored Aug 25, 2023
1 parent ca96d4e commit 3a2163e
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Use subheadings with the "=====" level for adding notes for unreleased changes:
* Prevent bad serialization in edge cases for span compression - {pull}3293[#3293]
* Allow overriding of transaction type for Servlet-API transactions - {pull}3226[#3226]
* Fix micrometer histogram serialization - {pull}3290[#3290]
* Fix transactions not being correctly handled in certain edge cases - {pull}3294[#3294]
[float]
===== Features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import co.elastic.apm.agent.impl.transaction.AbstractSpan;
import co.elastic.apm.agent.impl.transaction.ElasticContext;
import co.elastic.apm.agent.impl.transaction.ElasticContextWrapper;
import co.elastic.apm.agent.impl.transaction.Transaction;
import co.elastic.apm.agent.sdk.logging.Logger;
import co.elastic.apm.agent.sdk.logging.LoggerFactory;

Expand Down Expand Up @@ -65,12 +64,6 @@ class ActiveStack {
this.emptyContext = emptyContextForTracer;
}

@Nullable
Transaction currentTransaction() {
final ElasticContext<?> bottomOfStack = activeContextStack.peekLast();
return bottomOfStack != null ? bottomOfStack.getTransaction() : null;
}

/**
* @return the current context, potentially empty when no span, transaction or baggage is currently active.
*/
Expand All @@ -94,7 +87,7 @@ boolean activate(ElasticContext<?> context, List<ActivationListener> activationL
if (activeContextStack.size() == stackMaxDepth) {
if (overflowCounter == 0) {
logger.error(String.format("Activation stack depth reached its maximum - %s. This is likely related to activation" +
" leak. Current transaction: %s", stackMaxDepth, currentTransaction()),
" leak. Current transaction: %s", stackMaxDepth, currentContext().getTransaction()),
new Throwable("Stack of threshold-crossing activation: ")
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ private Transaction createTransaction() {
@Override
@Nullable
public Transaction currentTransaction() {
return activeStack.get().currentTransaction();
return currentContext().getTransaction();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import co.elastic.apm.agent.configuration.CoreConfiguration;
import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.impl.Tracer;
import co.elastic.apm.agent.impl.baggage.Baggage;
import co.elastic.apm.agent.impl.sampling.Sampler;
import co.elastic.apm.agent.sdk.logging.Logger;
Expand Down Expand Up @@ -177,17 +176,7 @@ public boolean asChildOf(TraceContext child, @Nullable Object carrier, HeaderGet
return false;
}
};
private static final ChildContextCreator<Tracer> FROM_ACTIVE = new ChildContextCreator<Tracer>() {
@Override
public boolean asChildOf(TraceContext child, Tracer tracer) {
final AbstractSpan<?> active = tracer.getActive();
if (active != null) {
return fromParent().asChildOf(child, active);

}
return false;
}
};
private static final ChildContextCreator<Object> AS_ROOT = new ChildContextCreator<Object>() {
@Override
public boolean asChildOf(TraceContext child, Object ignore) {
Expand Down Expand Up @@ -292,10 +281,6 @@ public static <C> HeaderChildContextCreator<byte[], C> getFromTraceContextBinary
return (HeaderChildContextCreator<byte[], C>) FROM_TRACE_CONTEXT_BINARY_HEADERS;
}

public static ChildContextCreator<Tracer> fromActive() {
return FROM_ACTIVE;
}

public static ChildContextCreator<TraceContext> fromParentContext() {
return FROM_PARENT_CONTEXT;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ void testStartSpanAfterTransactionHasEnded() {

try (Scope transactionScope = transaction.activateInScope()) {
assertThat(tracerImpl.getActive()).isEqualTo(transaction);
final Span span = tracerImpl.startSpan(TraceContext.fromActive(), tracerImpl, Baggage.EMPTY);
final Span span = tracerImpl.startSpan(transaction, Baggage.EMPTY, -1);
assertThat(span).isNotNull();
try (Scope scope = span.activateInScope()) {
assertThat(tracerImpl.currentTransaction()).isNotNull();
Expand Down

0 comments on commit 3a2163e

Please sign in to comment.