elastic / elastic/apm-agent-java
Possible Reactor context loss in TracedSubscriber and WebClientSubscriber
- Dominant language
- Java
- Stars
- 594
- Forks
- 338
- Avg merge
- 1d 13h
- Merged PRs (30d)
- 25
Description
After activating/attaching of agent to java process all custom information stored by Mono.contextWrite(..) becomes unreachable later in subscriber
## Steps to reproduce
Store something in the context
```
webClient
.get()
.uri(...)
.exchangeToMono(...)
.contextWrite(ctx -> ctx.put("KEY_1", "VAL_1"));
```
And later if we'll try to get `context.get("KEY_1")` this info from the context in a subscriber we'll get `NoSuchElementException`
Without activating/attaching of agent we'll get this info from the context.
Looks like the problem is in `co.elastic.apm.agent.reactor.TracedSubscriber` and `co.elastic.apm.agent.springwebclient.WebClientSubscriber`.
In `TracedSubscriber` the real subscriber's context is stored in the constructor but should be obtained each time in currentContext()
```
@Override
public Context currentContext() {
final AbstractSpan context = getContext();
if (context != null) {
return subscriber.currentContext().put(AbstractSpan.class, context);
} else {
return subscriber.currentContext();
}
}
```
In `WebClientSubscriber` default implementation of method `currentContext()` in parent `CoreSubscriber` recreates empty context but should delegate this call to a real subscriber:
```
@Override
public Context currentContext() {
return subscriber.currentContext();
}
```
This is a draft of pull request https://github.com/elastic/apm-agent-java/pull/2885
Contributor guide
Assessment
This issue has not been assessed yet.