Skip to content

Commit

Permalink
add configuration update check
Browse files Browse the repository at this point in the history
  • Loading branch information
EddeCCC committed Aug 21, 2024
1 parent 5ac0dc1 commit 96fba5c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package rocks.inspectit.gepard.agent.configuration;

import java.io.IOException;
import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rocks.inspectit.gepard.agent.configuration.file.ConfigurationFileReader;
import rocks.inspectit.gepard.agent.configuration.file.ConfigurationFileWriter;
import rocks.inspectit.gepard.agent.internal.configuration.model.InspectitConfiguration;
import rocks.inspectit.gepard.agent.internal.configuration.observer.ConfigurationReceivedSubject;
import rocks.inspectit.gepard.agent.internal.configuration.util.ConfigurationMapper;

public class ConfigurationPersistence {
private static final Logger log = LoggerFactory.getLogger(ConfigurationPersistence.class);
Expand Down Expand Up @@ -57,6 +59,25 @@ public void loadLocalConfiguration() {
* @param configuration the new configuration
*/
public void processConfiguration(InspectitConfiguration configuration) {
configurationSubject.notifyObservers(configuration);
if (configurationIsSame(configuration)) log.info("Configuration has not changed");
else configurationSubject.notifyObservers(configuration);
}

// TODO Remove this method and check for changes in the configuration server
/**
* Temporary method to check, whether the configuration has changed.
*
* @return true, if the new configuration differs from the current one
*/
private boolean configurationIsSame(InspectitConfiguration configuration) {
InspectitConfiguration currentConfig = reader.readConfiguration();
try {
String current = ConfigurationMapper.toString(currentConfig);
String update = ConfigurationMapper.toString(configuration);
return current.equals(update);
} catch (IOException e) {
log.error("Could not compare configurations", e);
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static ConfigurationFileReader create() {
public InspectitConfiguration readConfiguration() {
try {
byte[] rawFileContent = fileAccessor.readFile(filePath);
String fileContent = new String(rawFileContent); // TODO Add encoding?
String fileContent = new String(rawFileContent);
return ConfigurationMapper.toObject(fileContent);
} catch (IOException e) {
log.error("Could not read local configuration", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public void completed(SimpleHttpResponse result) {
if (result.getCode() == 200) {
String body = result.getBodyText();
try {
// ToDo Only process, if changes were made
InspectitConfiguration configuration = ConfigurationMapper.toObject(body);
persistence.processConfiguration(configuration);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,13 @@ public void run() {
successful = pollConfiguration();
} catch (Exception e) {
log.error("Error while polling configuration", e);
return;
successful = false;
}

if (successful) {
log.info("Configuration was polled successfully");
} else if (isFirstAttempt) {
log.warn("Configuration polling failed - Trying to load local configuration...");
if (!successful && isFirstAttempt) {
log.info("Trying to load local configuration...");
persistence.loadLocalConfiguration();
} else log.error("Configuration polling failed");
}

isFirstAttempt = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ private static String getDefaultPersistenceFile() {
String suffix = "inspectit-gepard/last-http-config.json";
File agentFile = JavaagentFileHolder.getJavaagentFile();

if (Objects.nonNull(agentFile))
return JavaagentFileHolder.getJavaagentFile().getParent() + "/" + suffix;
if (Objects.nonNull(agentFile)) return agentFile.getParent() + "/" + suffix;
return suffix;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ public boolean sendStartNotification() {
else {
log.info("Sending start notification to configuration server with url: {}", serverBaseUrl);
successful = startNotifier.sendNotification(serverBaseUrl);

if (successful) log.info("Successfully notified configuration server about start");
else log.warn("Could not notify configuration server about start");
}
return successful;
}
Expand Down

0 comments on commit 96fba5c

Please sign in to comment.