|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.resources; |
| 7 | + |
| 8 | +import static java.util.logging.Level.FINE; |
| 9 | + |
| 10 | +import io.opentelemetry.api.common.Attributes; |
| 11 | +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; |
| 12 | +import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider; |
| 13 | +import io.opentelemetry.sdk.autoconfigure.spi.internal.ConditionalResourceProvider; |
| 14 | +import io.opentelemetry.sdk.resources.Resource; |
| 15 | +import io.opentelemetry.semconv.ResourceAttributes; |
| 16 | +import java.io.BufferedReader; |
| 17 | +import java.io.IOException; |
| 18 | +import java.io.InputStreamReader; |
| 19 | +import java.nio.charset.StandardCharsets; |
| 20 | +import java.nio.file.FileSystems; |
| 21 | +import java.nio.file.Files; |
| 22 | +import java.nio.file.Path; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Locale; |
| 27 | +import java.util.function.Function; |
| 28 | +import java.util.function.Supplier; |
| 29 | +import java.util.logging.Logger; |
| 30 | + |
| 31 | +/** |
| 32 | + * {@link ResourceProvider} for automatically configuring <code>host.id</code> according to <a |
| 33 | + * href="https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/host.md#non-privileged-machine-id-lookup">the |
| 34 | + * semantic conventions</a> |
| 35 | + */ |
| 36 | +public final class HostIdResourceProvider implements ConditionalResourceProvider { |
| 37 | + |
| 38 | + private static final Logger logger = Logger.getLogger(HostIdResourceProvider.class.getName()); |
| 39 | + |
| 40 | + public static final String REGISTRY_QUERY = |
| 41 | + "reg query HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography /v MachineGuid"; |
| 42 | + |
| 43 | + private final Supplier<String> getOsType; |
| 44 | + |
| 45 | + private final Function<Path, List<String>> machineIdReader; |
| 46 | + |
| 47 | + private final Supplier<List<String>> queryWindowsRegistry; |
| 48 | + |
| 49 | + public HostIdResourceProvider() { |
| 50 | + this( |
| 51 | + HostIdResourceProvider::getOsTypeSystemProperty, |
| 52 | + HostIdResourceProvider::readMachineIdFile, |
| 53 | + HostIdResourceProvider::queryWindowsRegistry); |
| 54 | + } |
| 55 | + |
| 56 | + // Visible for testing |
| 57 | + |
| 58 | + HostIdResourceProvider( |
| 59 | + Supplier<String> getOsType, |
| 60 | + Function<Path, List<String>> machineIdReader, |
| 61 | + Supplier<List<String>> queryWindowsRegistry) { |
| 62 | + this.getOsType = getOsType; |
| 63 | + this.machineIdReader = machineIdReader; |
| 64 | + this.queryWindowsRegistry = queryWindowsRegistry; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public Resource createResource(ConfigProperties config) { |
| 69 | + if (runningWindows()) { |
| 70 | + return readWindowsGuid(); |
| 71 | + } |
| 72 | + if (runningLinux()) { |
| 73 | + return readLinuxMachineId(); |
| 74 | + } |
| 75 | + logger.log(FINE, "Unsupported OS type: {0}", getOsType.get()); |
| 76 | + return Resource.empty(); |
| 77 | + } |
| 78 | + |
| 79 | + private boolean runningLinux() { |
| 80 | + return getOsType.get().toLowerCase(Locale.ROOT).equals("linux"); |
| 81 | + } |
| 82 | + |
| 83 | + private boolean runningWindows() { |
| 84 | + return getOsType.get().startsWith("Windows"); |
| 85 | + } |
| 86 | + |
| 87 | + // see |
| 88 | + // https://github.com/apache/commons-lang/blob/master/src/main/java/org/apache/commons/lang3/SystemUtils.java |
| 89 | + // for values |
| 90 | + private static String getOsTypeSystemProperty() { |
| 91 | + return System.getProperty("os.name", ""); |
| 92 | + } |
| 93 | + |
| 94 | + private Resource readLinuxMachineId() { |
| 95 | + Path path = FileSystems.getDefault().getPath("/etc/machine-id"); |
| 96 | + List<String> lines = machineIdReader.apply(path); |
| 97 | + if (lines.isEmpty()) { |
| 98 | + return Resource.empty(); |
| 99 | + } |
| 100 | + return Resource.create(Attributes.of(ResourceAttributes.HOST_ID, lines.get(0))); |
| 101 | + } |
| 102 | + |
| 103 | + private static List<String> readMachineIdFile(Path path) { |
| 104 | + try { |
| 105 | + List<String> lines = Files.readAllLines(path); |
| 106 | + if (lines.isEmpty()) { |
| 107 | + logger.fine("Failed to read /etc/machine-id: empty file"); |
| 108 | + } |
| 109 | + return lines; |
| 110 | + } catch (IOException e) { |
| 111 | + logger.log(FINE, "Failed to read /etc/machine-id", e); |
| 112 | + return Collections.emptyList(); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private Resource readWindowsGuid() { |
| 117 | + List<String> lines = queryWindowsRegistry.get(); |
| 118 | + |
| 119 | + for (String line : lines) { |
| 120 | + if (line.contains("MachineGuid")) { |
| 121 | + String[] parts = line.trim().split("\\s+"); |
| 122 | + if (parts.length == 3) { |
| 123 | + return Resource.create(Attributes.of(ResourceAttributes.HOST_ID, parts[2])); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + logger.fine("Failed to read Windows registry: No MachineGuid found in output: " + lines); |
| 128 | + return Resource.empty(); |
| 129 | + } |
| 130 | + |
| 131 | + private static List<String> queryWindowsRegistry() { |
| 132 | + try { |
| 133 | + ProcessBuilder processBuilder = new ProcessBuilder("cmd", "/c", REGISTRY_QUERY); |
| 134 | + processBuilder.redirectErrorStream(true); |
| 135 | + Process process = processBuilder.start(); |
| 136 | + |
| 137 | + List<String> output = getProcessOutput(process); |
| 138 | + int exitedValue = process.waitFor(); |
| 139 | + if (exitedValue != 0) { |
| 140 | + logger.fine( |
| 141 | + "Failed to read Windows registry. Exit code: " |
| 142 | + + exitedValue |
| 143 | + + " Output: " |
| 144 | + + String.join("\n", output)); |
| 145 | + |
| 146 | + return Collections.emptyList(); |
| 147 | + } |
| 148 | + |
| 149 | + return output; |
| 150 | + } catch (IOException | InterruptedException e) { |
| 151 | + logger.log(FINE, "Failed to read Windows registry", e); |
| 152 | + return Collections.emptyList(); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + public static List<String> getProcessOutput(Process process) throws IOException { |
| 157 | + List<String> result = new ArrayList<>(); |
| 158 | + |
| 159 | + try (BufferedReader processOutputReader = |
| 160 | + new BufferedReader( |
| 161 | + new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) { |
| 162 | + String readLine; |
| 163 | + |
| 164 | + while ((readLine = processOutputReader.readLine()) != null) { |
| 165 | + result.add(readLine); |
| 166 | + } |
| 167 | + } |
| 168 | + return result; |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public boolean shouldApply(ConfigProperties config, Resource existing) { |
| 173 | + return !config |
| 174 | + .getMap("otel.resource.attributes") |
| 175 | + .containsKey(ResourceAttributes.HOST_ID.getKey()) |
| 176 | + && existing.getAttribute(ResourceAttributes.HOST_ID) == null; |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public int order() { |
| 181 | + // Run after cloud provider resource providers |
| 182 | + return Integer.MAX_VALUE - 1; |
| 183 | + } |
| 184 | +} |
0 commit comments