Skip to content

Commit

Permalink
stacktrace capture when span ends
Browse files Browse the repository at this point in the history
  • Loading branch information
SylvainJuge committed Dec 21, 2023
1 parent f1c5748 commit 0068255
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ public class ElasticApmTracer implements Tracer {
private static volatile boolean classloaderCheckOk = false;

private final ConfigurationRegistry configurationRegistry;
private final StacktraceConfiguration stacktraceConfiguration;
private final ApmServerClient apmServerClient;
private final List<LifecycleListener> lifecycleListeners = new CopyOnWriteArrayList<>();
private final ObjectPool<Transaction> transactionPool;
Expand Down Expand Up @@ -203,7 +202,6 @@ private static void checkClassloader() {
this.metricRegistry = metricRegistry;
this.configurationRegistry = configurationRegistry;
this.reporter = reporter;
this.stacktraceConfiguration = configurationRegistry.getConfig(StacktraceConfiguration.class);
this.apmServerClient = apmServerClient;
this.ephemeralId = ephemeralId;
this.metaDataFuture = metaDataFuture;
Expand Down Expand Up @@ -574,12 +572,6 @@ private void reportSpan(Span span) {
// makes sure that parents are also non-discardable
span.setNonDiscardable();

long spanStackTraceMinDurationMs = stacktraceConfiguration.getSpanStackTraceMinDurationMs();
if (spanStackTraceMinDurationMs >= 0 && span.isSampled() && span.getStackFrames() == null) {
if (span.getDurationMs() >= spanStackTraceMinDurationMs) {
span.withStacktrace(new Throwable());
}
}
reporter.report(span);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import co.elastic.apm.agent.impl.context.ServiceTarget;
import co.elastic.apm.agent.impl.context.SpanContext;
import co.elastic.apm.agent.impl.context.Url;
import co.elastic.apm.agent.impl.stacktrace.StacktraceConfiguration;
import co.elastic.apm.agent.tracer.util.ResultUtil;
import co.elastic.apm.agent.sdk.logging.Logger;
import co.elastic.apm.agent.sdk.logging.LoggerFactory;
Expand All @@ -43,6 +44,7 @@ public class Span extends AbstractSpan<Span> implements Recyclable, co.elastic.a
private static final Logger logger = LoggerFactory.getLogger(Span.class);
public static final long MAX_LOG_INTERVAL_MICRO_SECS = TimeUnit.MINUTES.toMicros(5);
private static long lastSpanMaxWarningTimestamp;
private final StacktraceConfiguration stacktraceConfiguration;

/**
* A subtype describing this span (eg 'mysql', 'elasticsearch', 'jsf' etc)
Expand Down Expand Up @@ -86,6 +88,7 @@ public void setNonDiscardable() {

public Span(ElasticApmTracer tracer) {
super(tracer);
this.stacktraceConfiguration = tracer.getConfig(StacktraceConfiguration.class);
}

public <T> Span start(TraceContext.ChildContextCreator<T> childContextCreator, T parentContext, Baggage parentBaggage, long epochMicros) {
Expand Down Expand Up @@ -263,6 +266,14 @@ public void beforeEnd(long epochMicros) {

@Override
protected void afterEnd() {
// capture stack trace when the span ends, relies on this method being called synchronously from the instrumentation
long spanStackTraceMinDurationMs = stacktraceConfiguration.getSpanStackTraceMinDurationMs();
if (spanStackTraceMinDurationMs >= 0 && isSampled() && stackFrames == null) {
if (getDurationMs() >= spanStackTraceMinDurationMs) {
this.stacktrace = new Throwable();
}
}

// Why do we increment references of this span here?
// The only thing preventing the "this"-span from being recycled is the initial reference increment in onAfterStart()
// There are multiple ways in afterEnd() on how this reference may be decremented and therefore potentially causing recycling:
Expand Down

0 comments on commit 0068255

Please sign in to comment.