Skip to content

Commit 59bebbe

Browse files
authored
Attempt to fix reference counting issues in ES client instrumentation (#3256)
1 parent 1b7e461 commit 59bebbe

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

CHANGELOG.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Use subheadings with the "=====" level for adding notes for unreleased changes:
4040
[float]
4141
===== Bug fixes
4242
* Fixed SQS NoClassDefFoundError in AWS SDK instrumentation - {pull}3254[#3254]
43+
* Fixed reference counting issues in elasticsearch instrumentation - {pull}3256[#3256]
4344
4445
[[release-notes-1.x]]
4546
=== Java Agent version 1.x

apm-agent-plugins/apm-es-restclient-plugin/apm-es-restclient-plugin-common/src/main/java/co/elastic/apm/agent/esrestclient/ResponseListenerWrapper.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ ResponseListenerWrapper withClientSpan(ResponseListener delegate, Span<?> span)
5353
// Order is important due to visibility - write to span last on this (initiating) thread
5454
this.delegate = delegate;
5555
this.isClientSpan = true;
56-
this.context = span;
56+
setContext(span);
5757
return this;
5858
}
5959

6060
ResponseListenerWrapper withContextPropagation(ResponseListener delegate, AbstractSpan<?> context) {
6161
// Order is important due to visibility - write to span last on this (initiating) thread
6262
this.delegate = delegate;
6363
this.isClientSpan = false;
64-
this.context = context;
64+
setContext(context);
6565
return this;
6666
}
6767

@@ -149,10 +149,20 @@ private void finishClientSpan(@Nullable Response response, @Nullable Throwable t
149149
}
150150
}
151151

152+
private void setContext(@Nullable AbstractSpan<?> newContext) {
153+
if (newContext != null) {
154+
newContext.incrementReferences();
155+
}
156+
if (context != null) {
157+
context.decrementReferences();
158+
}
159+
context = newContext;
160+
}
161+
152162
@Override
153163
public void resetState() {
154164
delegate = null;
155-
context = null;
165+
setContext(null);
156166
isClientSpan = false;
157167
}
158168
}

0 commit comments

Comments
 (0)