Share via

Want to know whether the apim gateway is setting taceparent header and forwarding this to backend api call?

Shekhar Nadide 201 Reputation points
Aug 9, 2023, 5:44 AM

Scenario :

Request -> APIM Gateway -> Backend Service

In the above flow..Will gateway set the traceparent header while calling the backend service.

If not Could you please provide way to add it to headers?

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,341 questions
0 comments No comments
{count} votes

Accepted answer
  1. navba-MSFT 27,495 Reputation points Microsoft Employee
    Aug 9, 2023, 7:22 AM

    @Shekhar Nadide Welcome to Microsoft Q&A Forum, Thank you for posting your query here!

    I see that you are checking if the Azure API Management (APIM) gateway sets the traceparent header while calling the backend service. If not, you want to know how to add this header before sending the request to the backend API.

    Background about traceparent header:
    A "traceparent" header is intended for use in a distributed tracing system (such as app insights), where one one request can branch to several different requests (client to front end, front end to back end). Each request needs to be uniquely identifieable - and at the same time, linkable to the original request. For this reason, traceparent has four parts:

    1. version
    2. trace-id
    3. parent-id
    4. trace-flags

    The "trace-id" is what links all the requests together, where each request has a unique "parent-id" or "span-id" so the request can be correctly tracked. More Info about traceparent, see this.

    Answer:
    Regarding your question, the APIM gateway can forward the traceparent header to the backend service if it is present in the incoming request. However, it does not add the traceparent header by default.

    To add the traceparent header to the headers of the outgoing request, you can use policies in APIM. Specifically, you can use the set-header policy to add the traceparent header to the headers of the outgoing request.

    Please note the exists-action="override"is set to override the value sent by client. Also note if the client doesn't send one, then you can also use the set-header policy as shown below and use exists-action="skip".

    Here is an example policy that adds the traceparent header with a unique ID:

    <policies>
        <inbound>
            ...
        </inbound>
        <backend>
            <set-header name="traceparent" exists-action="override">
                 <value>@($"00-{context.RequestId.ToString("N")}-0000000000000000-01")</value>
            </set-header>
        </backend>
        <outbound>
            ...
        </outbound>
        <on-error>
            ...
        </on-error>
    </policies>
    

    This policy adds the traceparent header with a value that starts with "00-" (indicating the version of the traceparent header), followed by a unique GUID, followed by zeros (indicating that there is no parent span), and ending with "01" (indicating the current span). You can customize this value as needed, depending on your tracing requirements.

    Here is one sample value if that helps 00-0af6543216cd43dd8448eb122d91420d-b8bb7c7169202223-01

    If you want to override the traceparent property (or simply pass the traceprarent property sent by the client to your backend), you will have to set the correlation protocol to "None" (Diagnostic Logs --> Application Insights):

    User's image

    Sharing some of relevant articles if that helps:
    https://learn.microsoft.com/en-us/answers/questions/627211/get-traceparent-generated-by-apim-and-return-from?childtoview=654703

    https://stackoverflow.com/questions/63868924/how-to-get-set-traceparent-header-in-apim-inbound-policy-if-client-dose-not-sent

    https://learn.microsoft.com/en-us/azure/api-management/set-header-policy#attributes

    Hope this helps.

    **
    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.