diff --git a/custom/src/main/java/com/solarwinds/opentelemetry/extensions/ResourceArbiter.java b/custom/src/main/java/com/solarwinds/opentelemetry/extensions/ResourceArbiter.java deleted file mode 100644 index 0ae45478..00000000 --- a/custom/src/main/java/com/solarwinds/opentelemetry/extensions/ResourceArbiter.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * © SolarWinds Worldwide, LLC. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.solarwinds.opentelemetry.extensions; - -import com.solarwinds.opentelemetry.extensions.config.provider.ResourceComponentProvider; -import io.opentelemetry.sdk.resources.Resource; - -public final class ResourceArbiter { - private ResourceArbiter() {} - - public static Resource resource() { - Resource resource = ResourceCustomizer.getResource(); - return resource == null ? ResourceComponentProvider.getResource() : resource; - } -} diff --git a/custom/src/main/java/com/solarwinds/opentelemetry/extensions/config/HostIdResourceUtil.java b/custom/src/main/java/com/solarwinds/opentelemetry/extensions/config/HostIdResourceUtil.java index 5707c0ff..138ce5b6 100644 --- a/custom/src/main/java/com/solarwinds/opentelemetry/extensions/config/HostIdResourceUtil.java +++ b/custom/src/main/java/com/solarwinds/opentelemetry/extensions/config/HostIdResourceUtil.java @@ -22,18 +22,26 @@ import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.semconv.K8sAttributes; -import io.opentelemetry.semconv.incubating.HostIncubatingAttributes; public final class HostIdResourceUtil { private HostIdResourceUtil() {} - public static Attributes createAttribute() { - AttributesBuilder builder = Attributes.builder(); - HostId hostId = ServerHostInfoReader.INSTANCE.getHostId(); - builder.put(AttributeKey.stringArrayKey("mac.addresses"), hostId.getMacAddresses()); + public static Attributes createUamsAttributes() { + return createUamsAttributes(ServerHostInfoReader.INSTANCE.getHostId()); + } - builder.put(HostIncubatingAttributes.HOST_ID, hostId.getHerokuDynoId()); - builder.put(AttributeKey.stringKey("sw.uams.client.id"), hostId.getUamsClientId()); + static Attributes createUamsAttributes(HostId hostId) { + return Attributes.builder() + .put(AttributeKey.stringKey("sw.uams.client.id"), hostId.getUamsClientId()) + .build(); + } + + public static Attributes createK8sAttributes() { + return createK8sAttributes(ServerHostInfoReader.INSTANCE.getHostId()); + } + + static Attributes createK8sAttributes(HostId hostId) { + AttributesBuilder builder = Attributes.builder(); HostId.K8sMetadata k8sMetadata = hostId.getK8sMetadata(); if (k8sMetadata != null) { @@ -44,4 +52,8 @@ public static Attributes createAttribute() { return builder.build(); } + + public static Attributes createAttribute() { + return createUamsAttributes().toBuilder().putAll(createK8sAttributes()).build(); + } } diff --git a/custom/src/main/java/com/solarwinds/opentelemetry/extensions/config/provider/ResourceComponentProvider.java b/custom/src/main/java/com/solarwinds/opentelemetry/extensions/config/provider/ApmResourceComponentProvider.java similarity index 71% rename from custom/src/main/java/com/solarwinds/opentelemetry/extensions/config/provider/ResourceComponentProvider.java rename to custom/src/main/java/com/solarwinds/opentelemetry/extensions/config/provider/ApmResourceComponentProvider.java index 6fbbc07a..a96e8797 100644 --- a/custom/src/main/java/com/solarwinds/opentelemetry/extensions/config/provider/ResourceComponentProvider.java +++ b/custom/src/main/java/com/solarwinds/opentelemetry/extensions/config/provider/ApmResourceComponentProvider.java @@ -25,19 +25,11 @@ import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties; import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider; import io.opentelemetry.sdk.resources.Resource; -import lombok.Getter; -import lombok.Setter; @AutoService(ComponentProvider.class) -public class ResourceComponentProvider implements ComponentProvider { +public class ApmResourceComponentProvider implements ComponentProvider { - public static final String COMPONENT_NAME = "swo/resource"; - - // Accumulated statically: create() merges into this field in place, and - // HostIdResourceComponentProvider reads it via getResource().merge(...). Assumes create() is - // invoked once; merging identical attributes is idempotent, but any new writer of this field - // must be aware the result is order-dependent. - @Getter @Setter private static Resource resource = Resource.getDefault(); + public static final String COMPONENT_NAME = "swo_apm"; @Override public Class getType() { @@ -54,7 +46,6 @@ public Resource create(DeclarativeConfigProperties declarativeConfigProperties) Attributes resourceAttributes = Attributes.of(moduleKey, "apm", versionKey, BuildConfig.SOLARWINDS_AGENT_VERSION); - resource = resource.merge(Resource.create(resourceAttributes)); - return resource; + return Resource.create(resourceAttributes); } } diff --git a/custom/src/main/java/com/solarwinds/opentelemetry/extensions/config/provider/CustomConfigCustomizerProvider.java b/custom/src/main/java/com/solarwinds/opentelemetry/extensions/config/provider/CustomConfigCustomizerProvider.java deleted file mode 100644 index 91f3a480..00000000 --- a/custom/src/main/java/com/solarwinds/opentelemetry/extensions/config/provider/CustomConfigCustomizerProvider.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * © SolarWinds Worldwide, LLC. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.solarwinds.opentelemetry.extensions.config.provider; - -import com.google.auto.service.AutoService; -import io.opentelemetry.sdk.autoconfigure.declarativeconfig.DeclarativeConfigurationCustomizer; -import io.opentelemetry.sdk.autoconfigure.declarativeconfig.DeclarativeConfigurationCustomizerProvider; -import io.opentelemetry.sdk.autoconfigure.declarativeconfig.model.ResourceModel; -import io.opentelemetry.sdk.autoconfigure.declarativeconfig.model.internal.ExperimentalResourceDetectionModel; -import io.opentelemetry.sdk.autoconfigure.declarativeconfig.model.internal.ExperimentalResourceDetectorModel; -import io.opentelemetry.sdk.autoconfigure.declarativeconfig.model.internal.ExperimentalResourceDetectorPropertyModel; -import io.opentelemetry.sdk.autoconfigure.declarativeconfig.model.internal.ResourceModelAccessor; -import java.util.ArrayList; -import java.util.List; - -@AutoService(DeclarativeConfigurationCustomizerProvider.class) -public class CustomConfigCustomizerProvider implements DeclarativeConfigurationCustomizerProvider { - - @Override - public void customize(DeclarativeConfigurationCustomizer customizer) { - customizer.addModelCustomizer( - configurationModel -> { - ResourceModel resourceModel = configurationModel.getResource(); - if (resourceModel == null) { - resourceModel = new ResourceModel(); - configurationModel.setResource(resourceModel); - } - - addResourceDetector(resourceModel); - return configurationModel; - }); - } - - private void addResourceDetector(ResourceModel resourceModel) { - ExperimentalResourceDetectionModel detectionDevelopment = - ResourceModelAccessor.getDetection(resourceModel); - if (detectionDevelopment == null) { - detectionDevelopment = new ExperimentalResourceDetectionModel(); - } - - List detectors = detectionDevelopment.getDetectors(); - if (detectors == null) { - detectors = new ArrayList<>(); - } - - // Ordering matters: SWO detectors are added first, then upstream detectors (e.g. aws/azure) - // are appended so they run last. With last-writer-wins merge semantics this lets upstream - // cloud attributes override our host-id attributes. Do not reorder without accounting for - // which detector's attributes should survive the merge. - List newDetectors = new ArrayList<>(); - newDetectors.add( - new ExperimentalResourceDetectorModel() - .setAdditionalProperty( - ResourceComponentProvider.COMPONENT_NAME, - new ExperimentalResourceDetectorPropertyModel())); - - newDetectors.add( - new ExperimentalResourceDetectorModel() - .setAdditionalProperty( - HostIdResourceComponentProvider.COMPONENT_NAME, - new ExperimentalResourceDetectorPropertyModel())); - - newDetectors.addAll(detectors); - detectionDevelopment.setDetectors(newDetectors); - ResourceModelAccessor.setDetection(resourceModel, detectionDevelopment); - } -} diff --git a/custom/src/main/java/com/solarwinds/opentelemetry/extensions/config/provider/K8sResourceComponentProvider.java b/custom/src/main/java/com/solarwinds/opentelemetry/extensions/config/provider/K8sResourceComponentProvider.java new file mode 100644 index 00000000..a6f20863 --- /dev/null +++ b/custom/src/main/java/com/solarwinds/opentelemetry/extensions/config/provider/K8sResourceComponentProvider.java @@ -0,0 +1,58 @@ +/* + * © SolarWinds Worldwide, LLC. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.solarwinds.opentelemetry.extensions.config.provider; + +import com.google.auto.service.AutoService; +import com.solarwinds.opentelemetry.extensions.config.HostIdResourceUtil; +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.common.AttributesBuilder; +import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties; +import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider; +import io.opentelemetry.sdk.resources.Resource; +import io.opentelemetry.semconv.K8sAttributes; + +@AutoService(ComponentProvider.class) +public class K8sResourceComponentProvider implements ComponentProvider { + + public static final String COMPONENT_NAME = "swo_k8s"; + + @Override + public Class getType() { + return Resource.class; + } + + @Override + public String getName() { + return COMPONENT_NAME; + } + + @Override + public Resource create(DeclarativeConfigProperties config) { + AttributesBuilder builder = HostIdResourceUtil.createK8sAttributes().toBuilder(); + putIfConfigured(builder, K8sAttributes.K8S_NAMESPACE_NAME, config.getString("namespace")); + putIfConfigured(builder, K8sAttributes.K8S_POD_UID, config.getString("pod_uid")); + putIfConfigured(builder, K8sAttributes.K8S_POD_NAME, config.getString("pod_name")); + return Resource.create(builder.build()); + } + + private static void putIfConfigured( + AttributesBuilder builder, AttributeKey key, String value) { + if (value != null && !value.isEmpty()) { + builder.put(key, value); + } + } +} diff --git a/custom/src/main/java/com/solarwinds/opentelemetry/extensions/config/provider/HostIdResourceComponentProvider.java b/custom/src/main/java/com/solarwinds/opentelemetry/extensions/config/provider/UamsResourceComponentProvider.java similarity index 80% rename from custom/src/main/java/com/solarwinds/opentelemetry/extensions/config/provider/HostIdResourceComponentProvider.java rename to custom/src/main/java/com/solarwinds/opentelemetry/extensions/config/provider/UamsResourceComponentProvider.java index 6fb10295..c00887f1 100644 --- a/custom/src/main/java/com/solarwinds/opentelemetry/extensions/config/provider/HostIdResourceComponentProvider.java +++ b/custom/src/main/java/com/solarwinds/opentelemetry/extensions/config/provider/UamsResourceComponentProvider.java @@ -24,9 +24,9 @@ import io.opentelemetry.sdk.resources.Resource; @AutoService(ComponentProvider.class) -public class HostIdResourceComponentProvider implements ComponentProvider { +public class UamsResourceComponentProvider implements ComponentProvider { - public static final String COMPONENT_NAME = "swo/hostIdResource"; + public static final String COMPONENT_NAME = "swo_uams"; @Override public Class getType() { @@ -40,9 +40,7 @@ public String getName() { @Override public Resource create(DeclarativeConfigProperties declarativeConfigProperties) { - Attributes attribute = HostIdResourceUtil.createAttribute(); - ResourceComponentProvider.setResource( - ResourceComponentProvider.getResource().merge(Resource.create(attribute))); + Attributes attribute = HostIdResourceUtil.createUamsAttributes(); return Resource.create(attribute); } } diff --git a/custom/src/test/java/com/solarwinds/opentelemetry/extensions/config/HostIdResourceUtilTest.java b/custom/src/test/java/com/solarwinds/opentelemetry/extensions/config/HostIdResourceUtilTest.java index 0d6661bd..dae68f58 100644 --- a/custom/src/test/java/com/solarwinds/opentelemetry/extensions/config/HostIdResourceUtilTest.java +++ b/custom/src/test/java/com/solarwinds/opentelemetry/extensions/config/HostIdResourceUtilTest.java @@ -16,18 +16,55 @@ package com.solarwinds.opentelemetry.extensions.config; -import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import com.solarwinds.joboe.core.HostId; import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; -import java.util.List; +import io.opentelemetry.semconv.K8sAttributes; import org.junit.jupiter.api.Test; class HostIdResourceUtilTest { @Test - void testCreateAttributes() { - Attributes attribute = HostIdResourceUtil.createAttribute(); - List macAddresses = attribute.get(AttributeKey.stringArrayKey("mac.addresses")); - assertNotNull(macAddresses); + void uamsAttributesDoNotIncludeDroppedKeys() { + Attributes attributes = HostIdResourceUtil.createUamsAttributes(); + assertNull(attributes.get(AttributeKey.stringArrayKey("mac.addresses"))); + assertNull(attributes.get(AttributeKey.stringKey("host.id"))); + } + + @Test + void k8sAttributesOnlyContainK8sKeys() { + HostIdResourceUtil.createK8sAttributes() + .forEach((key, value) -> assertTrue(key.getKey().startsWith("k8s."))); + } + + @Test + void uamsAttributesContainClientId() { + HostId hostId = HostId.builder().uamsClientId("client-id").herokuDynoId("dyno").build(); + Attributes attributes = HostIdResourceUtil.createUamsAttributes(hostId); + + assertEquals("client-id", attributes.get(AttributeKey.stringKey("sw.uams.client.id"))); + assertEquals(1, attributes.size()); + } + + @Test + void k8sAttributesAreMappedFromMetadata() { + HostId hostId = + HostId.builder() + .k8sMetadata( + HostId.K8sMetadata.builder().namespace("ns").podName("pod").podUid("uid").build()) + .build(); + Attributes attributes = HostIdResourceUtil.createK8sAttributes(hostId); + + assertEquals("ns", attributes.get(K8sAttributes.K8S_NAMESPACE_NAME)); + assertEquals("pod", attributes.get(K8sAttributes.K8S_POD_NAME)); + assertEquals("uid", attributes.get(K8sAttributes.K8S_POD_UID)); + } + + @Test + void k8sAttributesEmptyWithoutMetadata() { + assertEquals(0, HostIdResourceUtil.createK8sAttributes(HostId.builder().build()).size()); } } diff --git a/custom/src/test/java/com/solarwinds/opentelemetry/extensions/config/provider/CustomConfigCustomizerProviderTest.java b/custom/src/test/java/com/solarwinds/opentelemetry/extensions/config/provider/CustomConfigCustomizerProviderTest.java deleted file mode 100644 index 25a794cd..00000000 --- a/custom/src/test/java/com/solarwinds/opentelemetry/extensions/config/provider/CustomConfigCustomizerProviderTest.java +++ /dev/null @@ -1,151 +0,0 @@ -/* - * © SolarWinds Worldwide, LLC. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.solarwinds.opentelemetry.extensions.config.provider; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.mockStatic; - -import com.solarwinds.joboe.config.JavaRuntimeVersionChecker; -import io.opentelemetry.sdk.autoconfigure.declarativeconfig.DeclarativeConfigurationCustomizer; -import io.opentelemetry.sdk.autoconfigure.declarativeconfig.model.OpenTelemetryConfigurationModel; -import io.opentelemetry.sdk.autoconfigure.declarativeconfig.model.ResourceModel; -import io.opentelemetry.sdk.autoconfigure.declarativeconfig.model.internal.ExperimentalResourceDetectionModel; -import io.opentelemetry.sdk.autoconfigure.declarativeconfig.model.internal.ExperimentalResourceDetectorModel; -import io.opentelemetry.sdk.autoconfigure.declarativeconfig.model.internal.ExperimentalResourceDetectorPropertyModel; -import io.opentelemetry.sdk.autoconfigure.declarativeconfig.model.internal.ResourceModelAccessor; -import java.util.Collections; -import java.util.List; -import java.util.function.Function; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.ArgumentCaptor; -import org.mockito.Captor; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockedStatic; -import org.mockito.junit.jupiter.MockitoExtension; - -@ExtendWith(MockitoExtension.class) -class CustomConfigCustomizerProviderTest { - - @InjectMocks private CustomConfigCustomizerProvider tested; - - @Mock private DeclarativeConfigurationCustomizer declarativeConfigurationCustomizerMock; - - @Captor - private ArgumentCaptor> - functionArgumentCaptor; - - @Test - void verifyThatResourceDetectorIsAlwaysAdded() { - try (MockedStatic javaRuntimeVersionCheckerMockedStatic = - mockStatic(JavaRuntimeVersionChecker.class)) { - javaRuntimeVersionCheckerMockedStatic - .when(JavaRuntimeVersionChecker::isJdkVersionSupported) - .thenReturn(false); - - OpenTelemetryConfigurationModel openTelemetryConfigurationModel = - new OpenTelemetryConfigurationModel(); - - doNothing() - .when(declarativeConfigurationCustomizerMock) - .addModelCustomizer(functionArgumentCaptor.capture()); - - tested.customize(declarativeConfigurationCustomizerMock); - functionArgumentCaptor.getValue().apply(openTelemetryConfigurationModel); - - assertNotNull(openTelemetryConfigurationModel.getResource()); - ExperimentalResourceDetectionModel detection = - ResourceModelAccessor.getDetection(openTelemetryConfigurationModel.getResource()); - assertNotNull(detection); - assertFalse(detection.getDetectors().isEmpty()); - } - } - - @Test - void verifyThatExistingResourceDetectorsArePreservedAfterOurs() { - try (MockedStatic javaRuntimeVersionCheckerMockedStatic = - mockStatic(JavaRuntimeVersionChecker.class)) { - javaRuntimeVersionCheckerMockedStatic - .when(JavaRuntimeVersionChecker::isJdkVersionSupported) - .thenReturn(false); - - ResourceModel resourceModel = new ResourceModel(); - ResourceModelAccessor.setDetection( - resourceModel, - new ExperimentalResourceDetectionModel() - .setDetectors( - Collections.singletonList( - new ExperimentalResourceDetectorModel() - .setAdditionalProperty( - "upstream", new ExperimentalResourceDetectorPropertyModel())))); - OpenTelemetryConfigurationModel openTelemetryConfigurationModel = - new OpenTelemetryConfigurationModel().setResource(resourceModel); - - doNothing() - .when(declarativeConfigurationCustomizerMock) - .addModelCustomizer(functionArgumentCaptor.capture()); - - tested.customize(declarativeConfigurationCustomizerMock); - functionArgumentCaptor.getValue().apply(openTelemetryConfigurationModel); - - List detectors = - ResourceModelAccessor.getDetection(openTelemetryConfigurationModel.getResource()) - .getDetectors(); - assertEquals(3, detectors.size()); - assertTrue( - detectors - .get(0) - .getAdditionalProperties() - .containsKey(ResourceComponentProvider.COMPONENT_NAME)); - assertTrue( - detectors - .get(1) - .getAdditionalProperties() - .containsKey(HostIdResourceComponentProvider.COMPONENT_NAME)); - assertTrue(detectors.get(2).getAdditionalProperties().containsKey("upstream")); - } - } - - @Test - void processorsNotAddedWhenTracerProviderAbsent() { - try (MockedStatic javaRuntimeVersionCheckerMockedStatic = - mockStatic(JavaRuntimeVersionChecker.class)) { - javaRuntimeVersionCheckerMockedStatic - .when(JavaRuntimeVersionChecker::isJdkVersionSupported) - .thenReturn(true); - - OpenTelemetryConfigurationModel openTelemetryConfigurationModel = - new OpenTelemetryConfigurationModel(); - - doNothing() - .when(declarativeConfigurationCustomizerMock) - .addModelCustomizer(functionArgumentCaptor.capture()); - - tested.customize(declarativeConfigurationCustomizerMock); - functionArgumentCaptor.getValue().apply(openTelemetryConfigurationModel); - - assertNull(openTelemetryConfigurationModel.getTracerProvider()); - assertNotNull(openTelemetryConfigurationModel.getResource()); - } - } -} diff --git a/custom/src/test/java/com/solarwinds/opentelemetry/extensions/config/provider/K8sResourceComponentProviderTest.java b/custom/src/test/java/com/solarwinds/opentelemetry/extensions/config/provider/K8sResourceComponentProviderTest.java new file mode 100644 index 00000000..40bcff2e --- /dev/null +++ b/custom/src/test/java/com/solarwinds/opentelemetry/extensions/config/provider/K8sResourceComponentProviderTest.java @@ -0,0 +1,59 @@ +/* + * © SolarWinds Worldwide, LLC. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.solarwinds.opentelemetry.extensions.config.provider; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties; +import io.opentelemetry.sdk.resources.Resource; +import io.opentelemetry.semconv.K8sAttributes; +import org.junit.jupiter.api.Test; + +class K8sResourceComponentProviderTest { + private final K8sResourceComponentProvider tested = new K8sResourceComponentProvider(); + + @Test + void testName() { + assertEquals("swo_k8s", tested.getName()); + } + + @Test + void configuredValuesAreUsed() { + DeclarativeConfigProperties config = mock(DeclarativeConfigProperties.class); + when(config.getString("namespace")).thenReturn("ns"); + when(config.getString("pod_uid")).thenReturn("uid"); + when(config.getString("pod_name")).thenReturn("name"); + + Resource resource = tested.create(config); + + assertEquals("ns", resource.getAttribute(K8sAttributes.K8S_NAMESPACE_NAME)); + assertEquals("uid", resource.getAttribute(K8sAttributes.K8S_POD_UID)); + assertEquals("name", resource.getAttribute(K8sAttributes.K8S_POD_NAME)); + } + + @Test + void onlyConfiguredValuesAreSetWhenPartial() { + DeclarativeConfigProperties config = mock(DeclarativeConfigProperties.class); + when(config.getString("pod_name")).thenReturn("name"); + + Resource resource = tested.create(config); + assertEquals("name", resource.getAttribute(K8sAttributes.K8S_POD_NAME)); + assertEquals(1, resource.getAttributes().size()); + } +} diff --git a/custom/src/test/java/com/solarwinds/opentelemetry/extensions/config/provider/HostIdResourceComponentProviderTest.java b/custom/src/test/java/com/solarwinds/opentelemetry/extensions/config/provider/UamsResourceComponentProviderTest.java similarity index 80% rename from custom/src/test/java/com/solarwinds/opentelemetry/extensions/config/provider/HostIdResourceComponentProviderTest.java rename to custom/src/test/java/com/solarwinds/opentelemetry/extensions/config/provider/UamsResourceComponentProviderTest.java index b93afd4a..f4f8955d 100644 --- a/custom/src/test/java/com/solarwinds/opentelemetry/extensions/config/provider/HostIdResourceComponentProviderTest.java +++ b/custom/src/test/java/com/solarwinds/opentelemetry/extensions/config/provider/UamsResourceComponentProviderTest.java @@ -20,11 +20,11 @@ import org.junit.jupiter.api.Test; -class HostIdResourceComponentProviderTest { - private final HostIdResourceComponentProvider tested = new HostIdResourceComponentProvider(); +class UamsResourceComponentProviderTest { + private final UamsResourceComponentProvider tested = new UamsResourceComponentProvider(); @Test void testName() { - assertEquals("swo/hostIdResource", tested.getName()); + assertEquals("swo_uams", tested.getName()); } } diff --git a/custom/src/test/java/com/solarwinds/opentelemetry/extensions/provider/ResourceComponentProviderTest.java b/custom/src/test/java/com/solarwinds/opentelemetry/extensions/provider/ApmResourceComponentProviderTest.java similarity index 84% rename from custom/src/test/java/com/solarwinds/opentelemetry/extensions/provider/ResourceComponentProviderTest.java rename to custom/src/test/java/com/solarwinds/opentelemetry/extensions/provider/ApmResourceComponentProviderTest.java index fe6c5407..6084f992 100644 --- a/custom/src/test/java/com/solarwinds/opentelemetry/extensions/provider/ResourceComponentProviderTest.java +++ b/custom/src/test/java/com/solarwinds/opentelemetry/extensions/provider/ApmResourceComponentProviderTest.java @@ -19,7 +19,7 @@ import static com.solarwinds.opentelemetry.extensions.ApmResourceProvider.moduleKey; import static org.junit.jupiter.api.Assertions.assertEquals; -import com.solarwinds.opentelemetry.extensions.config.provider.ResourceComponentProvider; +import com.solarwinds.opentelemetry.extensions.config.provider.ApmResourceComponentProvider; import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties; import io.opentelemetry.sdk.resources.Resource; import org.junit.jupiter.api.Test; @@ -28,15 +28,15 @@ import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) -class ResourceComponentProviderTest { +class ApmResourceComponentProviderTest { - private final ResourceComponentProvider tested = new ResourceComponentProvider(); + private final ApmResourceComponentProvider tested = new ApmResourceComponentProvider(); @Mock private DeclarativeConfigProperties declarativeConfigPropertiesMock; @Test void testName() { - assertEquals("swo/resource", tested.getName()); + assertEquals("swo_apm", tested.getName()); } @Test diff --git a/custom/src/test/resources/sdk-config-bad.yaml b/custom/src/test/resources/sdk-config-bad.yaml index a23f006a..8e362c28 100644 --- a/custom/src/test/resources/sdk-config-bad.yaml +++ b/custom/src/test/resources/sdk-config-bad.yaml @@ -18,6 +18,12 @@ resource: attributes: - name: service.name value: java-apm-smoke-test + # Configure resource detectors. SolarWinds detectors are listed first so upstream detectors can override their attributes. + detection/development: + detectors: + - swo_apm: + - swo_uams: + - swo_k8s: tracer_provider: processors: diff --git a/custom/src/test/resources/sdk-config.yaml b/custom/src/test/resources/sdk-config.yaml index 5855f556..dcec6b67 100644 --- a/custom/src/test/resources/sdk-config.yaml +++ b/custom/src/test/resources/sdk-config.yaml @@ -18,6 +18,12 @@ resource: attributes: - name: service.name value: java-apm-smoke-test + # Configure resource detectors. SolarWinds detectors are listed first so upstream detectors can override their attributes. + detection/development: + detectors: + - swo_apm: + - swo_uams: + - swo_k8s: # Configure general attribute limits. See also tracer_provider.limits, logger_provider.limits. attribute_limits: diff --git a/libs/shared/src/test/resources/sdk-config.yaml b/libs/shared/src/test/resources/sdk-config.yaml index fc7bead7..61572cd8 100644 --- a/libs/shared/src/test/resources/sdk-config.yaml +++ b/libs/shared/src/test/resources/sdk-config.yaml @@ -18,6 +18,12 @@ resource: attributes: - name: service.name value: java-apm-smoke-test + # Configure resource detectors. SolarWinds detectors are listed first so upstream detectors can override their attributes. + detection/development: + detectors: + - swo_apm: + - swo_uams: + - swo_k8s: # Configure general attribute limits. See also tracer_provider.limits, logger_provider.limits. attribute_limits: diff --git a/sdk-config.yaml b/sdk-config.yaml index 11ada32e..f029a6a1 100644 --- a/sdk-config.yaml +++ b/sdk-config.yaml @@ -18,6 +18,12 @@ resource: attributes: - name: service.name value: swi-test-app + # Configure resource detectors. SolarWinds detectors are listed first so upstream detectors can override their attributes. + detection/development: + detectors: + - swo_apm: + - swo_uams: + - swo_k8s: # Provide shared config for multi-language deployment distribution: diff --git a/smoke-tests/sdk-config.yaml b/smoke-tests/sdk-config.yaml index 295a4689..e9958c0d 100644 --- a/smoke-tests/sdk-config.yaml +++ b/smoke-tests/sdk-config.yaml @@ -20,6 +20,12 @@ resource: value: java-apm-smoke-test - name: test.app value: java-apm-smoke-test-v2 + # Configure resource detectors. SolarWinds detectors are listed first so upstream detectors can override their attributes. + detection/development: + detectors: + - swo_apm: + - swo_uams: + - swo_k8s: # Provide shared config for multi-language deployment distribution: