Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

/**
* This file is part of Temporal package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Temporal\Samples\Interceptors\Interceptor;

use Psr\Log\LoggerInterface;
use Temporal\Interceptor\Trait\WorkflowInboundCallsInterceptorTrait;
use Temporal\Interceptor\WorkflowInbound\QueryInput;
use Temporal\Interceptor\WorkflowInbound\SignalInput;
use Temporal\Interceptor\WorkflowInbound\UpdateInput;
use Temporal\Interceptor\WorkflowInbound\WorkflowInput;
use Temporal\Interceptor\WorkflowInboundCallsInterceptor;

final class LoggerWorkflowInboundCallsInterceptor implements WorkflowInboundCallsInterceptor
{
use WorkflowInboundCallsInterceptorTrait;

public function __construct(
private readonly LoggerInterface $logger,
) {}

public function execute(WorkflowInput $input, callable $next): void
{
$input->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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
1 change: 1 addition & 0 deletions app/src/Interceptors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions app/src/Interceptors/worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Loading