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 e70a1455..5707c0ff 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 @@ -42,7 +42,6 @@ public static Attributes createAttribute() { builder.put(K8sAttributes.K8S_POD_NAME, k8sMetadata.getPodName()); } - builder.put(HostIncubatingAttributes.HOST_NAME, hostId.getHostname()); return builder.build(); } } diff --git a/custom/src/main/java/com/solarwinds/opentelemetry/extensions/config/HttpSettingsReader.java b/custom/src/main/java/com/solarwinds/opentelemetry/extensions/config/HttpSettingsReader.java index c8363ddc..c4c84293 100644 --- a/custom/src/main/java/com/solarwinds/opentelemetry/extensions/config/HttpSettingsReader.java +++ b/custom/src/main/java/com/solarwinds/opentelemetry/extensions/config/HttpSettingsReader.java @@ -23,8 +23,6 @@ import com.solarwinds.joboe.logging.Logger; import com.solarwinds.joboe.logging.LoggerFactory; import com.solarwinds.joboe.sampling.Settings; -import com.solarwinds.opentelemetry.extensions.ResourceArbiter; -import io.opentelemetry.semconv.incubating.HostIncubatingAttributes; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -51,15 +49,12 @@ public Settings getSettings() { String serviceName = ServiceKeyUtils.getServiceName(serviceKey); String apiToken = ServiceKeyUtils.getApiKey(serviceKey); - String hostname = ResourceArbiter.resource().getAttribute(HostIncubatingAttributes.HOST_NAME); - String settingsUrl = String.format("%s/v1/settings/%s/%s", collectorUrl, serviceName, hostname); + String settingsUrl = String.format("%s/v1/settings/%s", collectorUrl, serviceName); String tokenHeader = String.format("Bearer %s", apiToken); Settings fetchedSettings = delegate.fetchSettings(settingsUrl, tokenHeader); logger.debug( - String.format( - "Got settings from http: %s, serviceName: %s, hostname: %s", - fetchedSettings, serviceName, hostname)); + String.format("Got settings from http: %s, serviceName: %s", fetchedSettings, serviceName)); return fetchedSettings; } diff --git a/custom/src/test/java/com/solarwinds/opentelemetry/extensions/config/HttpSettingsReaderTest.java b/custom/src/test/java/com/solarwinds/opentelemetry/extensions/config/HttpSettingsReaderTest.java index 4ef0150c..126f98df 100644 --- a/custom/src/test/java/com/solarwinds/opentelemetry/extensions/config/HttpSettingsReaderTest.java +++ b/custom/src/test/java/com/solarwinds/opentelemetry/extensions/config/HttpSettingsReaderTest.java @@ -19,8 +19,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.mockStatic; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -28,15 +26,11 @@ import com.solarwinds.joboe.config.ConfigProperty; import com.solarwinds.joboe.config.InvalidConfigException; import com.solarwinds.joboe.sampling.Settings; -import com.solarwinds.opentelemetry.extensions.ResourceCustomizer; -import io.opentelemetry.sdk.resources.Resource; -import io.opentelemetry.semconv.incubating.HostIncubatingAttributes; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.MockedStatic; import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) @@ -57,184 +51,124 @@ void setUp() { void testGetSettings_UrlWithHttps() throws InvalidConfigException { String collectorUrl = "https://apm.collector.na-01.cloud.solarwinds.com"; String serviceKey = "test-api-key:test-service"; - String hostname = "test-hostname"; String expectedUrl = - "https://apm.collector.na-01.cloud.solarwinds.com/v1/settings/test-service/test-hostname"; + "https://apm.collector.na-01.cloud.solarwinds.com/v1/settings/test-service"; String expectedTokenHeader = "Bearer test-api-key"; ConfigManager.setConfig(ConfigProperty.AGENT_COLLECTOR, collectorUrl); - ConfigManager.setConfig(ConfigProperty.AGENT_SERVICE_KEY, serviceKey); - try (MockedStatic resourceCustomizerMock = - mockStatic(ResourceCustomizer.class)) { - - Resource mockResource = mock(Resource.class); - resourceCustomizerMock.when(ResourceCustomizer::getResource).thenReturn(mockResource); - when(mockResource.getAttribute(HostIncubatingAttributes.HOST_NAME)).thenReturn(hostname); - when(delegate.fetchSettings(expectedUrl, expectedTokenHeader)).thenReturn(mockSettings); - Settings result = tested.getSettings(); + when(delegate.fetchSettings(expectedUrl, expectedTokenHeader)).thenReturn(mockSettings); + Settings result = tested.getSettings(); - assertNotNull(result); - assertEquals(mockSettings, result); - verify(delegate).fetchSettings(expectedUrl, expectedTokenHeader); - } + assertNotNull(result); + assertEquals(mockSettings, result); + verify(delegate).fetchSettings(expectedUrl, expectedTokenHeader); } @Test void testGetSettings_UrlWithoutHttps() throws InvalidConfigException { String collectorUrl = "apm.collector.na-01.cloud.solarwinds.com"; String serviceKey = "test-api-key:test-service"; - String hostname = "test-hostname"; String expectedUrl = - "https://apm.collector.na-01.cloud.solarwinds.com/v1/settings/test-service/test-hostname"; + "https://apm.collector.na-01.cloud.solarwinds.com/v1/settings/test-service"; String expectedTokenHeader = "Bearer test-api-key"; ConfigManager.setConfig(ConfigProperty.AGENT_COLLECTOR, collectorUrl); - ConfigManager.setConfig(ConfigProperty.AGENT_SERVICE_KEY, serviceKey); - try (MockedStatic resourceCustomizerMock = - mockStatic(ResourceCustomizer.class)) { - Resource mockResource = mock(Resource.class); - resourceCustomizerMock.when(ResourceCustomizer::getResource).thenReturn(mockResource); - when(mockResource.getAttribute(HostIncubatingAttributes.HOST_NAME)).thenReturn(hostname); - - when(delegate.fetchSettings(expectedUrl, expectedTokenHeader)).thenReturn(mockSettings); - Settings result = tested.getSettings(); - - assertNotNull(result); - assertEquals(mockSettings, result); - verify(delegate).fetchSettings(expectedUrl, expectedTokenHeader); - } + + when(delegate.fetchSettings(expectedUrl, expectedTokenHeader)).thenReturn(mockSettings); + Settings result = tested.getSettings(); + + assertNotNull(result); + assertEquals(mockSettings, result); + verify(delegate).fetchSettings(expectedUrl, expectedTokenHeader); } @Test void testGetSettings_UrlWithHttp() throws InvalidConfigException { String collectorUrl = "http://apm.collector.na-01.cloud.solarwinds.com"; String serviceKey = "test-api-key:test-service"; - String hostname = "test-hostname"; String expectedUrl = - "https://apm.collector.na-01.cloud.solarwinds.com/v1/settings/test-service/test-hostname"; + "https://apm.collector.na-01.cloud.solarwinds.com/v1/settings/test-service"; String expectedTokenHeader = "Bearer test-api-key"; ConfigManager.setConfig(ConfigProperty.AGENT_COLLECTOR, collectorUrl); - ConfigManager.setConfig(ConfigProperty.AGENT_SERVICE_KEY, serviceKey); - try (MockedStatic resourceCustomizerMock = - mockStatic(ResourceCustomizer.class)) { - - Resource mockResource = mock(Resource.class); - resourceCustomizerMock.when(ResourceCustomizer::getResource).thenReturn(mockResource); - when(mockResource.getAttribute(HostIncubatingAttributes.HOST_NAME)).thenReturn(hostname); - when(delegate.fetchSettings(expectedUrl, expectedTokenHeader)).thenReturn(mockSettings); - Settings result = tested.getSettings(); + when(delegate.fetchSettings(expectedUrl, expectedTokenHeader)).thenReturn(mockSettings); + Settings result = tested.getSettings(); - assertNotNull(result); - assertEquals(mockSettings, result); - verify(delegate).fetchSettings(expectedUrl, expectedTokenHeader); - } + assertNotNull(result); + assertEquals(mockSettings, result); + verify(delegate).fetchSettings(expectedUrl, expectedTokenHeader); } @Test void testGetSettings_WithDefaultValues() { - String hostname = "test-hostname"; String expectedUrl = - "https://apm.collector.na-01.cloud.solarwinds.com/v1/settings/unknown-java/test-hostname"; + "https://apm.collector.na-01.cloud.solarwinds.com/v1/settings/unknown-java"; String expectedTokenHeader = "Bearer "; - try (MockedStatic resourceCustomizerMock = - mockStatic(ResourceCustomizer.class)) { + when(delegate.fetchSettings(expectedUrl, expectedTokenHeader)).thenReturn(mockSettings); + Settings result = tested.getSettings(); - Resource mockResource = mock(Resource.class); - resourceCustomizerMock.when(ResourceCustomizer::getResource).thenReturn(mockResource); - when(mockResource.getAttribute(HostIncubatingAttributes.HOST_NAME)).thenReturn(hostname); - - when(delegate.fetchSettings(expectedUrl, expectedTokenHeader)).thenReturn(mockSettings); - Settings result = tested.getSettings(); - - assertNotNull(result); - assertEquals(mockSettings, result); - verify(delegate).fetchSettings(expectedUrl, expectedTokenHeader); - } + assertNotNull(result); + assertEquals(mockSettings, result); + verify(delegate).fetchSettings(expectedUrl, expectedTokenHeader); } @Test void testGetSettings_WithCustomServiceKey() throws InvalidConfigException { String collectorUrl = "custom.collector.com"; String serviceKey = "my-api-key:my-service"; - String hostname = "prod-server"; - String expectedUrl = "https://custom.collector.com/v1/settings/my-service/prod-server"; + String expectedUrl = "https://custom.collector.com/v1/settings/my-service"; String expectedTokenHeader = "Bearer my-api-key"; ConfigManager.setConfig(ConfigProperty.AGENT_COLLECTOR, collectorUrl); - ConfigManager.setConfig(ConfigProperty.AGENT_SERVICE_KEY, serviceKey); - try (MockedStatic resourceCustomizerMock = - mockStatic(ResourceCustomizer.class)) { - Resource mockResource = mock(Resource.class); - resourceCustomizerMock.when(ResourceCustomizer::getResource).thenReturn(mockResource); - when(mockResource.getAttribute(HostIncubatingAttributes.HOST_NAME)).thenReturn(hostname); + when(delegate.fetchSettings(expectedUrl, expectedTokenHeader)).thenReturn(mockSettings); + Settings result = tested.getSettings(); - when(delegate.fetchSettings(expectedUrl, expectedTokenHeader)).thenReturn(mockSettings); - Settings result = tested.getSettings(); - - assertNotNull(result); - assertEquals(mockSettings, result); - verify(delegate).fetchSettings(expectedUrl, expectedTokenHeader); - } + assertNotNull(result); + assertEquals(mockSettings, result); + verify(delegate).fetchSettings(expectedUrl, expectedTokenHeader); } @Test void testGetSettings_DelegateThrowsException() throws InvalidConfigException { String collectorUrl = "https://apm.collector.na-01.cloud.solarwinds.com"; String serviceKey = "test-api-key:test-service"; - String hostname = "test-hostname"; String expectedUrl = - "https://apm.collector.na-01.cloud.solarwinds.com/v1/settings/test-service/test-hostname"; + "https://apm.collector.na-01.cloud.solarwinds.com/v1/settings/test-service"; String expectedTokenHeader = "Bearer test-api-key"; ConfigManager.setConfig(ConfigProperty.AGENT_COLLECTOR, collectorUrl); ConfigManager.setConfig(ConfigProperty.AGENT_SERVICE_KEY, serviceKey); - try (MockedStatic resourceCustomizerMock = - mockStatic(ResourceCustomizer.class)) { - Resource mockResource = mock(Resource.class); - resourceCustomizerMock.when(ResourceCustomizer::getResource).thenReturn(mockResource); - when(mockResource.getAttribute(HostIncubatingAttributes.HOST_NAME)).thenReturn(hostname); - - when(delegate.fetchSettings(expectedUrl, expectedTokenHeader)) - .thenThrow(new RuntimeException("Network error")); + when(delegate.fetchSettings(expectedUrl, expectedTokenHeader)) + .thenThrow(new RuntimeException("Network error")); - assertThrows(RuntimeException.class, () -> tested.getSettings()); - verify(delegate).fetchSettings(expectedUrl, expectedTokenHeader); - } + assertThrows(RuntimeException.class, () -> tested.getSettings()); + verify(delegate).fetchSettings(expectedUrl, expectedTokenHeader); } @Test void testGetSettings_UrlWithPortAndPath() throws InvalidConfigException { String collectorUrl = "https://custom.collector.com:8080/api"; String serviceKey = "test-key:test-app"; - String hostname = "server1"; - String expectedUrl = "https://custom.collector.com:8080/api/v1/settings/test-app/server1"; + String expectedUrl = "https://custom.collector.com:8080/api/v1/settings/test-app"; String expectedTokenHeader = "Bearer test-key"; ConfigManager.setConfig(ConfigProperty.AGENT_COLLECTOR, collectorUrl); ConfigManager.setConfig(ConfigProperty.AGENT_SERVICE_KEY, serviceKey); - try (MockedStatic resourceCustomizerMock = - mockStatic(ResourceCustomizer.class)) { - Resource mockResource = mock(Resource.class); - resourceCustomizerMock.when(ResourceCustomizer::getResource).thenReturn(mockResource); - when(mockResource.getAttribute(HostIncubatingAttributes.HOST_NAME)).thenReturn(hostname); - - when(delegate.fetchSettings(expectedUrl, expectedTokenHeader)).thenReturn(mockSettings); - Settings result = tested.getSettings(); + when(delegate.fetchSettings(expectedUrl, expectedTokenHeader)).thenReturn(mockSettings); + Settings result = tested.getSettings(); - assertNotNull(result); - assertEquals(mockSettings, result); - verify(delegate).fetchSettings(expectedUrl, expectedTokenHeader); - } + assertNotNull(result); + assertEquals(mockSettings, result); + verify(delegate).fetchSettings(expectedUrl, expectedTokenHeader); } }