-
Notifications
You must be signed in to change notification settings - Fork 314
Reuse SpanBuilder within a Thread #9537
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?
Changes from 32 commits
d2cc4ec
d83ff16
045e4c9
95e6f17
55b56e7
8de4b30
8adfe8f
5763a31
353afa2
23b03b8
b588b7a
68a6f03
ec730ec
2d22b41
55bd500
2258db5
079d1f5
a55095e
26482bf
e5161ef
40ba13d
70eccff
5b594a2
65fa1d7
136329e
2e7da54
98d5603
ff9acbe
4475e42
4d2ec09
aea9d3b
8bb8f29
6ed469b
e26959d
66f62fb
94970a4
388a288
86c03d9
8e3ee47
0b968ae
097e742
826f528
bd4b957
e9cf952
55169c7
eb2996d
5fd6e1d
e46f8f5
054485a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package datadog.environment; | ||
|
||
import java.lang.invoke.MethodHandle; | ||
import java.lang.invoke.MethodHandles; | ||
import java.lang.invoke.MethodType; | ||
|
||
/** | ||
* Helper class for working with Threads | ||
* | ||
* <p>Uses feature detection and provides static helpers to work with different versions of Java | ||
* | ||
* <p>This class is designed to use MethodHandles that constant propagate to minimize the overhead | ||
*/ | ||
public final class ThreadUtils { | ||
static final MethodHandle H_IS_VIRTUAL = lookupIsVirtual(); | ||
static final MethodHandle H_ID = lookupId(); | ||
|
||
private ThreadUtils() {} | ||
|
||
/** Provides the best id available for the Thread Uses threadId on 19+; getId on older JVMs */ | ||
public static final long threadId(Thread thread) { | ||
try { | ||
return (long) H_ID.invoke(thread); | ||
} catch (Throwable t) { | ||
return 0L; | ||
} | ||
} | ||
|
||
/** Indicates whether virtual threads are supported on this JVM */ | ||
public static final boolean supportsVirtualThreads() { | ||
return (H_IS_VIRTUAL != null); | ||
} | ||
|
||
/** Indicates if the current thread is a virtual thread */ | ||
public static final boolean isCurrentThreadVirtual() { | ||
// H_IS_VIRTUAL will constant propagate -- then dead code eliminate -- and inline as needed | ||
try { | ||
return (H_IS_VIRTUAL != null) && (boolean) H_IS_VIRTUAL.invoke(Thread.currentThread()); | ||
} catch (Throwable t) { | ||
return false; | ||
} | ||
} | ||
|
||
/** Indicates if the provided thread is a virtual thread */ | ||
public static final boolean isVirtual(Thread thread) { | ||
// H_IS_VIRTUAL will constant propagate -- then dead code eliminate -- and inline as needed | ||
try { | ||
return (H_IS_VIRTUAL != null) && (boolean) H_IS_VIRTUAL.invoke(thread); | ||
} catch (Throwable t) { | ||
return false; | ||
} | ||
} | ||
|
||
private static final MethodHandle lookupIsVirtual() { | ||
try { | ||
return MethodHandles.lookup() | ||
.findVirtual(Thread.class, "isVirtual", MethodType.methodType(boolean.class)); | ||
} catch (NoSuchMethodException | IllegalAccessException e) { | ||
return null; | ||
} | ||
} | ||
|
||
private static final MethodHandle lookupId() { | ||
MethodHandle threadIdHandle = lookupThreadId(); | ||
return threadIdHandle != null ? threadIdHandle : lookupGetId(); | ||
} | ||
|
||
private static final MethodHandle lookupThreadId() { | ||
try { | ||
return MethodHandles.lookup() | ||
.findVirtual(Thread.class, "threadId", MethodType.methodType(long.class)); | ||
} catch (NoSuchMethodException | IllegalAccessException e) { | ||
return null; | ||
} | ||
} | ||
|
||
private static final MethodHandle lookupGetId() { | ||
try { | ||
return MethodHandles.lookup() | ||
.findVirtual(Thread.class, "getId", MethodType.methodType(long.class)); | ||
} catch (NoSuchMethodException | IllegalAccessException e) { | ||
return null; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package datadog.environment; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.lang.invoke.MethodHandle; | ||
import java.lang.invoke.MethodHandles; | ||
import java.lang.invoke.MethodType; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class ThreadUtilsTest { | ||
@Test | ||
public void threadId() throws InterruptedException { | ||
Thread thread = new Thread("foo"); | ||
thread.start(); | ||
try { | ||
// always works on Thread's where getId isn't overridden by child class | ||
assertEquals(thread.getId(), ThreadUtils.threadId(thread)); | ||
} finally { | ||
thread.join(); | ||
} | ||
} | ||
|
||
@Test | ||
public void supportsVirtualThreads() { | ||
assertEquals( | ||
JavaVersion.getRuntimeVersion().isAtLeast(21), ThreadUtils.supportsVirtualThreads()); | ||
} | ||
|
||
@Test | ||
public void isVirtualThread_false() throws InterruptedException { | ||
Thread thread = new Thread("foo"); | ||
thread.start(); | ||
try { | ||
assertFalse(ThreadUtils.isVirtual(thread)); | ||
} finally { | ||
thread.join(); | ||
} | ||
} | ||
|
||
@Test | ||
public void isCurrentThreadVirtual_false() throws InterruptedException, ExecutionException { | ||
ExecutorService executor = Executors.newSingleThreadExecutor(); | ||
try { | ||
assertFalse(executor.submit(() -> ThreadUtils.isCurrentThreadVirtual()).get()); | ||
} finally { | ||
executor.shutdown(); | ||
} | ||
} | ||
|
||
@Test | ||
public void isVirtualThread_true() throws InterruptedException { | ||
if (!ThreadUtils.supportsVirtualThreads()) return; | ||
|
||
Thread vThread = startVirtualThread(() -> {}); | ||
try { | ||
assertTrue(ThreadUtils.isVirtual(vThread)); | ||
} finally { | ||
vThread.join(); | ||
} | ||
} | ||
|
||
@Test | ||
public void isCurrentThreadVirtual_true() throws InterruptedException { | ||
if (!ThreadUtils.supportsVirtualThreads()) return; | ||
|
||
AtomicBoolean result = new AtomicBoolean(); | ||
|
||
Thread vThread = | ||
startVirtualThread( | ||
() -> { | ||
result.set(ThreadUtils.isCurrentThreadVirtual()); | ||
}); | ||
|
||
vThread.join(); | ||
assertTrue(result.get()); | ||
} | ||
|
||
/* | ||
* Should only be called on JVMs that support virtual threads | ||
*/ | ||
static final Thread startVirtualThread(Runnable runnable) { | ||
MethodHandle h_startVThread; | ||
try { | ||
h_startVThread = | ||
MethodHandles.lookup() | ||
.findStatic( | ||
Thread.class, | ||
"startVirtualThread", | ||
MethodType.methodType(Runnable.class, Thread.class)); | ||
} catch (NoSuchMethodException | IllegalAccessException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
|
||
try { | ||
return (Thread) h_startVThread.invoke(runnable); | ||
} catch (Throwable e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,6 +110,7 @@ public final class GeneralConfig { | |
public static final String OPTIMIZED_MAP_ENABLED = "optimized.map.enabled"; | ||
public static final String TAG_NAME_UTF8_CACHE_SIZE = "tag.name.utf8.cache.size"; | ||
public static final String TAG_VALUE_UTF8_CACHE_SIZE = "tag.value.utf8.cache.size"; | ||
public static final String SPAN_BUILDER_REUSE_ENABLED = "span.builder.reuse.enabled"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if this is best class to be placing these in. Or if this is the proper namespace. dd.trace... might be better There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree |
||
public static final String STACK_TRACE_LENGTH_LIMIT = "stack.trace.length.limit"; | ||
|
||
public static final String SSI_INJECTION_ENABLED = "injection.enabled"; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package datadog.trace.core; | ||
|
||
import static java.util.concurrent.TimeUnit.MICROSECONDS; | ||
|
||
import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.Fork; | ||
import org.openjdk.jmh.annotations.Measurement; | ||
import org.openjdk.jmh.annotations.Mode; | ||
import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.annotations.Threads; | ||
import org.openjdk.jmh.annotations.Warmup; | ||
|
||
/** | ||
* Benchmark of key operations of the CoreTracer | ||
* | ||
* <p>NOTE: This is a multi-threaded benchmark; single threaded benchmarks don't accurately reflect | ||
* some of the optimizations. | ||
* | ||
* <p>Use -t 1, if you'd like to do a single threaded run | ||
*/ | ||
@State(Scope.Benchmark) | ||
@Warmup(iterations = 3) | ||
@Measurement(iterations = 5) | ||
@BenchmarkMode(Mode.Throughput) | ||
@Threads(8) | ||
@OutputTimeUnit(MICROSECONDS) | ||
@Fork(value = 1) | ||
public class CoreTracerBenchmark { | ||
static final CoreTracer TRACER = CoreTracer.builder().build(); | ||
|
||
@Benchmark | ||
public AgentSpan startSpan() { | ||
return TRACER.startSpan("foo", "bar"); | ||
} | ||
|
||
@Benchmark | ||
public AgentSpan buildSpan() { | ||
return TRACER.buildSpan("foo", "bar").start(); | ||
} | ||
|
||
@Benchmark | ||
public AgentSpan singleSpanBuilder() { | ||
return TRACER.singleSpanBuilder("foo", "bar").start(); | ||
} | ||
} |
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 think it is better to use JUnit
assume
here? Test will be marked as skipped.