|
3 | 3 | import static datadog.trace.agent.test.assertions.SpanMatcher.span; |
4 | 4 | import static datadog.trace.agent.test.assertions.TraceMatcher.SORT_BY_START_TIME; |
5 | 5 | import static datadog.trace.agent.test.assertions.TraceMatcher.trace; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
6 | 7 |
|
7 | 8 | import datadog.trace.agent.test.AbstractInstrumentationTest; |
| 9 | +import datadog.trace.api.CorrelationIdentifier; |
8 | 10 | import datadog.trace.api.Trace; |
9 | 11 | import java.util.concurrent.ThreadFactory; |
10 | 12 | import java.util.concurrent.TimeoutException; |
| 13 | +import java.util.concurrent.atomic.AtomicReference; |
11 | 14 | import org.junit.jupiter.api.DisplayName; |
12 | 15 | import org.junit.jupiter.api.Test; |
13 | 16 |
|
@@ -137,6 +140,68 @@ public void run() { |
137 | 140 | span().childOfPrevious().operationName("great-great-child"))); |
138 | 141 | } |
139 | 142 |
|
| 143 | + @DisplayName("test CorrelationIdentifier across virtual thread remount") |
| 144 | + @Test |
| 145 | + void testCorrelationIdentifierAcrossVirtualThreadRemount() throws InterruptedException { |
| 146 | + AtomicReference<String> parentTraceId = new AtomicReference<>(); |
| 147 | + AtomicReference<String> parentSpanId = new AtomicReference<>(); |
| 148 | + AtomicReference<String> traceIdBeforeRemount = new AtomicReference<>(); |
| 149 | + AtomicReference<String> spanIdBeforeRemount = new AtomicReference<>(); |
| 150 | + AtomicReference<String> traceIdAfterRemount = new AtomicReference<>(); |
| 151 | + AtomicReference<String> spanIdAfterRemount = new AtomicReference<>(); |
| 152 | + |
| 153 | + new Runnable() { |
| 154 | + @Override |
| 155 | + @Trace(operationName = "parent") |
| 156 | + public void run() { |
| 157 | + parentTraceId.set(CorrelationIdentifier.getTraceId()); |
| 158 | + parentSpanId.set(CorrelationIdentifier.getSpanId()); |
| 159 | + |
| 160 | + Thread thread = |
| 161 | + Thread.startVirtualThread( |
| 162 | + () -> { |
| 163 | + traceIdBeforeRemount.set(CorrelationIdentifier.getTraceId()); |
| 164 | + spanIdBeforeRemount.set(CorrelationIdentifier.getSpanId()); |
| 165 | + |
| 166 | + try { |
| 167 | + // Sleeping should park and later remount the virtual thread. |
| 168 | + Thread.sleep(10); |
| 169 | + } catch (InterruptedException e) { |
| 170 | + throw new RuntimeException(e); |
| 171 | + } |
| 172 | + |
| 173 | + traceIdAfterRemount.set(CorrelationIdentifier.getTraceId()); |
| 174 | + spanIdAfterRemount.set(CorrelationIdentifier.getSpanId()); |
| 175 | + }); |
| 176 | + |
| 177 | + try { |
| 178 | + thread.join(); |
| 179 | + } catch (InterruptedException e) { |
| 180 | + throw new RuntimeException(e); |
| 181 | + } |
| 182 | + } |
| 183 | + }.run(); |
| 184 | + |
| 185 | + assertEquals( |
| 186 | + parentTraceId.get(), |
| 187 | + traceIdBeforeRemount.get(), |
| 188 | + "trace id should be visible before the virtual thread remounts"); |
| 189 | + assertEquals( |
| 190 | + parentSpanId.get(), |
| 191 | + spanIdBeforeRemount.get(), |
| 192 | + "span id should be visible before the virtual thread remounts"); |
| 193 | + assertEquals( |
| 194 | + parentTraceId.get(), |
| 195 | + traceIdAfterRemount.get(), |
| 196 | + "trace id should survive a virtual thread remount"); |
| 197 | + assertEquals( |
| 198 | + parentSpanId.get(), |
| 199 | + spanIdAfterRemount.get(), |
| 200 | + "span id should survive a virtual thread remount"); |
| 201 | + |
| 202 | + assertTraces(trace(span().root().operationName("parent"))); |
| 203 | + } |
| 204 | + |
140 | 205 | /** Verifies the parent / child span relation. */ |
141 | 206 | void assertConnectedTrace() { |
142 | 207 | assertTraces( |
|
0 commit comments