elastic / elastic/apm-agent-java

Possible Reactor context loss in TracedSubscriber and WebClientSubscriber

Aperta
#2,884 2 commenti 2 reazioni 1 assegnatario Rivendicata da @SylvainJuge Vedi su GitHub
agent-java community
Lingua principale
Java
Stelle
594
Fork
338
Merge medio
1g 13h
PR unite (30g)
25

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.