Filtering workflow executions using python.

Hello, 

I am trying to filter on failed workflow execution by using the ListWorkflowsRequest client library. I have tried to follow the guide in the gcloud filtering documentation but I cant get it to work. I have posted a sample below and line 12 is the problematic line. Thanks

from google.cloud import workflows_v1
from google.cloud.workflows import executions_v1

# Create a client
workflow_client = workflows_v1.WorkflowsClient()
execution_client = executions_v1.ExecutionsClient()
project = "test_project"
location = "europe-west4"
# Initialize request argument(s)
request = workflows_v1.ListWorkflowsRequest(
    parent=f"projects/{project}/locations/{location}",
    filter="STATE:FAILED"
)
# Make the request
workflow_page_result = workflow_client.list_workflows(request=request)
# Handle the response
with open("./workflows.txt", "w") as workflow_file:
    for workflow_response in workflow_page_result:
        name = workflow_response.name
        request = executions_v1.ListExecutionsRequest(
            parent=name,
        )
        execution_page_result = execution_client.list_executions(request=request)
        # Handle the response
        for execution_response in execution_page_result:
            print(execution_response)
        workflow_file.write(name)

 

0 2 1,079
2 REPLIES 2

Hi @RichardDrury,

Welcome to Google Cloud Community,

The ListWorkflowsRequest method is used to list workflows in a given project and location. The parent parameter specifies the project and location for which you want to list workflows, and the filter parameter allows you to specify a filter to narrow down the list of workflows. In this case, the filter parameter is set to STATE:FAILED, which means that only failed workflows will be returned in the list.

Here is an example of how you could use the ListWorkflowsRequest method to list failed workflows in a project and location:

# Import the Workflows client library
from google.cloud import workflows_v1

# Create a client
client = workflows_v1.WorkflowsClient()

# Set the project and location
project = "my-project"
location = "my-location"

# Set the filter to only return failed workflows
filter = "STATE:FAILED"

# List the workflows
response = client.list_workflows(
    parent=f"projects/{project}/locations/{location}",
    filter=filter
)

# Print the results
for workflow in response:
    print(workflow.name)

 Thanks.

Hello @christianpaula Thank you for your response. 

I tried the suggestion and it returned the following error:

Traceback (most recent call last):
  File "/Users/richard.drury/code/gcp/git/service-level-monitoring/daily_checks/sensor_checks/failedWorkflows.py", line 15, in <module>
    response = client.list_workflows(
TypeError: WorkflowsClient.list_workflows() got an unexpected keyword argument 'filter'

The documentation does suggest that adding the filter can be applied to the class ListWorkflowsRequest() and not the list_workflows(). I have tested out the ListWorkflowsRequest() with the following and it works

filter = "name:name"

But when I change it from name to state it fails with the following

Traceback (most recent call last):
  File "/Users/richard.drury/code/gcp/git/service-level-monitoring/venv/lib/python3.10/site-packages/google/api_core/grpc_helpers.py", line 50, in error_remapped_callable
    return callable_(*args, **kwargs)
  File "/Users/richard.drury/code/gcp/git/service-level-monitoring/venv/lib/python3.10/site-packages/grpc/_channel.py", line 946, in __call__
    return _end_unary_response_blocking(state, call, False, None)
  File "/Users/richard.drury/code/gcp/git/service-level-monitoring/venv/lib/python3.10/site-packages/grpc/_channel.py", line 849, in _end_unary_response_blocking
    raise _InactiveRpcError(state)
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
	status = StatusCode.INVALID_ARGUMENT
	details = "The request was invalid: invalid list filter: Invalid literal 'FAILED' for 'resource.state'."
	debug_error_string = "{"created":"@1670851873.305290000","description":"Error received from peer ipv4:142.250.187.234:443","file":"src/core/lib/surface/call.cc","file_line":967,"grpc_message":"The request was invalid: invalid list filter: Invalid literal 'FAILED' for 'resource.state'.","grpc_status":3}"
>

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/richard.drury/code/gcp/git/service-level-monitoring/daily_checks/sensor_checks/workflows.py", line 20, in <module>
    workflow_page_result = workflow_client.list_workflows(request=request)
  File "/Users/richard.drury/code/gcp/git/service-level-monitoring/venv/lib/python3.10/site-packages/google/cloud/workflows_v1/services/workflows/client.py", line 537, in list_workflows
    response = rpc(
  File "/Users/richard.drury/code/gcp/git/service-level-monitoring/venv/lib/python3.10/site-packages/google/api_core/gapic_v1/method.py", line 154, in __call__
    return wrapped_func(*args, **kwargs)
  File "/Users/richard.drury/code/gcp/git/service-level-monitoring/venv/lib/python3.10/site-packages/google/api_core/grpc_helpers.py", line 52, in error_remapped_callable
    raise exceptions.from_grpc_error(exc) from exc
google.api_core.exceptions.InvalidArgument: 400 The request was invalid: invalid list filter: Invalid literal 'FAILED' for 'resource.state'. [field_violations {
  field: "filter"
  description: "invalid list filter: Invalid literal \'FAILED\' for \'resource.state\'."
}
]

 

Top Labels in this Space