* fix(TDI-44623): [OBSERVABILITY] : add PID and (Thread-ID OR UUID) information * decrease the expensive call number for getting pid * fix(TDI-44807): Observability: initialize moment only when necessary * fix(TDI-44806): Observability: Add customization for event frequency
45 lines
967 B
Java
45 lines
967 B
Java
package routines.system;
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.lang.management.ManagementFactory;
|
|
import java.lang.management.RuntimeMXBean;
|
|
import java.util.UUID;
|
|
|
|
public class ProcessIdAndThreadId {
|
|
|
|
private static class PTId {
|
|
String processId;
|
|
String threadId;
|
|
}
|
|
|
|
private static final ThreadLocal<PTId> Id = new ThreadLocal<PTId>() {
|
|
@Override
|
|
protected PTId initialValue() {
|
|
PTId id = new PTId();
|
|
id.processId = getPid();
|
|
id.threadId = UUID.randomUUID().toString();
|
|
return id;
|
|
}
|
|
|
|
};
|
|
|
|
private static String getPid() {
|
|
RuntimeMXBean mx = ManagementFactory.getRuntimeMXBean();
|
|
String processName = mx.getName();
|
|
try {
|
|
return UUID.nameUUIDFromBytes(processName.getBytes("UTF8")).toString();
|
|
} catch (UnsupportedEncodingException e) {
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static String getProcessId() {
|
|
return Id.get().processId;
|
|
}
|
|
|
|
public static String getThreadId() {
|
|
return Id.get().threadId;
|
|
}
|
|
|
|
}
|