1

By correlate requests in Azure APIM and Application Insight with W3C distributed tracing Azure, if the client dose not send the traceparent header, seems APIM would checks the incoming request and if no traceparent, the APIM would generate and set from the request goes to backend.

In this case, we would like to return the traceparent information from the frontend response header so the consumer who inoke the API would get it, they could report API issue with this traceparent id so we can better trace/diagnose it with service log.

The question is how to get the APIM generated traceparent from inbound/outbound policy, please suggest a approach, thanks !

Distance
  • 21
  • 1
  • If the posted answer helped, can you may mark it as the answer by clicking the check mark. Doing so can help other community members. – SauravDas-MT Nov 22 '21 at 04:07
  • 1
    Thanks @SauravDas-MT for your reply, I tried the solution your provide but it seems blocked by APIM auto-generated traceparent id. Please check my detailed reply below. – Distance Dec 06 '21 at 15:07

1 Answers1

1

The traceparent HTTP header field identifies the incoming request in a tracing system. The traceparent describes the position of the incoming request in its trace graph in a portable, fixed-length format.

If the traceparent HTTP header is not available then, you can set it by using the set-header policy of API Management policies. The set-header policy is used to assigns a value to an existing response and/or request header or adds a new response and/or request header.

When placed in an inbound pipeline, this policy sets the HTTP headers for the request being passed to the target service. When placed in an outbound pipeline, this policy sets the HTTP headers for the response being sent to the gateway’s client.

The below snippet shows how you can set a new correlation context header if none is available.

<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. But don't forget to set the correlation format in the Settings tab to W3C.

And with this you will get the traceparent in the header section of your request.

I would suggest you to read this W3C document on traceparent headers and Correlation headers using W3C TraceContext for more information.

SauravDas-MT
  • 1,224
  • 1
  • 4
  • 10
  • 1
    Thanks @SaurawDas-MT, we set the new correlation context header and seems APIM would just ignore and replace it when proxy the request to backend service. Is it by design from APIM ? If so, we need a way to get APIM generated traceparent and set to response header, the purpose is to make the API caller get this id and they could report API issue with this traceparent id so we can better trace/diagnose it with service log. – Distance Dec 06 '21 at 15:05