Structured concurrency support
Is your feature request related to a problem? Please describe.
Hello team, Do we plan support automatic context propagation for structured concurrency? I understand it's in a preview, and I do not know if we provide instrumentation for preview features. If not, I would like to ask, what's needs to be done in order to implement such support in custom instrumentation.
Example:
span = tracer.spanBuilder("structConcSpan").startSpan();
try (var ignore = span.makeCurrent()) {
try (var taskScope = new StructuredTaskScope.ShutdownOnFailure()) {
StructuredTaskScope.Subtask<Void> fork1 = taskScope.fork(() -> {
System.out.println(
"structConcSpan: " +
Span.current().getSpanContext().getSpanId());
return null;
});
StructuredTaskScope.Subtask<Void> fork2 = taskScope.fork(() -> {
System.out.println(
"structConcSpan: " +
Span.current().getSpanContext().getSpanId());
return null;
});
taskScope.join();
}
} finally {
span.end();
}
Outputs zero span ids.
Describe the solution you'd like
Javaagent automatically propagates span context to forks submitted into StructuredTaskScope.
Describe alternatives you've considered
Capture current span, and propagate it via ScopedValue manually.
Additional context
May be related to https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/2527
We'd welcome a contribution for this
If not, I would like to ask, what's needs to be done in order to implement such support in custom instrumentation.
My guess would be that you'd need to instrument https://github.com/openjdk/jdk/blob/3ccbc6d4d014fb1ea92c47d270efd5f7ec05b0c3/src/java.base/share/classes/java/util/concurrent/StructuredTaskScope.java#L565 and wrap the passed callable with something similar to https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/instrumentation/executors/bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/executors/ContextPropagatingRunnable.java That should let you propagate context into the callable.
We'd welcome a contribution for this
If not, I would like to ask, what's needs to be done in order to implement such support in custom instrumentation.
My guess would be that you'd need to instrument https://github.com/openjdk/jdk/blob/3ccbc6d4d014fb1ea92c47d270efd5f7ec05b0c3/src/java.base/share/classes/java/util/concurrent/StructuredTaskScope.java#L565 and wrap the passed callable with something similar to https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/instrumentation/executors/bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/executors/ContextPropagatingRunnable.java That should let you propagate context into the callable.
I've opened an initial PR with initial instrumentation for structured concurrency.
Resolves with https://github.com/open-telemetry/opentelemetry-java-instrumentation/pull/11202