mirror of https://github.com/dapr/java-sdk.git
Improving and Adding Logs for Workflow (#978)
* Initial push for improving/adding logs for workflow Signed-off-by: Ryan Lettieri <ryanLettieri@microsoft.com> * Cleaning up linter issues Signed-off-by: Ryan Lettieri <ryanLettieri@microsoft.com> * Altering log level for logging registered activities and workflows Signed-off-by: Ryan Lettieri <ryanLettieri@microsoft.com> * Adding in time format to workflow log statements Signed-off-by: Ryan Lettieri <ryanLettieri@microsoft.com> * Fixing month format for workflow logs Signed-off-by: Ryan Lettieri <ryanLettieri@microsoft.com> * Adding in logging test for workflow Signed-off-by: Ryan Lettieri <ryanLettieri@microsoft.com> * Removing log-level setting from workflow sdk Signed-off-by: Ryan Lettieri <ryanLettieri@microsoft.com> * Removing time format logging from workflow sdk Signed-off-by: Ryan Lettieri <ryanLettieri@microsoft.com> * Fixing workflow logging test Signed-off-by: Ryan Lettieri <ryanLettieri@microsoft.com> * Update WorkflowRuntimeBuilderTest.java Signed-off-by: Artur Souza <asouza.pro@gmail.com> --------- Signed-off-by: Ryan Lettieri <ryanLettieri@microsoft.com> Signed-off-by: Artur Souza <asouza.pro@gmail.com> Co-authored-by: Artur Souza <asouza.pro@gmail.com>
This commit is contained in:
parent
40617f1c62
commit
81a3058223
|
@ -19,14 +19,26 @@ import io.dapr.workflows.Workflow;
|
||||||
import io.dapr.workflows.internal.ApiTokenClientInterceptor;
|
import io.dapr.workflows.internal.ApiTokenClientInterceptor;
|
||||||
import io.grpc.ClientInterceptor;
|
import io.grpc.ClientInterceptor;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public class WorkflowRuntimeBuilder {
|
public class WorkflowRuntimeBuilder {
|
||||||
private static volatile WorkflowRuntime instance;
|
private static volatile WorkflowRuntime instance;
|
||||||
private DurableTaskGrpcWorkerBuilder builder;
|
private DurableTaskGrpcWorkerBuilder builder;
|
||||||
|
private Logger logger;
|
||||||
|
private Set<String> workflows = new HashSet<String>();
|
||||||
|
private Set<String> activities = new HashSet<String>();
|
||||||
private static ClientInterceptor WORKFLOW_INTERCEPTOR = new ApiTokenClientInterceptor();
|
private static ClientInterceptor WORKFLOW_INTERCEPTOR = new ApiTokenClientInterceptor();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs the WorkflowRuntimeBuilder.
|
||||||
|
*/
|
||||||
public WorkflowRuntimeBuilder() {
|
public WorkflowRuntimeBuilder() {
|
||||||
this.builder = new DurableTaskGrpcWorkerBuilder().grpcChannel(
|
this.builder = new DurableTaskGrpcWorkerBuilder().grpcChannel(
|
||||||
NetworkUtils.buildGrpcManagedChannel(WORKFLOW_INTERCEPTOR));
|
NetworkUtils.buildGrpcManagedChannel(WORKFLOW_INTERCEPTOR));
|
||||||
|
this.logger = Logger.getLogger(WorkflowRuntimeBuilder.class.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -42,6 +54,7 @@ public class WorkflowRuntimeBuilder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.logger.log(Level.INFO, "Successfully built dapr workflow runtime");
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,7 +69,8 @@ public class WorkflowRuntimeBuilder {
|
||||||
this.builder = this.builder.addOrchestration(
|
this.builder = this.builder.addOrchestration(
|
||||||
new OrchestratorWrapper<>(clazz)
|
new OrchestratorWrapper<>(clazz)
|
||||||
);
|
);
|
||||||
|
this.logger.log(Level.INFO, "Registered Workflow: " + clazz.getSimpleName());
|
||||||
|
this.workflows.add(clazz.getSimpleName());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,5 +84,8 @@ public class WorkflowRuntimeBuilder {
|
||||||
this.builder = this.builder.addActivity(
|
this.builder = this.builder.addActivity(
|
||||||
new ActivityWrapper<>(clazz)
|
new ActivityWrapper<>(clazz)
|
||||||
);
|
);
|
||||||
|
this.logger.log(Level.INFO, "Registered Activity: " + clazz.getSimpleName());
|
||||||
|
this.activities.add(clazz.getSimpleName());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
}
|
|
@ -5,8 +5,15 @@ import io.dapr.workflows.Workflow;
|
||||||
import io.dapr.workflows.WorkflowStub;
|
import io.dapr.workflows.WorkflowStub;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNotEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.PrintStream;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
import java.util.logging.Handler;
|
||||||
|
import java.util.logging.LogRecord;
|
||||||
|
|
||||||
public class WorkflowRuntimeBuilderTest {
|
public class WorkflowRuntimeBuilderTest {
|
||||||
public static class TestWorkflow extends Workflow {
|
public static class TestWorkflow extends Workflow {
|
||||||
@Override
|
@Override
|
||||||
|
@ -37,4 +44,43 @@ public class WorkflowRuntimeBuilderTest {
|
||||||
public void buildTest() {
|
public void buildTest() {
|
||||||
assertDoesNotThrow(() -> new WorkflowRuntimeBuilder().build());
|
assertDoesNotThrow(() -> new WorkflowRuntimeBuilder().build());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void loggingOutputTest() {
|
||||||
|
// Set the output stream for log capturing
|
||||||
|
ByteArrayOutputStream outStreamCapture = new ByteArrayOutputStream();
|
||||||
|
System.setOut(new PrintStream(outStreamCapture));
|
||||||
|
|
||||||
|
LogCaptureHandler testLoggerHandler = new LogCaptureHandler();
|
||||||
|
Logger testLogger = Logger.getLogger(WorkflowRuntimeBuilder.class.getName());
|
||||||
|
|
||||||
|
testLogger.addHandler(testLoggerHandler);
|
||||||
|
|
||||||
|
// indexOf will return -1 if the string is not found.
|
||||||
|
assertDoesNotThrow(() -> new WorkflowRuntimeBuilder().registerWorkflow(TestWorkflow.class));
|
||||||
|
assertNotEquals(-1, testLoggerHandler.capturedLog.indexOf("Registered Workflow: TestWorkflow"));
|
||||||
|
assertDoesNotThrow(() -> new WorkflowRuntimeBuilder().registerActivity(TestActivity.class));
|
||||||
|
assertNotEquals(-1, testLoggerHandler.capturedLog.indexOf("Registered Activity: TestActivity"));
|
||||||
|
|
||||||
|
WorkflowRuntimeBuilder wfRuntime = new WorkflowRuntimeBuilder();
|
||||||
|
|
||||||
|
wfRuntime.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class LogCaptureHandler extends Handler {
|
||||||
|
private StringBuilder capturedLog = new StringBuilder();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void publish(LogRecord record) {
|
||||||
|
capturedLog.append(record.getMessage()).append(System.lineSeparator());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void flush(){
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws SecurityException {
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue