DataDog / dd-trace-java

Datadog APM client for Java
https://docs.datadoghq.com/tracing/languages/java
Apache License 2.0
576 stars 286 forks source link

DD tracing is not working with virtual threads for a micronaut 4 application #6468

Open Ajay1735 opened 9 months ago

Ajay1735 commented 9 months ago

Problem I have a Micronaut-based microservice on micronaut 4 and java 21. It is not sending traces to Datadog when used with virtual threads, but is able to send traces when used with event loops. Basically tracing is not working with changing the @ExecuteOn(TaskExecutors.IO) annotation to @ExecuteOn(TaskExecutors.BLOCKING) on controller.

I was trying to debug in local and using latest dd agent, I could see that the span is not created when the controller method is executed with virtual threads.

Used below block in my controller to fetch trace_id.

        Tracer tracer = GlobalTracer.get();
        Span currentSpan = tracer.activeSpan();
        if (currentSpan != null) {
            // Retrieve the trace ID
            String traceId = currentSpan.context().toTraceId();

            // Use the trace ID as needed
            log.info("dd.trace_id=" + traceId);
        } else {
            log.info("No active span found.");
        }
ketola commented 9 months ago

I have also noticed an issue with virtual threads. I have tried agent versions 1.26.1 and 1.27.0 to rule out the possibility of the regression coming from the agent upgrade. I also tested with both eclipse-temurin 21 and amazon-corretto 21, so it does not seem to be related to the java distribution. I can see the resources and their response times, but in the resource names the path parameter names have been replaced by question marks and I cannot see any spans or jdbc data from the calls.

I am using Quarkus 3.6.4 with virtual thread enabled for endpoints.

nayeem-kamal commented 9 months ago

@Ajay1735 Thank you for your feedback. At this time, the Java tracer does not have support for virtual threads with Java 21. Because of this, the behavior you are observing is not unexpected. This is something we are looking to add support for in the near future.

ketola commented 9 months ago

@Ajay1735 Thank you for your feedback. At this time, the Java tracer does not have support for virtual threads with Java 21. Because of this, the behavior you are observing is not unexpected. This is something we are looking to add support for in the near future.

@nayeem-kamal I wish you could try to increase the priority on this one as virtual threads are a core feature of Java 21 and it's considered have major performance improvements on Java web applications. Also the major frameworks like Quarkus, Micronaut and Spring are already supporting it.

Ajay1735 commented 9 months ago

@nayeem-kamal

At this time, the Java tracer does not have support for virtual threads with Java 21.

is this framework agnostic or it's not yet supported for specific frameworks like micronaut, quarkus but it's supported for spring

jhchee commented 7 months ago

@am312 Sorry to tag you here, will this be fixed with your PR? https://github.com/DataDog/dd-trace-java/pull/6789

am312 commented 6 months ago

@jhchee I think so but have not tested w/Micronaut using those particular annotations. If you find that something does not work, please file an issue with Datadog support. Thanks very much.

Ajay1735 commented 6 months ago

Have done basic testing with latest version of dd-agent, spans are getting created and tracing is working now. Thanks for the fix!

ketola commented 6 months ago

I can also confirm that spans are created in a Quarkus application with an endpoint annotated with @RunOnVirtualThread (though there seems to be a delay for the spans. I can see the request almost immediately but it took some time for the spans to appear, but anyway it's working :+1: )

severous commented 3 months ago

I am very interested in passing traces across virtual threads for this. Could you provide some demo code?