Sample API management policy – Add a header containing correlation id – Azure API Management

Table of Contents0.1 In this article1 Policy2 Next steps Article 09/19/2021 2 minutes to read 5 contributors In this article This article shows an Azure API management policy sample that demonstrates how to add a header containing a […]

This article shows an Azure API management policy sample that demonstrates how to add a header containing a correlation id to the inbound request. To set or edit a policy code, follow the steps described in Set or edit a policy. To see other examples, see policy samples.

Policy

Paste the code into the inbound block.

<!-- The policy defined in this file demonstrates how to add a header containing a correlation id to the inbound request. -->
<!-- The id could be used to correlate requests forwarded by Azure API Management to requests in your backend. -->
<!-- The snippet uses a COMB GUID as an id value for efficient query performance. -->

<!-- NOTE: If COMB format is not needed, context.RequestId should be used as a value of correlation id. -->
<!-- context.RequestId is unique for each request and is stored as part of gateway log records. -->

<!-- Copy the following snippet into the inbound section. Applying this snippet at the global level will ensure that correlation id is added to all requests. -->

<policies>
    <inbound>
        <base />
        <set-header name="correlationid" exists-action="skip">
            <value>@{ 
                var guidBinary = new byte[16];
                Array.Copy(Guid.NewGuid().ToByteArray(), 0, guidBinary, 0, 10);
                long time = DateTime.Now.Ticks;
                byte[] bytes = new byte[6];
                unchecked
                {
                       bytes[5] = (byte)(time >> 40);
                       bytes[4] = (byte)(time >> 32);
                       bytes[3] = (byte)(time >> 24);
                       bytes[2] = (byte)(time >> 16);
                       bytes[1] = (byte)(time >> 8);
                       bytes[0] = (byte)(time);
                }
                Array.Copy(bytes, 0, guidBinary, 10, 6);
                return new Guid(guidBinary).ToString();
            }
            </value>
        </set-header>    
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

Next steps

Learn more about APIM policies:

Source Article

Next Post

Optimize your cloud investment with Cost Management - Microsoft Cost Management

Thu Jun 23 , 2022
Table of Contents0.1 In this article1 Methodology1.1 Key principles1.1.1 Planning1.1.2 Visibility1.1.3 Accountability1.1.4 Optimization1.1.5 Iteration2 Plan with cost in mind2.1 Azure onboarding options2.1.1 Free2.1.2 Pay as you go2.1.3 Enterprise Agreement2.1.4 Azure in CSP3 Estimate the cost of your solution3.1 Azure pricing calculator3.2 Azure Migrate4 Analyze and manage your costs4.1 Organize resources […]
Exit mobile version