-
Notifications
You must be signed in to change notification settings - Fork 319
Add context tracking support for virtual threads #10040
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
base: master
Are you sure you want to change the base?
Conversation
BenchmarksStartupParameters
See matching parameters
SummaryFound 0 performance improvements and 0 performance regressions! Performance is the same for 59 metrics, 6 unstable metrics. Startup time reports for petclinicgantt
title petclinic - global startup overhead: candidate=1.57.0-SNAPSHOT~1934b72f79, baseline=1.57.0-SNAPSHOT~4e48384724
dateFormat X
axisFormat %s
section tracing
Agent [baseline] (1.079 s) : 0, 1078927
Total [baseline] (10.825 s) : 0, 10824651
Agent [candidate] (1.082 s) : 0, 1081728
Total [candidate] (10.91 s) : 0, 10909776
section appsec
Agent [baseline] (1.27 s) : 0, 1270040
Total [baseline] (11.119 s) : 0, 11119137
Agent [candidate] (1.266 s) : 0, 1265851
Total [candidate] (11.175 s) : 0, 11175206
section iast
Agent [baseline] (1.225 s) : 0, 1224616
Total [baseline] (11.24 s) : 0, 11240468
Agent [candidate] (1.222 s) : 0, 1222483
Total [candidate] (11.224 s) : 0, 11223962
section profiling
Agent [baseline] (1.204 s) : 0, 1203730
Total [baseline] (10.997 s) : 0, 10997494
Agent [candidate] (1.206 s) : 0, 1205955
Total [candidate] (10.991 s) : 0, 10991449
gantt
title petclinic - break down per module: candidate=1.57.0-SNAPSHOT~1934b72f79, baseline=1.57.0-SNAPSHOT~4e48384724
dateFormat X
axisFormat %s
section tracing
crashtracking [baseline] (1.185 ms) : 0, 1185
crashtracking [candidate] (1.189 ms) : 0, 1189
BytebuddyAgent [baseline] (646.487 ms) : 0, 646487
BytebuddyAgent [candidate] (648.429 ms) : 0, 648429
GlobalTracer [baseline] (282.106 ms) : 0, 282106
GlobalTracer [candidate] (282.424 ms) : 0, 282424
AppSec [baseline] (32.222 ms) : 0, 32222
AppSec [candidate] (32.314 ms) : 0, 32314
Debugger [baseline] (68.091 ms) : 0, 68091
Debugger [candidate] (68.296 ms) : 0, 68296
Remote Config [baseline] (649.076 µs) : 0, 649
Remote Config [candidate] (652.894 µs) : 0, 653
Telemetry [baseline] (8.979 ms) : 0, 8979
Telemetry [candidate] (9.096 ms) : 0, 9096
Flare Poller [baseline] (3.695 ms) : 0, 3695
Flare Poller [candidate] (3.799 ms) : 0, 3799
section appsec
crashtracking [baseline] (1.189 ms) : 0, 1189
crashtracking [candidate] (1.189 ms) : 0, 1189
BytebuddyAgent [baseline] (690.458 ms) : 0, 690458
BytebuddyAgent [candidate] (688.658 ms) : 0, 688658
GlobalTracer [baseline] (261.205 ms) : 0, 261205
GlobalTracer [candidate] (259.768 ms) : 0, 259768
IAST [baseline] (24.956 ms) : 0, 24956
IAST [candidate] (24.605 ms) : 0, 24605
AppSec [baseline] (175.784 ms) : 0, 175784
AppSec [candidate] (174.001 ms) : 0, 174001
Debugger [baseline] (67.413 ms) : 0, 67413
Debugger [candidate] (68.747 ms) : 0, 68747
Remote Config [baseline] (678.926 µs) : 0, 679
Remote Config [candidate] (756.122 µs) : 0, 756
Telemetry [baseline] (8.923 ms) : 0, 8923
Telemetry [candidate] (8.845 ms) : 0, 8845
Flare Poller [baseline] (3.944 ms) : 0, 3944
Flare Poller [candidate] (3.775 ms) : 0, 3775
section iast
crashtracking [baseline] (1.185 ms) : 0, 1185
crashtracking [candidate] (1.192 ms) : 0, 1192
BytebuddyAgent [baseline] (789.569 ms) : 0, 789569
BytebuddyAgent [candidate] (790.387 ms) : 0, 790387
GlobalTracer [baseline] (256.146 ms) : 0, 256146
GlobalTracer [candidate] (255.806 ms) : 0, 255806
IAST [baseline] (27.004 ms) : 0, 27004
IAST [candidate] (26.852 ms) : 0, 26852
AppSec [baseline] (35.862 ms) : 0, 35862
AppSec [candidate] (35.068 ms) : 0, 35068
Debugger [baseline] (66.893 ms) : 0, 66893
Debugger [candidate] (65.478 ms) : 0, 65478
Remote Config [baseline] (567.941 µs) : 0, 568
Remote Config [candidate] (567.779 µs) : 0, 568
Telemetry [baseline] (8.539 ms) : 0, 8539
Telemetry [candidate] (8.401 ms) : 0, 8401
Flare Poller [baseline] (3.486 ms) : 0, 3486
Flare Poller [candidate] (3.441 ms) : 0, 3441
section profiling
crashtracking [baseline] (1.188 ms) : 0, 1188
crashtracking [candidate] (1.196 ms) : 0, 1196
BytebuddyAgent [baseline] (701.194 ms) : 0, 701194
BytebuddyAgent [candidate] (703.136 ms) : 0, 703136
GlobalTracer [baseline] (220.922 ms) : 0, 220922
GlobalTracer [candidate] (221.085 ms) : 0, 221085
AppSec [baseline] (32.217 ms) : 0, 32217
AppSec [candidate] (32.135 ms) : 0, 32135
Debugger [baseline] (68.219 ms) : 0, 68219
Debugger [candidate] (67.969 ms) : 0, 67969
Remote Config [baseline] (630.172 µs) : 0, 630
Remote Config [candidate] (631.17 µs) : 0, 631
Telemetry [baseline] (8.988 ms) : 0, 8988
Telemetry [candidate] (8.994 ms) : 0, 8994
Flare Poller [baseline] (3.829 ms) : 0, 3829
Flare Poller [candidate] (3.789 ms) : 0, 3789
ProfilingAgent [baseline] (96.936 ms) : 0, 96936
ProfilingAgent [candidate] (97.331 ms) : 0, 97331
Profiling [baseline] (97.53 ms) : 0, 97530
Profiling [candidate] (97.921 ms) : 0, 97921
Startup time reports for insecure-bankgantt
title insecure-bank - global startup overhead: candidate=1.57.0-SNAPSHOT~1934b72f79, baseline=1.57.0-SNAPSHOT~4e48384724
dateFormat X
axisFormat %s
section tracing
Agent [baseline] (1.08 s) : 0, 1079546
Total [baseline] (8.758 s) : 0, 8757617
Agent [candidate] (1.088 s) : 0, 1088488
Total [candidate] (8.755 s) : 0, 8755295
section iast
Agent [baseline] (1.233 s) : 0, 1232710
Total [baseline] (9.491 s) : 0, 9491256
Agent [candidate] (1.222 s) : 0, 1222355
Total [candidate] (9.487 s) : 0, 9486611
gantt
title insecure-bank - break down per module: candidate=1.57.0-SNAPSHOT~1934b72f79, baseline=1.57.0-SNAPSHOT~4e48384724
dateFormat X
axisFormat %s
section tracing
crashtracking [baseline] (1.192 ms) : 0, 1192
crashtracking [candidate] (1.203 ms) : 0, 1203
BytebuddyAgent [baseline] (647.573 ms) : 0, 647573
BytebuddyAgent [candidate] (652.573 ms) : 0, 652573
GlobalTracer [baseline] (282.227 ms) : 0, 282227
GlobalTracer [candidate] (284.288 ms) : 0, 284288
AppSec [baseline] (32.225 ms) : 0, 32225
AppSec [candidate] (32.581 ms) : 0, 32581
Debugger [baseline] (67.276 ms) : 0, 67276
Debugger [candidate] (68.537 ms) : 0, 68537
Remote Config [baseline] (647.386 µs) : 0, 647
Remote Config [candidate] (645.294 µs) : 0, 645
Telemetry [baseline] (9.001 ms) : 0, 9001
Telemetry [candidate] (9.144 ms) : 0, 9144
Flare Poller [baseline] (3.667 ms) : 0, 3667
Flare Poller [candidate] (3.737 ms) : 0, 3737
section iast
crashtracking [baseline] (1.215 ms) : 0, 1215
crashtracking [candidate] (1.192 ms) : 0, 1192
BytebuddyAgent [baseline] (798.049 ms) : 0, 798049
BytebuddyAgent [candidate] (790.664 ms) : 0, 790664
GlobalTracer [baseline] (257.479 ms) : 0, 257479
GlobalTracer [candidate] (255.653 ms) : 0, 255653
IAST [baseline] (27.09 ms) : 0, 27090
IAST [candidate] (26.982 ms) : 0, 26982
AppSec [baseline] (34.387 ms) : 0, 34387
AppSec [candidate] (34.877 ms) : 0, 34877
Debugger [baseline] (66.047 ms) : 0, 66047
Debugger [candidate] (65.152 ms) : 0, 65152
Remote Config [baseline] (558.01 µs) : 0, 558
Remote Config [candidate] (571.499 µs) : 0, 571
Telemetry [baseline] (8.617 ms) : 0, 8617
Telemetry [candidate] (8.44 ms) : 0, 8440
Flare Poller [baseline] (3.504 ms) : 0, 3504
Flare Poller [candidate] (3.458 ms) : 0, 3458
LoadParameters
See matching parameters
SummaryFound 0 performance improvements and 2 performance regressions! Performance is the same for 18 metrics, 16 unstable metrics.
Request duration reports for petclinicgantt
title petclinic - request duration [CI 0.99] : candidate=1.57.0-SNAPSHOT~1934b72f79, baseline=1.57.0-SNAPSHOT~4e48384724
dateFormat X
axisFormat %s
section baseline
no_agent (18.22 ms) : 18032, 18407
. : milestone, 18220,
appsec (18.55 ms) : 18361, 18739
. : milestone, 18550,
code_origins (18.119 ms) : 17938, 18301
. : milestone, 18119,
iast (17.574 ms) : 17399, 17749
. : milestone, 17574,
profiling (19.141 ms) : 18945, 19336
. : milestone, 19141,
tracing (17.735 ms) : 17560, 17911
. : milestone, 17735,
section candidate
no_agent (17.233 ms) : 17055, 17410
. : milestone, 17233,
appsec (18.617 ms) : 18428, 18806
. : milestone, 18617,
code_origins (17.676 ms) : 17498, 17854
. : milestone, 17676,
iast (17.768 ms) : 17593, 17944
. : milestone, 17768,
profiling (19.075 ms) : 18883, 19266
. : milestone, 19075,
tracing (17.887 ms) : 17710, 18064
. : milestone, 17887,
Request duration reports for insecure-bankgantt
title insecure-bank - request duration [CI 0.99] : candidate=1.57.0-SNAPSHOT~1934b72f79, baseline=1.57.0-SNAPSHOT~4e48384724
dateFormat X
axisFormat %s
section baseline
no_agent (1.219 ms) : 1207, 1231
. : milestone, 1219,
iast (3.192 ms) : 3147, 3238
. : milestone, 3192,
iast_FULL (5.671 ms) : 5615, 5726
. : milestone, 5671,
iast_GLOBAL (3.575 ms) : 3521, 3628
. : milestone, 3575,
profiling (2.11 ms) : 2091, 2128
. : milestone, 2110,
tracing (1.801 ms) : 1786, 1816
. : milestone, 1801,
section candidate
no_agent (1.195 ms) : 1183, 1206
. : milestone, 1195,
iast (3.222 ms) : 3178, 3267
. : milestone, 3222,
iast_FULL (5.942 ms) : 5882, 6001
. : milestone, 5942,
iast_GLOBAL (3.602 ms) : 3549, 3655
. : milestone, 3602,
profiling (2.003 ms) : 1985, 2021
. : milestone, 2003,
tracing (1.84 ms) : 1823, 1857
. : milestone, 1840,
DacapoParameters
See matching parameters
SummaryFound 0 performance improvements and 0 performance regressions! Performance is the same for 10 metrics, 2 unstable metrics. Execution time for biojavagantt
title biojava - execution time [CI 0.99] : candidate=1.57.0-SNAPSHOT~1934b72f79, baseline=1.57.0-SNAPSHOT~4e48384724
dateFormat X
axisFormat %s
section baseline
no_agent (14.754 s) : 14754000, 14754000
. : milestone, 14754000,
appsec (14.746 s) : 14746000, 14746000
. : milestone, 14746000,
iast (18.169 s) : 18169000, 18169000
. : milestone, 18169000,
iast_GLOBAL (17.956 s) : 17956000, 17956000
. : milestone, 17956000,
profiling (14.529 s) : 14529000, 14529000
. : milestone, 14529000,
tracing (14.719 s) : 14719000, 14719000
. : milestone, 14719000,
section candidate
no_agent (15.399 s) : 15399000, 15399000
. : milestone, 15399000,
appsec (14.87 s) : 14870000, 14870000
. : milestone, 14870000,
iast (18.146 s) : 18146000, 18146000
. : milestone, 18146000,
iast_GLOBAL (18.221 s) : 18221000, 18221000
. : milestone, 18221000,
profiling (14.458 s) : 14458000, 14458000
. : milestone, 14458000,
tracing (14.768 s) : 14768000, 14768000
. : milestone, 14768000,
Execution time for tomcatgantt
title tomcat - execution time [CI 0.99] : candidate=1.57.0-SNAPSHOT~1934b72f79, baseline=1.57.0-SNAPSHOT~4e48384724
dateFormat X
axisFormat %s
section baseline
no_agent (1.474 ms) : 1462, 1485
. : milestone, 1474,
appsec (2.475 ms) : 2424, 2527
. : milestone, 2475,
iast (2.215 ms) : 2150, 2279
. : milestone, 2215,
iast_GLOBAL (2.266 ms) : 2201, 2331
. : milestone, 2266,
profiling (2.09 ms) : 2036, 2144
. : milestone, 2090,
tracing (2.055 ms) : 2004, 2105
. : milestone, 2055,
section candidate
no_agent (1.475 ms) : 1463, 1486
. : milestone, 1475,
appsec (3.679 ms) : 3463, 3895
. : milestone, 3679,
iast (2.222 ms) : 2158, 2287
. : milestone, 2222,
iast_GLOBAL (2.262 ms) : 2197, 2327
. : milestone, 2262,
profiling (2.495 ms) : 2334, 2657
. : milestone, 2495,
tracing (2.06 ms) : 2009, 2110
. : milestone, 2060,
|
56e90dc to
2e3f97b
Compare
...rc/main/java/datadog/trace/instrumentation/java/lang/jdk21/VirtualThreadInstrumentation.java
Outdated
Show resolved
Hide resolved
| */ | ||
| @SuppressWarnings("unused") | ||
| @AutoService(InstrumenterModule.class) | ||
| public final class VirtualThreadInstrumentation extends InstrumenterModule.Tracing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes TaskRunnerInstrumentation unnecessary. Perhaps we could just remove it now and move its test VirtualThreadPerTaskExecutorTest into this project.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The TaskRunnerInstrumentation is still needed. Its unit tests end with disconnected traces and unfinished spans without it.
I also included the (direct) virtual thread API into the concurrent smoke test (21) to make sure both instrumentations don't mess with each other.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just double-checked, and the VirtualThreadPerTaskExecutorTest in TaskRunnerInstrumentation passes when TaskRunnerInstrumentation is disabled. Remember to add testImplementation project(':dd-java-agent:instrumentation:java:java-lang:java-lang-21.0') when running ./gradlew :dd-java-agent:instrumentation:java:java-concurrent:java-concurrent-21.0:test -PtestJvm=21
ygree
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Should we remove TaskRunnerInstrumentation then now?
| class VirtualThreadApiTest extends InstrumentationSpecification { | ||
| def "test builder - started"() { | ||
| setup: | ||
| def threadBuilder = Thread.ofVirtual().name("builder - started") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could also add a test for Thread.startVirtualThread(). It works, I checked, but since it's another public method, it'd be nice to have a test just to be sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dd-java-agent/instrumentation/java/java-lang/java-lang-21.0/src/test/java/JavaAsyncChild.java
Show resolved
Hide resolved
dd-java-agent/instrumentation/java/java-lang/java-lang-21.0/build.gradle
Show resolved
Hide resolved
Add suppressed from Advice Improve test naming
Add context tracking for VirtualThread API
745324c to
1934b72
Compare

What Does This Do
This pull request brings context tracking for the VirtualThread APIs from
java.lang.Before that, only virtual thread executor (
newThreadPerTask) was supported fromjava.concurrency.Motivation
Context tracking was broken when creating a virtual thread. It's runnable did not inherits from current context when created.
Additional Notes
This PR instruments
VirtualThreadto capture active state at creation, active it on continuation mount, and close the scope from activation on continuation unmount.The instrumentation uses two context stores. The first from
Runnable(asVirtualThreadinherits fromRunnable) to store the capturedStateto restore later. It additionally stores theAgentScopeto be able to close it later as activation / close is not done around the same method (so passing the scope fromOnMethodEnter/OnMethodExitusing advice return value is not possible).Instrumenting the internal
VirtualThread.runContinuation()method does not work as the current thread is still the carrier thread and not a virtual thread. Activating the state when on the carrier thread (ie a platform thread) would store the active context intoThreadLocalusing the platform thread as key, making the tracer unable to retrieve the stored context from the current virtual thread (ThreadLocalwill not return the value associated to the underlying platform thread as they are considered to be different).An alternative implementation could be to instrument
jdk.internal.vm.Continuationand its children for state capture (at continuation creation<init>), state activation (aftermount), and scope close (beforeunmount). I ended up instrumentingVirtualThreadinstead to instrumentjava.langrather thanjdk.internal.vm, and because thread set is done inVirtualThreaditself, making it simpler to hook context tracking where thread activity happens.Contributor Checklist
type:and (comp:orinst:) labels in addition to any useful labelsclose,fixor any linking keywords when referencing an issue.Use
solvesinstead, and assign the PR milestone to the issueJira ticket: APMLP-647 / APMS-17629 / #9984 / #6468