Skip to content

Commit

Permalink
Prevent NPE when thread is not set for event (#743)
Browse files Browse the repository at this point in the history
* Prevent NPE when thread is not set for event

* skip os thread when it is not known

* add null checks
  • Loading branch information
laurit authored Apr 20, 2022
1 parent d497a45 commit d1198c5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,13 @@ public void export(RecordedEvent event, AllocationEventSampler sampler, SpanCont
pprof.addLabel(sample, SOURCE_EVENT_TIME, time.toEpochMilli());

RecordedThread thread = event.getThread();
if (thread.getJavaThreadId() != -1) {
pprof.addLabel(sample, THREAD_ID, thread.getJavaThreadId());
pprof.addLabel(sample, THREAD_NAME, thread.getJavaName());
if (thread != null) {
if (thread.getJavaThreadId() != -1) {
pprof.addLabel(sample, THREAD_ID, thread.getJavaThreadId());
pprof.addLabel(sample, THREAD_NAME, thread.getJavaName());
}
pprof.addLabel(sample, THREAD_OS_ID, thread.getOSThreadId());
}
pprof.addLabel(sample, THREAD_OS_ID, thread.getOSThreadId());
pprof.addLabel(sample, THREAD_STATE, "RUNNABLE");

if (spanContext != null && spanContext.isValid()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public class SpanContextualizer {
* ContextAttached events.
*/
public void updateContext(RecordedEvent event) {
// jdk 17 doesn't report thread for events that happened on a thread that has terminated by now
if (event.getThread() == null) {
return;
}
String traceId = event.getString("traceId");
String spanId = event.getString("spanId");
long javaThreadId = event.getThread().getJavaThreadId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ public void export(StackToSpanLinkage stackToSpanLinkage) {
pprof.addLabel(sample, THREAD_ID, stackTrace.getThreadId());
pprof.addLabel(sample, THREAD_NAME, stackTrace.getThreadName());
}
pprof.addLabel(sample, THREAD_OS_ID, stackTrace.getOsThreadId());
if (stackTrace.getOsThreadId() != -1) {
pprof.addLabel(sample, THREAD_OS_ID, stackTrace.getOsThreadId());
}
pprof.addLabel(sample, THREAD_STATE, stackTrace.getThreadState());

for (StackTraceParser.StackTraceLine stl : stackTrace.getStackTraceLines()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public void addLabel(Sample.Builder sample, AttributeKey<String> key, String val
}

public void addLabel(Sample.Builder sample, String name, String value) {
if (value == null) {
return;
}
addLabel(sample, name, label -> label.setStr(stringTable.get(value)));
}

Expand Down

0 comments on commit d1198c5

Please sign in to comment.