Monitor Long-running Power Automate Flows with a Flow.

One of the major challenges we see with the growing number of flows in an Enterprise is – The ‘governance’ of Flows for IT Support. It is very easy to lose track of the flows (specially with the throttling limits in place) and if any of them are doing business critical activities/regulatory stuff, the end result would be chaos.

Guess what, Flow to the rescue again. Yes, Monitor your Flows with a Flow and provide critical alerts to IT Support so that necessary action can be taken.– isn’t it cool? We can use Flow APIs to see all the running/Failed Flows and trigger messages/mails accordingly.

In this blog we will see how we can use Flow APIs to monitor long running flows. Let’s quickly create a Monitor Flow which runs on schedule (I would suggest to keep it an hour but it is up to you).

Btw, before starting, this is what the output we want to see in teams as an end result of this monitoring flow.

Ok, now let’s start with creating a Monitor Flow which runs on schedule

Step1: Get All the Flows in current environment.

Use Power Automate Management connector and select List Flows As Admin action to get all the flows in the current environment.

💡 Use an Administration login to use this action or else you may not see all the flows running in your environment.

Provide the required environment and rename the action to a meaningful name. I have named it as GetAllFlows.

💡 If you want to use the steps as it is, ensure you are giving the same names as some of the steps further below references to these names.

Step 2: Loop through All the flows

Do an Apply For Each loop and loop through the “value” as shown below.

Step 3: Get all the Running instances of flows

With in the Loop add an HTTP with Azure AD connector and select Invoke HTTP Action.

Provide the connection details as mentioned below.

Base Resource URL : https://api.flow.microsoft.com/

Azure AD Resource URI: https://service.flow.microsoft.com/

💡 Ensure both the URLs are exactly the same including the ‘/‘ at the end.

After authentication, provide the Flow URL, method as GET and rename the action to GetRunningFlows.

Flow URL :  https://api.flow.microsoft.com/providers/Microsoft.ProcessSimple/environments/@{items('LoopthroughAllFlows')?['properties/environment/name']}/flows/@{items('LoopthroughAllFlows')?['name']}/runs?api-version=2016-11-01&$filter=Status%20eq%20%27running%27

💡 use the @{} content as dynamic content.

Parse the output of the REST API call to JSON definition using parseJSON action (note the name of the action changed to GetRunningFlowsJson).

Use the schema –

{
    "type": "object",
    "properties": {
        "value": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string"
                    },
                    "id": {
                        "type": "string"
                    },
                    "type": {
                        "type": "string"
                    },
                    "properties": {
                        "type": "object",
                        "properties": {
                            "startTime": {
                                "type": "string"
                            },
                            "status": {
                                "type": "string"
                            },
                            "correlation": {
                                "type": "object",
                                "properties": {
                                    "clientTrackingId": {
                                        "type": "string"
                                    }
                                }
                            },
                            "trigger": {
                                "type": "object",
                                "properties": {
                                    "name": {
                                        "type": "string"
                                    },
                                    "inputsLink": {
                                        "type": "object",
                                        "properties": {
                                            "uri": {
                                                "type": "string"
                                            },
                                            "contentVersion": {
                                                "type": "string"
                                            },
                                            "contentSize": {
                                                "type": "integer"
                                            },
                                            "contentHash": {
                                                "type": "object",
                                                "properties": {
                                                    "algorithm": {
                                                        "type": "string"
                                                    },
                                                    "value": {
                                                        "type": "string"
                                                    }
                                                }
                                            }
                                        }
                                    },
                                    "outputsLink": {
                                        "type": "object",
                                        "properties": {
                                            "uri": {
                                                "type": "string"
                                            },
                                            "contentVersion": {
                                                "type": "string"
                                            },
                                            "contentSize": {
                                                "type": "integer"
                                            },
                                            "contentHash": {
                                                "type": "object",
                                                "properties": {
                                                    "algorithm": {
                                                        "type": "string"
                                                    },
                                                    "value": {
                                                        "type": "string"
                                                    }
                                                }
                                            }
                                        }
                                    },
                                    "startTime": {
                                        "type": "string"
                                    },
                                    "endTime": {
                                        "type": "string"
                                    },
                                    "originHistoryName": {
                                        "type": "string"
                                    },
                                    "correlation": {
                                        "type": "object",
                                        "properties": {
                                            "clientTrackingId": {
                                                "type": "string"
                                            }
                                        }
                                    },
                                    "status": {
                                        "type": "string"
                                    }
                                }
                            }
                        }
                    }
                },
                "required": [
                    "name",
                    "id",
                    "type",
                    "properties"
                ]
            }
        }
    }
}
 

Step 4: Get Long Running Flows

Use a Condition action, with expression length(body(‘GetRunningFlowsJson’)?[‘value’]) is greater than 0

Under the Yes Branch Filter for Long running flows.

Add a Filter action and filter the “value” from GetRunningFlowsJson where startTime+15 minutes is less than Current Time. (Rename the Action to LongRunningFlows)

💡 In my case, I have considered any flow that is running for more than 15 minutes as long running. Choose your best suitable number here.

{
"inputs": { "from": "@body('GetRunningFlowsJson')?['value']", "where": "@less(addMinutes(item()?['properties']?['startTime'], 15), utcNow())" }
}

Use Condition action with expression length(body('LongRunningFlows')) is greater than 0 to check if any running instances are filtered based in the 15 mins criteria.

In the Yes branch, Post a message to your IT Support team.

{
"inputs": {
"host": {
"connectionName": "shared_teams_1",
"operationId": "PostUserNotification",
"apiId": "/providers/Microsoft.PowerApps/apis/shared_teams"
},
"parameters": {
"PostNotificationRequest/recipient/to": "xxx.x@xyz.com",
"PostNotificationRequest/messageBody": "Hey, \n\nFor the flow - @{items('LoopthroughAllFlows')?['properties/displayName']}, out of @{length(body('GetRunningFlowsJson')?['value'])} running flows @{length(body('LongRunningFlows'))} flows are running for more than 15 minutes. Please crosscheck !!",
"PostNotificationRequest/messageTitle": "Long Running Flow - @{items('LoopthroughAllFlows')?['properties/displayName']}"
},
"authentication": "@parameters('$authentication')"
}
}

That’s it… Hope you liked it 😊

Happy FLOWing.. !!

The full Flow just in case 🙂

Monitoring Flow
Monitoring Flow full view

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s