Skip to content
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

Structured concurrency support #11139

Closed
ArtyomGabeev opened this issue Apr 15, 2024 · 3 comments
Closed

Structured concurrency support #11139

ArtyomGabeev opened this issue Apr 15, 2024 · 3 comments
Labels
contribution welcome Request makes sense, maintainers probably won't have time, contribution would be welcome enhancement New feature or request

Comments

@ArtyomGabeev
Copy link
Contributor

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 #2527

@ArtyomGabeev ArtyomGabeev added enhancement New feature or request needs triage New issue that requires triage labels Apr 15, 2024
@laurit laurit added contribution welcome Request makes sense, maintainers probably won't have time, contribution would be welcome and removed needs triage New issue that requires triage labels Apr 17, 2024
@laurit
Copy link
Contributor

laurit commented Apr 17, 2024

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.

@ArtyomGabeev
Copy link
Contributor Author

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.

@laurit
Copy link
Contributor

laurit commented May 14, 2024

Resolves with #11202

@laurit laurit closed this as completed May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution welcome Request makes sense, maintainers probably won't have time, contribution would be welcome enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants