11
11
import io .opentelemetry .sdk .autoconfigure .spi .internal .ConditionalResourceProvider ;
12
12
import io .opentelemetry .sdk .resources .Resource ;
13
13
import io .opentelemetry .semconv .ResourceAttributes ;
14
- import io .opentelemetry .testing .internal .armeria .internal .shaded .guava .base .Charsets ;
15
14
import java .io .BufferedReader ;
16
15
import java .io .IOException ;
17
16
import java .io .InputStreamReader ;
17
+ import java .nio .charset .StandardCharsets ;
18
18
import java .nio .file .FileSystems ;
19
19
import java .nio .file .Files ;
20
20
import java .nio .file .Path ;
@@ -39,39 +39,23 @@ public final class HostIdResourceProvider implements ConditionalResourceProvider
39
39
public static final String REGISTRY_QUERY =
40
40
"reg query HKEY_LOCAL_MACHINE\\ SOFTWARE\\ Microsoft\\ Cryptography /v MachineGuid" ;
41
41
42
- private final Supplier <OsType > getOsType ;
42
+ private final Supplier <String > getOsType ;
43
43
44
44
private final Function <Path , List <String >> machineIdReader ;
45
45
46
46
private final Supplier <List <String >> queryWindowsRegistry ;
47
47
48
- enum OsType {
49
- WINDOWS ,
50
- LINUX ,
51
- OTHER
52
- }
53
-
54
- static class ExecResult {
55
- int exitCode ;
56
- List <String > lines ;
57
-
58
- public ExecResult (int exitCode , List <String > lines ) {
59
- this .exitCode = exitCode ;
60
- this .lines = lines ;
61
- }
62
- }
63
-
64
48
public HostIdResourceProvider () {
65
49
this (
66
- HostIdResourceProvider ::getOsType ,
50
+ HostIdResourceProvider ::getOsTypeSystemProperty ,
67
51
HostIdResourceProvider ::readMachineIdFile ,
68
52
HostIdResourceProvider ::queryWindowsRegistry );
69
53
}
70
54
71
55
// Visible for testing
72
56
73
57
HostIdResourceProvider (
74
- Supplier <OsType > getOsType ,
58
+ Supplier <String > getOsType ,
75
59
Function <Path , List <String >> machineIdReader ,
76
60
Supplier <List <String >> queryWindowsRegistry ) {
77
61
this .getOsType = getOsType ;
@@ -81,19 +65,31 @@ public HostIdResourceProvider() {
81
65
82
66
@ Override
83
67
public Resource createResource (ConfigProperties config ) {
84
- OsType osType = getOsType .get ();
85
- switch (osType ) {
86
- case WINDOWS :
87
- return readWindowsGuid ();
88
- case LINUX :
89
- return readLinuxMachineId ();
90
- case OTHER :
91
- break ;
68
+ if (runningWindows ()){
69
+ return readWindowsGuid ();
70
+ }
71
+ if (runningLinux ()){
72
+ return readLinuxMachineId ();
92
73
}
93
- logger .fine ("Unsupported OS type: " + osType );
74
+ logger .fine ("Unsupported OS type: " + getOsType . get () );
94
75
return Resource .empty ();
95
76
}
96
77
78
+ private boolean runningLinux () {
79
+ return getOsType .get ().toLowerCase (Locale .ROOT ).equals ("linux" );
80
+ }
81
+
82
+ private boolean runningWindows () {
83
+ return getOsType .get ().startsWith ("Windows" );
84
+ }
85
+
86
+ // see
87
+ // https://github.com/apache/commons-lang/blob/master/src/main/java/org/apache/commons/lang3/SystemUtils.java
88
+ // for values
89
+ private static String getOsTypeSystemProperty () {
90
+ return System .getProperty ("os.name" , "" );
91
+ }
92
+
97
93
private Resource readLinuxMachineId () {
98
94
Path path = FileSystems .getDefault ().getPath ("/etc/machine-id" );
99
95
List <String > lines = machineIdReader .apply (path );
@@ -116,23 +112,6 @@ private static List<String> readMachineIdFile(Path path) {
116
112
}
117
113
}
118
114
119
- private static OsType getOsType () {
120
- // see
121
- // https://github.com/apache/commons-lang/blob/master/src/main/java/org/apache/commons/lang3/SystemUtils.java
122
- // for values
123
- String osName = System .getProperty ("os.name" );
124
- if (osName == null ) {
125
- return OsType .OTHER ;
126
- }
127
- if (osName .startsWith ("Windows" )) {
128
- return OsType .WINDOWS ;
129
- }
130
- if (osName .toLowerCase (Locale .ROOT ).equals ("linux" )) {
131
- return OsType .LINUX ;
132
- }
133
- return OsType .OTHER ;
134
- }
135
-
136
115
private Resource readWindowsGuid () {
137
116
List <String > lines = queryWindowsRegistry .get ();
138
117
@@ -177,7 +156,7 @@ public static List<String> getProcessOutput(Process process) throws IOException
177
156
List <String > result = new ArrayList <>();
178
157
179
158
try (BufferedReader processOutputReader =
180
- new BufferedReader (new InputStreamReader (process .getInputStream (), Charsets .UTF_8 ))) {
159
+ new BufferedReader (new InputStreamReader (process .getInputStream (), StandardCharsets .UTF_8 ))) {
181
160
String readLine ;
182
161
183
162
while ((readLine = processOutputReader .readLine ()) != null ) {
0 commit comments