Skip to content

Commit

Permalink
Merge pull request #1 from breedx-splk/host-id-resource-provider_remo…
Browse files Browse the repository at this point in the history
…ve_enum

Refactor to remove enum
  • Loading branch information
zeitlinger authored Mar 11, 2024
2 parents fd67647 + e1216e6 commit 93ea5ad
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import io.opentelemetry.sdk.autoconfigure.spi.internal.ConditionalResourceProvider;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.semconv.ResourceAttributes;
import io.opentelemetry.testing.internal.armeria.internal.shaded.guava.base.Charsets;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -39,39 +39,23 @@ public final class HostIdResourceProvider implements ConditionalResourceProvider
public static final String REGISTRY_QUERY =
"reg query HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography /v MachineGuid";

private final Supplier<OsType> getOsType;
private final Supplier<String> getOsType;

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

private final Supplier<List<String>> queryWindowsRegistry;

enum OsType {
WINDOWS,
LINUX,
OTHER
}

static class ExecResult {
int exitCode;
List<String> lines;

public ExecResult(int exitCode, List<String> lines) {
this.exitCode = exitCode;
this.lines = lines;
}
}

public HostIdResourceProvider() {
this(
HostIdResourceProvider::getOsType,
HostIdResourceProvider::getOsTypeSystemProperty,
HostIdResourceProvider::readMachineIdFile,
HostIdResourceProvider::queryWindowsRegistry);
}

// Visible for testing

HostIdResourceProvider(
Supplier<OsType> getOsType,
Supplier<String> getOsType,
Function<Path, List<String>> machineIdReader,
Supplier<List<String>> queryWindowsRegistry) {
this.getOsType = getOsType;
Expand All @@ -81,19 +65,31 @@ public HostIdResourceProvider() {

@Override
public Resource createResource(ConfigProperties config) {
OsType osType = getOsType.get();
switch (osType) {
case WINDOWS:
return readWindowsGuid();
case LINUX:
return readLinuxMachineId();
case OTHER:
break;
if(runningWindows()){
return readWindowsGuid();
}
if(runningLinux()){
return readLinuxMachineId();
}
logger.fine("Unsupported OS type: " + osType);
logger.fine("Unsupported OS type: " + getOsType.get());
return Resource.empty();
}

private boolean runningLinux() {
return getOsType.get().toLowerCase(Locale.ROOT).equals("linux");
}

private boolean runningWindows() {
return getOsType.get().startsWith("Windows");
}

// see
// https://github.com/apache/commons-lang/blob/master/src/main/java/org/apache/commons/lang3/SystemUtils.java
// for values
private static String getOsTypeSystemProperty() {
return System.getProperty("os.name", "");
}

private Resource readLinuxMachineId() {
Path path = FileSystems.getDefault().getPath("/etc/machine-id");
List<String> lines = machineIdReader.apply(path);
Expand All @@ -116,23 +112,6 @@ private static List<String> readMachineIdFile(Path path) {
}
}

private static OsType getOsType() {
// see
// https://github.com/apache/commons-lang/blob/master/src/main/java/org/apache/commons/lang3/SystemUtils.java
// for values
String osName = System.getProperty("os.name");
if (osName == null) {
return OsType.OTHER;
}
if (osName.startsWith("Windows")) {
return OsType.WINDOWS;
}
if (osName.toLowerCase(Locale.ROOT).equals("linux")) {
return OsType.LINUX;
}
return OsType.OTHER;
}

private Resource readWindowsGuid() {
List<String> lines = queryWindowsRegistry.get();

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

try (BufferedReader processOutputReader =
new BufferedReader(new InputStreamReader(process.getInputStream(), Charsets.UTF_8))) {
new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) {
String readLine;

while ((readLine = processOutputReader.readLine()) != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Collection<DynamicTest> createResourceLinux() {
() -> {
HostIdResourceProvider provider =
new HostIdResourceProvider(
() -> HostIdResourceProvider.OsType.LINUX, testCase.pathReader, null);
() -> "linux", testCase.pathReader, null);

assertHostId(testCase.expectedValue, provider);
}))
Expand All @@ -90,7 +90,7 @@ Collection<DynamicTest> createResourceWindows() {
() -> {
HostIdResourceProvider provider =
new HostIdResourceProvider(
() -> HostIdResourceProvider.OsType.WINDOWS,
() -> "Windows 95",
null,
testCase.queryWindowsRegistry);

Expand Down

0 comments on commit 93ea5ad

Please sign in to comment.