1
votes

we want to correlate requests in Azure APIM and Application Insight. For the API we have a policy that uses send-request in inbound and outbound section. We are using W3C distributed tracing Azure , specification

Now if the client dose not send the traceparent header the send-request in the inbound is not being correlated in application insight.

if we try to set the traceparent header in the inbound policy it will be overwritten in the backend part of the policy. It looks like APIM checks the incoming request and if no traceparent is set it will generate it. But we cannot add headers to the incoming request in the policy (read only).

Sample policy

<policies>
    <inbound>
        <base />
        <send-request mode="new" response-variable-name="inboundresponse" timeout="10" ignore-error="true">
            <set-url>https://someUrl.com</set-url>
            <set-method>GET</set-method>
            <set-header name="traceparent" exists-action="skip">
                <value>00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01</value>
            </set-header>
            <set-body></set-body>
        </send-request>
        <!-- for test set fixed value, but this value is overwritten by Azure APIM in backend
         and all 3 requests are not coorrelated -->
        <set-header name="traceparent" exists-action="skip">
            <value>00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01</value>
        </set-header>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
        <!-- traceparent value we get here is not the same that we set -->
        <send-request mode="new" response-variable-name="outboundresponse" timeout="10" ignore-error="true">
            <set-url>https://someUrl.com</set-url>
            <set-method>GET</set-method>
            <set-header name="traceparent" exists-action="skip">
                <value>00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01</value>
            </set-header>
            <set-body></set-body>
        </send-request>
        
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

Trace

2

2 Answers

0
votes

Have you tried using the set-header policy like this:

<set-header name="traceparent" exists-action="override">
     <value>00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01</value>
</set-header>

Note the override exists-action. In my tests the traceparent was set and sent to the backend as expected.

Update

If you want to only set a new correlation context header if none is available, I'd suggest you go with following statement:

<set-header name="traceparent" exists-action="skip">
    <value>@($"00-{context.RequestId.ToString("N")}-0000000000000000-01")</value>
</set-header>

With the context.RequestId.ToString("N") you get the internally correlated id of the request and format it without dashes. Another thing to mention:

Have you set the correlation format in the Settings tab to W3C? enter image description here

0
votes

I solved my issue by adding the following headers in inbound. You can take a look into the documentation.

 <inbound> 
  <allowed-headers>
    .....
    <header>request-id</header>
    <header>request-context</header>
    <header>traceparent</header>
   </allowed-headers>
 </inbound>