33import com .exceptionless .exceptionlessclient .configuration .ConfigurationManager ;
44import com .exceptionless .exceptionlessclient .models .Event ;
55import com .exceptionless .exceptionlessclient .models .EventPluginContext ;
6+ import com .exceptionless .exceptionlessclient .models .enums .EnvironmentInfoPropertyKey ;
7+ import com .exceptionless .exceptionlessclient .models .services .EnvironmentInfo ;
68import com .exceptionless .exceptionlessclient .plugins .EventPluginIF ;
79import com .exceptionless .exceptionlessclient .services .EnvironmentInfoGetArgs ;
10+ import com .sun .management .OperatingSystemMXBean ;
811import lombok .Builder ;
12+ import lombok .extern .slf4j .Slf4j ;
913
14+ import java .lang .management .ManagementFactory ;
15+ import java .net .InetAddress ;
16+ import java .net .UnknownHostException ;
17+ import java .nio .ByteOrder ;
18+ import java .util .HashMap ;
19+ import java .util .Map ;
20+
21+ @ Slf4j
1022public class EnvironmentInfoPlugin implements EventPluginIF {
1123 private static final Integer DEFAULT_PRIORITY = 80 ;
1224
25+ private EnvironmentInfo defaultEnvironmentInfo ;
26+
1327 @ Builder
14- public EnvironmentInfoPlugin () {}
28+ public EnvironmentInfoPlugin () {
29+ initDefaultEnvironmentInfo ();
30+ }
31+
32+ private void initDefaultEnvironmentInfo () {
33+ OperatingSystemMXBean operatingSystemMXBean =
34+ (OperatingSystemMXBean ) ManagementFactory .getOperatingSystemMXBean ();
35+ this .defaultEnvironmentInfo =
36+ EnvironmentInfo .builder ()
37+ .processorCount (Runtime .getRuntime ().availableProcessors ())
38+ .totalPhysicalMemory (operatingSystemMXBean .getTotalPhysicalMemorySize ())
39+ .osName (operatingSystemMXBean .getName ())
40+ .architecture (operatingSystemMXBean .getArch ())
41+ .osVersion (operatingSystemMXBean .getVersion ())
42+ .runtimeVersion (System .getProperty ("java.version" ))
43+ .processName (String .valueOf (ProcessHandle .current ().pid ()))
44+ .processName (ManagementFactory .getRuntimeMXBean ().getName ())
45+ .commandLine (ProcessHandle .current ().info ().commandLine ().orElse (null ))
46+ .build ();
47+ }
1548
1649 @ Override
1750 public int getPriority () {
@@ -24,15 +57,68 @@ public void run(
2457 Event event = eventPluginContext .getEvent ();
2558 if (event .getEnvironmentInfo ().isEmpty ()) {
2659 event .addEnvironmentInfo (
27- configurationManager
28- .getEnvironmentInfoCollector ()
29- .getEnvironmentInfo (
30- EnvironmentInfoGetArgs .builder ()
31- .includeIpAddress (
32- configurationManager .getPrivateInformationInclusions ().getIpAddress ())
33- .includeMachineName (
34- configurationManager .getPrivateInformationInclusions ().getMachineName ())
35- .build ()));
60+ getEnvironmentInfo (
61+ EnvironmentInfoGetArgs .builder ()
62+ .includeIpAddress (
63+ configurationManager .getPrivateInformationInclusions ().getIpAddress ())
64+ .includeMachineName (
65+ configurationManager .getPrivateInformationInclusions ().getMachineName ())
66+ .build ()));
67+ }
68+ }
69+
70+ public EnvironmentInfo getEnvironmentInfo (EnvironmentInfoGetArgs args ) {
71+ OperatingSystemMXBean operatingSystemMXBean =
72+ (OperatingSystemMXBean ) ManagementFactory .getOperatingSystemMXBean ();
73+
74+ EnvironmentInfo .EnvironmentInfoBuilder <?, ?> builder =
75+ EnvironmentInfo .builder ()
76+ .processorCount (defaultEnvironmentInfo .getProcessorCount ())
77+ .totalPhysicalMemory (defaultEnvironmentInfo .getTotalPhysicalMemory ())
78+ .availablePhysicalMemory (operatingSystemMXBean .getFreePhysicalMemorySize ())
79+ .commandLine (defaultEnvironmentInfo .getCommandLine ())
80+ .processName (defaultEnvironmentInfo .getProcessName ())
81+ .processId (defaultEnvironmentInfo .getProcessId ())
82+ .processMemorySize (ManagementFactory .getMemoryMXBean ().getHeapMemoryUsage ().getUsed ())
83+ .threadId (String .valueOf (Thread .currentThread ().getId ()))
84+ .architecture (defaultEnvironmentInfo .getArchitecture ())
85+ .osName (defaultEnvironmentInfo .getOsName ())
86+ .osVersion (defaultEnvironmentInfo .getOsVersion ())
87+ .runtimeVersion (defaultEnvironmentInfo .getRuntimeVersion ())
88+ .data (getData ());
89+
90+ try {
91+ InetAddress localhost = InetAddress .getLocalHost ();
92+ if (args .isIncludeMachineName ()) {
93+ builder .machineName (localhost .getHostName ());
94+ }
95+ if (args .isIncludeIpAddress ()) {
96+ builder .ipAddress (localhost .getHostAddress ());
97+ }
98+ } catch (UnknownHostException e ) {
99+ log .error ("Error while getting machine name" , e );
36100 }
101+
102+ return builder .build ();
103+ }
104+
105+ private Map <String , Object > getData () {
106+ Map <String , Object > data = new HashMap <>();
107+ OperatingSystemMXBean operatingSystemMXBean =
108+ (OperatingSystemMXBean ) ManagementFactory .getOperatingSystemMXBean ();
109+ data .put (
110+ EnvironmentInfoPropertyKey .LOAD_AVG .value (),
111+ String .valueOf (operatingSystemMXBean .getSystemLoadAverage ()));
112+ data .put (EnvironmentInfoPropertyKey .TMP_DIR .value (), System .getProperty ("java.io.tmpdir" ));
113+ data .put (
114+ EnvironmentInfoPropertyKey .UP_TIME .value (),
115+ String .valueOf (ManagementFactory .getRuntimeMXBean ().getUptime ()));
116+ data .put (EnvironmentInfoPropertyKey .ENDIANESS .value (), getEndianess ());
117+
118+ return data ;
119+ }
120+
121+ private String getEndianess () {
122+ return ByteOrder .nativeOrder ().equals (ByteOrder .BIG_ENDIAN ) ? "Big-endian" : "Little-endian" ;
37123 }
38124}
0 commit comments