Skip to content

Commit

Permalink
Merge pull request #144 from SolaceProducts/moodiRealist/DATAGO-64298…
Browse files Browse the repository at this point in the history
…-remove-old-logs

DATAGO-64298: rotate command log files.
  • Loading branch information
moodiRealist authored Dec 14, 2023
2 parents 3d003c8 + eb55988 commit e686c6e
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ public class CommandLogMessage extends MOPMessage {

String commandCorrelationId;

String eventManagementAgentId;

String level;

String log;

Long timestamp;

public CommandLogMessage(String orgId, String commandCorrelationId, String traceId, String actorId, String level, String log, Long timestamp) {
public CommandLogMessage(String orgId, String commandCorrelationId, String traceId, String actorId,
String level, String log, Long timestamp, String runtimeAgentId) {
super();
withMessageType(MOPMessageType.generic)
.withProtocol(MOPProtocol.epConfigPush)
Expand All @@ -27,6 +30,7 @@ public CommandLogMessage(String orgId, String commandCorrelationId, String trace

this.orgId = orgId;
this.commandCorrelationId = commandCorrelationId;
eventManagementAgentId = runtimeAgentId;
this.level = level;
this.log = log;
this.timestamp = timestamp;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.solace.maas.ep.event.management.agent.logging;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.spi.FilterReply;
import com.solace.maas.ep.event.management.agent.plugin.constants.RouteConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

@Slf4j
public class CommandFilter extends Filter<ILoggingEvent> {

@Override
public FilterReply decide(ILoggingEvent event) {

String commandCorrelationId = event.getMDCPropertyMap().get(RouteConstants.COMMAND_CORRELATION_ID);

if (StringUtils.isNotEmpty(commandCorrelationId)) {
return FilterReply.ACCEPT;
}

return FilterReply.DENY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void process(Exchange exchange) throws Exception {
String messagingServiceId = (String) properties.get(RouteConstants.MESSAGING_SERVICE_ID);

CommandLogMessage logDataMessage = new CommandLogMessage(orgId, commandCorrelationId, traceId, actorId, event.getLevel().toString(),
String.format("%s%s", event.getFormattedMessage(), "\n"), event.getTimeStamp());
String.format("%s%s", event.getFormattedMessage(), "\n"), event.getTimeStamp(), runtimeAgentId);

topicDetails.put("orgId", orgId);
topicDetails.put("runtimeAgentId", runtimeAgentId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
COMMAND_PATH=${HOME}/commands
23 changes: 23 additions & 0 deletions service/application/src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<configuration>

<property name="LOG_LEVEL_PATTERN" value="%clr(%5p) %clr([%X{traceId:-}]){yellow}"/>
<property resource="command-configs.properties"/>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>

Expand Down Expand Up @@ -35,17 +36,39 @@
</appender>
<appender name="StreamingAppender" class="com.solace.maas.ep.event.management.agent.logging.StreamingAppender"/>


<appender name="RollingCommandsFile" class="ch.qos.logback.core.rolling.RollingFileAppender">

<filter class="com.solace.maas.ep.event.management.agent.logging.CommandFilter"/>

<file>${COMMAND_PATH}/logs/command-logs.log</file>

<encoder>
<pattern>[%X{COMMAND_CORRELATION_ID}] ------ %date [%level] [%thread] %logger{10} [%file:%line] %msg%n</pattern>
</encoder>

<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${COMMAND_PATH}/logs/command-logs-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>1MB</maxFileSize>
<maxHistory>30</maxHistory>
<totalSizeCap>10MB</totalSizeCap>
<cleanHistoryOnStart>false</cleanHistoryOnStart>
</rollingPolicy>
</appender>

<springProfile name="default,mysql,mysql-dev,DEV,TEST">
<root level="INFO">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="RollingFile"/>
<appender-ref ref="SiftLogger"/>
<appender-ref ref="RollingCommandsFile"/>
<appender-ref ref="StreamingAppender"/>
</root>
<logger name="com.solace.maas" level="DEBUG" additivity="false">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="RollingFile"/>
<appender-ref ref="SiftLogger"/>
<appender-ref ref="RollingCommandsFile"/>
<appender-ref ref="StreamingAppender"/>
</logger>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ void testCommandLogMessageMOPProtocol() {
"traceId",
"actorId",
"level",
"log", Instant.now().toEpochMilli());
"log", Instant.now().toEpochMilli(),
"runtimeAgentId");

assertThat(commandLogMessage.getMopProtocol()).isEqualTo(MOPProtocol.epConfigPush);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
@Service
@Data
public class TerraformProperties {
@Value("${plugins.terraform.workingDirectoryRoot:${HOME}/tfconfig}")
@Value("${COMMAND_PATH:${HOME}/tfconfig}")
private String workingDirectoryRoot;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.solace.maas.ep.event.management.agent.plugin.terraform.configuration;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:command-configs.properties")
public class TerraformPropertiesFromFile {
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
package com.solace.maas.ep.event.management.agent.plugin.terraform.manager;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.solace.maas.ep.event.management.agent.plugin.command.model.CommandRequest;
import com.solace.maas.ep.event.management.agent.plugin.command.model.CommandResult;
import com.solace.maas.ep.event.management.agent.plugin.command.model.JobStatus;
import com.solace.maas.ep.event.management.agent.plugin.terraform.configuration.TerraformProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;

Expand All @@ -33,32 +25,10 @@ public class TerraformLogProcessingService {
public static final String KEY_TIMESTAMP = "@timestamp";
public static final String KEY_DIAGNOSTIC_DETAIL = "diagnosticDetail";
public static final String KEY_DIAGNOSTIC = "diagnostic";
private final TerraformProperties terraformProperties;
private final ObjectMapper objectMapper;

public TerraformLogProcessingService(ObjectMapper objectMapper, TerraformProperties terraformProperties) {
public TerraformLogProcessingService(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
this.terraformProperties = terraformProperties;
}

public void saveLogToFile(CommandRequest request, List<String> logs) throws IOException {
Path logPath = Paths.get(terraformProperties.getWorkingDirectoryRoot()
+ File.separator
+ request.getContext()
+ "-"
+ request.getServiceId()
+ File.separator
+ "logs"
);

if (Files.notExists(logPath)) {
Files.createDirectories(logPath);
}

Path out = Files.createTempFile(logPath, System.currentTimeMillis() + "-"
+ request.getCommandCorrelationId() + "-job", ".log");

Files.write(out, logs, Charset.defaultCharset());
}

public CommandResult buildTfCommandResult(List<String> jsonLogs) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.solace.maas.ep.event.management.agent.plugin.terraform.manager;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.solace.maas.ep.event.management.agent.plugin.command.model.Command;
import com.solace.maas.ep.event.management.agent.plugin.command.model.CommandRequest;
import com.solace.maas.ep.event.management.agent.plugin.command.model.CommandResult;
Expand All @@ -8,6 +9,7 @@
import com.solace.maas.ep.event.management.agent.plugin.terraform.client.TerraformClient;
import com.solace.maas.ep.event.management.agent.plugin.terraform.client.TerraformClientFactory;
import com.solace.maas.ep.event.management.agent.plugin.terraform.configuration.TerraformProperties;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.slf4j.MDC;
Expand All @@ -29,10 +31,11 @@
@Slf4j
public class TerraformManager {
public static final String LOG_LEVEL_ERROR = "ERROR";
private static final ObjectMapper objectMapper = new ObjectMapper();
private final TerraformLogProcessingService terraformLogProcessingService;
private final TerraformProperties terraformProperties;
private final TerraformClientFactory terraformClientFactory;
private final static String TF_CONFIG_FILENAME = "config.tf";
private static final String TF_CONFIG_FILENAME = "config.tf";

public TerraformManager(TerraformLogProcessingService terraformLogProcessingService,
TerraformProperties terraformProperties, TerraformClientFactory terraformClientFactory) {
Expand All @@ -54,7 +57,7 @@ public void execute(CommandRequest request, Command command, Map<String, String>
Path configPath = createConfigPath(request);
List<String> logOutput = setupTerraformClient(terraformClient, configPath);
String commandVerb = executeTerraformCommand(command, envVars, configPath, terraformClient);
processTerraformResponse(request, command, commandVerb, logOutput);
processTerraformResponse(command, commandVerb, logOutput);
} catch (InterruptedException e) {
log.error("Received a thread interrupt while executing the terraform command", e);
Thread.currentThread().interrupt();
Expand All @@ -72,11 +75,28 @@ private static List<String> setupTerraformClient(TerraformClient terraformClient
// Also write the output to the main log to be streamed back to EP
terraformClient.setOutputListener(tfLog -> {
output.add(tfLog);
log.debug("Terraform output: {}", tfLog);
logToConsole(tfLog);
});
return output;
}

@SneakyThrows
private static void logToConsole(String tfLog) {

String logMessage = String.format("Terraform output: %s", tfLog);

Map<String, Object> logMop = objectMapper.readValue(tfLog, Map.class);
String logLevel = (String) logMop.get("@level");
switch (logLevel) {
case "trace" -> log.trace(logMessage);
case "debug" -> log.debug(logMessage);
case "info" -> log.info(logMessage);
case "warn" -> log.warn(logMessage);
case "error" -> log.error(logMessage);
default -> log.error("cannot map the logLevel properly for tfLog {}", tfLog);
}
}

private static String executeTerraformCommand(Command command, Map<String, String> envVars, Path configPath, TerraformClient terraformClient) throws IOException, InterruptedException, ExecutionException {
String commandVerb = command.getCommand();
switch (commandVerb) {
Expand All @@ -93,7 +113,7 @@ private static String executeTerraformCommand(Command command, Map<String, Strin
return commandVerb;
}

private void processTerraformResponse(CommandRequest request, Command command, String commandVerb, List<String> output) throws IOException {
private void processTerraformResponse(Command command, String commandVerb, List<String> output) {
// Process logs and create the result
if (Boolean.TRUE.equals(command.getIgnoreResult())) {
command.setResult(CommandResult.builder()
Expand All @@ -102,7 +122,6 @@ private void processTerraformResponse(CommandRequest request, Command command, S
.build());
} else {
if (!"write_HCL".equals(commandVerb)) {
terraformLogProcessingService.saveLogToFile(request, output);
command.setResult(terraformLogProcessingService.buildTfCommandResult(output));
} else {
command.setResult(CommandResult.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public TerraformProperties getTfProperties() {

@Bean
@Primary
public TerraformLogProcessingService getTfLogProcessingService(ObjectMapper objectMapper, TerraformProperties terraformProperties) {
return new TerraformLogProcessingService(objectMapper, terraformProperties);
public TerraformLogProcessingService getTfLogProcessingService(ObjectMapper objectMapper) {
return new TerraformLogProcessingService(objectMapper);
}

@Bean
Expand Down

0 comments on commit e686c6e

Please sign in to comment.