diff --git a/app/composer.json b/app/composer.json index f5cb987..7af5ef0 100644 --- a/app/composer.json +++ b/app/composer.json @@ -2,7 +2,7 @@ "minimum-stability": "dev", "prefer-stable": true, "require": { - "temporal/sdk": "^2.8.0", + "temporal/sdk": "^2.12.0", "spiral/tokenizer": "^3.7", "temporal/open-telemetry-interceptors": "dev-master", "open-telemetry/exporter-otlp": "^1.1", diff --git a/app/src/Interceptors/Interceptor/LoggerWorkflowInboundCallsInterceptor.php b/app/src/Interceptors/Interceptor/LoggerWorkflowInboundCallsInterceptor.php new file mode 100644 index 0000000..7d48d83 --- /dev/null +++ b/app/src/Interceptors/Interceptor/LoggerWorkflowInboundCallsInterceptor.php @@ -0,0 +1,99 @@ +info->isReplaying or $this->log( + "Executing workflow {$input->info->type->name}", + [ + 'workflowId' => $input->info->execution->getID(), + 'runId' => $input->info->execution->getRunID(), + 'headers' => $input->header, + 'arguments' => $input->arguments->getValues(), + ], + ); + + $next($input); + } + + public function handleSignal(SignalInput $input, callable $next): void + { + $input->info->isReplaying or $this->log( + "Handling signal {$input->info->type->name}->{$input->signalName}", + [ + 'workflowId' => $input->info->execution->getID(), + 'runId' => $input->info->execution->getRunID(), + 'headers' => $input->header, + 'arguments' => $input->arguments->getValues(), + ], + ); + + $next($input); + } + + public function handleQuery(QueryInput $input, callable $next): mixed + { + $this->log( + "Handling query {$input->info->type->name}->{$input->queryName}", + [ + 'workflowId' => $input->info->execution->getID(), + 'runId' => $input->info->execution->getRunID(), + 'arguments' => $input->arguments->getValues(), + ], + ); + + return $next($input); + } + + public function handleUpdate(UpdateInput $input, callable $next): mixed + { + $input->info->isReplaying or $this->log( + "Handling update {$input->info->type->name}->{$input->updateName}", + [ + 'workflowId' => $input->info->execution->getID(), + 'runId' => $input->info->execution->getRunID(), + 'updateId' => $input->updateId, + 'headers' => $input->header, + 'arguments' => $input->arguments->getValues(), + ], + ); + + return $next($input); + } + + public function validateUpdate(UpdateInput $input, callable $next): void + { + $next($input); + } + + private function log(string $message, array $context = []): void + { + $this->logger->info($message, $context); + } +} diff --git a/app/src/Interceptors/Interceptor/RequestLoggerInterceptor.php b/app/src/Interceptors/Interceptor/RequestLoggerInterceptor.php index e1fe42e..7b5e80e 100644 --- a/app/src/Interceptors/Interceptor/RequestLoggerInterceptor.php +++ b/app/src/Interceptors/Interceptor/RequestLoggerInterceptor.php @@ -19,9 +19,8 @@ final class RequestLoggerInterceptor implements WorkflowOutboundRequestInterceptor { public function __construct( - private LoggerInterface $logger, - ) { - } + private readonly LoggerInterface $logger, + ) {} public function handleOutboundRequest(RequestInterface $request, callable $next): PromiseInterface { diff --git a/app/src/Interceptors/README.md b/app/src/Interceptors/README.md index 47a4820..25296f3 100644 --- a/app/src/Interceptors/README.md +++ b/app/src/Interceptors/README.md @@ -4,6 +4,7 @@ This sample demonstrates how you can use Interceptors. There are few interceptors: - RequestLoggerInterceptor - logs all internal requests from the Workflow into the RoadRunner log. +- WorkflowInboundLogger - logs all inbound workflow calls to the RoadRunner log. - ActivityAttributesInterceptor - reads [ActivityOption](./Attribute/ActivityOption.php) attributes and applies them to the Activity options. ```bash diff --git a/app/src/Interceptors/worker.php b/app/src/Interceptors/worker.php index 15e3acc..f316c66 100644 --- a/app/src/Interceptors/worker.php +++ b/app/src/Interceptors/worker.php @@ -13,17 +13,21 @@ use Temporal\Samples\Interceptors\Activity\Sleeper; use Temporal\Samples\Interceptors\Interceptor\ActivityAttributesInterceptor; use Temporal\Samples\Interceptors\Interceptor\RequestLoggerInterceptor; +use Temporal\Samples\Interceptors\Interceptor\LoggerWorkflowInboundCallsInterceptor; use Temporal\Samples\Interceptors\Workflow\TestActivityAttributesInterceptor; +use Temporal\SampleUtils\Logger; use Temporal\WorkerFactory; ini_set('display_errors', 'stderr'); include "../../vendor/autoload.php"; $factory = WorkerFactory::create(); +$logger = new Logger(); $worker = $factory->newWorker(taskQueue: 'interceptors', interceptorProvider: new SimplePipelineProvider([ - new RequestLoggerInterceptor(new \Temporal\SampleUtils\Logger()), - new ActivityAttributesInterceptor() + new RequestLoggerInterceptor($logger), + new ActivityAttributesInterceptor(), + new LoggerWorkflowInboundCallsInterceptor($logger), ])) ->registerWorkflowTypes(TestActivityAttributesInterceptor::class) ->registerActivityImplementations(new Sleeper());