Skip to content

Commit 93ea5ad

Browse files
authored
Merge pull request #1 from breedx-splk/host-id-resource-provider_remove_enum
Refactor to remove enum
2 parents fd67647 + e1216e6 commit 93ea5ad

File tree

2 files changed

+28
-49
lines changed

2 files changed

+28
-49
lines changed

instrumentation/resources/library/src/main/java/io/opentelemetry/instrumentation/resources/HostIdResourceProvider.java

Lines changed: 26 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
import io.opentelemetry.sdk.autoconfigure.spi.internal.ConditionalResourceProvider;
1212
import io.opentelemetry.sdk.resources.Resource;
1313
import io.opentelemetry.semconv.ResourceAttributes;
14-
import io.opentelemetry.testing.internal.armeria.internal.shaded.guava.base.Charsets;
1514
import java.io.BufferedReader;
1615
import java.io.IOException;
1716
import java.io.InputStreamReader;
17+
import java.nio.charset.StandardCharsets;
1818
import java.nio.file.FileSystems;
1919
import java.nio.file.Files;
2020
import java.nio.file.Path;
@@ -39,39 +39,23 @@ public final class HostIdResourceProvider implements ConditionalResourceProvider
3939
public static final String REGISTRY_QUERY =
4040
"reg query HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography /v MachineGuid";
4141

42-
private final Supplier<OsType> getOsType;
42+
private final Supplier<String> getOsType;
4343

4444
private final Function<Path, List<String>> machineIdReader;
4545

4646
private final Supplier<List<String>> queryWindowsRegistry;
4747

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-
6448
public HostIdResourceProvider() {
6549
this(
66-
HostIdResourceProvider::getOsType,
50+
HostIdResourceProvider::getOsTypeSystemProperty,
6751
HostIdResourceProvider::readMachineIdFile,
6852
HostIdResourceProvider::queryWindowsRegistry);
6953
}
7054

7155
// Visible for testing
7256

7357
HostIdResourceProvider(
74-
Supplier<OsType> getOsType,
58+
Supplier<String> getOsType,
7559
Function<Path, List<String>> machineIdReader,
7660
Supplier<List<String>> queryWindowsRegistry) {
7761
this.getOsType = getOsType;
@@ -81,19 +65,31 @@ public HostIdResourceProvider() {
8165

8266
@Override
8367
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();
9273
}
93-
logger.fine("Unsupported OS type: " + osType);
74+
logger.fine("Unsupported OS type: " + getOsType.get());
9475
return Resource.empty();
9576
}
9677

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+
9793
private Resource readLinuxMachineId() {
9894
Path path = FileSystems.getDefault().getPath("/etc/machine-id");
9995
List<String> lines = machineIdReader.apply(path);
@@ -116,23 +112,6 @@ private static List<String> readMachineIdFile(Path path) {
116112
}
117113
}
118114

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-
136115
private Resource readWindowsGuid() {
137116
List<String> lines = queryWindowsRegistry.get();
138117

@@ -177,7 +156,7 @@ public static List<String> getProcessOutput(Process process) throws IOException
177156
List<String> result = new ArrayList<>();
178157

179158
try (BufferedReader processOutputReader =
180-
new BufferedReader(new InputStreamReader(process.getInputStream(), Charsets.UTF_8))) {
159+
new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) {
181160
String readLine;
182161

183162
while ((readLine = processOutputReader.readLine()) != null) {

instrumentation/resources/library/src/test/java/io/opentelemetry/instrumentation/resources/HostIdResourceProviderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Collection<DynamicTest> createResourceLinux() {
6565
() -> {
6666
HostIdResourceProvider provider =
6767
new HostIdResourceProvider(
68-
() -> HostIdResourceProvider.OsType.LINUX, testCase.pathReader, null);
68+
() -> "linux", testCase.pathReader, null);
6969

7070
assertHostId(testCase.expectedValue, provider);
7171
}))
@@ -90,7 +90,7 @@ Collection<DynamicTest> createResourceWindows() {
9090
() -> {
9191
HostIdResourceProvider provider =
9292
new HostIdResourceProvider(
93-
() -> HostIdResourceProvider.OsType.WINDOWS,
93+
() -> "Windows 95",
9494
null,
9595
testCase.queryWindowsRegistry);
9696

0 commit comments

Comments
 (0)