Skip to content

Commit 2c10280

Browse files
authored
Webclient fix NPE (#2702)
* prevent NPE when span is null * fix & simplify http client outcome
1 parent f241f91 commit 2c10280

File tree

1 file changed

+7
-14
lines changed
  • apm-agent-plugins/apm-spring-webflux/apm-spring-webclient-plugin/src/main/java/co/elastic/apm/agent/springwebclient

1 file changed

+7
-14
lines changed

apm-agent-plugins/apm-spring-webflux/apm-spring-webclient-plugin/src/main/java/co/elastic/apm/agent/springwebclient/WebClientSubscriber.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import co.elastic.apm.agent.collections.WeakConcurrentProviderImpl;
2222
import co.elastic.apm.agent.impl.Tracer;
23+
import co.elastic.apm.agent.impl.context.web.ResultUtil;
2324
import co.elastic.apm.agent.impl.transaction.Outcome;
2425
import co.elastic.apm.agent.impl.transaction.Span;
2526
import co.elastic.apm.agent.sdk.weakconcurrent.WeakMap;
@@ -72,13 +73,11 @@ public void onNext(T t) {
7273
boolean hasActivated = doEnter("onNext", span);
7374
Throwable thrown = null;
7475
try {
75-
if (t instanceof ClientResponse) {
76+
if (span != null && t instanceof ClientResponse) {
7677
ClientResponse clientResponse = (ClientResponse) t;
7778
int statusCode = clientResponse.rawStatusCode();
78-
if (400 <= statusCode && statusCode < 500 && span.getOutcome() == null) {
79-
span.withOutcome(Outcome.FAILURE);
80-
}
81-
span.getContext().getHttp().withStatusCode(clientResponse.rawStatusCode());
79+
span.withOutcome(ResultUtil.getOutcomeByHttpClientStatus(statusCode));
80+
span.getContext().getHttp().withStatusCode(statusCode);
8281
}
8382
subscriber.onNext(t);
8483
} catch (Throwable e) {
@@ -96,7 +95,6 @@ public void onError(Throwable throwable) {
9695
boolean hasActivated = doEnter("onError", span);
9796
try {
9897
subscriber.onError(throwable);
99-
span = span.withOutcome(Outcome.FAILURE);
10098
} finally {
10199
doExit(hasActivated, "onError", span);
102100
discardIf(true);
@@ -110,9 +108,6 @@ public void onComplete() {
110108
boolean hasActivated = doEnter("onComplete", span);
111109
try {
112110
subscriber.onComplete();
113-
if (span.getOutcome() == null) {
114-
span.withOutcome(Outcome.SUCCESS);
115-
}
116111
} finally {
117112
doExit(hasActivated, "onComplete", span);
118113
discardIf(true);
@@ -190,12 +185,10 @@ private void cancelSpan() {
190185
Span span = getSpan();
191186
debugTrace(true, "cancelSpan", span);
192187
try {
193-
if (span == null) {
194-
return;
188+
if (span != null) {
189+
endSpan(null, span);
190+
spanMap.remove(this);
195191
}
196-
endSpan(null, span);
197-
198-
spanMap.remove(this);
199192
} finally {
200193
debugTrace(false, "cancelSpan", span);
201194
}

0 commit comments

Comments
 (0)