Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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));
Comment thread
cleverchuk marked this conversation as resolved.

return fetchedSettings;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,18 @@
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;

import com.solarwinds.joboe.config.ConfigManager;
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)
Expand All @@ -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<ResourceCustomizer> 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<ResourceCustomizer> 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<ResourceCustomizer> 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<ResourceCustomizer> 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<ResourceCustomizer> 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<ResourceCustomizer> 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<ResourceCustomizer> 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);
}
}
Loading