diff --git a/sdk-extensions/autoconfigure/build.gradle.kts b/sdk-extensions/autoconfigure/build.gradle.kts
index 3f2402b5f9f..62fcd639c79 100644
--- a/sdk-extensions/autoconfigure/build.gradle.kts
+++ b/sdk-extensions/autoconfigure/build.gradle.kts
@@ -11,7 +11,6 @@ dependencies {
api(project(":sdk-extensions:autoconfigure-spi"))
implementation(project(":api:events"))
- implementation("com.github.f4b6a3:uuid-creator:5.3.3")
annotationProcessor("com.google.auto.value:auto-value")
diff --git a/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/ResourceConfiguration.java b/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/ResourceConfiguration.java
index 057ddf546db..010d219acba 100644
--- a/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/ResourceConfiguration.java
+++ b/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/ResourceConfiguration.java
@@ -8,7 +8,6 @@
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
-import io.opentelemetry.sdk.autoconfigure.internal.ServiceInstanceIdResourceProvider;
import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
@@ -107,8 +106,6 @@ static Resource configureResource(
result = result.merge(resourceProvider.createResource(config));
}
- result = result.merge(ServiceInstanceIdResourceProvider.createResource(result.getAttributes()));
-
result = filterAttributes(result, config);
return resourceCustomizer.apply(result, config);
diff --git a/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/HostIdResourceProvider.java b/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/HostIdResourceProvider.java
deleted file mode 100644
index 3702cecee7f..00000000000
--- a/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/HostIdResourceProvider.java
+++ /dev/null
@@ -1,176 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.sdk.autoconfigure.internal;
-
-import io.opentelemetry.api.common.AttributeKey;
-import io.opentelemetry.api.common.Attributes;
-import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
-import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
-import io.opentelemetry.sdk.autoconfigure.spi.internal.ConditionalResourceProvider;
-import io.opentelemetry.sdk.resources.Resource;
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.nio.charset.StandardCharsets;
-import java.nio.file.FileSystems;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.util.List;
-import java.util.function.Function;
-import java.util.function.Supplier;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-import java.util.stream.Collectors;
-
-/** {@link ResourceProvider} for automatically configuring host.id
. */
-public final class HostIdResourceProvider implements ConditionalResourceProvider {
-
- private static final Logger logger = Logger.getLogger(HostIdResourceProvider.class.getName());
-
- public static final AttributeKey HOST_ID = AttributeKey.stringKey("host.id");
- public static final String REGISTRY_QUERY =
- "reg query HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography /v MachineGuid";
-
- private final Supplier getOsType;
-
- private final Function> pathReader;
-
- private final Supplier queryWindowsRegistry;
-
- enum OsType {
- WINDOWS,
- LINUX
- }
-
- static class ExecResult {
- int exitCode;
- List lines;
-
- public ExecResult(int exitCode, List lines) {
- this.exitCode = exitCode;
- this.lines = lines;
- }
- }
-
- public HostIdResourceProvider() {
- this(
- HostIdResourceProvider::getOsType,
- path -> {
- try {
- return Files.readAllLines(path);
- } catch (IOException e) {
- throw new IllegalStateException(e);
- }
- },
- HostIdResourceProvider::queryWindowsRegistry);
- }
-
- // Visible for testing
- HostIdResourceProvider(
- Supplier getOsType,
- Function> pathReader,
- Supplier queryWindowsRegistry) {
- this.getOsType = getOsType;
- this.pathReader = pathReader;
- this.queryWindowsRegistry = queryWindowsRegistry;
- }
-
- @Override
- public Resource createResource(ConfigProperties config) {
- OsType osType = getOsType.get();
- switch (osType) {
- case WINDOWS:
- return readWindowsGuid();
- case LINUX:
- return readLinuxMachineId();
- }
- throw new IllegalStateException("Unsupported OS type: " + osType);
- }
-
- private Resource readLinuxMachineId() {
- Path path = FileSystems.getDefault().getPath("/etc/machine-id");
- try {
- List lines = pathReader.apply(path);
- if (lines.isEmpty()) {
- logger.warning("Failed to read /etc/machine-id: empty file");
- return Resource.empty();
- }
- return Resource.create(Attributes.of(HOST_ID, lines.get(0)));
- } catch (RuntimeException e) {
- logger.log(Level.WARNING, "Failed to read /etc/machine-id", e);
- return Resource.empty();
- }
- }
-
- private static OsType getOsType() {
- String osName = System.getProperty("os.name");
- return osName != null && osName.startsWith("Windows") ? OsType.WINDOWS : OsType.LINUX;
- }
-
- private Resource readWindowsGuid() {
-
- try {
- ExecResult execResult = queryWindowsRegistry.get();
-
- if (execResult.exitCode != 0) {
- logger.warning(
- "Failed to read Windows registry. Exit code: "
- + execResult.exitCode
- + " Output: "
- + String.join("\n", execResult.lines));
- return Resource.empty();
- }
-
- for (String line : execResult.lines) {
- if (line.contains("MachineGuid")) {
- String[] parts = line.trim().split("\\s+");
- if (parts.length == 3) {
- return Resource.create(Attributes.of(HOST_ID, parts[2]));
- }
- }
- }
- logger.warning(
- "Failed to read Windows registry: No MachineGuid found in output: " + execResult.lines);
- return Resource.empty();
- } catch (RuntimeException e) {
- logger.log(Level.WARNING, "Failed to read Windows registry", e);
- return Resource.empty();
- }
- }
-
- private static ExecResult queryWindowsRegistry() {
- try {
- Process process = Runtime.getRuntime().exec(REGISTRY_QUERY);
-
- if (process.waitFor() != 0) {
- return new ExecResult(process.exitValue(), getLines(process.getErrorStream()));
- }
-
- return new ExecResult(0, getLines(process.getInputStream()));
- } catch (IOException | InterruptedException e) {
- throw new IllegalStateException(e);
- }
- }
-
- private static List getLines(InputStream inputStream) {
- return new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))
- .lines()
- .collect(Collectors.toList());
- }
-
- @Override
- public boolean shouldApply(ConfigProperties config, Resource existing) {
- return !config.getMap("otel.resource.attributes").containsKey(HOST_ID.getKey())
- && existing.getAttribute(HOST_ID) == null;
- }
-
- @Override
- public int order() {
- // Run after cloud provider resource providers
- return Integer.MAX_VALUE - 1;
- }
-}
diff --git a/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ServiceInstanceIdResourceProvider.java b/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ServiceInstanceIdResourceProvider.java
index f544280ebac..8eb81e6461d 100644
--- a/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ServiceInstanceIdResourceProvider.java
+++ b/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/internal/ServiceInstanceIdResourceProvider.java
@@ -5,192 +5,35 @@
package io.opentelemetry.sdk.autoconfigure.internal;
-import com.github.f4b6a3.uuid.UuidCreator;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
+import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
+import io.opentelemetry.sdk.autoconfigure.spi.internal.ConditionalResourceProvider;
import io.opentelemetry.sdk.resources.Resource;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
import java.util.UUID;
-import java.util.stream.Collectors;
/**
* does not implement {@link ResourceProvider}, because it depends on all attributes discovered by
* the other providers.
*/
-public final class ServiceInstanceIdResourceProvider {
-
- private static final AttributeKey SERVICE_NAMESPACE =
- AttributeKey.stringKey("service.namespace");
+public final class ServiceInstanceIdResourceProvider implements ConditionalResourceProvider {
public static final AttributeKey SERVICE_INSTANCE_ID =
AttributeKey.stringKey("service.instance.id");
- private static final AttributeKey SERVICE_NAME = AttributeKey.stringKey("service.name");
-
- private static final String UNKNOWN_SERVICE = "unknown_service:java";
-
- private static final AttributeKey TELEMETRY_SDK_NAME =
- AttributeKey.stringKey("telemetry.sdk.name");
-
- private static final AttributeKey TELEMETRY_SDK_LANGUAGE =
- AttributeKey.stringKey("telemetry.sdk.language");
-
- private static final AttributeKey K8S_POD_NAME = AttributeKey.stringKey("k8s.pod.name");
-
- private static final AttributeKey K8S_NAMESPACE_NAME =
- AttributeKey.stringKey("k8s.namespace.name");
-
- private static final AttributeKey K8S_CONTAINER_NAME =
- AttributeKey.stringKey("k8s.container.name");
-
- private static final AttributeKey CONTAINER_ID = AttributeKey.stringKey("container.id");
-
- private static final UUID SERVICE_INSTANCE_ID_NAMESPACE =
- UUID.fromString("4d63009a-8d0f-11ee-aad7-4c796ed8e320");
-
// multiple calls to this resource provider should return the same value
public static final Resource RANDOM =
- Resource.create(
- Attributes.of(SERVICE_INSTANCE_ID, UuidCreator.getRandomBasedFast().toString()));
- private static final String REQUEST_RANDOM_UUID_V4_VALUE = "uuidv4";
-
- private ServiceInstanceIdResourceProvider() {}
-
- interface Variant {
- boolean matches(Attributes attributes);
-
- Resource generate(Attributes attributes);
- }
-
- private static final List>> GENERATION_VARIANTS =
- Arrays.asList(
- Arrays.asList(
- TELEMETRY_SDK_NAME,
- TELEMETRY_SDK_LANGUAGE,
- K8S_NAMESPACE_NAME,
- SERVICE_NAMESPACE,
- SERVICE_NAME,
- K8S_POD_NAME,
- K8S_CONTAINER_NAME),
- Arrays.asList(
- TELEMETRY_SDK_NAME,
- TELEMETRY_SDK_LANGUAGE,
- K8S_NAMESPACE_NAME,
- SERVICE_NAME,
- K8S_POD_NAME,
- K8S_CONTAINER_NAME),
- Arrays.asList(
- TELEMETRY_SDK_NAME,
- TELEMETRY_SDK_LANGUAGE,
- K8S_NAMESPACE_NAME,
- K8S_POD_NAME,
- K8S_CONTAINER_NAME),
- Arrays.asList(
- TELEMETRY_SDK_NAME,
- TELEMETRY_SDK_LANGUAGE,
- SERVICE_NAMESPACE,
- SERVICE_NAME,
- CONTAINER_ID),
- Arrays.asList(TELEMETRY_SDK_NAME, TELEMETRY_SDK_LANGUAGE, SERVICE_NAME, CONTAINER_ID),
- Arrays.asList(
- TELEMETRY_SDK_NAME,
- TELEMETRY_SDK_LANGUAGE,
- SERVICE_NAMESPACE,
- SERVICE_NAME,
- HostIdResourceProvider.HOST_ID),
- Arrays.asList(
- TELEMETRY_SDK_NAME,
- TELEMETRY_SDK_LANGUAGE,
- SERVICE_NAME,
- HostIdResourceProvider.HOST_ID));
-
- private static List createVariants() {
- List result = new ArrayList<>();
-
- result.add(
- new Variant() {
- @Override
- public boolean matches(Attributes attributes) {
- return REQUEST_RANDOM_UUID_V4_VALUE.equals(attributes.get(SERVICE_INSTANCE_ID));
- }
-
- @Override
- public Resource generate(Attributes attributes) {
- return RANDOM;
- }
-
- @Override
- public String toString() {
- return REQUEST_RANDOM_UUID_V4_VALUE;
- }
- });
-
- result.add(
- new Variant() {
- @Override
- public boolean matches(Attributes attributes) {
- return attributes.get(SERVICE_INSTANCE_ID) != null;
- }
-
- @Override
- public Resource generate(Attributes attributes) {
- return Resource.empty();
- }
-
- @Override
- public String toString() {
- return "none";
- }
- });
-
- for (List> variant : GENERATION_VARIANTS) {
- result.add(
- new Variant() {
- @Override
- public boolean matches(Attributes attributes) {
- return variant.stream()
- .allMatch(
- key -> {
- Map, Object> map = attributes.asMap();
- if (key == SERVICE_NAME) {
- return !UNKNOWN_SERVICE.equals(map.getOrDefault(key, ""));
- }
- return map.containsKey(key);
- });
- }
-
- @Override
- public Resource generate(Attributes attributes) {
- String input = variant.stream().map(attributes::get).collect(Collectors.joining("."));
- return Resource.create(
- Attributes.of(
- SERVICE_INSTANCE_ID,
- UuidCreator.getNameBasedSha1(SERVICE_INSTANCE_ID_NAMESPACE, input)
- .toString()));
- }
-
- @Override
- public String toString() {
- return variant.stream().map(AttributeKey::getKey).collect(Collectors.joining(","));
- }
- });
- }
- return result;
- }
-
- private static final List VARIANTS = createVariants();
+ Resource.create(Attributes.of(SERVICE_INSTANCE_ID, UUID.randomUUID().toString()));
- public static Resource createResource(Attributes attributes) {
- return findVariant(attributes).map(variant -> variant.generate(attributes)).orElse(RANDOM);
+ @Override
+ public Resource createResource(ConfigProperties config) {
+ return RANDOM;
}
- // Visible for testing
- static Optional findVariant(Attributes attributes) {
- return VARIANTS.stream().filter(variant -> variant.matches(attributes)).findFirst();
+ @Override
+ public boolean shouldApply(ConfigProperties config, Resource existing) {
+ return config.getString(SERVICE_INSTANCE_ID.getKey()) == null
+ && existing.getAttribute(SERVICE_INSTANCE_ID) == null;
}
}
diff --git a/sdk-extensions/autoconfigure/src/main/resources/META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider b/sdk-extensions/autoconfigure/src/main/resources/META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider
index 44b98d51112..d80a7ad293c 100644
--- a/sdk-extensions/autoconfigure/src/main/resources/META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider
+++ b/sdk-extensions/autoconfigure/src/main/resources/META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider
@@ -1,2 +1,2 @@
-io.opentelemetry.sdk.autoconfigure.internal.HostIdResourceProvider
io.opentelemetry.sdk.autoconfigure.internal.EnvironmentResourceProvider
+io.opentelemetry.sdk.autoconfigure.internal.ServiceInstanceIdResourceProvider
diff --git a/sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/internal/HostIdResourceProviderTest.java b/sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/internal/HostIdResourceProviderTest.java
deleted file mode 100644
index 748995f7ae7..00000000000
--- a/sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/internal/HostIdResourceProviderTest.java
+++ /dev/null
@@ -1,144 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.sdk.autoconfigure.internal;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-import io.opentelemetry.api.common.AttributeKey;
-import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties;
-import io.opentelemetry.sdk.resources.Resource;
-import java.nio.file.Path;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-import java.util.function.Function;
-import java.util.function.Supplier;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
-import org.assertj.core.api.MapAssert;
-import org.junit.jupiter.api.DynamicTest;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.TestFactory;
-
-class HostIdResourceProviderTest {
-
- private static class LinuxTestCase {
- private final String name;
- private final String expectedValue;
- private final Function> pathReader;
-
- private LinuxTestCase(
- String name, String expectedValue, Function> pathReader) {
- this.name = name;
- this.expectedValue = expectedValue;
- this.pathReader = pathReader;
- }
- }
-
- private static class WindowsTestCase {
- private final String name;
- private final String expectedValue;
- private final Supplier queryWindowsRegistry;
-
- private WindowsTestCase(
- String name,
- String expectedValue,
- Supplier queryWindowsRegistry) {
- this.name = name;
- this.expectedValue = expectedValue;
- this.queryWindowsRegistry = queryWindowsRegistry;
- }
- }
-
- @TestFactory
- Collection createResourceLinux() {
- return Stream.of(
- new LinuxTestCase("default", "test", path -> Collections.singletonList("test")),
- new LinuxTestCase("empty file", null, path -> Collections.emptyList()),
- new LinuxTestCase(
- "error reading",
- null,
- path -> {
- throw new IllegalStateException("can't read file");
- }))
- .map(
- testCase ->
- DynamicTest.dynamicTest(
- testCase.name,
- () -> {
- HostIdResourceProvider provider =
- new HostIdResourceProvider(
- () -> HostIdResourceProvider.OsType.LINUX, testCase.pathReader, null);
-
- assertHostId(testCase.expectedValue, provider);
- }))
- .collect(Collectors.toList());
- }
-
- @TestFactory
- Collection createResourceWindows() {
- return Stream.of(
- new WindowsTestCase(
- "default",
- "test",
- () ->
- new HostIdResourceProvider.ExecResult(
- 0,
- Arrays.asList(
- "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography",
- " MachineGuid REG_SZ test"))),
- new WindowsTestCase(
- "error code",
- null,
- () -> new HostIdResourceProvider.ExecResult(1, Collections.emptyList())),
- new WindowsTestCase(
- "short output",
- null,
- () -> new HostIdResourceProvider.ExecResult(0, Collections.emptyList())))
- .map(
- testCase ->
- DynamicTest.dynamicTest(
- testCase.name,
- () -> {
- HostIdResourceProvider provider =
- new HostIdResourceProvider(
- () -> HostIdResourceProvider.OsType.WINDOWS,
- null,
- testCase.queryWindowsRegistry);
-
- assertHostId(testCase.expectedValue, provider);
- }))
- .collect(Collectors.toList());
- }
-
- private static void assertHostId(String expectedValue, HostIdResourceProvider provider) {
- MapAssert, Object> that =
- assertThat(provider.createResource(null).getAttributes().asMap());
-
- if (expectedValue == null) {
- that.isEmpty();
- } else {
- that.containsEntry(HostIdResourceProvider.HOST_ID, expectedValue);
- }
- }
-
- @Test
- void shouldApply() {
- HostIdResourceProvider provider = new HostIdResourceProvider();
- assertThat(
- provider.shouldApply(
- DefaultConfigProperties.createFromMap(Collections.emptyMap()),
- Resource.getDefault()))
- .isTrue();
- assertThat(
- provider.shouldApply(
- DefaultConfigProperties.createFromMap(
- Collections.singletonMap("otel.resource.attributes", "host.id=foo")),
- null))
- .isFalse();
- }
-}
diff --git a/sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/internal/ServiceInstanceIdResourceProviderTest.java b/sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/internal/ServiceInstanceIdResourceProviderTest.java
index c73d6890684..d3dcf304358 100644
--- a/sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/internal/ServiceInstanceIdResourceProviderTest.java
+++ b/sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/internal/ServiceInstanceIdResourceProviderTest.java
@@ -8,16 +8,11 @@
import static org.assertj.core.api.Assertions.assertThat;
import com.google.common.collect.ImmutableMap;
-import io.opentelemetry.api.common.Attributes;
-import io.opentelemetry.api.common.AttributesBuilder;
+import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties;
import io.opentelemetry.sdk.resources.Resource;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.List;
+import java.util.Collections;
import java.util.Map;
-import java.util.Optional;
-import java.util.stream.Collectors;
+import java.util.stream.Stream;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
@@ -26,161 +21,46 @@ class ServiceInstanceIdResourceProviderTest {
private static class TestCase {
private final String name;
final String expectedValue;
- final String expectedVariant;
final Map attributes;
- TestCase(
- String name, String expectedValue, String expectedVariant, Map attributes) {
+ TestCase(String name, String expectedValue, Map attributes) {
this.name = name;
this.expectedValue = expectedValue;
- this.expectedVariant = expectedVariant;
this.attributes = attributes;
}
}
- private static final List TEST_CASES =
- Arrays.asList(
- new TestCase(
- "user provided service.instance.id",
- null,
- "none",
- ImmutableMap.of(
- "service.instance.id",
- "custom",
- "service.name",
- "service",
- "service.namespace",
- "ns",
- "host.id",
- "host")),
- new TestCase(
- "user requested random service.instance.id",
- "random",
- "uuidv4",
- ImmutableMap.of(
- "service.instance.id",
- "uuidv4",
- "service.name",
- "service",
- "service.namespace",
- "ns",
- "host.id",
- "host")),
- new TestCase(
- "k8s pod",
- "f3a5f61b-9fff-5707-8d41-d3a9d2423b7d",
- "telemetry.sdk.name,telemetry.sdk.language,k8s.namespace.name,service.name,k8s.pod.name,k8s.container.name",
- ImmutableMap.of(
- "telemetry.sdk.name",
- "opentelemetry",
- "telemetry.sdk.language",
- "go",
- "k8s.pod.name",
- "vendors-pqr-jh7d2",
- "k8s.namespace.name",
- "accounting",
- "k8s.container.name",
- "some-sidecar",
- "service.name",
- "vendors",
- "host.id",
- "graviola")),
- new TestCase(
- "host with service namespace",
- "b6c5414c-1aae-5e72-aeea-866f47b7ea64",
- "telemetry.sdk.name,telemetry.sdk.language,service.namespace,service.name,host.id",
- ImmutableMap.of(
- "telemetry.sdk.name",
- "opentelemetry",
- "telemetry.sdk.language",
- "go",
- "service.name",
- "service",
- "service.namespace",
- "ns",
- "host.id",
- "host")),
- new TestCase(
- "host without service namespace",
- "17ffc8fd-6ed7-5069-a5fb-2fed78f5455f",
- "telemetry.sdk.name,telemetry.sdk.language,service.name,host.id",
- ImmutableMap.of(
- "telemetry.sdk.name",
- "opentelemetry",
- "telemetry.sdk.language",
- "go",
- "service.name",
- "customers",
- "host.id",
- "graviola")),
- new TestCase(
- "random value - default service name",
- "random",
- null,
- ImmutableMap.of(
- "telemetry.sdk.name",
- "opentelemetry",
- "telemetry.sdk.language",
- "go",
- "service.name",
- "unknown_service:java",
- "service.namespace",
- "ns",
- "host.id",
- "host")),
- new TestCase(
- "random value - no host found",
- "random",
- null,
- ImmutableMap.of(
- "telemetry.sdk.name",
- "opentelemetry",
- "telemetry.sdk.language",
- "go",
- "service.name",
- "service",
- "service.namespace",
- "ns")));
-
@TestFactory
- Collection createResource() {
- return TEST_CASES.stream()
+ Stream createResource() {
+ return Stream.of(
+ new TestCase(
+ "user provided service.instance.id",
+ null,
+ ImmutableMap.of("service.instance.id", "custom")),
+ new TestCase("random value", "random", Collections.emptyMap()))
.map(
testCase ->
- DynamicTest.dynamicTest(testCase.name, () -> runCreateResourceTest(testCase)))
- .collect(Collectors.toList());
- }
-
- private static void runCreateResourceTest(TestCase testCase) {
- // use "go" to make it comparable to the spec
- // https://github.com/open-telemetry/semantic-conventions/pull/312
- Map map = new HashMap<>(testCase.attributes);
- map.put("telemetry.sdk.name", "opentelemetry");
- map.put("telemetry.sdk.language", "go");
- Attributes attributes = parseAttributes(map);
-
- Optional variant =
- ServiceInstanceIdResourceProvider.findVariant(attributes);
- assertThat(variant.map(ServiceInstanceIdResourceProvider.Variant::toString).orElse(null))
- .isEqualTo(testCase.expectedVariant);
-
- Resource resource = ServiceInstanceIdResourceProvider.createResource(attributes);
-
- String actual =
- resource.getAttributes().get(ServiceInstanceIdResourceProvider.SERVICE_INSTANCE_ID);
- if ("random".equals(testCase.expectedValue)) {
- assertThat(actual).isNotNull();
- } else {
- assertThat(actual).isEqualTo(testCase.expectedValue);
- }
- }
-
- private static Attributes parseAttributes(Map map) {
- AttributesBuilder builder = Attributes.builder();
- for (Map.Entry entry : map.entrySet()) {
- builder.put(entry.getKey(), entry.getValue());
- }
+ DynamicTest.dynamicTest(
+ testCase.name,
+ () -> {
+ ServiceInstanceIdResourceProvider provider =
+ new ServiceInstanceIdResourceProvider();
+ DefaultConfigProperties config =
+ DefaultConfigProperties.createFromMap(testCase.attributes);
+ Resource resource =
+ provider.shouldApply(config, Resource.getDefault())
+ ? provider.createResource(config)
+ : Resource.empty();
- return builder.build();
+ String actual =
+ resource
+ .getAttributes()
+ .get(ServiceInstanceIdResourceProvider.SERVICE_INSTANCE_ID);
+ if ("random".equals(testCase.expectedValue)) {
+ assertThat(actual).isNotNull();
+ } else {
+ assertThat(actual).isEqualTo(testCase.expectedValue);
+ }
+ }));
}
}