Skip to content

Commit 0029332

Browse files
add host.id resource provider (#10627)
Co-authored-by: Jason Plumb <[email protected]>
1 parent c8f2cc5 commit 0029332

File tree

3 files changed

+317
-0
lines changed

3 files changed

+317
-0
lines changed

Diff for: instrumentation/resources/library/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ Implemented attributes:
2626
- `host.name`
2727
- `host.arch`
2828

29+
Provider: `io.opentelemetry.instrumentation.resources.HostIdResourceProvider`
30+
31+
Specification: <https://github.com/open-telemetry/semantic-conventions/blob/main/docs/resource/host.md>
32+
33+
Implemented attributes:
34+
35+
- `host.id`
36+
2937
### Operating System
3038

3139
Provider: `io.opentelemetry.instrumentation.resources.OsResource`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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 org.assertj.core.api.Assertions.assertThat;
9+
10+
import io.opentelemetry.api.common.AttributeKey;
11+
import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties;
12+
import io.opentelemetry.sdk.resources.Resource;
13+
import io.opentelemetry.semconv.ResourceAttributes;
14+
import java.nio.file.Path;
15+
import java.util.Arrays;
16+
import java.util.Collection;
17+
import java.util.Collections;
18+
import java.util.List;
19+
import java.util.function.Function;
20+
import java.util.function.Supplier;
21+
import java.util.stream.Collectors;
22+
import java.util.stream.Stream;
23+
import org.assertj.core.api.MapAssert;
24+
import org.junit.jupiter.api.DynamicTest;
25+
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.TestFactory;
27+
28+
class HostIdResourceProviderTest {
29+
30+
private static class LinuxTestCase {
31+
private final String name;
32+
private final String expectedValue;
33+
private final Function<Path, List<String>> pathReader;
34+
35+
private LinuxTestCase(
36+
String name, String expectedValue, Function<Path, List<String>> pathReader) {
37+
this.name = name;
38+
this.expectedValue = expectedValue;
39+
this.pathReader = pathReader;
40+
}
41+
}
42+
43+
private static class WindowsTestCase {
44+
private final String name;
45+
private final String expectedValue;
46+
private final Supplier<List<String>> queryWindowsRegistry;
47+
48+
private WindowsTestCase(
49+
String name, String expectedValue, Supplier<List<String>> queryWindowsRegistry) {
50+
this.name = name;
51+
this.expectedValue = expectedValue;
52+
this.queryWindowsRegistry = queryWindowsRegistry;
53+
}
54+
}
55+
56+
@TestFactory
57+
Collection<DynamicTest> createResourceLinux() {
58+
return Stream.of(
59+
new LinuxTestCase("default", "test", path -> Collections.singletonList("test")),
60+
new LinuxTestCase("empty file or error reading", null, path -> Collections.emptyList()))
61+
.map(
62+
testCase ->
63+
DynamicTest.dynamicTest(
64+
testCase.name,
65+
() -> {
66+
HostIdResourceProvider provider =
67+
new HostIdResourceProvider(() -> "linux", testCase.pathReader, null);
68+
69+
assertHostId(testCase.expectedValue, provider);
70+
}))
71+
.collect(Collectors.toList());
72+
}
73+
74+
@TestFactory
75+
Collection<DynamicTest> createResourceWindows() {
76+
return Stream.of(
77+
new WindowsTestCase(
78+
"default",
79+
"test",
80+
() ->
81+
Arrays.asList(
82+
"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography",
83+
" MachineGuid REG_SZ test")),
84+
new WindowsTestCase("short output", null, Collections::emptyList))
85+
.map(
86+
testCase ->
87+
DynamicTest.dynamicTest(
88+
testCase.name,
89+
() -> {
90+
HostIdResourceProvider provider =
91+
new HostIdResourceProvider(
92+
() -> "Windows 95", null, testCase.queryWindowsRegistry);
93+
94+
assertHostId(testCase.expectedValue, provider);
95+
}))
96+
.collect(Collectors.toList());
97+
}
98+
99+
private static void assertHostId(String expectedValue, HostIdResourceProvider provider) {
100+
MapAssert<AttributeKey<?>, Object> that =
101+
assertThat(provider.createResource(null).getAttributes().asMap());
102+
103+
if (expectedValue == null) {
104+
that.isEmpty();
105+
} else {
106+
that.containsEntry(ResourceAttributes.HOST_ID, expectedValue);
107+
}
108+
}
109+
110+
@Test
111+
void shouldApply() {
112+
HostIdResourceProvider provider = new HostIdResourceProvider();
113+
assertThat(
114+
provider.shouldApply(
115+
DefaultConfigProperties.createFromMap(Collections.emptyMap()),
116+
Resource.getDefault()))
117+
.isTrue();
118+
assertThat(
119+
provider.shouldApply(
120+
DefaultConfigProperties.createFromMap(
121+
Collections.singletonMap("otel.resource.attributes", "host.id=foo")),
122+
null))
123+
.isFalse();
124+
}
125+
}

0 commit comments

Comments
 (0)