Skip to content

Commit

Permalink
Move MDC to setOutputListener
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmeldrum committed Dec 7, 2023
1 parent 6c7b0fc commit e1ab208
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import com.solace.maas.ep.event.management.agent.plugin.jacoco.ExcludeFromJacocoGeneratedReport;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;

import java.io.BufferedReader;
import java.io.File;
Expand Down Expand Up @@ -64,13 +65,21 @@ final class ProcessLauncher {
}

void setOutputListener(Consumer<String> listener) {
Map<String, String> mdcContext = MDC.getCopyOfContextMap();
assert process == null;
outputListener = listener;
outputListener = (log) -> {
MDC.setContextMap(mdcContext);
listener.accept(log);
};
}

void setErrorListener(Consumer<String> listener) {
Map<String, String> mdcContext = MDC.getCopyOfContextMap();
assert process == null;
errorListener = listener;
errorListener = (error) -> {
MDC.setContextMap(mdcContext);
listener.accept(error);
};
}

void setInheritIO(boolean inheritIO) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,14 @@ public void execute(CommandRequest request, Command command, Map<String, String>

MDC.put(RouteConstants.COMMAND_CORRELATION_ID, request.getCorrelationId());
MDC.put(RouteConstants.MESSAGING_SERVICE_ID, request.getServiceId());
String traceId = MDC.get(RouteConstants.TRACE_ID);
String spanId = MDC.get("spanId");
String commandCorrelationId = MDC.get(RouteConstants.COMMAND_CORRELATION_ID);
String actorId = MDC.get(RouteConstants.ACTOR_ID);
String scheduleId = MDC.get(RouteConstants.SCHEDULE_ID);
String messagingServiceId = MDC.get(RouteConstants.MESSAGING_SERVICE_ID);

log.debug("Executing command {} for serviceId {} correlationId {} context {}", command.getCommand(), request.getServiceId(),
request.getCorrelationId(), request.getContext());

try (TerraformClient terraformClient = terraformClientFactory.createClient()) {

Path configPath = createConfigPath(request);
List<String> logOutput = setupTerraformClient(terraformClient, configPath, traceId, spanId, commandCorrelationId,
actorId, scheduleId, messagingServiceId);
List<String> logOutput = setupTerraformClient(terraformClient, configPath);
String commandVerb = executeTerraformCommand(command, envVars, configPath, terraformClient);
processTerraformResponse(request, command, commandVerb, logOutput);
} catch (InterruptedException e) {
Expand All @@ -71,21 +64,13 @@ public void execute(CommandRequest request, Command command, Map<String, String>
}
}

private static List<String> setupTerraformClient(TerraformClient terraformClient, Path configPath,
String traceId, String spanId, String commandCorrelationId, String actorId,
String scheduleId, String messagingServiceId) {
private static List<String> setupTerraformClient(TerraformClient terraformClient, Path configPath) {
terraformClient.setWorkingDirectory(configPath.toFile());
List<String> output = new ArrayList<>();

// Write each terraform to the output list so that it can be processed later
// Also write the output to the main log to be streamed back to EP
terraformClient.setOutputListener(tfLog -> {
MDC.put(RouteConstants.TRACE_ID, traceId);
MDC.put("spanId", spanId);
MDC.put(RouteConstants.COMMAND_CORRELATION_ID, commandCorrelationId);
MDC.put(RouteConstants.ACTOR_ID, actorId);
MDC.put(RouteConstants.SCHEDULE_ID, scheduleId);
MDC.put(RouteConstants.MESSAGING_SERVICE_ID, messagingServiceId);
output.add(tfLog);
log.debug("Terraform output: {}", tfLog);
});
Expand Down

0 comments on commit e1ab208

Please sign in to comment.