@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:
- version
- trace-id
- parent-id
- 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):
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://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.