Blog Post

Azure PaaS Blog
3 MIN READ

How to track requests after integrating APIM with Logic App services

Jay_Gong's avatar
Jay_Gong
Icon for Microsoft rankMicrosoft
Jan 12, 2022

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

 

 

2.On the Logic App side:

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

 

 

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

 

 

  • 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.

 

 

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

 

 

 

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.

 

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

 

 

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

 

 

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.

 

 

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

 

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

 

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

Published Jan 12, 2022
Version 1.0
No CommentsBe the first to comment