Nested Logic App And Log Analytics

Purpose

 
The goal of this article is to understand the nested logic app working with log analytics and marking the status of parent app based on the last run status of child logic app.
 
Reusability in programming is very common. We tend to create common classes that have many reusable functions and are used to perform a required operation rather than writing the code once again.
 
The child logic app determines the status of parent logic app. The child logic app is functionally similar to common classes that performs any business logic, any special logic that avoids longer workflow (it makes an app difficult to manage). Here, we will understand the integration of nested logic app and getting the child logic app status from log analytics.
 

Steps

 
Before creating a logic app, check whether you have log analytics workspace if not, create one (Enable log analytics workspace in logic app).
Create two logic apps named as parent logic app and child logic app.
 
Nested Logic App And Log AnalyticsNested Logic App And Log Analytics
 
Here, I am using HTTP Request-Response template for the child logic app and will call this child logic app from my parent logic app. Child app performs a few business logics and returns the response.
 
Nested Logic App And Log Analytics
 
In the end the child logic app returns the unique identifier (Correlation Identifier). We will use the workflow().run.name in the expression and pass it in the body field of the response. When the parent logic app calls the child logic app it receives the unique id of the child logic app. 
 
Nested Logic App And Log Analytics Nested Logic App And Log Analytics
 
The child logic app can be integrated to the parent logic app via a connector (Azure Logic Apps). This connector can be found in the action section (pictured below).
 
Nested Logic App And Log AnalyticsNested Logic App And Log AnalyticsNested Logic App And Log Analytics
 
Now, add another connector that is a variable and select the action initialize variable. I have named the variable as an identifier. Click on the value field to see the body of ChildApp in the Dynamic content. Select the body and the connector is ready.
 
Nested Logic App And Log Analytics 
 
Add another variable for saving the status of the child logic app and initialize the variable with an empty value.
 
Nested Logic App And Log Analytics 
 
Add a new step, use Until loop and select the Azure Monitor Logs, we will select Run query and list results action. Subsequently, fill the details.
 
Nested Logic App And Log AnalyticsNested Logic App And Log Analytics Nested Logic App And Log AnalyticsNested Logic App And Log Analytics
 
After filling in the details, use delay connector. Why? If you’re trying to fetch the log data of the logic app in run time it takes a few minutes to reflect the log data in the log analytics and to fetch the data from log analytics. The delay happens due to multiple reasons. One of the reasons is due to the Azure region selected, using until loop without a delay connector speeds up the execution without fetching the log records as there are no records available at that time.
 
We will set the query at the end and see how it works.
 
Nested Logic App And Log Analytics
 
I added a delay connector with four seconds of delay. You, on the other hand, can input the delay time based on your expectation and analysis.
 
 Nested Logic App And Log Analytics
 
Insert a new step to add action set variable that comes under variable connector.
 
Nested Logic App And Log Analytics
 
Select status from the dropdown list.
 
Nested Logic App And Log Analytics
 
Now, when you will click on value field, you will see the dynamic content appearing, thanks to the AI (Artificial intelligence) function available in the logic app. Scroll down to find Run query and list results section.
 
Nested Logic App And Log AnalyticsNested Logic App And Log Analytics
 
Search for the status in search dynamic content field, to see status_s in Run query and list results. Select the status_s and you will find foreach loop and variable status in it. Again, thanks to artificial intelligence in the logic app that understands what you’re tying to do.
 
Nested Logic App And Log AnalyticsNested Logic App And Log Analytics
 
The Until loop section fetches the logs from log analytics and stores the status_s in status variable. Now, modify the query in Run query and list results for fetching the only row that is required.
 
AzureDiagnostics
| where resource_runId_s==”ChildApp Correlationid”
| where OperationName =="Microsoft.Logic/workflows/workflowRunCompleted"
 
Here in the code (above), resource_runId_s is the unique identifier of the logic app run. We will use this identifier variable and to determine the final status of the child app run. I have used operationName as the final indicator that says logic app has completed a particular run and I will have the status of the logic app.
 
Nested Logic App And Log Analytics
 
After modifying the query based on our expectation (only one row that shows the logic app run is completed) we will add conditions in until loop.
 
How long will this loop go on?
 
In the image below I have applied a condition that ends the loop following the change in the value of the status variable.
 
equals(variables('Status'), 'empty') == false
 
Nested Logic App And Log Analytics Nested Logic App And Log Analytics
 
Once the until loop condition is completed check for the “succeeded” status in the status variable.
 
Add if else condition to check the status of child logic app in order to terminate the parent logic app.
 
Nested Logic App And Log AnalyticsNested Logic App And Log Analytics
 
Select a value from the dynamic content (status variable) and match it with string Succeeded.
 
If it matches with succeeded string it will terminate successfully, if it receive a different status (i.e. Cancelled, failed.) the parent app will terminate as Failed.
 
"@variables('Status')"=="Succeeded"
 
Nested Logic App And Log Analytics
 
In if true condition add and action named terminate and select the status from the drop down list of the terminate action. Then click succeeded. If false we will have Failed termination status.
 
Nested Logic App And Log Analytics Nested Logic App And Log Analytics
 
In the overview of the logic app you can see the run history with failed and succeeded status based on child logic app run.
 
Nested Logic App And Log Analytics
 
................Keep Learning !!!
Next Recommended Reading How To Add Parameters In Azure Logic App