-
Notifications
You must be signed in to change notification settings - Fork 992
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
Pass observation to tasks #5491
base: main
Are you sure you want to change the base?
Pass observation to tasks #5491
Conversation
As developer, I would like parallelize my code and observe subtasks (https://stackoverflow.com/questions/78746378/spring-boot-3-micrometer-tracing-in-mdc/78765658#78765658 https://stackoverflow.com/questions/78889603/traceid-propagation-to-virtual-thread). I found solution with `ContextExecutorService`, but according to discussion micrometer-metrics/context-propagation#295, ContextExecutorService is low level API, which should not be used by application code. `Observation` is enough high level to be used by application code.
As explained in micrometer-metrics/context-propagation#295 (comment) While context-propagation is a lower level API it needs to be used in custom cases when users want to propagate context to |
At the risk of saying the same thing @chemicL already said but in a different way: Maybe we could add something to our Micrometer Observation documentation to talk about using the context-propagation library as a solution for propagating Observation across thread switches, such as when using an ExecutorService. |
Tests here might help, particularly this one: Lines 57 to 70 in 4a29322
You should be able to open a scope for your parent Observation inside of the task and then create a child observation (and a child scope). The limiting thing is that the parent observation should be "visible" from the code of the task but in your use-case this might not be an issue. Something like this: ExecutorService executor = Executors.newSingleThreadExecutor();
Observation parent = Observation.start("parent", registry);
executor.submit(() -> {
try (Observation.Scope ignored = parent.openScope()) {
// create a child observation here and do what you want
}
});
// collect futures and wait for every future to finish their job
parent.stop(); This should work with parallel streams too. |
@jonatan-ivanov, let me recap
So question is simple: what is correct solution to solve my problem: passing observation to tasks. Manual solution for typical problem looks as reinventing wheel UpdateMaybe my question could be much simple: I would like pass observation to task. I would like add a static method to Observation API. Should implementation use |
As developer, I would like parallelize my code and observe subtasks (https://stackoverflow.com/questions/78746378/spring-boot-3-micrometer-tracing-in-mdc/78765658#78765658 https://stackoverflow.com/questions/78889603/traceid-propagation-to-virtual-thread).
I found solution with
ContextExecutorService
, but according to discussion micrometer-metrics/context-propagation#295, ContextExecutorService is low level API, which should not be used by application code.Observation
is enough high level to be used by application code.