Wednesday, January 12, 2022

How to track requests after integrating APIM with Logic App services

Scenario:

Assuming that we encounter failed requests or unexpected response while invoking Azure Logic App API through Azure API Management service, we need to narrow down the issue based on the request monitor log in the APIM and Azure Logic App at the same time. However, as APIM service and Logic App service are two completely different azure services, they have different logging mechanisms in these two systems. Hence, there is no built-in property or field in the log which links both two systems.

 

This blog’s purpose is to guide users to manually add one property named x-correlation-id in both of APIM and Azure Logic App and do some extra steps to link these two systems using the generated x-correlation-id for troubleshooting specific request.

Pre-requirements

Before we start, we could make sure that we have exiting in the APIM service and Azure Logic App.

Steps:

We need settings on both APIM and logic App sides. 

1.On APIM side:

Navigate to inbound policy on API or Operation layer, do the following changes:

 

  • Create a GUID for your API request,
  • Use <set-header> to create a GUID for the ‘correlation-id’
  • Add a <trace> policy to trace the information inside your APIM Application Insights.

 

Snippet of policy definition:

 

<inbound>

    <!--Use consumer correlation id or generate new one-->

        <set-variable name="correlation-id" value="@(context.Request.Headers.GetValueOrDefault("x-correlation-id", Guid.NewGuid().ToString()))" />

        <!--Set header for end-to-end correlation-->

        <set-header name="x-correlation-id" exists-action="override">

            <value>@((string)context.Variables["correlation-id"])</value>

        </set-header>

        <!--Trace the correlation id-->

        <trace source="Global APIM Policy" severity="information">

            <message>@(String.Format("{0} | {1}", context.Api.Name, context.Variables[“correlation-id”]))</message>

            <metadata name="correlation-id" value="@((string)context.Variables["correlation-id"])" />

        </trace>

        <base />

    </inbound>

 

You could refer to the statement of above <message> and <metadata> from this link: https://docs.microsoft.com/en-us/azure/api-management/api-management-advanced-policies#elements-14

 

Jay_Gong_12-1641957595001.png

 

2.On the Logic App side:

  • Enable the Diagnostic Setting and populate the log into generated Log Analytics workspace.

 

Jay_Gong_13-1641957595008.png

 

  • Create a Compose action below the original HTTP trigger action:

Jay_Gong_14-1641957595009.png

 

 

  • Then click ‘add dynamic content’, in the expression, copy and paste: triggerOutputs()['headers']['x-correlation-id'], the header name needs to be the exact value which we configured in the previous APIM <set-header> policy.

 

Jay_Gong_15-1641957595014.png

 

  • In the “Compose” settings, add a new tracked properties as following and set the Tracked Properties, like “@action()[‘outputs’]”

Jay_Gong_16-1641957595017.png

 

 

Jay_Gong_17-1641957595028.png

 

3. Test and monitor:

Now, we could send request to Logic App through APIM to test our request tracking.

In the APIM, send a call to Logic App.

Jay_Gong_18-1641957595037.png

 

We could observe the generated x-correlation-id header in the trace log.

 

Jay_Gong_19-1641957595040.png

 

The value of generated correlation-id in the <message> could be found in the APIM Portal---- Logs----- ApiManagementGatewayLogs table ----- TraceRecords property.

 

Jay_Gong_20-1641957595048.png

 

Some Tips:

 

  • The default CorrelationId column is generated by ApiManagementGatewayLogs by default which can’t be managed by ourselves. This is not related to the current customize correlation-id totally.
  • ApiManagementGatewayLogs will be enabled after configuring Diagnostics Settings in the APIM portal and populating the GatewayLog into Log Analytics workspace.

 

Jay_Gong_21-1641957595055.png

 

  • The value of generated correlation-id in the <metadata> could be found in the Linked Application Insights---- Logs----- Traces table ----- customDimensions property.

Jay_Gong_22-1641957595063.png

 

  • Navigate to the Log Analytics workspace of Logic App, we could run the Kusto query below to retrieve the log with tracked properties.

Jay_Gong_23-1641957595077.png

 

Now, we could narrow down further regarding specific request between separate two services(APIM and Logic App) which have different log systems.

Posted at https://sl.advdat.com/3GmYscv