Skip to content
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

use native threadid for attach/detach events #4343

Merged
merged 1 commit into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,15 @@ private static one.profiler.AsyncProfiler inferFromOsAndArch()
arch, os));
}

void addCurrentThread() {
if (asyncProfiler != null) {
asyncProfiler.addThread(Thread.currentThread());
void addThread(int tid) {
if (asyncProfiler != null && tid >= 0) {
asyncProfiler.addThread(tid);
}
}

void removeCurrentThread() {
if (asyncProfiler != null) {
asyncProfiler.removeThread(Thread.currentThread());
void removeThread(int tid) {
if (asyncProfiler != null && tid >= 0) {
asyncProfiler.removeThread(tid);
}
}

Expand Down Expand Up @@ -389,7 +389,7 @@ private int clamp(int min, int max, int value) {
}

public void setContext(int tid, long spanId, long rootSpanId) {
if (asyncProfiler != null) {
if (asyncProfiler != null && tid >= 0) {
try {
asyncProfiler.setContext(tid, spanId, rootSpanId);
} catch (IllegalStateException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ public class ContextThreadFilter implements ProfilingContextIntegration {
private static final AsyncProfiler ASYNC_PROFILER = AsyncProfiler.getInstance();

@Override
public void onAttach() {
public void onAttach(int tid) {
if (AsyncProfilerConfig.isWallClockProfilerEnabled()) {
ASYNC_PROFILER.addCurrentThread();
ASYNC_PROFILER.addThread(tid);
}
}

@Override
public void onDetach() {
public void onDetach(int tid) {
if (AsyncProfilerConfig.isWallClockProfilerEnabled()) {
ASYNC_PROFILER.removeCurrentThread();
ASYNC_PROFILER.removeThread(tid);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ class TestProfilingContextIntegration implements ProfilingContextIntegration {
final AtomicInteger attachments = new AtomicInteger()
final AtomicInteger detachments = new AtomicInteger()
@Override
void onAttach() {
void onAttach(int tid) {
attachments.incrementAndGet()
}

@Override
void onDetach() {
void onDetach(int tid) {
detachments.incrementAndGet()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,16 +471,6 @@ static final class ScopeStackThreadLocal extends ThreadLocal<ScopeStack> {
protected ScopeStack initialValue() {
return new ScopeStack(profilingContextIntegration);
}

@Override
public void remove() {
detach();
super.remove();
}

private void detach() {
profilingContextIntegration.onDetach();
}
}

/**
Expand Down Expand Up @@ -600,13 +590,13 @@ private void onTopChanged(ContinuableScope top) {

/** Notifies context thread listeners that this thread has a context now */
private void onBecomeNonEmpty() {
profilingContextIntegration.onAttach();
profilingContextIntegration.onAttach(nativeThreadId);
}

/** Notifies context thread listeners that this thread no longer has a context */
private void onBecomeEmpty() {
profilingContextIntegration.setContext(nativeThreadId, 0, 0);
profilingContextIntegration.onDetach();
profilingContextIntegration.onDetach(nativeThreadId);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ class ScopeManagerTest extends DDCoreSpecification {
1 * statsDClient.incrementCounter("scope.close.error")
1 * rootSpanCheckpointer.onRootSpanStarted(_)
3 * listener.setContext(_, _, _)
1 * listener.onAttach()
1 * listener.onAttach(_)
0 * _

when:
Expand All @@ -479,7 +479,7 @@ class ScopeManagerTest extends DDCoreSpecification {
1 * rootSpanCheckpointer.onRootSpanFinished(_, _)
_ * statsDClient.close()
1 * listener.setContext(_, 0, 0)
1 * listener.onDetach()
1 * listener.onDetach(_)
assertEvents([ACTIVATE, ACTIVATE, CLOSE, CLOSE])
0 * _

Expand All @@ -506,7 +506,7 @@ class ScopeManagerTest extends DDCoreSpecification {
tracer.activeScope() == firstScope
assertEvents([ACTIVATE])
1 * rootSpanCheckpointer.onRootSpanStarted(_)
1 * listener.onAttach()
1 * listener.onAttach(_)
1 * listener.setContext(_, _, _)
0 * _

Expand Down Expand Up @@ -584,7 +584,7 @@ class ScopeManagerTest extends DDCoreSpecification {
])
_ * statsDClient.close()
1 * listener.setContext(_, 0, 0)
1 * listener.onDetach()
1 * listener.onDetach(_)
0 * _
}

Expand Down Expand Up @@ -970,7 +970,7 @@ class ScopeManagerTest extends DDCoreSpecification {
assert scopeManager.active() == null
}).get()
then: "the listener is not notified"
0 * listener.onAttach()
0 * listener.onAttach(_)

when: "scopes activate on threads"
AgentSpan span = tracer.buildSpan("foo").start()
Expand All @@ -991,7 +991,7 @@ class ScopeManagerTest extends DDCoreSpecification {
}

then: "the first activation notifies the listener"
numThreads * listener.onAttach()
numThreads * listener.onAttach(_)
_ * _

cleanup:
Expand Down
2 changes: 1 addition & 1 deletion gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final class CachedData {
testcontainers: '1.17.3',
jmc : "8.1.0-SNAPSHOT",
autoservice : "1.0-rc7",
asyncprofiler : "2.8.3-DD-20221128_1",
asyncprofiler : "2.8.3-DD-20221129",
asm : "9.2"
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

public interface ProfilingContextIntegration {
/** Invoked when a trace first propagates to a thread */
void onAttach();
void onAttach(int tid);

/** Invoked when a thread exits */
void onDetach();
void onDetach(int tid);

void setContext(int tid, long rootSpanId, long spanId);

Expand All @@ -16,10 +16,10 @@ final class NoOp implements ProfilingContextIntegration {
public static final NoOp INSTANCE = new NoOp();

@Override
public void onAttach() {}
public void onAttach(int tid) {}

@Override
public void onDetach() {}
public void onDetach(int tid) {}

@Override
public void setContext(int tid, long rootSpanId, long spanId) {}
Expand Down