-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Instrument EventListener-annotated methods for observability #30089
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Would it be also possible to support propagating trace from For example by extending |
I'm not sure we should implement this actually. For me a consistent observability instrumentation makes sense if:
It can be used for a lot of cases where their execution is really quick, does not involve I/O, does not switch threads. Also, besides the method name and the event type there's nothing much we can extract as metadata. In the case of I think this issue is very close to #30832 ( In the case of |
Thanks for the feedback. I think I should've put more context in the description when I opened this issue. To me, this is analogous to #30335 in terms of the high-level flow and observability expectations - the difference being that the event isn't being transmitted over some external entity (message broker), but rather simply over the application context's event publishing facilities. By default, as listener is invoked in the same thread, everything should work out of the box, however if the listener is async then whatever work it does it won't be a part of the same trace. Additionally, I sometimes work with 3rd libraries that involve some kind of event-driven integration, requiring the developer to implement listener for certain event type - in this case the observation is not started in the 3rd party code, so Spring should probably take care of that when the event is published using
|
Potentially, in the above scenario there's some overlap between what's requested in this issue and #29977 (or #30832). |
I think you've lost me. Could you share concretely what observation are you expecting for |
Sorry for not being clear enough. I'll try with a very simple example: spring.application.name=sample
logging.pattern.level=%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}] @SpringBootApplication
@RestController
public class SampleApplication {
private static final Logger logger = LoggerFactory.getLogger(SampleApplication.class);
public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
}
@Autowired
private ApplicationEventPublisher eventPublisher;
@GetMapping(path = "/")
String home() {
logger.info("home handler invoked");
this.eventPublisher.publishEvent(new SampleEvent("sample"));
return "Hello World!";
}
@EventListener(SampleEvent.class)
void handleGreetEvent(SampleEvent event) {
logger.info("handled event: {}", event);
}
record SampleEvent(String name) {
}
} By default, observation context will get propagated and if I hit the endpoint, the following will be logged:
However, if I opt into running listener in a different thread using this: @Bean(name = "applicationEventMulticaster")
public SimpleApplicationEventMulticaster simpleApplicationEventMulticaster() {
SimpleApplicationEventMulticaster eventMulticaster = new SimpleApplicationEventMulticaster();
eventMulticaster.setTaskExecutor(new SimpleAsyncTaskExecutor());
return eventMulticaster;
} Then obviously from observability perspective things don't work as before:
The same effect is also present if I don't provide custom Other example is listening for events where publishing side is not a part of an existing observation. In this case I'd like to see a new observation started with the Hopefully this makes things a bit clearer. Forgot to note, when I'm mentioning |
Superseded by #31130 |
This is analogous to #29883, just for
@EventListener
.The text was updated successfully, but these errors were encountered: