Skip to content

Commit

Permalink
change log levels for packages
Browse files Browse the repository at this point in the history
  • Loading branch information
V Udayani authored and V Udayani committed Nov 3, 2023
1 parent eddb6eb commit adb8b42
Show file tree
Hide file tree
Showing 17 changed files with 225 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.springframework.ide.vscode.commons.protocol;

public class LiveProcessLoggersSummary {

private String processType;
private String processKey;
private String processName;
private String processID;
private String packageName;
private String effectiveLevel;
private String configuredLevel;


public String getProcessType() {
return processType;
}

public void setProcessType(String processType) {
this.processType = processType;
}

public String getProcessKey() {
return processKey;
}

public void setProcessKey(String processKey) {
this.processKey = processKey;
}


public String getProcessName() {
return processName;
}

public void setProcessName(String processName) {
this.processName = processName;
}

public String getProcessID() {
return processID;
}

public void setProcessID(String processID) {
this.processID = processID;
}

public String getPackageName() {
return packageName;
}

public void setPackageName(String packageName) {
this.packageName = packageName;
}

public String getEffectiveLevel() {
return effectiveLevel;
}

public void setEffectiveLevel(String effectiveLevel) {
this.effectiveLevel = effectiveLevel;
}

public String getConfiguredLevel() {
return configuredLevel;
}

public void setConfiguredLevel(String configuredLevel) {
this.configuredLevel = configuredLevel;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public interface STS4LanguageClient extends LanguageClient, SpringIndexLanguageC
void liveProcessLoggersDataUpdated(LiveProcessSummary processKey);

@JsonNotification("sts/liveprocess/loglevel/updated")
void liveProcessLogLevelUpdated(LiveProcessSummary processKey);
void liveProcessLogLevelUpdated(LiveProcessLoggersSummary processKey);

@JsonNotification("sts/highlight")
void highlight(HighlightParams highlights);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer;
import org.springframework.ide.vscode.commons.protocol.CursorMovement;
import org.springframework.ide.vscode.commons.protocol.HighlightParams;
import org.springframework.ide.vscode.commons.protocol.LiveProcessLoggersSummary;
import org.springframework.ide.vscode.commons.protocol.LiveProcessSummary;
import org.springframework.ide.vscode.commons.protocol.STS4LanguageClient;
import org.springframework.ide.vscode.commons.protocol.java.ClasspathListenerParams;
Expand Down Expand Up @@ -441,6 +442,10 @@ public void indexUpdated() {
receiveIndexUpdated();
}

@Override
public void liveProcessLogLevelUpdated(LiveProcessLoggersSummary processKey) {
}

});

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ public interface ActuatorConnection {
String getLiveMetrics(String metricName, String tags) throws IOException;

String getLoggers() throws IOException;

String configureLogLevel(Map<String, String> args) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,15 @@ public String getLoggers() throws IOException {
}

@Override
public String changeLogLevel(Map<String, String> args) throws IOException {
public String configureLogLevel(Map<String, String> args) throws IOException {
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromPath("/loggers/"+args.get("packageName"));
if (args != null) {
// for (Entry<String, String> e : args.entrySet()) {
uriBuilder.queryParam("configuredLevel", args.get("configuredLevel"));
// }
}
// /{ownerId}/new cases make REST Template URI template handler to think that {ownerId} is a URI parameter which it is not.
String url = actuatorUrl + uriBuilder.toUriString();
return restTemplate.getForObject(URI.create(url), String.class);
return restTemplate.postForObject(URI.create(url), null, String.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class SpringProcessCommandHandler {
private static final String COMMAND_GET_REFRESH_METRICS = "sts/livedata/refresh/metrics";
private static final String COMMAND_GET_LOGGERS = "sts/livedata/getLoggers";
private static final String COMMAND_FETCH_LOGGERS_DATA = "sts/livedata/fetch/loggersData";
private static final String COMMAND_CHANGE_LOGLEVEL = "sts/livedata/change/logLevel";
private static final String COMMAND_CONFIGURE_LOGLEVEL = "sts/livedata/configure/logLevel";

private final SpringProcessConnectorService connectorService;
private final SpringProcessConnectorLocal localProcessConnector;
Expand Down Expand Up @@ -103,10 +103,10 @@ public SpringProcessCommandHandler(SimpleLanguageServer server, SpringProcessCon
});
log.info("Registered command handler: {}",COMMAND_FETCH_LOGGERS_DATA);

server.onCommand(COMMAND_CHANGE_LOGLEVEL, (params) -> {
return changeLogLevel(params);
server.onCommand(COMMAND_CONFIGURE_LOGLEVEL, (params) -> {
return configureLogLevel(params);
});
log.info("Registered command handler: {}",COMMAND_CHANGE_LOGLEVEL);
log.info("Registered command handler: {}",COMMAND_CONFIGURE_LOGLEVEL);

server.onCommand(COMMAND_LIST_CONNECTED, (params) -> {
List<LiveProcessSummary> result = new ArrayList<>();
Expand Down Expand Up @@ -361,17 +361,18 @@ private CompletableFuture<Object> handleFetchLoggersDataRequest(ExecuteCommandPa
return CompletableFuture.completedFuture(null);
}

private CompletableFuture<Object> changeLogLevel(ExecuteCommandParams params) {
private CompletableFuture<Object> configureLogLevel(ExecuteCommandParams params) {
Map<String, String> args = new HashMap<>();
args.put("packageName", getArgumentByKey(params, "packageName"));
args.put("configuredLevel", getArgumentByKey(params, "configuredLevel"));
args.put("effectiveLevel", getArgumentByKey(params, "effectiveLevel"));
SpringProcessParams springProcessParams = new SpringProcessParams();
springProcessParams.setProcessKey(getProcessKey(params));
springProcessParams.setEndpoint("loggers");
springProcessParams.setEndpoint(getArgumentByKey(params, "endpoint"));
springProcessParams.setArgs(args);

if (springProcessParams.getProcessKey() != null) {
connectorService.changeLogLevel(springProcessParams);
connectorService.configureLogLevel(springProcessParams);
}

return CompletableFuture.completedFuture(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ public interface SpringProcessConnector {
SpringProcessGcPausesMetricsLiveData refreshGcPausesMetrics(SpringProcessLiveData current, String metricName, String tags) throws Exception;
SpringProcessMemoryMetricsLiveData refreshMemoryMetrics(SpringProcessLiveData current, String metricName, String tags) throws Exception;
SpringProcessLoggersData getLoggers(SpringProcessLiveData currentData) throws Exception;
void changeLogLevel(SpringProcessLiveData currentData, Map<String, String> args) throws Exception;
SpringProcessUpdatedLogLevelData configureLogLevel(SpringProcessLiveData currentData, Map<String, String> args) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,21 +185,22 @@ public SpringProcessLoggersData getLoggers(SpringProcessLiveData currentData)


@Override
public void changeLogLevel(SpringProcessLiveData currentData, Map<String, String> args) throws Exception {
public SpringProcessUpdatedLogLevelData configureLogLevel(SpringProcessLiveData currentData, Map<String, String> args) throws Exception {
if (actuatorConnection != null) {
SpringProcessLoggersData loggersData = new SpringProcessLiveDataExtractorOverHttp().changeLogLevel(getProcessType(), actuatorConnection, processID, processName, currentData, args);
SpringProcessUpdatedLogLevelData springProcessUpdatedLoggersData = new SpringProcessLiveDataExtractorOverHttp().configureLogLevel(getProcessType(), actuatorConnection, processID, processName, currentData, args);

if (this.processID == null) {
this.processID = loggersData.getProcessID();
this.processID = springProcessUpdatedLoggersData.getProcessID();
}

if (this.processName == null) {
this.processName = loggersData.getProcessName();
this.processName = springProcessUpdatedLoggersData.getProcessName();
return springProcessUpdatedLoggersData;
}

}

throw new Exception("no loggers data received, lets try again");
throw new Exception("configure log levels failed, lets try again");
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,26 +235,27 @@ public SpringProcessLoggersData getLoggers(SpringProcessLiveData currentData) th
}

@Override
public void changeLogLevel(SpringProcessLiveData currentData, Map<String, String> args) throws Exception {
public SpringProcessUpdatedLogLevelData configureLogLevel(SpringProcessLiveData currentData, Map<String, String> args) throws Exception {
log.info("try to open JMX connection to: " + jmxURL);

if (jmxConnection != null) {
try {
SpringProcessLiveDataExtractorOverJMX springJMXConnector = new SpringProcessLiveDataExtractorOverJMX();

log.info("retrieve live data from: " + jmxURL);
springJMXConnector.changeLogLevel(getProcessType(), jmxConnection, processID, processName, currentData, args);
SpringProcessUpdatedLogLevelData springProcessUpdatedLoggersData = springJMXConnector.configureLogLevel(getProcessType(), jmxConnection, processID, processName, currentData, args);

// if (loggersData != null) {
// return loggersData;
// }
return springProcessUpdatedLoggersData;
}
catch (Exception e) {
log.error("exception while connecting to jmx: " + jmxURL, e);
}
}

throw new Exception("no loggers data received, lets try again");
throw new Exception("configure log level failed, lets try again");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,36 +362,36 @@ public void getLoggersData(IndefiniteProgressTask progressTask, SpringProcessPar

}

public void changeLogLevel(SpringProcessParams springProcessParams) {
public void configureLogLevel(SpringProcessParams springProcessParams) {
log.info("change log level: " + springProcessParams.getProcessKey());

SpringProcessConnector connector = this.connectors.get(springProcessParams.getProcessKey());
if (connector != null) {
final IndefiniteProgressTask progressTask = getProgressTask(
"spring-process-connector-service-change-log-level" + springProcessParams.getProcessKey(), "Loggers", null);
"spring-process-connector-service-configure-log-level" + springProcessParams.getProcessKey(), "Loggers", null);
System.out.println(progressTask);
changeLogLevel(progressTask, springProcessParams, connector, 0, TimeUnit.SECONDS, 0);
configureLogLevel(progressTask, springProcessParams, connector, 0, TimeUnit.SECONDS, 0);
}
}

private void changeLogLevel(IndefiniteProgressTask progressTask, SpringProcessParams springProcessParams,
private void configureLogLevel(IndefiniteProgressTask progressTask, SpringProcessParams springProcessParams,
SpringProcessConnector connector, long delay, TimeUnit unit, int retryNo) {
String processKey = springProcessParams.getProcessKey();
String endpoint = springProcessParams.getEndpoint();

String progressMessage = "change log level for Spring process: " + processKey + " - retry no: " + retryNo;
String progressMessage = "configure log level for Spring process: " + processKey + " - retry no: " + retryNo;
log.info(progressMessage);

this.scheduler.schedule(() -> {

try {
progressTask.progressEvent(progressMessage);
if(LOGGERS.equals(endpoint)) {
connector.changeLogLevel(this.liveDataProvider.getCurrent(processKey), springProcessParams.getArgs());
SpringProcessUpdatedLogLevelData springProcessUpdatedLoggersData = connector.configureLogLevel(this.liveDataProvider.getCurrent(processKey), springProcessParams.getArgs());

// if (loggersData != null) {
// if (!this.liveDataProvider.addLoggers(processKey, loggersData)) {
this.liveDataProvider.updateLoggers(processKey, null);
this.liveDataProvider.updateLogLevel(processKey, springProcessUpdatedLoggersData);
// }

this.connectedSuccess.put(processKey, true);
Expand All @@ -405,7 +405,7 @@ private void changeLogLevel(IndefiniteProgressTask progressTask, SpringProcessPa
log.info("problem occured during process live data refresh", e);

if (retryNo < maxRetryCount && isKnownProcessKey(processKey)) {
getLoggersData(progressTask, springProcessParams, connector, retryDelayInSeconds, TimeUnit.SECONDS,
configureLogLevel(progressTask, springProcessParams, connector, retryDelayInSeconds, TimeUnit.SECONDS,
retryNo + 1);
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public Loggers getLoggers(ActuatorConnection connection) {
return null;
}

public void changeLogLevel(ProcessType processType, ActuatorConnection connection, String processID, String processName,
public SpringProcessUpdatedLogLevelData configureLogLevel(ProcessType processType, ActuatorConnection connection, String processID, String processName,
SpringProcessLiveData currentData, Map<String, String> args) {

try {
Expand All @@ -346,31 +346,35 @@ public void changeLogLevel(ProcessType processType, ActuatorConnection connectio
}
}

Loggers loggers = changeLogLevel(connection);

configureLogLevel(connection, args);
return new SpringProcessUpdatedLogLevelData(
processType,
processName,
processID,
args.get("packageName"),
args.get("effectiveLevel"),
args.get("configuredLevel")
);

}
catch (Exception e) {
log.error("error reading live metrics data from: " + processID + " - " + processName, e);
log.error("error reading live metrics data from: " + processID + " - " + processName + " : "+ args.get("packageName"), e);
}
return null;

}

public Loggers changeLogLevel(ActuatorConnection connection) {
public void configureLogLevel(ActuatorConnection connection, Map<String, String> args) {
try {
String result = connection.getLoggers();
connection.configureLogLevel(args);

if (result instanceof String) {
return gson.fromJson((String)result, Loggers.class);
} else if(result != null){
ObjectMapper mapper = new ObjectMapper();
return mapper.convertValue(result, Loggers.class);
}
} catch (IOException e) {
// ignore
} catch (Exception e) {
log.error("Error parsing loggers", e);
log.error("Error parsing response", e);
throw e;
}
return null;
return;
}

private StartupMetricsModel getStartupMetrics(ActuatorConnection connection, StartupMetricsModel currentStartup) {
Expand Down
Loading

0 comments on commit adb8b42

Please sign in to comment.