1
1
package com .exceptionless .exceptionlessclient .plugins .preconfigured ;
2
2
3
3
import com .exceptionless .exceptionlessclient .configuration .ConfigurationManager ;
4
+ import com .exceptionless .exceptionlessclient .enums .EnvironmentInfoPropertyKey ;
5
+ import com .exceptionless .exceptionlessclient .models .EnvironmentInfo ;
4
6
import com .exceptionless .exceptionlessclient .models .Event ;
5
7
import com .exceptionless .exceptionlessclient .models .EventPluginContext ;
6
8
import com .exceptionless .exceptionlessclient .plugins .EventPluginIF ;
7
- import com .exceptionless .exceptionlessclient .services .EnvironmentInfoGetArgs ;
9
+ import com .exceptionless .exceptionlessclient .plugins .preconfigured .args .EnvironmentInfoGetArgs ;
10
+ import com .sun .management .OperatingSystemMXBean ;
8
11
import lombok .Builder ;
12
+ import lombok .extern .slf4j .Slf4j ;
9
13
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
10
22
public class EnvironmentInfoPlugin implements EventPluginIF {
11
23
private static final Integer DEFAULT_PRIORITY = 80 ;
12
24
25
+ private EnvironmentInfo defaultEnvironmentInfo ;
26
+
13
27
@ 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
+ }
15
48
16
49
@ Override
17
50
public int getPriority () {
@@ -24,15 +57,68 @@ public void run(
24
57
Event event = eventPluginContext .getEvent ();
25
58
if (event .getEnvironmentInfo ().isEmpty ()) {
26
59
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
+ private 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 );
36
100
}
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" ;
37
123
}
38
124
}
0 commit comments