Observability - PHP SDK
The observability section of the Temporal Developer's guide covers the many ways to view the current state of your Temporal Application—that is, ways to view which Workflow Executions are tracked by the Temporal Platform and the state of any specified Workflow Execution, either currently or at points of an execution.
This section covers features related to viewing the state of the application, including:
How to use Visibility APIs
The term Visibility, within the Temporal Platform, refers to the subsystems and APIs that enable an operator to view Workflow Executions that currently exist within a Temporal Service.
How to use Search Attributes
The typical method of retrieving a Workflow Execution is by its Workflow Id.
However, sometimes you'll want to retrieve one or more Workflow Executions based on another property. For example, imagine you want to get all Workflow Executions of a certain type that have failed within a time range, so that you can start new ones with the same arguments.
You can do this with Search Attributes.
- Default Search Attributes like
WorkflowType
,StartTime
andExecutionStatus
are automatically added to Workflow Executions. - Custom Search Attributes can contain their own domain-specific data (like
customerId
ornumItems
).- A few generic Custom Search Attributes like
CustomKeywordField
andCustomIntField
are created by default in Temporal's Docker Compose.
- A few generic Custom Search Attributes like
The steps to using custom Search Attributes are:
- Create a new Search Attribute in your Temporal Service using
temporal operator search-attribute create
or the Cloud UI. - Set the value of the Search Attribute for a Workflow Execution:
- On the Client by including it as an option when starting the Execution.
- In the Workflow by calling
UpsertSearchAttributes
.
- Read the value of the Search Attribute:
- On the Client by calling
DescribeWorkflow
. - In the Workflow by looking at
WorkflowInfo
.
- On the Client by calling
- Query Workflow Executions by the Search Attribute using a List Filter:
- In the Temporal CLI.
- In code by calling
ListWorkflowExecutions
.
Here is how to query Workflow Executions:
Use the listWorkflowExecutions() method on the Client and pass a List Filter as an argument to filter the listed Workflows.
The result is an iterable paginator, so you can use the foreach
loop to iterate over the results.
$paginator = $workflowClient->listWorkflowExecutions('WorkflowType="GreetingWorkflow"');
foreach ($paginator as $info) {
echo "Workflow ID: {$info->execution->getID()}\n";
}
How to set custom Search Attributes
After you've created custom Search Attributes in your Temporal Service (using temporal operator search-attribute create
or the Cloud UI), you can set the values of the custom Search Attributes when starting a Workflow.
To set custom Search Attributes, use the withTypedSearchAttributes
method on WorkflowOptions
for a Workflow stub.
Typed search attributes are a TypedSearchAttributes
collection.
$keyDestinationTime = SearchAttributeKey::forDatetime('DestinationTime');
$keyOrderId = SearchAttributeKey::forKeyword('OrderId');
$workflow = $workflowClient->newWorkflowStub(
OrderWorkflowInterface::class,
WorkflowOptions::new()
->withWorkflowExecutionTimeout('10 minutes')
->withTypedSearchAttributes(
TypedSearchAttributes::empty()
->withValue($keyOrderId, $orderid)
->withValue($keyDestinationTime, new \DateTimeImmutable('2028-11-05T00:10:07Z'))
),
);
How to upsert Search Attributes
Within the Workflow code, you can dynamically add or update Search Attributes using upsertTypedSearchAttributes
.
This method is particularly useful for Workflows whose attributes need to change based on internal logic or external events.
#[Workflow\UpdateMethod]
public function postponeDestinationTime(\DateInterval $interval)
{
// Get the key for the DestinationTime attribute
$keyDestinationTime = SearchAttributeKey::forDatetime('DestinationTime');
/** @var DateTimeImmutable $destinationTime */
$destinationTime = Workflow::getInfo()->typedSearchAttributes->get($keyDestinationTime);
Workflow::upsertTypedSearchAttributes(
$keyDestinationTime->valueSet($destinationTime->add($interval)),
);
}
How to remove a Search Attribute from a Workflow
To remove a Search Attribute that was previously set, set it to an empty Map.
#[Workflow\UpdateMethod]
public function unsetDestinationTime()
{
// Get the key for the DestinationTime attribute
$keyDestinationTime = SearchAttributeKey::forDatetime('DestinationTime');
Workflow::upsertTypedSearchAttributes(
$keyDestinationTime->valueUnset(),
);
}