opentelemetry-java-instrumentation icon indicating copy to clipboard operation
opentelemetry-java-instrumentation copied to clipboard

Structured concurrency support

Open ArtyomGabeev opened this issue 1 year ago • 2 comments

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

ArtyomGabeev avatar Apr 15 '24 17:04 ArtyomGabeev

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.

laurit avatar Apr 17 '24 09:04 laurit

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.

ArtyomGabeev avatar Apr 22 '24 14:04 ArtyomGabeev

Resolves with https://github.com/open-telemetry/opentelemetry-java-instrumentation/pull/11202

laurit avatar May 14 '24 12:05 laurit