Skip to content

Commit

Permalink
NH-84912: remove layer handling logic and assume single settings in l…
Browse files Browse the repository at this point in the history
…ist.
  • Loading branch information
cleverchuk committed Jul 24, 2024
1 parent 7f7e9a3 commit 5d7efd1
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 61 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ subprojects {
opentelemetryJavaagent: "2.5.0",
bytebuddy : "1.12.10",
guava : "30.1-jre",
joboe : "10.0.6",
agent : "2.5.0", // the custom distro agent version
joboe : "10.0.8",
agent : "2.5.1", // the custom distro agent version
autoservice : "1.0.1",
caffeine : "2.9.3",
json : "20231013",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import com.solarwinds.joboe.sampling.Settings;
import com.solarwinds.joboe.sampling.SettingsFetcher;
import com.solarwinds.joboe.sampling.SettingsListener;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;

public class AwsLambdaSettingsFetcher implements SettingsFetcher {
Expand All @@ -45,26 +43,7 @@ public Settings getSettings() {
if (settings == null
|| System.currentTimeMillis() - settings.getTimestamp() > settings.getTtl() * 1000) {
try {
Map<String, Settings> allSettings = settingsReader.getSettings();
if (allSettings.isEmpty()) {
logger.warn("No Settings returned by the Settings Reader!");

} else {
settings = allSettings.get(DEFAULT_LAYER);
if (settings == null) {
logger.debug("Cannot find Settings with empty ID. Trying to locate the default record");
Optional<Settings> defaultSetting =
allSettings.values().stream().filter(Settings::isDefault).findFirst();

if (defaultSetting.isPresent()) {
settings = defaultSetting.get();
} else {
logger.warn(
"Cannot find Settings with empty ID nor a default record... using the first record");
settings = allSettings.values().iterator().next();
}
}
}
settings = settingsReader.getSettings();

} catch (SamplingException e) {
logger.debug(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class FileSettingsReader {
Expand All @@ -43,23 +42,25 @@ public FileSettingsReader(String settingsFilePath) {
this.settingsFilePath = settingsFilePath;
}

public Map<String, Settings> getSettings() throws SamplingException {
public Settings getSettings() throws SamplingException {
Settings settings = null;
try {
byte[] bytes = Files.readAllBytes(Paths.get(settingsFilePath));
Map<String, Settings> kvSetting =
convertToSettingsMap(gson.fromJson(new String(bytes), type));
List<Settings> kvSetting = convertToSettingsMap(gson.fromJson(new String(bytes), type));
logger.debug(String.format("Got settings from file: %s", kvSetting));

return kvSetting;
if (!kvSetting.isEmpty()) {
settings = kvSetting.get(0);
}

} catch (IOException e) {
logger.debug(String.format("Failed to read settings from file, error: %s", e));
throw new SamplingException("Error reading settings from file");
}
return settings;
}

private Map<String, Settings> convertToSettingsMap(List<JsonSettings> jsonSettings) {
return jsonSettings.stream()
.collect(Collectors.toMap(JsonSettings::getLayer, FileSettings::new));
private List<Settings> convertToSettingsMap(List<JsonSettings> jsonSettings) {
return jsonSettings.stream().map(FileSettings::new).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import com.solarwinds.joboe.sampling.SamplingException;
import com.solarwinds.joboe.sampling.Settings;
import com.solarwinds.joboe.sampling.SettingsListener;
import java.util.Collections;
import java.util.HashMap;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
Expand All @@ -45,13 +43,7 @@ class AwsLambdaSettingsFetcherTest {

@Test
void returnSettingsWhenNoSettings() throws SamplingException {
when(fileSettingsReaderMock.getSettings())
.thenReturn(
new HashMap<>() {
{
put("", settingsMock);
}
});
when(fileSettingsReaderMock.getSettings()).thenReturn(settingsMock);

tested.getSettings();

Expand All @@ -60,13 +52,7 @@ void returnSettingsWhenNoSettings() throws SamplingException {

@Test
void returnSettingsWhenSettingsExpired() throws SamplingException {
when(fileSettingsReaderMock.getSettings())
.thenReturn(
new HashMap<>() {
{
put("", settingsMock);
}
});
when(fileSettingsReaderMock.getSettings()).thenReturn(settingsMock);

when(settingsMock.getTimestamp()).thenReturn(0L);
when(settingsMock.getTtl()).thenReturn(0L);
Expand All @@ -79,13 +65,7 @@ void returnSettingsWhenSettingsExpired() throws SamplingException {

@Test
void callListenerOnSettingsRetrieved() throws SamplingException {
when(fileSettingsReaderMock.getSettings())
.thenReturn(
new HashMap<>() {
{
put("", settingsMock);
}
});
when(fileSettingsReaderMock.getSettings()).thenReturn(settingsMock);

tested.registerListener(settingsListenerMock);
tested.getSettings();
Expand All @@ -95,7 +75,7 @@ void callListenerOnSettingsRetrieved() throws SamplingException {

@Test
void returnNullWhenNoSettings() throws SamplingException {
when(fileSettingsReaderMock.getSettings()).thenReturn(Collections.emptyMap());
when(fileSettingsReaderMock.getSettings()).thenReturn(null);

tested.getSettings();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,20 @@

package com.solarwinds.opentelemetry.extensions;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import com.solarwinds.joboe.sampling.SamplingException;
import com.solarwinds.joboe.sampling.Settings;
import java.util.Map;
import org.junit.jupiter.api.Test;

class FileSettingsReaderTest {
private FileSettingsReader tested =
private final FileSettingsReader tested =
new FileSettingsReader(
FileSettingsReader.class.getResource("/solarwinds-apm-settings.json").getPath());

@Test
void returnSettings() throws SamplingException {
Map<String, Settings> settings = tested.getSettings();
assertEquals(1, settings.size());
Settings settings = tested.getSettings();
assertNotNull(settings);
}
}
Binary file renamed libs/config-10.0.6.jar → libs/config-10.0.8.jar
Binary file not shown.
Binary file renamed libs/core-10.0.6.jar → libs/core-10.0.8.jar
Binary file not shown.
Binary file renamed libs/logging-10.0.6.jar → libs/logging-10.0.8.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 5d7efd1

Please sign in to comment.